Skip to main content

Timeline for if and else or if and return?

Current License: CC BY-SA 4.0

6 events
when toggle format what by license comment
Jan 21, 2020 at 9:18 comment added Alexander Farber Instead of if (str.equals("xxx")) it is safer to use if ("xxx".equals(str)). This might spare you an NPE when the str is null
Jul 20, 2018 at 3:17 comment added candied_orange @esoterik I really like making it about the name.
Jul 17, 2018 at 17:53 comment added esoterik @Orici I meant if your code resemebles the safeguard clause use that pattern, if it doesn't use what ever is cleaner
Jul 17, 2018 at 12:37 comment added Flater @Orici: Imagine if the "safeguard clause" (as you call it) would also log a message that the safeguard clause was activated. While it is technically not just a return statement, it really still is a "safeguard clause". The question is where you draw the line of what is a safeguard clause and what is not. Finding a universal rule that is pedantically correct, in my opinion, is a futile effort. In most cases, the surrounding context of the method already makes it abundantly clear whether it's a "safeguard clause" or not.
Jul 17, 2018 at 7:16 comment added Orici I think, your example don't respond exactly to my question, the return in the first code example is a simple "safeguard clause", the if block only check if a condition is valid before to continue the execution of code, it don't execute nothing in it.
Jul 17, 2018 at 0:46 history answered esoterik CC BY-SA 4.0