When we write code, it should follow some standard, which will improve the optimization, readability and quick action on the code, which also improves the security aspects of the code.
Follow the things given below.
- Naming convention
- Check for null
- Removed unused using’s
- Spacing
- Comment
- Code re-usability
- Project warnings
Naming convention
Pascal Casing: First characters of all words are upper case and other characters are lower case. Use PascalCasing for the class names and the method names.
- public class InfoCoutureActivity {
- public void SocialActivity() {
-
- }
- public void ProgrammingActivity() {
-
- }
- }
Camel Casing
First characters of all the words, except the first word are upper case and other characters are lower case. Use camelCasing for the method arguments and the local variables.
- public class EmployeeLog {
- public void Add(LogEvent logEvent) {
- int itemCount = logEvent.Items.Count;
-
- }
- }
For more details, visit here.
Check for null
Wherever null occurrence is possible, make an explicit null check, which avoids the execution error.
- public string GetUserProfile(string userId) {
- if (userId != null) {
-
- }
- }
Removed Unused Usings
Using statement will load all the assemblies, so it’s better to remove the unwanted assembly namespace reference in the codes.
Spacing
It is recommended to use TAB space rather than a white space and this helps for better code readability. Use single space between two lines of codes and the two lines between the methods.
Comment
It is always good to use the comments lines for each class, method and some logic which is too complex for a reader to understand.
Code re-usability
Try to use the available code and use the concepts like generics to avoid the repetitive codes. Code re-usability is the art of using an existing code to achieve better productivity and ease of maintenance.
Project warnings
Always avoid the project warning, which will help hackers to find a path to get into your code and break the applications.