Coding Standards and Conventions

For some time I've being gathering ideas from here and there about Coding Standards and Conventions...

There are many opinions about coding style and, from what I see, the only way to follow rules that don't really exist (strictly I mean) is to put your personal experience in game. Of course that .Net has its own concept, but things tend to be a little blurry when people come from other languages.

Some ideas

                Naming Conventions:

·       Pascal Case: First letter of all words in Upper-case (PascalCase).

o    Used for names of NameSpaces, Classes, Methods, Properties, etc…

Eg.:

class ClassName

{

               public string Property {get; set;}

              

               private void TestMethod()

               {

                               //Do some work here…

}

}

o    Namespace: <CompanyName>.[.Feature][.Design]

Eg.:

     StretchSoftware.Web.Control

 

·       Camel Case: First word Lower-Case and the rest of words like Pascal Case (camelCase).

o    Used for parameters and variables.

Eg.:

.

.

private string personName;

private void MethodNameHere(int number, string name)

{

                   //Code Here…

}

.

.

 

Tips:

v  When naming a variable, parameter or even a method, do not use abbreviations, try to use a name that describes the variable usability, if you’re sharing code it will be easier to read the code.

Eg.:

               string userName;

                              

 

v  When using Boolean variables is easy to know the type if you use a prefix like “is”.

Eg.:

               bool isConnected;

 

v  Regarding UI elements the important thing is to distinguish from other elements and can be done using a prefix that reflect the component, like lbl (Label), txt (Textbox), lst (Listbox) and so on.

Eg.:

               Label lblTitle = new Label();

               

                Indentation:

·         Use Tabs instead of spaces (usually 4 spaces tab size).

·         Always keep your curly braces ({}) align at the same level of column.

Eg.:

   .

   .

   {

                   //some code here

   }

   .

   .

               

                Clean and Organized Code:

·         Use regions (#region) to group code.

You can organize your code by creating region to organize private methods, public methods, properties and do on.

Eg.:

               #region PRIVATE METHODS

               private void TempMethod()

               {

                               //Code…

}

               #end region

 

 

 

               #region PUBLIC METHODS

               public void OtherTempMethod()

               {

                               //Code…

}

               #end region

 

·         Use XML comments to describe what a certain method does.

This can be a great advantage because you can later use those comments to generate Documentation using SandCastle or any similar software.

 

·         Comment your code has much as you can, but you don’t have to comment every line, just enough to be comprehensive.

·         Try not to use multiline comments “/**/” unless you’re in the middle of developing process.

 

                Good Practices:

·         Use methods for a specific job. A method should only do one job if you need more create another method.

·         Avoid doing methods with more than 30 lines of code, if you’re doing a huge method like this you should break it into as many parts (methods) as you want.

·         Use constants if the value is not meant to change.

·         Use string.empty instead of “”.

·         Use variables as private and use properties to expose them.

·         Always try to use methods instead of coding directly in event scopes.

·         Throw specific error messages.

·         Use public properties and methods only if they are meant to be accessed from outside, otherwise use internal if they’re going to be access from other classes at the same assembly.

·         Use one variable declaration per line.

·         Use StringBuilder to build a string instead of string. For bigger operations using string is a lot heavier than StringBuilder.

·         Is better to process your code with If statement then doing lazy code with Try..Catch. Try-Catch statements are meant to catch exceptions that are beyond the programmers control.

·         If you’re not looking to store multiple types of objects, use List<> instead of ArrayList and you will gain some performance.

0 comentários:

Post a Comment