3 ms·
This is a good point. I think it can be made more readable by forcing labeled else statements, like: 'outer: if a() { 'inner: if b() {
by jeroenhd 3y ago
This is a good point. I think it can be made more readable by forcing labeled else statements, like:
'outer: if a() {
'inner: if b() {
if c() {
breakelse 'inner;
}
if d() {
breakelse 'outer';
}
} else {
// E2
}
} else {
// E1
}
This way, you can trigger syntax errors when someone refactors the code. It's not particularly pretty, but it does communicate the control flow better than simple gotos in my opinion.
Thinking about this, I imagine there's even a way this could be made useful: in languages where if/case/match statements are expressions, you could make breakelse an expression that returns the result of the else branch. On the other hand, perhaps a "checkagain" statement to re-evaluate the if statement once would be better for that:
if isInDatabase(id) && isProfileComplete(id) {
return db.fetchUser(id);
} else {
api.autocompleteUser(id);
let retry = checkagain;
if retry != None {
db.update(retry);
}
}
Honestly I think it's ugly as hell but I think there could be something here if someone competent at designing languages would take a look at this.