Home > DeveloperSection > Interviews > Basic rule for Naming Convention in C#.

Posted on    September-27-2016 6:18 AM

 C# C# 
Ratings:
 1 Answer(s)
  105  View(s)
Rate this:

Abhishek Srivasatava
Abhishek Srivasatava

Total Post:70

Points:350
Posted on    September-27-2016 6:18 AM

Naming Convention : 

1:     Always try to choose easily readable identifiers names. eg. : Identifier to indicate girl height and boy height, we can use GirlHeight and BoyHeight.

Example:
  class Databasehandler  
    {
        int girlHeight, boyHeight; // don't use word like g, h or GlHgt etc. which is not able to understand.
    }


2.     Always use brevity words.

 Brevity means concise and exact use of words in writing or speech.

Example:
 
class Databasehandler  
    {
        int girlHeight, boyHeight; // don't use phrases like elevation_above_ground
    }


3.     Avoid use of underscores, hyphens or any other non-alphanumeric characters. Like don't use Girl_Height, Girl1 etc.

Example:
 
class Databasehandler  
    {
        int girlHeight, boyHeight; // Don't use variable name  like girl1 boy2 etc.
    }
 


4.     Avoid use of Hungarian Notation. It creates contradiction as well as ambiguity to the developer.

What is Hungarian notation?

Notation which start like intA or charC , stringZ etc. are called Hungarian Notation.

There are some language where there is no datatype for example Basic Combined Programming Language (BCPL) so there is necessary to use this notation.

Example:
 
class Databasehandler  
    {
        int girlHeight, boyHeight; // Don't use variable name like intGirl flootBoy2 etc.
    }

 

5.    Do not use identifiers that conflict with keywords of widely used programming language.

Don’t use name as class, exception, string etc.

Example:
 
class Databasehandler  
 
    {
        string errMessage; // Don't use variable name like exception etc.
    }
 


6.   Do not use abbreviation or contradiction as part of identifiers names.

Example:

 

class Databasehandler  
    {
        int girlHeight, boyHeight; // Don't use variable name like girlchar, boyname.
    }
 


7.  Class name should be a noun and meaningful, avoid the use of verbs. Don't prefix (“C”) in class name. Always use 1st word as capital letter.

Example:
class Databasehandler // Don't use word like CdatabaseHandler.  Don’t use handling instead of handling.
    {
        int girlHeight, boyHeight;
    }


8.    Namespace:  names should be meaningful and complete. Always use 1st word as capital letter

Example:
 
namespace SchoolProject.
{
    class Databasehandler
    { }
};


9:  Methods: Always use a  verb-noun pair. Avoid use of camel casing.

Camel Casing means that the word which starts with small letter and then capital letter eg: iPhone. there is no use of spaces or hyphen  in this format

Example:
 
namespace SchoolProject
{
    class Databasehandler
          {
           public void EntryDetails() // Don’t use the word like eDetails etc.
          {
            //
        }
}
};
 

10.  Private Member Variables: if we are declaring private member variable then we should add prefix or suffix to all class level member variables with  (underscore) eg:. m_ or _m.

Example:
 
class databasehandler
    {
            private int entry_;
    }  
 


11.   Interfaces: It should have prefix 'I'  interface.

Example:      
namespace SchoolProject
{
    interface IdatabaseHandler
    {
        int newEntry;
    }

 

12. Custom Exception: We should use ‘Exception’ keyword at the end of naming of custom Exception.

Example:
class databaseException : Exception
{
 databaseException()
        {
           …
        }
    }   

Modified On Sep-27-2016 06:19:22 AM

Don't want to miss updates? Please click the below button!

Follow MindStick