C# Coding Standard: Methods
01 September, 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 Saddam Hossain .NET.
The website is owned by none other than Md. Saddam Hossain, who holds the esteemed title of Senior Software Engineer and is known for his unwavering passion for .NET development . To learn more about Md. Saddam Hossain, You are invited to visit the website for comprehensive details and insights.
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 methods in accordance with C# coding standards that I rigorously apply in my projects.
Methods :
Naming:
Method names should be a summary of what the method is doing, it needs to stay percise and short and representative of the operation with respect to synchrony. Method names must contain verbs in them to represent the action it performs.
public List<Employee> GetEmployees() { ... ... }
Asynchronousy:
Asynchronous methods should be postfixed by the term Async such as methods returning Task or ValueTask in general.
public async Task<List<Employee>> GetEmployeesAsync() { ... ... }
Input Parameters:
Input parameters should be explicit about what property of an object they will be assigned to, or will be used for any action such as search.
public async Task<Employee> GetEmployeeByNameAsync(string employeeName) { ... ... }
Action Parameters:
If our method is performing an action with a particular parameter specify it.
public async Task<Employee> GetEmployeeByIdAsync(Guid employeeId) { ... ... }
Passing Parameters:
When utilizing a method, if the input parameters aliases match the passed in variables in part or in full, then we don't have to use the aliases, otherwise we must specify our values with aliases. Assume we have a method:
Employee GetEmployeeByNameAsync(string employeeName); string employeeName = "Rana"; Employee employee = await GetEmployeeByNameAsync(employeeName);
Single-Liners:
Any method that contains only one line of code should use fat arrows
public List<Employee> GetEmployees() => _employeeService.GetEmployees(); string employeeName = "Rana"; Employee employee = await GetEmployeeByNameAsync(employeeName);
If a one-liner method exceeds the length of 120 characters then break after the fat arrow with an extra tab for the new line.
public async Task<List<Employee>> GetAllItDepartmentsEmployeesAsync() => await _employeeService.GetEmployeesAsync();
Multiple-Liners:
If a method contains multiple liners separated or connected via chaining it must have a scope. Unless the parameters are going on the next line then a one-liner method with multi-liner params is allowed.
public Employee AddEmployee(Employee employee) { ValidateEmployee(employee); return _employeeService.InsertEmployee(employee); }
or
public Employee AddEmployee( Employee employee) { return _employeeService.InsertEmployee(employee); }
Returns:
For multi-liner methods, take a new line between the method logic and the final return line (if any).
public List<Employee> GetEmployees() { EmployeesClient employeeApiClient = InitializeEmployeeApiClient(); return employeesApiClient.GetEmployees(); }
Multiple Calls:
With multiple method calls, if both calls are less than 120 characters then they may stack unless the final call is a method return, otherwise separate with a new line.
public List<Employee> GetEmployees() { EmployeesClient employeeApiClient = InitializeEmployeeApiClient(); List<Employee> employees = employeeApiClient.GetEmployees(); return employees; }
or
public async Task<List<Employee>> GetEmployeesAsync() GetAllItDepartmentsEmployeesAsync { EmployeesClient ItDepartmentsEmployeesApiClient = await InitializeItDepartmentsEmployeesApiClientAsync(); List<Employee> employees = employeeApiClient.GetEmployees(); return employees; }
Declaration:
A method declaration should not be longer than 120 characters.
public async Task<List<Employee>> GetAllItDepartmentsEmployeesAsync( EmployeesQuery employeesQuery) { ... }
Multiple Parameters:
List<Employee> ItEmployees = await QueryAllItEmployeesBySalaryAndAgeAsync( MinimumSalary: 5000, Age: 20);
Chaining:
Some methods offer extensions to call other methods. For instance, we can call a Select() method after a Where() method. And so on until a full query is completed. we can beautify our code using chain method. Here's some examples:
employees .Where(employee => employee.Name is "Rana") .Select(employee => employee.Name) .OrderBy(employee => employee.Name) .ToList(); ProcessEmployees(employees);
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.