Skip to main content

How to Code Properly | Best Practices

 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.

 

 

 

$this->load->view('includes/header');

$this->load->view($main_content);

$this->load->view('includes/footer');

 

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

Here are some posts you should view for more information.

How to install Django step by step easily

  How to install Django in windows easily! The step-by-step process of how to install Python and Django in your Windows machine. Are you excited? Let’s go... Step 1: Check if python3 is installed already. Inside cmd where python (It should print paths to the python3 where it’s installed) Step 2: Install Python in windows if you don’t have one . Search python on google, or you can click here for the official python website.  Hover your mouse on the Downloads section and click on Windows.  Now you should see the python releases page for windows, click on your desired python3.x version, recommended version 3.7+ Scroll down ⇾ click on Windows installer based on your system architecture (You can check your system architecture here ). Follow the guidelines of installation setup, while installing make sure you’ve checked “ Add python to path ”. For the full process of installing python click here . Step 3: Check python has recognized in your windows command prompt. Inside cmd python --versi

My journey from graduate to Python Developer and mistakes I made in the same

Let's Get Started...                                                                                                                                 What you should expect from this blog... Mistakes that are made by me throughout the course of Computer Engineering. How I should avoid those mistakes with appropriate decisions. Why I chose python as my primary software developer tool. How did I learn my first programming language? Top myths about Python programming language. How I got my first job? Note: This blog going to be big, I divided it into different parts, In this blog, I only shared what are the mistakes made by me and how you should avoid them. so in this blog 1st, one of those above bullet points is going to be covered  Mistakes that are made by me throughout the course of engineering and the cost I paid for that. My first and big mistake about my computer engineering that cost  1.5 years of waste of valuable time. When I already know that I'm gonna take my admission