Coding Guidelines

Posted in News on November 4, 2020 by Henk Verlinde ‐ 2 min read

Coding Guidelines

Conditionals

Do not use int instead of bool

Take for example the following function:

function isValid() as int {
	//evaluate some condition

	if (condition) {
		return 1;
	}
	else {
		return 0;
	}
}

Functions the return only true or false should never use int or other type of data as their return value. The function in the previous example should return bool:

function isValid() as bool

Returning the result from this function should take care into consideration next guideline.

Do not check a condition and then return true or false

Take for example the following function:

function isValid() as bool {
	var condition = true; //some evaluation

	if (condition == true) {
		return true;
	}
	else {
		return false;
	}
}

The function in the previous example should be rewritten as:

function isValid() as bool {
	var condition = true; //some evaluation
	return condition;
}

Do not create entire method or function ifs

Take for example the following function:

method validate(condition as bool) {
	if (condition) {
		//Do something
	}
}

Entire method or function ifs creates unnecessary nesting of code blocks. The approach for this type of method should be to exit as soon as possible if the condition is NOT fulfilled. The function in the previous example should be rewritten as:

method validate(condition as bool) {
	if (!condition) {
		return null;
	}

	//Do something
}