public boolean addPlayer(String nickname) {
return false;
if (players.contains(nickname)) { // Nickname already in use
return false;
// FIXME What happens if String = null?
}
else { // Nickname not in use
// TODO Need to initialize player
return true;
}
}
in old style origramming you use if else AND you return the result. (structured programming)
in modern style (early return.. in swift... or rurst..) so you code would be:
if (players.contains(nickname))
return false;
// NO NEED else
return true;
}
cleaner annd avoiding another lever of {} on "impliciti " else
You write:
public boolean addPlayer(String nickname) { return false; if (players.contains(nickname)) { // Nickname already in use return false; // FIXME What happens if String = null? } else { // Nickname not in use // TODO Need to initialize player return true; } }
in old style origramming you use if else AND you return the result. (structured programming) in modern style (early return.. in swift... or rurst..) so you code would be: if (players.contains(nickname)) return false;
}
cleaner annd avoiding another lever of {} on "impliciti " else
OR... use ternary: "https://en.wikipedia.org/wiki/Ternary_conditional_operator"
return (a=b) ? x; y
I repeat: only a matte of style, but yours is a mix. ;)