How to write code so that it grows in height, not in width

Rule 0. Don’t listen to me, listen to your style guide

Rule 1. Put else as close as possible to if block

    // 20 more lines before this
String[] params = text.split(" ");
members.clear()
members.add(params[1]);
members.add(params[2]);
tournamentEnabled = true;
markup = new Markup();
rows = List.of(members);
markup.setRows(rows);
markup.setAction("tournament");
status = Message.create()
.withText("Let's go!")
.withMarkup(markup)
.toChat(chatId)
.send();
validateStatus(status);
} else {
Log.error("Empty command");
}
void handleMessage(Message message) {
String text = message.getText();
if (text != null) {
// 35 lines of code
} else {
Log.error("Empty command");
}
}
void handleMessage(Message message) {
String text = message.getText();
if (text == null) {
Log.error("Empty command");
} else {
// 20 more lines before this
String[] params = text.split(" ");
members.clear()
members.add(params[1]);
members.add(params[2]);
tournamentEnabled = true;
markup = new Markup();
rows = List.of(members);
markup.setRows(rows);
markup.setAction("tournament");
status = Message.create()
.withText("Let's go!")
.withMarkup(markup)
.toChat(chatId)
.send();
validateStatus(status);
}
}

Rule 2. if/else can be replaced with if-return

void handleMessage(Message message) {
String text = message.getText();
if (text == null) {
Log.error("Empty command");
return;
}
// 20 more lines before this
String[] params = text.split(" ");
members.clear()
members.add(params[1]);
members.add(params[2]);
tournamentEnabled = true;
markup = new Markup();
rows = List.of(members);
markup.setRows(rows);
markup.setAction("tournament");
status = Message.create()
.withText("Let's go!")
.withMarkup(markup)
.toChat(chatId)
.send();
validateStatus(status);
}
if (command.startWith("/help")) {
Message.create()
.withText("Help message")
.toChat(chatId)
.send();
} else if (command.startWith("/clear")) {
data.clear();
Message.create()
.withText("Cleared!")
.toChat(chatId)
.send();
} else if (command.startWith("/add") && isAdmin) {
data.add(params[1]);
sendStatusNessage();
} else if // ...
if (command.startWith("/help")) {
Message.create()
.withText("Help message")
.toChat(chatId)
.send();
return;
}
if (command.startWith("/clear")) {
data.clear();
Message.create()
.withText("Cleared!")
.toChat(chatId)
.send();
return;
}
if (command.startWith("/add") && isAdmin) {
data.add(params[1]);
sendStatusNessage();
return;
}
// ...

Rule 3. if/else can be replaced with if-continue inside loops

for (Members member : members) {
if (member.isBlocked()) {
Log.warning("Member {} is blocked", member);
tournamentMembers.remove(member);
if (tournamentMembers.size() > MIN_MEMBERS_COUNT) {
tournamentEnabled = false;
}
} else {
Member opponent = tournamentMembers.pop();
tournamentEnabled = true;
Message.create()
.withText("Tournament started. Your opponent is {}", opponent)
.toChat(member.getId())
.send();
Message.create()
.withText("Tournament started. Your opponent is {}", member)
.toChat(opponent.getId())
.send();
Message.create()
.withText("Tournament started. {} vs {}", member, opponent)
.toChat(chatId)
.send();
}
}
for (Members member : members) {
if (member.isBlocked()) {
Log.warning("Member {} is blocked", member);
tournamentMembers.remove(member);
if (tournamentMembers.size() > MIN_MEMBERS_COUNT) {
tournamentEnabled = false;
}
continue;
}

Member opponent = tournamentMembers.pop();
tournamentEnabled = true;
Message.create()
.withText("Tournament started. Your opponent is {}", opponent)
.toChat(member.getId())
.send();
Message.create()
.withText("Tournament started. Your opponent is {}", member)
.toChat(opponent.getId())
.send();
Message.create()
.withText("Tournament started. {} vs {}", member, opponent)
.toChat(chatId)
.send();
}

Rule 4. Avoid unnecessary changes

if (chatId == config.tournamentChat()) {
processTournamentChat(message);
} else {
processRegularCommand(message);
}
if (chatId == config.tournamentChat()) {
processTournamentChat(message);
return;
}
processRegularCommand(message);

Rule 5. If nesting exceeds the allowed level, introduce a new method

I love programming, open source, art, learning and sharing knowledge

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store