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?
1 Answer 1
I'd remove both else
s:
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
toCONSTANT.equals(inputParameter)
. Now it handlesnull
inputs too and does not throwNullPointerException
.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
toId
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?
-
1\$\begingroup\$ FWIW, Guard Clause is more about checking method invariants prior to logic execution. E.g.
if (facilityHexId == null) return null;
would be a Guard Clause. \$\endgroup\$Chris Knight– Chris Knight2013年02月25日 12:38:07 +00:00Commented Feb 25, 2013 at 12:38 -
1\$\begingroup\$ @ChrisKnight: I've thought the same when I wrote the answer but "checks for trivial cases, and gets rid of them quickly" (from the linked C2 wiki) seems appropriate here. refactoring.com/catalog/… also reinforces that. What do you think? \$\endgroup\$palacsint– palacsint2013年02月25日 13:34:49 +00:00Commented Feb 25, 2013 at 13:34
-
1\$\begingroup\$ @palacsint: Well, to me, the name 'Guard Clause' implies its guarding against something which isn't the purpose of the if statements in the above code. In the refactoring.com link the line seems blurred between guarding against something and performing execution logic. Interesting link though. \$\endgroup\$Chris Knight– Chris Knight2013年02月25日 14:03:08 +00:00Commented Feb 25, 2013 at 14:03
switch
? Java7 exists. \$\endgroup\$