C# Coding conventions

These coding conventions were originally written by my colleagues Filip Kassovic and Michal Třešňák in ST-Software. I have only made slight updates and published it on my blog.

Do not use “var” keyword when not necessary

var response = SomeClass.SomeMethod(input);

Reason

  • we don’t see the type of “response”
  • “Find Usages” or “Find all references” doesn’t find usage of type when using “var”

Exceptions
obviously, you have to use it for anonymous types

var anonymous = new { A = 1, B = 2 };

Do not use complex object initializer

PaymentTransaction transaction = new PaymentTransaction
			{
				Token = statusResponse.RefNo,
				Error = GetError(statusResponse), //avoid method call in initializer
				State = GetState(statusResponse.ResponseCode),
				ProviderResponse = statusResponse.Response,
				Sign = statusResponse.Sign,
				Sign2 = statusResponse.Sign2
			};

Reason

  • it is not possible to debug it.
  • We are not able to determine line with exception. For example, if there was an exception in GetState() method, we wouldn’t know if the exception was in GetError() or in GetState() method etc.
  • And this is problem in logs because in case of an exception, we would get only line number of “PaymentTransaction transaction = new PaymentTransaction” and not “GetState()”.

Exception
Allowed in simple scenarios like this

SomeDto dto = new SomeDto
{
	Id = entity.Id,
	Name = entity.Name,
};

Code Formatting – keep tabs

tabs

Setup formatting like this: Indenting – Smart, Tab – Keep tabs

Reason

  • files are much smaller
  • easier to navigate using keyboard
  • compatible with CoffeeScript

Do not return new objects / method results / query results directly

public static UserRoleDetailDto ToUserRoleDetailDto(UserRole entity)
{
	return new UserRoleDetailDto
	{
		Id = entity.Id,
		Name = entity.Name,
	};
}

//another case
public static List Aaa(UserRoleDetailDto dto)
{
  //not possible to see return value in debugger
  return dto.ExcelReportIds
 .Where(x => x.ToString()
 .StartsWith("1"))
 .ToList();
}

How to write it instead

public static List<Guid> Aaa(UserRoleDetailDto dto)
{
 List<Guid> ret = dto.ExcelReportIds
 .Where(x => x.ToString()
 .StartsWith("1"))
 .ToList();

 //You can set breakpoint here and check the return value
 return ret;
}

Reason
If you return the result right away, then it is not possible to debug it and you can’t see the returning value

Use explicit initialization for Enum

public enum Status {
 Accepted: 0,
 Denied: 1,
 Approved: 2,
 Rejected: 3,
 Submitted: 4,
 SomethingElse: 5
}

Reason
It is hard to match enum values stored in database as int. So always declare it along with numberic value of that enum.

Be careful about FirstOrDefault() method

Reason
When we expect that there should be just 1 matching item, then Single() or SingleOrDefault() should be used. Otherwise it might happen that your DB will accidentally contain duplicated representation of some important items (for example Orders or Bills). FirstOrDefault() has caused us several headaches, which could have been easily avoided if we’ve used SingleOrDefault() in the first place.

 

Have fun 🙂

java