Write code that you want to read
ADVERTISEMENT
Table of Contents
Introduction
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:
void doescode(char *str) {
printf(str);
}
Great code - you know exactly what it's doing.
Take below for example:
//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?
- The top comment outlines the overview of the function
- 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.
Conclusion
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!
Final Notes
Recommended product: Coding Essentials Guidebook for Developers