Go experiment: de-noising
CoffeeScript is a great example of how to de-noise a language like Javascript. (Of course, I know people that consider braces to be a good thing, but lots of us consider them noise and prefer significant whitespace, so I’m speaking to those folks.) What would Go code look like with some of CoffeeScript’s denoising?
TL;DR : the answer is that de-noised Go would not look much different than normal Go…
As an experiment, I picked some rules from CoffeeScript and re-wrote the Mandelbrot example from The Computer Benchmarks Game. Note: this is someone else’s original Go code, so I can’t vouch for the quality of the Go code….
Here’s the original Go code:
/* targeting a q6600 system, one cpu worker per core */ const pool = 4 const ZERO float64 = 0 const LIMIT = 2.0 const ITER = 50 // Benchmark parameter const SIZE = 16000 var rows []byte var bytesPerRow int // This func is responsible for rendering a row of pixels, // and when complete writing it out to the file. func renderRow(w, h, bytes int, workChan chan int,iter int, finishChan chan bool) { var Zr, Zi, Tr, Ti, Cr float64 var x,i int for y := range workChan { offset := bytesPerRow * y Ci := (2*float64(y)/float64(h) - 1.0) for x = 0; x < w; x++ { Zr, Zi, Tr, Ti = ZERO, ZERO, ZERO, ZERO Cr = (2*float64(x)/float64(w) - 1.5) for i = 0; i < iter && Tr+Ti <= LIMIT*LIMIT; i++ { Zi = 2*Zr*Zi + Ci Zr = Tr - Ti + Cr Tr = Zr * Zr Ti = Zi * Zi } // Store the value in the array of ints if Tr+Ti <= LIMIT*LIMIT { rows[offset+x/8] |= (byte(1) << uint(7-(x%8))) } } } /* tell master I'm finished */ finishChan <- true
My quick de-noising rules are:
- Eliminate var since it can be inferred.
- Use ‘:’ instead of const (a la Ruby’s symbols).
- Eliminate func in favor of ‘-> and variables for functions.
- Replace braces {} with significant whitespace
- Replace C-style comments with shell comments “#”
- Try to leave other spacing along to not fudge on line count
- Replace simple loops with an “in” and range form
The de-noised Go code:
# targeting a q6600 system, one cpu worker per core :pool = 4 :ZERO float64 = 0 # These are constants :LIMIT = 2.0 :ITER = 50 # Benchmark parameter :SIZE = 16000 rows []byte bytesPerRow int # This func is responsible for rendering a row of pixels, # and when complete writing it out to the file. renderRow = (w, h, bytes int, workChan chan int,iter int, finishChan chan bool) -> Zr, Zi, Tr, Ti, Cr float64 x,i int for y := range workChan offset := bytesPerRow * y Ci := (2*float64(y)/float64(h) - 1.0) for x in [0..w] Zr, Zi, Tr, Ti = ZERO, ZERO, ZERO, ZERO Cr = (2*float64(x)/float64(w) - 1.5) i = 0 while i++ < iter && Tr+Ti <= LIMIT*LIMIT Zi = 2*Zr*Zi + Ci Zr = Tr - Ti + Cr Tr = Zr * Zr Ti = Zi * Zi # Store the value in the array of ints if Tr+Ti <= LIMIT*LIMIT rows[offset+x/8] |= (byte(1) << uint(7-(x%8))) # tell master I'm finished finishChan <- true
That seems to be a pretty small win in return for a syntax adjustment that does not produce significantly enhanced readability. Some bits are nice: I prefer the significant whitespace, but the braces just aren’t that obtrusive in Go; I do prefer the shell comment style, but it’s not a deal breaker; the simplified loop is nice, but not incredible; eliding “var” is okay, but harms readability given the need to declare the types of some variables; I do prefer the colon for constants. Whereas Coffeescript can dramatically shorten and de-noise a Javascript file, it looks as though Go is already pretty terse.
Obviously, I didn’t deal with all of Go in this experiment, so I’ll look over more of it soon, but Go appears to be quite terse already given its design…
IMHO you actually add even more noise by replacing the for with a while in certain situations. I don’t really see the point about this.
FUZxl
16 May 13 at 6:45 pm edit_comment_link(__('Edit', 'sandbox'), ' ', ''); ?>
:you actually add even more noise by replacing the for with a while
Generally, agreed. Wanted to try something to see how the code would look/be different.
:I don’t really see the point about this.
I’m not sure if you’re referring to the for/while thing or to the overall experiment. If for/while, the “for” does seem simpler. If you’re referring to the overall post, then, as noted, it was a Gedankenexperiment to see if significant whitespace would be a helpful adjustment (transpilation) to Go.
alson
18 May 13 at 12:45 pm edit_comment_link(__('Edit', 'sandbox'), ' ', ''); ?>