How to Code Properly? | Best Practices
This blog covers the best practices that should follow whenever you code.
Ask yourself, do you care about your code readability, when it works fine?
I think most of us NOT including me but, I realized later, how important readability of code is.
This article helps you follow best practices in your coding career.
So without any delay let’s dive in…
Readability of Code is the universal aspect in the whole Computer Programming.
Being a Programmer doesn’t mean you just write the code that works fine, but it should be written smartly and looks pleasantly! to eyes, so that your team or people that work with it afterward, can understand it easily.
➤ Consistent Indentation:
In most of the programming languages, spaces and indentations do not affect the function or method, but it compromises with readability, edit, and understanding of code.
So the rule of thumb is to be consistent with your style.
If you are using an IDE, it is recommended that you create a formatted config, so everyone uses the same one.
DO NOT
int do_stuff(a,b){
int c;
c=a+b;
return c;
}
DO LIKE
int do_stuff(a, b)
{
int c;
c = a + b;
return c;
}
Note: //the space between operators, //follow either one thing spaces or tabs in the entire document. and should look pleasant and easy to read.
➤ Consistent Name Scheme and Follow Design Patterns:
When coming to the name scheme, the general rule of thumb is to be with your coding language.
Every language follows a certain naming scheme:
-Java -> CamelCase
-GOM0-> MixedCaps
-.Net/C# -> PascalCase
-Python and Ruby:
UpperCamelCase for classes, CAPITALIZED_WITH_UNDERSCORES for constants, and lowercase_separated_by_underscores for other names.
➤ Commenting & Documentation.
Often we forget to write comments wherever needed, but it helps you and other people to understand what the block of code is doing? just at first glance.
It helps you to memorize your logic that you have written while writing that code.
In any organization there are many programmers who work on the same or similar project. So the well commented code blocks helps them to easily understand logic behind solving any problem
Note: Avoid Obvious Comments such as commenting about what the operation is doing “adding two vars”. Here the operation itself is saying what the expression is about…
Example:
DO LIKE
//About what is doing simple words, author Name, version No.
function do_stuff()
{
------
-----
}
➤ Avoid Deep Nesting in code
Deep Nesting kills readability and is highly error-prone.
DO NOT
if (is_writable($folder)) {
if ($fp = fopen($file_path,'w')) {
if ($stuff = get_some_stuff()) {
if (fwrite($fp,$stuff)) {
// ...
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
}
DO LIKE:
function do_stuff() {
// ...
if (!is_writable($folder)) {
return false;
}
if (!$fp = fopen($file_path,'w')) {
return false;
}
if (!$stuff = get_some_stuff()) {
return false;
}
if (fwrite($fp,$stuff)) {
// ...
} else {
return false;
}
}
Here Some of the tips you can read here
➤ Follow DRY Principle
Also known as Duplication is Evil, Dry Stands for Don’t Repeat Yourself.
For example, most web applications consist of many pages.
It's highly likely that these pages will contain common elements. Headers and footers are usually best candidates for this. It's not a good idea to keep copy pasting these headers and footers into every page.
A famous quote on the same by Andrew Hunt--
“The Dry principle tells us to keep the low-level knowledge in the code, where it belongs, and reserve the space for other, high-level explanations. ”
➤ Code Refactoring:
Code Refactoring doesn’t include bug fixes or changing it’s Functionality, it just includes improving readability, making it shorter in length, improving its efficiency in terms of space and time.
Example:
Append first 10 elements in a list:
DO NOT
def appen10():
list1 = []
for i in range(1,11):
list1.append(i)
return list1
DO LIKE
def append10():
list2 = [i for i in range(1,11)]
return list2
If you like the content or if you have any suggestions, requests, proposals then just drop a comment. I will respond or bring the newer content frequently.
Share this content with your friends and help them to understand “How to code properly and the best practices of it ” which is the most important aspect in every programmer’s career.
Thank you...
Comments
Post a Comment
If you have any doubts please let me know