Code Heuristics

21 Oct 2014

In our profession we are not always taught to care about the quality of our code. Evaluating and making the distinction between good code and bad code can be subjective, and team dependent.

I think we all can agree upon certain common standards for what is good, or desirable in our code. The problem is that as you go from evaluating syntax, to names, abstractions, and the very structure of the code and the system, it gets more complex to discern good from wrong. What you value will vary depending on the problem at hand, your team, and even the organization. This is probably the reason why a universal list of heuristics can't exist.

However, to know that such value systems exist might be good enough to start developing your eye for good code and come up with your own heuristics.

Some of my favorite measures to achieve a good system are:

  • Remove comments

    Code should be self-explanatory, comments quickly become obsolete and in most cases we can get rid of them by using descriptive names. In the rare case that a comment is required, be concise.

  • Automate system builds and testing

    Get the system running with a single command, run all the tests with a single command.

  • Avoid selector arguments

    A selector argument dictates the functionality of a method, i.e. a method does two or more things at different times, depending on the value of an argument. Instead, call each of those smaller methods when you mean to.

  • Consistency

    Pick carefully the names and metaphors you use, and then use them consistently throughout the system.

  • Eliminate duplication

    Repeated constants can become variables, repeated blocks can become functions, repeated functions can become modules or classes, and so on... You get the idea: raise the abstraction level.

  • Use descriptive names

    This goes in hand with eliminating comments, and making your intention clear.

Each team member must be mature enough to realize that it doesn't matter a whit where you put your braces so long as you all agree on where to put them. (Uncle Bob, Clean Code)

Different teams will value different things, what matters is that you and your coworkers understand the reason behind your standards, and behave professionally by respecting them.