Do it Dirty, First

I have spent so much time spinning in place trying to come up with some epiphany that’ll allow me to write perfect code the very first time I put virtual pen to IDE. It needs to stop.

I’ve read so many articles that talk about blowing through the first version of the code just to get it to work. Don’t worry about the efficiency or the way it’s configured, just get it down. You can refactor from that point.

I’m going through The Go Programming Language and one of the example challenges is to build a function that will take a string of numbers and inject commas at the appropriate intervals. The example used recursion and was very short (because recursion) but the challenge required the use of buffers since that’s what we were meant to learn next.

My first swing at it:

It wasn’t horrible, but I’m going through the string backwards, injecting commas every 3 digits as long as I wasn’t completely out of them, and then I had to reverse the entire string again before I handed it back — using two buffers instead of one.

I hopped onto slack and had a chat with @radovsky_b who turned me on to a way to step through the digits while taking into consideration that any position that’s a multiple of 3 (i.e., mod 3 == 0), as long as we hadn’t reached the end of the string, could be used to add commas from the front to back.

A much more compact solution for sure, and now that I’m one with the `bytes.Buffer` I can move on to the next example! So the moral of the story: do it dirty once, then rework it to eliminate the cruft. I need to write that on my wall somewhere. Maybe I can get @mrsxinu to make me a banner or something.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.