Understanding and How to Apply Clean Code
First let me explain what a clean code is, a clean code is a code that when everyone reads it they will understand the function of the code easily and that it is easy to modify. Understanding the function means you understand the whole concept of the code, even how the objects interact each other and what a variable consists of.
Why Should Programmers Care About Clean Code?
Imagine getting stuck on coding and you needed a reference on solving it, you found only 1 post that has the title that can solve your problems but after you look at the code, you can’t understand a single line and now you are stuck again. However if the code was clean you would be able to understand how it works and then solve your problem.
So in essence, clean coding is like public service, helping people who are troubled by their coding problems and also might help you if you ever need reference, a mutual relationship between programmers.
How to Apply Clean Code
By following a set of rules we can make a clean code, these sets of rules are:
Correct and Understandable Naming
A variable, class or function should have names that makes the reader understand what it does, easiest way is to name it after by what it does or what it consists of.
Functions
A function should have a clear name, easily describing what the function does, and does only 1 thing and nothing else. So if you want to make a function that print sum of integers in an array then the name should describe that it sums integers and that it only prints the sum. Another rule is that functions should keep as less argument as possible to avoid conflicts because if there are too many then high chance that it collides with another function.
Commenting Only Necessary
Comment only when you cannot describe it with codes because ideally there should be no comments, with correct naming there should be no reason to describe your codes. An example of commenting would be the site where the source code could be found.
Dont Repeat Yourself(DRY)
There should be no repitition of codes as writing many lines of codes could make the code messy. A clean code should be easy to read and change, if there were duplicate codes then you have to change all the duplicates but if there was no repition there you just need to change in one place.
Group Files That are Connected
To make people easier to read a program would be to group and save files that are connected to each other and in a folder with names that are easy to understand, an example would be code files should be saved in a folder with the name “src” and images are safed in another folder.
It can be very hard and tedious to write a clean code,however by applying clean code you can help other people from your code and in the future it will help yourself in becoming a better programmer.