Thursday, April 30, 2015

What is good code?

Regardless of your reasons to be writing code, you should be writing good code that does what it has to do and does it well.

This is an introduction to a series of articles about how to improve code quality.

Code is a kind of text used to model objects and processes and it is usually written and edited by humans.

What are the characteristics of good code? How to write good code? What does it mean to write “good code”?

I will define good code in three layman's terms:

Good code is Simple, Consistent and Readable

Simple - Consistent - Readable
No acronyms
These three terms are your principles.
Develop each of them all the time.


This post is not about the best algorithm or the best pattern for the situation, is about good practices that will help you write good code regardless of the programming language or problem domain.

Simple Code:

Simplicity: The quality of being easy to understand or use. The state or quality of being plain or not fancy or complicated. Something that is simple or ordinary but enjoyable"
Merriam-Webster Dictionary

How to achieve simplicity?

  • Find pride in a simple solution to a complex problem not the opposite.
  • Ask questions, don't read minds.
  • Do only what you need to do things well.

How to write simple code?

  • Get a good enough understanding of what you need to accomplish and how to validate the results.
  • Divide your code into simpler parts.
  • Be consistent.
  • Make it readable.
  • Repeat as needed. (Iterate)

Simplification has a positive impact on:

  • Architecture
  • Identification of needs and responsibilities.
  • Project structure.
  • Time/Cost estimation.
  • Code test-ability.
  • Consistency.
  • Readability.

Keep it simple!

Consistent Code:

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

How to achieve consistency?

  • Do things the same way all the time.
  • Have a good and clear reason to do what you do.
  • Keep it simple.

How to write consistent code?

  • Get familiar with common industry patterns and use them.
  • Choose a simple coding standard and follow it.
  • Avoid synonyms. If you choose the word "human" over the word "person", use human always.
  • Favor processes that can be repeated all the time.
  • Test and Unit Test your code.

Consistency has a positive impact on:

  • Good, sustainable development pace.
  • Effective interaction between people and systems.
  • Easier code changes.
  • Simplicity.
  • Readability.

Be consistent!

Readable Code:

The quality of written language that makes it easy to read and understand."
TheFreeDictionary.com

Code is text, technical text, and someone will need to maintain it.
This is really important do not underestimate it.

How to achieve readability?

  • Choose the right words to convey your ideas.
  • Develop one idea at a time.
  • Use correct syntax and grammar.
  • Keep it simple.
  • Be consistent.

How to write readable code?

Apply the principles of simplicity and consistency.

Lets focus on five basic ideas:
  • Verbs mean actions, used them for methods
  • Nouns represent items or things in general, use them for class names (in singular) or collections (in plural)
  • Adjectives and characteristics, use them for variables or properties
  • States represent current, boolean conditions. For instance an engine that is started, has a variable named "started" and a method called "IsStarted"
  • Capabilities are usually boolean conditions. For instance an engine that "can start", has a method called "CanStart"

Readability has a positive impact on:

  • Maintenance
  • Collaboration
  • Simplicity
  • Consistency.

Make it readable!

Final notes

Good code is the result of applying a set of good practices that help you achieve simplicity, consistency and readability.

Each one of this principles builds on the other and together become a powerful tool that you can use to improve your code which in turn will save you time and money.

Writing good code is an art, it takes time and dedication, but start now, you wont regret it.
Start writing good code.

The best time to plant a tree was always 20 years ago.
The second best time is always today."
Chinese proverb

0 comments :

Post a Comment