Many software developers have learned programming without even learning its principles and foundations that make the code more organized, readable and understandable, so a lot of the code seems complex and difficult to understand and it is cumbersome to review and fix its errors.
If we presented you with an incomplete novel and asked you to complete it, you would first have to understand the whole story in general and then get to know the characters of this novel and their roles in its chapters, and which character is the main one who will complete the journey with you to the end of the novel and which character will have a limited role and appear In a few chapters and ends early, you also need to know what connects the characters as well as the parts of the story to each other.
Programming is a lot like completing a novel by another programmer, the variables are the characters of your story and you have to understand them and know their role and interrelationship with the different functions in the code in order to be able to complete them and add your lines with ease and mastery. And if the code lacks clarity, it will turn into a horror story or a terrifying nightmare for you to deal with.
It is easy to write code, but writing perfect or excellent code or what is called (clean code) can be a bit challenging. The code can be bad in many ways, it can be messy full of redundant variables, or its cohesion may be spoiled by a simple modification, Or it has not been properly designed and succeeds when tested once and fails when tested with different inputs.
Here are some programming principles that are important to follow as you code, to help you become a better programmer, and improve the level, quality, and clarity of your code in an effort to reach the perfect code (if you will):
Separate tasks and don’t group them in one place
Try as much as possible to make every function or every first class every file does one job, do not stack jobs in one place, separating jobs reduces the size of the code and makes it easier for the programmer to follow and understand the code and thus facilitates the process of maintenance and smooth access to the place of errors and repairs. It must also be taken into account that the functions are not dependent on each other, for example, the part of the code that deals with the database, does not necessarily need to know how to display this data on the browser. Modifying one of them will not have to modify the other with it. This results in a blade that is easy to adjust and repair.
Make it simple!
Make your code simple and generic, don’t try to complicate things and appear like a programmer who can write complex code, you are not programming to impress anyone, if you can write what is required in one line then do it!. Make variable names clear and meaningful and make use of the available ready-made libraries, because this is one way to learn programming.
Make your code reusable so that you (and other programmers) can make use of it in multiple programs, adding what you need without modifying the original code. Try to write only the necessary lines and avoid branching out to situations that obviously won’t happen, or adding solutions to problems that don’t exist!, and remember that your code will need maintenance, the more lines of code you have, the more testing and maintenance you need.
If your code needs long lines of notes to explain what you’re doing, it may be because it’s a bit complicated, so you’ll need to review it and try to rewrite it in an easier and more meaningful way. This can often be due to errors in the design process, choosing the right model for your programming problem, or choosing an algorithm that isn’t quite right for what you’re doing.
Write notes on only what is necessary
Comments and notes on the code are important, but they should not be overused or placed on each part of it, try to choose the appropriate places where the code may seem unclear or to justify placing unusual code, it is always preferable to have a note at the beginning of the code to explain its purpose, contents, and He wrote it and when it was written, this note will make it easier for you to understand the code when you return to it after leaving it for a period of time rather than having to read it in its entirety and follow it line by line to understand it again.
Test your code and make sure it’s of good quality
Run many tests and experiments on your code, replace manual tests with automated test programs, learn these systems well and use them often.
Don’t repeat your code
If you have a variable value that is repeated more than once in your code, make it a global constant that can be accessed from more than one place.
Refine your code
The first version of your code doesn’t have to be perfect, you may have to revise it more than once, your code is constantly evolving and it’s normal to review your work and try to improve it as much as possible, or even rewrite it. This is not a disadvantage at all, look for what can be shortened or improved and make it more effective while of course keeping your results matching what is required of you.
Regardless of your quality as a programmer, in programming your job is not just to write code that works well. Always keep in mind that you are writing your code for other programmers and not for a machine, and that your code is not set in stone so it must be flexible, simple, scalable, modifiable and additive. And remember a good programmer means good readable code.