• Kirill Smelkov's avatar
    encoder: Raise signal/noise ratio when writing data out · 423fe587
    Kirill Smelkov authored
    Currently we often use constructs like
    
    	_, err := e.w.Write([]byte{opNone})
    	return err
    
    however with added Encoder.emit helper it can become only
    
    	return e.emit(opNone)
    
    which is more clearly readable.
    
    We can do similarly for formatted output:
    
    -	_, err := fmt.Fprintf(e.w, "%c%dL\n", opLong, b)
    -	return err
    +	return e.emitf("%c%dL\n", opLong, b)
    
    Due to much boilerplate in one place (encodeInt) the return of
    fmt.Fprintf was not even checked. We change the code there with
    
    -		_, err = e.w.Write([]byte{opInt})
    -		if err != nil {
    -			return err
    -		}
    -		fmt.Fprintf(e.w, "%d\n", i)
    +		return e.emitf("%c%d\n", opInt, i)
    
    which is now hopefully more visible for what is going on and is easier
    to catch potential mistake by eyes.
    
    A corresponding test for "int64 encoded as string" will be added later.
    423fe587
encode.go 8.22 KB