patternjavaModerate
Which is a better style to write default return case in if-else
Viewed 0 times
caseelsereturnwritebetterstyledefaultwhich
Problem
private String GetFacilityName(String facilityHexID) {
if (facilityHexID.equals(FACILITY_AIRCON_HEX_ID)) {
return FACILITY_AIRCON_NAME;
}
else if (facilityHexID.equals(FACILITY_FAN_HEX_ID)) {
return FACILITY_FAN_NAME;
}
return facilityHexID;
}Or
private String GetFacilityName(String facilityHexID) {
if (facilityHexID.equals(FACILITY_AIRCON_HEX_ID)) {
return FACILITY_AIRCON_NAME;
}
else if (facilityHexID.equals(FACILITY_FAN_HEX_ID)) {
return FACILITY_FAN_NAME;
}
else {
return facilityHexID;
}
}They essentially do the same thing. I'm thinking first one is better from the standpoint of "Write more code only if you have to" and the second one is better from readability aspect.
I'd use a switch statement with default case but I'm using a old version of JDK.. so I can't use it on Strings.
Which one is better?
Solution
I'd remove both
I think it's easier to follow. (It's called Guard Clause.)
Some other notes:
-
I've switched the
-
I've renamed the method to camelCase. It's more common in Java to start method names with small letters. (Code Conventions for the Java Programming Language, 9 - Naming Conventions)
-
I've changed
While uppercase may be more common,
a strong argument can made in favor of capitalizing only the first
letter: even if multiple acronyms occur back-to-back, you can still
tell where one word starts and the next word ends.
Which class name would you rather see, HTTPURL or HttpUrl?
elses:private String getFacilityName(String facilityHexId) {
if (FACILITY_AIRCON_HEX_ID.equals(facilityHexId)) {
return FACILITY_AIRCON_NAME;
}
if (FACILITY_FAN_HEX_ID.equals(facilityHexId)) {
return FACILITY_FAN_NAME;
}
return facilityHexId;
}I think it's easier to follow. (It's called Guard Clause.)
Some other notes:
-
I've switched the
equals to CONSTANT.equals(inputParameter). Now it handles null inputs too and does not throw NullPointerException.-
I've renamed the method to camelCase. It's more common in Java to start method names with small letters. (Code Conventions for the Java Programming Language, 9 - Naming Conventions)
-
I've changed
ID to Id in the variable names. I prefer camelCase here too because it's usually easier to read. From Effective Java, 2nd edition, Item 56: Adhere to generally accepted naming conventions: While uppercase may be more common,
a strong argument can made in favor of capitalizing only the first
letter: even if multiple acronyms occur back-to-back, you can still
tell where one word starts and the next word ends.
Which class name would you rather see, HTTPURL or HttpUrl?
Code Snippets
private String getFacilityName(String facilityHexId) {
if (FACILITY_AIRCON_HEX_ID.equals(facilityHexId)) {
return FACILITY_AIRCON_NAME;
}
if (FACILITY_FAN_HEX_ID.equals(facilityHexId)) {
return FACILITY_FAN_NAME;
}
return facilityHexId;
}Context
StackExchange Code Review Q#22963, answer score: 10
Revisions (0)
No revisions yet.