Skip to content

Variables

Hassan Rezk Habib edited this page Aug 15, 2020 · 3 revisions

0. Variables

0.0 Naming

Variable names should be concise and representative of nature and the quantity of the value it holds or will potentially hold.

0.0.0 Names

Do
var student = new Student();
Don't
var s = new Student();
Also, Don't
var stdnt = new Student();

The same rule applies to lambda expressions:

Do
students.Where(student => student ... );
Don't
studetns.Where(s => s ... );

0.0.1 Plurals

Do
var students = new List<Student>();
Don't
var studentList = new List<Student>();

0.0.2 Names with Types

Do
var student = new Student();
Don't
var studentModel = new Student();
Also, Don't
var studentObj = new Student();

0.0.3 Nulls or Defaults

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.

Do
Student noStudent = null;
Don't
Student student = null;
Also, Do
int noChangeCount = 0;
But, Don,t
int changeCount = 0;



0.1 Declarations

Declaring a variable and instantiating it should indicate the immediate type of the variable, even if the value is to be determined later.

0.1.0 Clear Types

If the right side type is clear, then use var to declare your variable

Do
var student = new Student();
Don't
Student student = new Student();

0.1.1 Semi-Clear Types

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.

Do
Student student = GetStudent();
Don't
var student = GetStudent();

0.1.2 Unclear Types

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.

Do
var student = new
{
    Name = "Hassan",
    Score = 100
};



0.2 Organization

0.2.0 Breakdown

If a variable declaration exceeds 120 characters, break it down starting from the equal sign.

Do
List<Student> washingtonSchoolsStudentsWithGrades = 
    await GetAllWashingtonSchoolsStudentsWithTheirGradesAsync();
Don't
List<Student> washgintonSchoolsStudentsWithGrades = await GetAllWashingtonSchoolsStudentsWithTheirGradesAsync();

0.2.1 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.

Do
Student student = GetStudent();

List<Student> washingtonSchoolsStudentsWithGrades = 
    await GetAllWashingtonSchoolsStudentsWithTheirGradesAsync();

School school = await GetSchoolAsync();
Don't
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.

Do
Student student = GetStudent();
School school = await GetSchoolAsync();
Don't
Student student = GetStudent();

School school = await GetSchoolAsync();