Search
Winners

Win Prizes

Social Presence
Like us on Facebook
Advertisements
Ads

Coding Standards

Vuyiswamb
Posted by Vuyiswamb under Design Pattern & Practices on 10/5/2011 1:38:09 PM | Points: 75 | Views : 10170 | Status : [Member] [MVP] [Administrator]

As is said before in my previous posts, that every development
house or any company that attempts to create software, should have coding
standards. If you don't have one in your company, I suggest you continue
reading this post because you might learn a lot.  Microsoft or any company that have an ability to create a
programming language does not need to set coding standards for your company. But the coding standards can be defined within the organisations and agreed
upon.



There are many companies that have stand-alone programmers,
in that case the standards are not a priority and enforcing them is good but it don't
make much of a difference, if there is one person writing the code. But it
becomes a problem when the team grows. Bad habits into the code get's introduced
and everyone is doing his or her thing. This makes an application not maintainable.



Normally the coding standards are defined by a Technical
Lead or a Development Manager. But Developers can contribute towards the
formation of the coding standards and these open opportunities for everyone in
the team to raise concerns about certain evil and good behaviour's they have
come across before.



Every company that write software, want to reduce the number
of support calls they receive monthly or year or even daily. These support calls
can be reduced if the quality of the software is evaluated accurately and timeously.

The Quality of Software is defined by
your support calls that are software related that you receive daily or monthly. So basically to see if there is a quality issue
in the software or there is a QA issue , you must look at the support calls you
receive every financial year and compare it with the previous year, this should
give you an idea. Not all Companies can afford to have a QA department, but most of
the companies depend on the developers  to deploy a quality product in production. But that is not always the case, Developers always let
the bugs to production, it is a fact. it is not a responsibility of a developer
to do a QA, but I expect a Developer to test his code thoroughly and follow the
coding standards.



How do draw coding standards? Well it is simple. These are
just rules that can be numbered as Follows.



 



Rules



  1. Never use naked SQL statements inside your
    pages or Presentation Layer,
    Use a Stored Procedure, no matter how small the
    functionality that the Stored Procedure needs to do.
  2.  Don't Build a String using a string class,
    use String Builder instead
  3.  All the local variables should be prefixed
    with "m_"
  4.  "m_Age"
  5.      Never create a method that accepts more than
    5 parameters, it's better to use a class.
  6.       If you have SQL joins that join more than 5
    Tables you must know that your System is poorly designed, the more it grows the
    more performance issues you will hit.
  7.       Always Trap for Exception if you are not
    the provider of the parameter (Never Trust the User's Input.
  8.       Don't Declare a Variable inside a loop
  9.       Don't Wrap a code in a try, if it does not
    execute anything e.g.



















    public void tEST()


        {

           

 

            try

            {

 

con = new SqlConnection(strCon);

 

            cmdselect = new SqlCommand();

 

            cmdselect.CommandText = "sp_Registeruser";

 

            cmdselect.CommandType = CommandType.StoredProcedure;

 

            cmdselect.Connection = con;

 

            cmdselect.Parameters.Add("@FIRSTNAME", SqlDbType.VarChar).Value = RegistrationModel.sFirstName;

 

            cmdselect.Parameters.Add("@LASTNAME", SqlDbType.VarChar).Value = RegistrationModel.sLastname;

 

            cmdselect.Parameters.Add("@Username", SqlDbType.VarChar).Value = RegistrationModel.sUsername;

 

            cmdselect.Parameters.Add("@Password", SqlDbType.VarChar).Value = RegistrationModel.sPassword;

 

            cmdselect.Parameters.Add("@GUIDR", SqlDbType.VarChar).Value = RegistrationModel.sGUIDR;

 

            cmdselect.Parameters.Add("@Email", SqlDbType.VarChar).Value = RegistrationModel.sEmail;

 

            cmdselect.Parameters.Add("@Tel", SqlDbType.VarChar).Value = RegistrationModel.sTel;

 

            cmdselect.Parameters.Add("@Province", SqlDbType.VarChar).Value = RegistrationModel.sProvince;

 

                con.Open();

 

                cmdselect.ExecuteNonQuery();

            }

            catch (SqlException ex)

            {

                throw ex;

            }

            finally

            {

                con.Close();

            }

 

        }



 



Why would you put
this code inside the try,only the part that should open the connection and
execute the command is suitable to be in a try block.



  

  public void tEST()


        {

            con = new SqlConnection(strCon);

 

            cmdselect = new SqlCommand();

 

            cmdselect.CommandText = "sp_Registeruser";

 

            cmdselect.CommandType = CommandType.StoredProcedure;

 

            cmdselect.Connection = con;

 

            cmdselect.Parameters.Add("@FIRSTNAME", SqlDbType.VarChar).Value = RegistrationModel.sFirstName;

 

            cmdselect.Parameters.Add("@LASTNAME", SqlDbType.VarChar).Value = RegistrationModel.sLastname;

 

            cmdselect.Parameters.Add("@Username", SqlDbType.VarChar).Value = RegistrationModel.sUsername;

 

            cmdselect.Parameters.Add("@Password", SqlDbType.VarChar).Value = RegistrationModel.sPassword;

 

            cmdselect.Parameters.Add("@GUIDR", SqlDbType.VarChar).Value = RegistrationModel.sGUIDR;

 

            cmdselect.Parameters.Add("@Email", SqlDbType.VarChar).Value = RegistrationModel.sEmail;

 

            cmdselect.Parameters.Add("@Tel", SqlDbType.VarChar).Value = RegistrationModel.sTel;

 

            cmdselect.Parameters.Add("@Province", SqlDbType.VarChar).Value = RegistrationModel.sProvince;

 

 

            try

            {

                 con.Open();


                cmdselect.ExecuteNonQuery();

            }

            catch (SqlException ex)

            {

                throw ex;

            }

            finally

            {

                con.Close();

            }

 

        }



This has nothing to do with performance, but it makes the code
readable. Even if you put the command object in a try or outside it
does not make a difference , if there is something wrong in the parameters or
the object it will fail.



 



Well the list can be endless, feel free to add more.



 



Thanks 


Thank you for posting at Dotnetfunda
[Administrator]

Comments or Responses


Login to post response