← Software

Setting Coding Standards and Reviewing Code

4 min read

This post is also available in Turkish: Kodlama standartları belirlemek ve Kod review etmek →

C# .NET coding standards with some examples:

Language and Framework Versions: You should specify which C# language version and which .NET Framework version will be used.

Naming Conventions: Specific rules should be set for naming variables, methods, classes, enums and other constructs. For example, variables starting with a lower-case letter, methods being named as verbs, classes being named in PascalCase, and so on.

Code Formatting Rules: Code formatting matters for readability. Specific rules help standardise formatting features such as indentation, spacing and the use of curly braces.

Property Management: Specific rules should be set for the properties of classes and methods, such as the access levels of properties, how fields are defined and what properties do.

Error Handling: Error handling is important for the stability of applications. Setting specific error-handling standards helps the application be better prepared for unexpected situations.

Documentation: Specific documentation standards should be set so that code is readable and understandable. This will be useful for understanding what the code does and how it works.

General Rules:

Code should be understandable, readable and consistent.

The names of variables, classes, methods and other constructs should be descriptive and meaningful.

A specific formatting standard should be used throughout the code.

Unused code blocks and comments should be cleaned up.

Class Rules:

Classes should be named in PascalCase.

Class fields, properties and methods should be declared private, and only declared public when needed.

Constructors, finalisers and other special methods should be placed at the top of the class.

Suitable interfaces should be used according to the logic of the class.

Method Rules:

Methods should be named as verbs.

Methods should take at most 3 parameters.

The complexity of methods should be kept low.

Methods should be protected against errors with try-catch blocks.

Variable Rules:

Variables should start with a lower-case letter.

Variables should be descriptive and meaningful.

The use of global variables should be kept to a minimum wherever possible.

The access levels of variables should be restricted when necessary.

Comment Rules:

Comments in code improve readability and understandability.

When commenting code, comments should be descriptive and meaningful.

Adding comments to help others understand the code makes it more readable.

Exception Rules:

Error handling should be done with try-catch blocks.

Catch blocks should include an explanation of how the error can be fixed.

Throwing exceptions should be kept to a minimum wherever possible.

Examples in Code:

**

**

Code Review:

Code review is the process of going over written code in terms of quality, readability, performance and security. Code review is important for improving code quality and for reducing costs by catching errors at an early stage. Here is an example of a code review methodology:

Planning: The people who will carry out the code review need to be chosen, and time needs to be set aside for it. A timeline should be set for the review process, and this plan should be followed.

Preparation: Before the code review, the person who wrote the code needs to tidy it up along with its comments. Once the code has been cleaned up, the reviewers are given a copy of the code they will be reviewing.

Review: The review process begins and the code review team goes through the code step by step. During the code review, the following steps are followed:

a. Code quality: It is checked whether the code quality standards have been followed. In this step, the readability, maintainability, testability, reusability and consistency of the code are checked.

b. Performance: It is checked whether the code is suitable in terms of performance. In this step, things such as whether the code has been optimised and whether there are any problematic spots in terms of timing are checked.

c. Security: It is checked whether the code is secure. In this step, things such as identifying possible security vulnerabilities in the code are checked.

d. Debugging: It is checked whether the code is testable from a debugging point of view. In this step, the code is tested against unexpected errors, and errors are identified and fixed.

Follow-up: At the end of the review process, the code review team needs to compile the errors found and the suggestions made into a report and record it. This report can be used for reviewing the code again later or for fixing problems.

Code review methodologies can be applied in different ways by different companies and teams. However, these steps form the general outline of a code review process, and applying them is important for improving the quality of the code that gets written.