Write code that is easy to read – Clean Code Post 4

Writing code is relatively easy, but reading it can be hard if care has not been taken by the Author of the code. Taking the time to write clean code pays off as you are not just making your live easier, if you have to revisit code, but you are also making future readers of your code lives easier too.

Writing sloppy code injects technical debt into your code base and as you may know technical debt is depressing. Technical debt de-motivates people and will potentially drive people away from your organisation.

Being sloppy slows you down in the long run as your code is likely to have bugs and need tidying up in the future.

One thing you can do to improve the quality of you code you are writing is to follow the TED rule:

Terse
Your code should not be excessively wordy

Expressive:
It is clear what your code is trying to do

Do one thing
Your code should have a clear responsibility. It should do that one thing well

Make use of Enums – Clean Code Post 3

It is better to make use of an Enum than to use hardcoded strings in your code. I have heard the use of hardcoded strings be referred to as Stringly Typed rather than Strongly Typed. Using an Enum would make your code Strongly Typed.

Here is an example of some bad Stringly Typed code:

if (customerType == "Premier")
{
                
}

Here is an example of how you can improve this code by making use of an Enum called CustomerType:

if (customer.Type == CustomerType.Premier)
{
               
}

The code to define the CustomerType Enum is below.

public enum CustomerType
{
   Standard, Elevated, Premier
}

By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1. In the CustomerType enumeration above, Standard is 0, Elevated is 1 and Premier is 2. If you wanted to assign different values you can do something like the code below:

public enum CustomerType
{
   Standard = 100, 
   Elevated = 200, 
   Premier = 300
}

If you want to retrieve the integral value from an Enum you can use the code below:

   var customerType = (int)customer.Type;

Make use of Ternary (Conditional) Operator – Clean Code Post 2

It is more elegant in code to use the Ternary Operator than an IF statement. The Ternary Operator is also commonly known as the Conditional Operator.

Here is a C# example of a Ternary Operator in use:

int bonus = employee.Age < 50 ? 20 : 40;

You can see that it takes up less lines of code then the IF statement below:

int bonus;

if (employee.Age < 50)
{
  bonus = 20;
}
else
{
  bonus = 50;
}

You can see that it is much cleaner to use the Ternary Operator and results in using one line of code rather than ten.

Writing Clean Code

I recently watched a very good course on Pluralsight on writing Clean Code.  It is called Clean Code: Writing Code for Humans and is authored by Cory House.  I highly recommend it if you are lucky to have a Pluralsight subscription.

After watching the course I thought it would be good to write a few posts on writing Clean Code, which is what I plan to do over the coming weeks.  I will be including some tips I have picked up over the years.

Other Resources
There are various other resources already available on writing clean code and on how to improve as a Software Engineer.

Robert C Martin (Uncle Bob) has a collection of videos that you can pay to watch on Clean Code on the website https://cleancoders.com.

Below are a few books that are also well known in the industry

  • Clean code by Robert C Martin (Uncle Bob)
  • Code complete by Steve McConnell
  • The pragmatic programmer by Andrew Hunt and David Thomas

If you are reading this and have any other suggestions please feel free to leave a comment with the details.