DEV Community

Discussion on: Else after return: yea or nay?

Collapse
 
strredwolf profile image
STrRedWolf

A better example.

def addticket(subject, detail):
    if already_exists(subject):
        appendticket(subject, detail)
        return

    createticket(subject,detail)

Or... in C#...

void addticket(string subject, string detail) {
    if(already_exists(subject)) {
        appendticket(subject,detail);
        return;
    }

    createticket(subject,detail);
}

Now expand those methods out, incorporating them into the main routine. Gets complicated real fast, eh?

But what if some stuff needs to be valid to continue? Don't nest if you don't have to, and don't split stuff out into too many routines. Test it and get out if it fails. No need to }else{}else{}else{} if you can make it readable there.