C# Coding Standard: Variables
01 November, 2023
READ TIME - 5 MINUTES
Thank you to our sponsors who help keep this blog post free for the reader:
This month's issue is proudly sponsored by Milan Jovanovic.
Pragmatic Clean Architecture: Learn how to confidently ship well-architected production-ready apps using clean architecture. Milan used exact principles to build large-scale systems in the past 6 years.
Introduction:
I consistently strive to adhere to the highest coding standards and adopt a pragmatic approach in my software development endeavors. Below, I present the guidelines for Variables in accordance with C# coding standards that I rigorously apply in my projects.
Variables :
Naming:
Variable names should be concise and representative of nature and the quantity of the value it holds or will potentially hold.
Names:
var employee = new Employee();
The same rule applies to lambda expressions:
employees.Where(employee => employee ... );
Plurals:
var employees = new List<Employee>();
Names with Types:
var employee = new Employee();
Nulls or Defaults:
If a variable value is it's default such as 0 for int or null for strings and we are not planning on changing that value (for testing purposes for instance) then the name should identify that value.
Employee noEmployee = null;
or
int noChangeCount = 0;
Declarations
Declaring a variable and instantiating it should indicate the immediate type of the variable, even if the value is to be determined later.
Clear Types
If the right side type is clear, then use var to declare our variable
var employee = new Employee();
Semi-Clear Types
If the right side isn't clear (but known) of the returned value type, then we must explicitly declare our variable with it's type.
Employee employee = GetEmployee();
Unclear Types
If the right side isn't clear and unknown (such as an anonymous types) of the returned value type, we may use var as your variable type.
var employee = new { Name = "Rana", Salary = 5000 };
Single-Property Types
Assign properties directly if we are declaring a type with one property.
var inputEmployeeEvent = new EmployeeEvent(); inputEmployeeEvent.Employee = inputProcessedEmployee;
or
var employeeEvent = new EmployeeEvent { Employee = someEmployee, Date = someDate }
Breakdown
If a variable declaration exceeds 120 characters, break it down starting from the equal sign.
List<Employee> ItDepartmentsEmployeesWithSalaries = await GetAllItDepartmentsEmployeesWithSalariesAsync();
Multiple Declarations
Declarations that occupy two lines or more should have a new line before and after them to separate them from previous and next variables declarations.
Employee employee = GetEmployee(); List<Employee> ItDepartmentsEmployeesWithSalaries = await GetAllItDepartmentsEmployeesWithSalariesAsync(); Department department = await GetDepartmentAsync();
Also, declarations of variables that are of only one line should have no new lines between them.
Employee employee = GetEmployee(); Department department = await GetDepartmentAsync();
About the Blogs
As a dedicated .NET developer, I maintain a Patreon account where I share exclusive content related to .NET development. There, you will also gain access to the codebase of this blog post. By becoming a Patreon member, you will have the opportunity to explore and learn from my projects firsthand.
If you have found my contributions helpful in any way, I kindly ask you to consider becoming a Patreon supporter. Your support enables me to continue producing high-quality content, empowering developers like yourself to enhance their skills and stay up to date with the latest developments in the .NET ecosystem. Thank you for considering joining my Patreon community!
Subscribe to the Newsletter
Learn, Grow & Upgrade Your .NET Skills
Join 10+ subscribers
Recent Posts
01 November, 2023
01 October, 2023
Share This Article On:
© Copyright 2023 Md. Saddam Hossain | saddamhossain.net. All Rights Reserved.