In programming language naming convention in .net are used to reduce the extra effort to read and understand the code. It is an initial step for beginner to understand and learn the programming language.
Various casing styles are used:-
- Camel Case: - First letter of the word is lower case and then each first letter of the part of the word is upper case. e.g.: numberOfDays.
- Pascal Case: - First letter of the word is upper case and then each first letter of the part of the word is upper case. e.g.: DataTable.
- Underscore Prefix: - The word begins with underscore singe and for the rest of the word use camelCase rule. e.g.: _strFirstName.
- Upper Case: - All the letter of the word is in uppercase. e.g.: ID.
Naming Convention Guideline:-
- Namespace: - Pascal case, no underscore. Namespace can be the name of the Company, Technology, if do not have any use the domain name as Namespace.
- Assemblies: - Assembly name can be same as the name of the Namespace if the single assembly is present. An Assembly can follow the namespace.
- Classes and Struct:- Pascal Case, no underscore, never write C or cls before class name. Classes should not have the same name as the namespace. Try to avoid abbreviations and always use the nouns.
- Collections Classes: - Follow the class naming convention and write Collection at the end.
- Delegate Classes: - Follow the class naming convention and write Delegate at the end.
- Exception Classes: - Follow the class naming convention and write Exception at the end.
- Attribute Classes: - Follow the class naming convention and write Attribute at the end.
- Interface: - Follow the class naming convention and start the name with capital letter “I”.
- Enumerations: - Use the pascal case. Don’t write the “Enum” to the end of the Enumertaion.If the enumeration represents a set of bitwise flags; end the name with a plural. e.g.:- SearchOptions (bitwise flags).
- Event: - Use pascal case.
- Variable(Private/Local) or Function:- Use the Camel case. Must begin with a letter, Must be less than 256 characters. Must not contain special characters and keywords. May not have spaces in them.
- Constant: - Use the uppercase for the constant declaration separated by underscore. It is recommended to use the grouping scheme for the naming. Example: AP_WIN_MIN_WIDTH.
- Parameters: - Use camel case. The name should determine the parameter usage. Try to avoid Abbreviations.
- Method: - Use the Pascal case. A method name shows what is going inside it. A method do only one work. Do not combine multiple jobs in the same method.
- ASP.NET Controls: - ASP.NET controls naming is made using the Camel case.
Example: Button=btn. DropDown List= ddl, Image= img, Form View= fv, Repeater=rpt, Range Validator= rv.
Best Practices for Coding:-
- Make coding very simple by reducing the size of the code.
- Make the code readable, for this you should follow the naming conventions.
- Make your program in small modules; break your large program into small blocks that helps to make blocks (components) reusable.
- Not to use images and icons that has text in your application.
- Avoid deep nesting.
- Use the braces, never consider the braces as optional even in single line statements you should use them.
- Use the proper commenting, this will describe the intentions, algorithmic and logical view of the code.
File organization should be proper like:-
- Source file should contain only public type.
- Source file should be given the name of the public class in the file.
- Folder structure should follow the Namespace for the class.