coding standard and best programming practices by iFour Technolab Pvt. Ltd.

25
iFour Consultancy Coding standard, Naming conventions and Good programming practices

Transcript of coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Page 1: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

iFour Consultancy

Coding standard, Naming conventions and Good programming practices

Page 2: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Naming conventionsDesign conventionsGood programming practicesCode readabilityException handlingCommenting conventionsAvoid nested loopingCode reusabilityNaming convention in ASP.NET MVC ApplicationNaming convention for Controller Actions

INDEX

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 3: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Naming conventions

Pascal Casing : First character of all words in uppercase and other characters are small Use Pascal casing for Class names and Method names

Camel Casing : First character of all words, except the first word are Upper Case and other characters are lower case Use Camel casing for variables and method parameters

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 4: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Naming conventions

Use the prefix “I” with Camel Casing for interfaces ( Example: IEmployee)

Do not use Hungarian notation to name variables.

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 5: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Naming conventions

Don’t use abbreviations for variable declaration, Use meaningful or descriptive words

Do not use single character variable names like i, n, s etc. One exception in this case : variables used for iterations in loops:

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 6: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Naming conventions

Use predefined type names instead of system types like Int32, Boolean, String, etc

Do not use underscores (_) for local variable names

Exception : you can prefix private static variables with an underscore

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 7: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Design conventions Use appropriate prefix for the UI elements

Control Prefix

Label lbl

TextBox txt

Button btn

Hyperlink hlk

DropDownList ddl

CheckBox chk

CheckBoxList cbl

ListBox lst

Image img

RadioButton rdb

Table tbl

Panel Pnl

GridView gv

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 8: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Good programming practices

Avoid writing very long methodsA method should have 1-25 lines of code. If a method has more than 25 lines of code, re-factor into

separate methods Method name should tell what it does. Don’t use misleading names. If the method name is

obvious, there is no need of explaining what the method does Good :

Not Good :

Do not have more than one class in a single file

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 9: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Code readability

A method should do only one operationDo not combine more than one operation in a single method

Good :

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 10: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Code readability

A method should do only one operation Do not combine more than one operation in a single method

Not Good :

Do not hardcode strings, use resource files Do not write comments for every line of code and every variable declared

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 11: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Good programming practices

Use enum wherever requiredDo not use numbers or strings to indicate different values

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 12: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Good programming practices

Use enum wherever requiredDo not use numbers or strings to indicate different values

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 13: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Good programming practices

Always use String.Compare or String.Equals for string comparison. This will ensure the string will match even if the string being compared has a different case

Use string.IsNullOrEmpty() instead of “” or null

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 14: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Good programming practices

Use #region to group related pieces of code together. If you use proper grouping using #region, the page should like this when all definitions are collapsed

Keep private member variables, properties and methods in the top of the file and public members in bottom

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 15: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Good programming practices

Always check for unexpected values, Example : if you are using parameter with 2 possible values, then never assume that if one does not match then only possibility is the other value

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 16: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Good programming practices

Use blank line to separate logical group of codes

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 17: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Good programming practices

Never hardcode a path or drive name in codeGet the application path programmatically and use relative path

Avoid having very large files. If a single file has more than 1000 lines of code, it is a good candidate for refactoring. Split them logically into two or more classesAvoid Passing too many parameters to a method, if method having more the 5-6 parameters

then good to define a class or structure

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 18: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Exception handling

Always use to try-catch block for exception handling and Never do a catch exception and do nothing If you hide an exception, you will never know if the exception occurred or not. In the case of exceptions, give a user

friendly message , but log the actual error with all possible details about the error

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 19: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Commenting conventions

Comment should be always same level as the code : Use Ctrl+K+D for Format document

Use // or /// for comments. Avoid using /* … */

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 20: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Avoid nested looping

Avoid Deep Nesting : Too many levels of nesting can make code harder to read and follow

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 21: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Code reusability

Avoid repeated code : The same code should not be repeated more than twice, create method or function for that

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 22: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Naming convention in ASP.NET MVC Application

Controller : Its name must end with ‘controller’ word. E.g. PersonController, EmployeeController Generally all controllers should be kept inside ~/Controllers folder of the project

Model : Model name should be similar to the database table name (not mandatory). For example, if the database table name is "Person" then the model name should be "PersonDetail“Generally, all models are kept inside the ~/Models folder of the project.

Views : There should be a view folder inside ~/Views folder corresponding to each controller. Like for PersonController, there should be a folder ~/Views/Person.

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 23: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Naming convention for Controller Actions

Here is the table of suggested standard names for controller actions:Controller Action

Type URL Description

Index GET Employee/Index Displays a list of employeesDetail GET Employee/

Details/1Displays a single employee detail with an Id of 1

Create or Add GET Employee/Create Displays a form for creating employeeSave or Insert POST Employee/Save Insert a record into the databaseEdit GET Employee/Edit/1 Displays form for editing an existing employeeUpdate POST Employee/Update Update an existing recordDestroy GET Employee/

Destroy/1Delete an existing record

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 24: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

https://msdn.microsoft.com/en-us/library/ff926074.aspx https://www.codeproject.com/Articles/8971/C-Coding-Standards-and-Best-Programming-

Practices http://www.dofactory.com/reference/csharp-coding-standards

References

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India

Page 25: coding standard and best programming practices by iFour Technolab Pvt. Ltd.

Questions?

http://www.ifourtechnolab.com/

ASP.NET Software Development Companies India