Write code that you want to read

mac.jpg



Want to improve your programming skills? Make this your mantra - “I will only write code that I want to read”. With every line of code you write, you have the power to enhance it and make it more readible for the next developer. Simple to read is always better.

Great code Vs. Spaghetti code

What is Spaghetti code? Well it’s not pretty… There is zero thought involved into writing it, no care taken for variable names, edge cases? nope, and no formatting anywhere in sight.

Below is a example of Spaghetti code:

1
2
3
4
5
6
void doescode(char *str) {
printf(str);
}



Great code - you know exactly what it’s doing.

Take below for example:

1
2
3
4
5
6
7
//Receives the input and prints it to stdout
void printInput(char *input) {
//print to stdout
printf(input);
}



Notice how the above block of code is easy to read?

  1. The top comment outlines the overview of the function
  2. The comment inside the body of the function describes the line below it

There is no confusing what this function is doing.

Simplicity

Keep your code simple and easy to read. Following a style guide is often a good idea. Remember, someone will have to decipher what you have written. Make sure to keep your functions modular and broken down so you can also have the option to re-purpose them.

Time and Code

Time and Code sometimes don’t go so well together. Be wary of time spent coding, if you are taking more than 30 minutes on a function, then you might want to re-address the problem instead of writing code. If the problem is solved, coding is actually the easy part.

Documenting Code

Everyone codes differently. Your style might be very different from another developer, so commenting your code is almost always a really good idea. If you don’t have the time to comment your whole code base, that’s fine, then why not just focus on the complex routines instead? Complex code takes time to understand.

Everyone codes differently. Your style might be very different from another developer, so commenting your code is almost always a really good idea. If you don’t have the time to comment your whole code base, that’s fine, then why not just focus on the complex routines instead? Complex code takes time to understand.

Read more about writing great documentation in our previous post

Summary

In summary, it is good practice to write simple, clean code that is easy to read. By thinking about how other people will read your code, will in turn, increase the quality of your own code. Thanks for reading and happy coding!