Thursday, May 28, 2015

Consistent Code

The result of a process on which randomness is involved, cannot be predicted.

You want to be right all the time, consistently.

Ironically enough, you want to be consistent in the things that you do well and also in the thing that you are not so good at.

If there is something wrong in your process, you want it to be consistently wrong.

Consistent and predictable errors can be analyzed and corrected, random errors will always come back to haunt you.

Consistency: Steadfast adherence to the same principles, course, form, etc..Agreement, harmony, or compatibility, especially correspondence or uniformity among the parts of a complex thing.The condition of cohering or holding together and retaining form; solidity or firmness."
Dictionary.com

If you have not read my introduction to good code: "What is good code?", please do it. It will take you just a couple of minutes and will give you the context to follow this post.

How to write consistent code?


Get familiar with common industry patterns and use them.
Software patterns are a concrete and structured solutions to common problems in a given context.

If you are building a webpage you might use MVC (Model View Controller) or you could build and API for the server side functionality and MVVM (Model View View Model) or similar for the presentation.

It is not about the features or functionality or a particular framework, is about the naming conventions, files and directory structure, its about the mechanics and the flows of the application.


Choose a simple coding standard and follow it.
Code standards are conventions, agreements that you or people before you made to prevent that software is written with different styles and practices.

Not everybody writes code in the same way, but if they did, things will be easier to understand and to work with. This is why we like code standards.

Some of the leading companies in the software industry have made public their code standard, you could choose the one that you like the most or define your own.

Use a tool to enforce your code standard, I recommend checking out:
Resharper, CodeItRight, JustCode, CodeRush and StyleCop.


Avoid synonyms.
Novel writers try to choose synonyms all the time to avoid cacophony, but in software you want to use the same word all the time. If you choose the word "human" over the word "person", use human always.


Favor processes that can be repeated all the time by everyone.
One of the best examples I can think of is Continuous Integration.

Create an automation for each different part of your development process. For instance: Compile, Test, Package, Deploy.


Document your work.
I favor small simple documentation, automated if possible.

General project information:
Every project you start should have at least a Readme file, it should be in txt or MarkDown or similar, but it has to be a format that can be read without any special tool.

The Readme file should contain: Description of the project, prerequisites, how to compile, how to run, pointer to other important sources of information, Owner or contact information.

Code internal documentation:
Your code should be self documenting, which means that your code is very simplified (check my post on Simple Code), the names that you choose to name your parts are very descriptive and straight to the point (check my post on Readable Code).

If you decide to create internal documentation, keep it to the minimum.

Most of the large and popular development IDEs have automated ways to add comments by typing "///", "/**" or similar. For .Net I like using a tool called GhostDoc (I'm not getting payed to mention it) as it can be integrated in your build process to automatically create your internal code documentation.


Test and Unit Test your code.
If you can apply test driven development go for it, its worth the effort

Creating unit tests is a good way to discover problems with our code early in the game so even if you write them as you go or at the end of your coding they are still valuable.

Automated Unit Tests execution allows you to refactor the code with a "safety net" and helps you have a more predictable outcome.


Consistent directory naming.
First of all, follow your code standard if it already has rules about folder naming.

A folder or directory is like a bucket, a bag, a sack, its a container and is it meant to store files.
It is a good idea to take advantage of the name of the directory to indicate what kind of files it contains.
Directories should be named in plural, a directory that has only one file is a special case in the universe of directories, therefore in this case it can be ignored.

Controller classes should live in a Controllers folder, Exception classes should live in an Exceptions folder, Views should live in a Views folder and so on.

Final notes

In software development, it is of the highest importance to be predictable and consistent.

Consistency allows you improve, allows new team members to get on board faster and it is a most have for code that is meant to be supported.

An organization benefits more from a good and consistent development process and consistent code, than from a randomly awesome piece of software that is also randomly wrong..

0 comments :

Post a Comment