lundi 26 octobre 2015

Should i (not) use 'else' when previous 'if' can exit program?

I don't completely understand how to use else statements.

When i do write else statements any chance i get, i find myself indenting code several layers deep, and every else statement seems pretty useless and not very readable, so i would like to get a deeper understanding of when to (not) use else statements.

Consider the following code:

if (file == NULL) {
    exit;
}

//  Do stuff with file

1: Is it more efficient for computing to (not) include an else statement?

2: Is it only to group related statments together, so that it's more readable?

3: My thinking: "I would like to run a bunch of code, but if something is wrong, i'd like to exit the program." In that logic, i don't see a reason to include else statements for readability. Is that wrong? Any other reason?

Aucun commentaire:

Enregistrer un commentaire