As a developer you will always encounter situations where there are multiple ways to accomplish the same task. Evaluating your options can be done by acting as if you are reviewing the resulting code.
The 4Cs are a useful tool for doing this and you’ll get better at each with pratice. Code clarity makes projects easier to work on for all levels of developers by being as close to self documenting as possible. If you can read a piece of code and understand immediately what it is doing, you’ll end up saving more time than if you wrote the most succinct code which can end up being slower to read. Clarity has a huge payoff but can be difficult to master. Fortunately it is easy to practice as it’s just as important that a single line be clear as an entire classes or method definition.
Let's use the following example:
We’re importing data for a project which contains ZIP codes stored as strings. Some records have a full nine digit ZIP code while others just have the more common five. For the application’s purposes, we do not need more information than the first five digits. Let’s look at a few ways to get the first five characters of a five or more character string:
Ruby’s String
All of the above are correct, complete, and concise but
This choice isn’t perfect and arriving at it takes a bit of thought. It’s a method added to String from Rails and not native Ruby. While the coupling is not concerning as it is incredibly unlikely the application will be separated from Rails, a developer could be confused as to why
Furthermore, I checked the source and
Reviewing the other options also places them below
This simple example illustrates how small choices can shape your project and how to quickly evaluate them. It may seem minor but the clarity of your codebase will reflect the clarity of each and every line. Don’t get too wrapped up though. There are often two approaches that are equally as clear. It should take longer to read this post than to make that decision as clarity has a feel about it. In a future post I will demonstrate how some choices are equal and their differences may not necessarily matter. In these cases making a fast decision is for the best.
Do you think different? Is there an option I forgot or discounted too quickly? Let me know in the comments or on Twitter.
Aaron Rosenberg (@agrberg)