What Is Clean Code?
Since I began programming, I’ve always considered a program to be extremely similar to a math problem. You start off with a little information/idea, work hard towards your solution, and then get your end result. What happens in between the idea and finished product shouldn’t matter, “as long as it works”. The notion of such a simple process was one of the worst mentalities that ever crossed my mind. There are lots of things to be accounted for during the formulation of the end result in programming, similar to math.
How can one person’s code be better than someone else’s?
The distinction between a good mathematician and a bad one is very similar to the test you put a programmer through to see his/her skills. The end product is obviously a huge portion of computer programming, but there are other factors that determine whether your code is beautiful and readable comparable to another programmers’ work.
Some factors I take into account to make my code organized and readable:
Readability
- Can someone look at your code and understand what’s going on?
Or is it a cluster of equations with no specific order? - Ask a friend to read over what you’ve written. If he/she can’t
understand the processes and the functions, consider adding
comments or having better variable names.
Indentation
- Though this may fall under readability, I feel this is something that needs to be pointed out. Sometimes, order and placement of code may not fully matter (in terms of readability), but the way you’re indenting your code is critical for it’s organization.
- If your code looks like an essay, consider indenting; Indentation provides an actual outlook on how processes are being run in the program.
- Example of good indentation:
- Example of bad indentation:
Performance
- Is your code working or is there a bug every time you move your mouse? Make your code as simple as possible for the purpose of keeping bugs out and for keeping functions working.
- Is it simplistic? Some base performance on the amount of lines written for the program. If you can write your program in a minimal amount of lines, you can code well-performed code.
Variable names
- If you’re still using variable names like ‘x’ and ‘a’, you need to learn to be more descriptive. Being illustrative with your variable names will help you use them later, and not initializing more variables with the same algorithm.
If you’re a novice programmer, consider these factors as you write your Hello Worlds. Though they may seem insignificant, once you’re writing thousands of lines of code, creating clean code is needed for your program to work well. If you’ve ever wondered what clean code consists of, I hope I answered your question.