Variable names should be concise and representative of nature and the quantity of the value it holds or will potentially hold.
var student = new Student();
var s = new Student();
var stdnt = new Student();
The same rule applies to lambda expressions:
students.Where(student => student ... );
students.Where(s => s ... );
var students = new List<Student>();
var studentList = new List<Student>();
var student = new Student();
var studentModel = new Student();
var studentObj = new Student();
If a variable value is it's default such as 0
for int
or null
for strings and you are not planning on changing that value (for testing purposes for instance) then the name should identify that value.
Student noStudent = null;
Student student = null;
int noChangeCount = 0;
int changeCount = 0;
Declaring a variable and instantiating it should indicate the immediate type of the variable, even if the value is to be determined later.
If the right side type is clear, then use var
to declare your variable
var student = new Student();
Student student = new Student();
If the right side isn't clear (but known) of the returned value type, then you must explicitly declare your variable with it's type.
Student student = GetStudent();
var student = GetStudent();
If the right side isn't clear and unknown (such as an anonymous types) of the returned value type, you may use var
as your variable type.
var student = new
{
Name = "Hassan",
Score = 100
};
Assign properties directly if you are declaring a type with one property.
var inputStudentEvent = new StudentEvent();
inputStudentEvent.Student = inputProcessedStudent;
var inputStudentEvent = new StudentEvent
{
Student = inputProcessedStudent
};
var studentEvent = new StudentEvent
{
Student = someStudent,
Date = someDate
}
var studentEvent = new StudentEvent();
studentEvent.Student = someStudent;
studentEvent.Date = someDate;
If a variable declaration exceeds 120 characters, break it down starting from the equal sign.
List<Student> washingtonSchoolsStudentsWithGrades =
await GetAllWashingtonSchoolsStudentsWithTheirGradesAsync();
List<Student> washgintonSchoolsStudentsWithGrades = await GetAllWashingtonSchoolsStudentsWithTheirGradesAsync();
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.
Student student = GetStudent();
List<Student> washingtonSchoolsStudentsWithGrades =
await GetAllWashingtonSchoolsStudentsWithTheirGradesAsync();
School school = await GetSchoolAsync();
Student student = GetStudent();
List<Student> washgintonSchoolsStudentsWithGrades =
await GetAllWashingtonSchoolsStudentsWithTheirGradesAsync();
School school = await GetSchoolAsync();
Also, declarations of variables that are of only one line should have no new lines between them.
Student student = GetStudent();
School school = await GetSchoolAsync();
Student student = GetStudent();
School school = await GetSchoolAsync();