Tuesday 12 July 2016

Programming best practice guide for developers



programming best practice w3workers
Programming Best Practice 
Programming is the most interesting work for coders because you have so many challenges and creativity to do with your programming logic.

Programming work needed sharp brain and concentration to develop efficient code and many times you have to update your coding functions to keep your application free from bugs and optimized to run smoothly on allocated hardware/resources you provided to run that application.


If you are working on a complex and long term project then first step which is you have to keep in mind before starting work on programming how to manage your codes.


So here we will discuss how to manage large project and how to organize project files.


Every programmer having its own choice from largest group of programming language such as Java, ASP, PERL, RUBY, PHP etc and many more it’s your choice which language you want to use for your project.


Once you selected programming language you want to use for coding your project you can go for the next step.


In next step you have to plan architecture for your project to manage coding files so you can efficiently work together with your team.


Large application development needed team work and it’s very important to organize coding files to share with each member of your development team so they can able to work on same files and later can merge files.


Things to keep in mind for best practice coding and project management is describe below:



Select some good Framework:
  1. Frameworks means basic structure like skeleton.
  2. Frameworks is designed to organize your project files with some architecture and having predefined set of functions and libraries to make things easier.
  3. MVC architecture is most popular architecture used by latest frameworks.
  4. MVC stands for models,views and controllers. 
  5. Models used to write database queries and accessible from all controllers so you can manage all database queries in one place.
  6. Controllers to call functions, controllers is only way to access project based on mvc architecture.
  7. Views are HTML files for your projects.
Select IDE or SDK:
  1. Integrated Development Environment (IDE) is a software application consist editor, debugger, automation hints and tools to help coders for fixing errors quickly and can run project to see output.
  2. Software Development Kit is advance software tool for developing computer software or web application with the help of drag and drop components, emulator to run and check output for different devices, debuggers to fixing bugs.

Indentation:


Indentation term is used to provide structure and style to programs blocks. 

Example 1:

function nameNum()
{
      if(condition)
     {

     }
     else
    {

    }
}

Example 2:

function nameNum2(){
     if(condition){

           }else{

           }
}

its your choice what type of structure you want to use for your coding .


Commenting in codes:

Commenting in programming is readable hints to understand about that code by programmers. purpose of commenting to make source code easy to learn by other coders.


Inline-commenting:


// this is comment about variables initialization 

   var i=j=k=0;

Descriptive-blocks:

/**
   *@desc: description about that block of code 
   *@para: parameters purpose in function
   *@result: result return by function
*/

Example:


function addition($num1,$num2) {

/**
   *@desc: for adding two numbers 
   *@para: two parameter num1,num2 
   *@result: result return addition of parameters
*/
$addition = $num1+$num2;
return $addition;
}

These are some example of commenting in codes



  • Code grouping:


when you making a function having multiple blocks of code in that case you should use some lines gap between block.

For example:

  
function example(){
// comment about block1
     block1

// comment about block2

     block2

// comment about block3

     block3
}

We can see more detailed example


function calculation($num1,$num2){

     // variables initialization
     $sum=$multiply=$division=$subtraction=0;
      
     // calculation all operation
     $sum = $num1+$num2;
     $multiply = $num1*$num2;
     $division = $num1/$num2;
     $subtraction = $num1-$num2;

      // result array

      $arr=array( 
                         'sum'=>$sum, 
                         'multiply'=>$multiply,
                         'division=>$division,
                         'subtraction=>$subtraction 
                       );

       // return final result

       return $arr
}

in this example we have multiple blocks in same function to make function readable we need some spacing between blocks and small comments just above that block for quick learning of that block.



Functions naming convection:
when you writing function its very important to keep follow rules and same format for all functions in your code.

For example


Format1: In this example we have camelcase to keep after each word end and new word start with capital later.


function calculateValue(){

}

Format2: In this example we have underscore after each word.


function calculate_value(){

}

So try to follow such rules for all your function and do not make mistake like some function created with Format1 and some created with Format2 make sure which format you want to use and use same format in all functions in your code.


Variable naming convection:
some basic rules of variable declaration are:

allow capital letters (A-Z)

allow small letter (a-z)
allow underscore(_)
allow digits (0-9) only after letters or underscore

some variable combinations:


$num1 (Right)

$num_1 (Right)
$Num_ (Right)
$_num(Right)
$_Num(Right)
$2_Num (Wrong can not start variable with digits)

Do not use all capital letters to define like $NUM,$LENGTH

because in programming capital letters used to define constants. 

its very important to keep make variable readable avoid making variable like $x, $y, $z ,$a , $b, $c, $var1, $var2 or $value1, $value2.


Try to give some meaningful name to variable you using in functions.


Example:  


function area($val1,$val2){

       $val3=$val1*$val2;
}

in this function we have three variables val1,val2,val3 and we can't understand anything with this until we not read function what is the purpose of these variable.


So we can create some meaningful variable instead of val1=>length

, val2=>width, val3=>rectArea

function area($length,$width){

$rectArea=$length*$width;
}

Avoid Repetitions:
Avoid repetition of codes like if you have some section of code
which is you need in multiple function in that case you can create a global function to use it in all place.

Avoid Nesting:

Do not make complex nested loops or if else condition try to keep function smaller and easier to read and manage.

You can create multiple functions instead of creating larger function.


just pass argument to the function and call from place where you need it and avoid nesting and complexity. 



Capitalize SQL Special words:

SQL allows both small and capital letters to create database queries but in best practice try to use SQL special words in capital letters.

For example  


SELECT users FROM table_user;


So these are few things which should keep in mind to make your code readable and easier to maintain.



Thanks for reading if you have any question add in comments section.

No comments:

Post a Comment