Categories
Uncategorized

Clean code that doesn’t smell

Well we are back from winter break, and that means getting back to work. We are finishing up the planning and design for our algorithmic trading program which means we can start the fun of actually coding it and seeing results. This means that we are going to have to write some clean code that doesn’t stink, that’s what we are going to be talking about here today.

So what is clean code?
Clean code should be well commented code that is easy to read and understand. A common quote on the subject that I enjoy is,

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”

This encapsulates the core idea of writing clean code. It needs to be understandable to any other developer that may be working on the same code presently, or in the future.

This article does a good job discussing what clean code means and how to achieve it. Some good rules to follow when writing clean code includes:

  1. Avoid hard coded numbers
    • Hard coded numbers makes it more difficult to understand the purpose of the value when it could have a descriptive variable name in place.
  2. Use meaningful and descriptive names
    • The names of variables and functions should give anyone reading it an idea of what it does or what its purpose is.
  3. Leave meaningful comments
  4. Write functions that do a single thing
  5. Follow DRY (Don’t Repeat Yourself)

After looking into what makes clean code and how to avoid code smells I have found a few areas I could improve in when it comes to writing clean code.

I often implement overly complex functions that do more than one task and it could simply be broken into multiple simpler functions. I also could leave better comments in my code, I often comment code more with myself in mind than others and commonly leave comments where I think they will be useful to me. As for descriptive names and hard coded numbers I believe I do a good job following these standards in my code.

Going forward I would like to make sure to leave better comments in my code with other people in mind so that they can more easily understand what is happening within the code. As well as, write simpler functions that only do one task so that their functionality is more intuitive and easy for others to understand.