Disagree, at least for languages that support `goto` which sadly neither Java nor ECMAScript do.

Using `goto end` for your guard clauses where `end:` is a label just before `return`, or just before cleanup results in cleaner, easier to refactor code over time.

Try it next time you are programming in PHP, GoLang or some other language that supports `goto`.

--

--

I totally agree with you. I wish the "return early" madness would go away.

Here is another way to write the code you refactored (sadly if ECMAScript had goto like PHP and Go then it would be much cleaner):

function notifyGroupMembers(group: Group): number {

let notifiedMemberCount = 0

end:

do {

if (group.isActive() !== true) {

break end;

} else if (group.getMemberCount() < 1) {

break end;

}

for (const member of group.getMembers()) {

if (member.isActive() === true) {

notifyMember(member)

notifiedMemberCount += 1

}

}

} while (0)

return notifiedMemberCount

}

--

--

Guard clauses? YES!

Multiple returns? NO!!!

(Well, I guess in Java, yes. 😢)

Too bad Java doesn't support goto, or break to a later label, because if it did you could have the best of both worlds; guard clauses and single returns. As you can in PHP, GoLang, etc.

--

--