Saturday, November 22, 2014

Cleaning Code - Episode 1

This is a series of posts where we take a code snippet, analyse it's cleanliness and produce a simplified version of the code. The metric for cleanliness is arbitrary, but I hope most readers will agree that the modified code is simpler to deal with. This is optimizing code for the human brain.

Let's look at our first snippet, randomly selected from a moderate-sized C++ codebase.

This is a chunk of a C++ function called from a scripting language. It extracts parameters and builds a string from them.

What are the aesthetic issues? Same line declarations, single line complexity, repeated code, old C scoping for loops.

Cleaning up always gives the same benefits. Faster mental parsing. easier debugging/stepping, minimize surprises, canonical code shapes, easier rearranging. The tradeoff is increased verbosity, but text input is not a significant time expense comparatively.

Let's see the change-by-change commented version:

And the final version without comments:

Feel free to send in snippets for the next post! The messier the better!


  1. This is a smart blog. I mean it. You have so much knowledge about this issue, and so much passion. You also know how to make people rally behind it, obviously from the responses.

    1. Hello It’s really a nice and helpful piece of info. I am happy that you shared this helpful info with us. Please stay us informed like this. Thank you for sharing. bye
      Tangki Panel
      Tangki Fiberglass
      Jual Septic Tank

  2. That appears to be excellent however i am still not too sure that I like it. At any rate will look far more into it and decide personally! Greenworks GPW2001 2000 PSI

  3. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! Mop

  4. Clearly, is the actual advisory article, all advice is familiar real. Thank you for this administration with us. I start interesting and adviser it. Looking for Advanced updates that have been added. biosafety cabinet

  5. Great post. It’s a fact that your blog posts are so unique and interesting. I have always admired your site. Thanks for the great tips and work. filter papers

  6. This a good way to appreciate the teacher as they put their efforts to train students. UK dissertation Writers appreciates the teachers.  cheap carpet cleaning fayetteville nc

  7. Great info! I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have. Best Hospital in Hyderabad

  8. Thank you very much for this wonderful site well written and informative content you have.
    pest control services in Delhi
    pest control Gurgaon

  9. Great post. I came here while searching for Generate Press reviews & impressed with the features you explained here. It looks promising and going to buy this. End of Lease Cleaning Melbourne

  10. Great Information sharing I am very happy to read this article. Thanks for giving us go through info. I appreciate this post.
    Best Hospital in Hyderabad

  11. Thank you for sharing of some of the articles we read this one article is very interesting for us, I like it. best hospitals in Hyderabad

  12. This really is additionally an excellent publish that we truly loved reading through. It's not daily which i possesses the likelihood to determine something. Best Fertility Centre In Hyderabad

  13. Very well written post. You have shared a wonderful article which is very helpful. Thanks for sharing, have a good day.
    Best Bluetooth speakers under 2000

  14. This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post.
    Student Visa Australia
    Skilled Visa Australia
    482 Visa Australia

  15. All the contents you mentioned in post is too good and can be very useful. I will keep it in mind, thanks for sharing the information keep updating, looking forward for more posts.Thanks deep cleaning uk

  16. Great Information sharing I am very happy to read this article. Thanks for giving us go through info. I appreciate this post.
    nitrocellulose membrane