S
stevengarcia
In Java, if you don't initialize a variable then you can't use it in
your code. This can sometimes lead to weird looking code. Consider
the following (silly) example:
Which exception block below do you prefer coding and/or reading? This
is in reference to a class that will generate commonly found and
consistent exceptions in our application. Please give your reasons:
public void sendCommand() {
String command = null;
if (condA) {
command = getDriverCommand();
}
else if (condB) {
command = getUniqueCommand();
}
else {
ExceptionUtils.throwSpecificException(...);
}
sendCommandInternal(command);
}
OR
public void sendCommand() {
String command;
if (condA) {
command = getDriverCommand();
}
else if (condB) {
command = getUniqueCommand();
}
else {
ExceptionUtils.throwSpecificException(...);
return;
}
sendCommandInternal(command);
}
The differences are subtle. In the first block, I initialize the
String command field to be null. If condA or conB are not met, I need
to throw an exception, and I have a helper class that will throw an
exception inside throwSpecificException(...).
In the second block, I do NOT initialize the String command field,
because I know that in the subsequent lines it must be set. If condA
and condB are not met, I am going to throw an exception. But I must
add the "return" statement directly after the ExceptionUtils line
because the class will not compile if I don't: Java complains that on
the line sendCommandInternal(command) that "the variable command may
not be initialized."
Which way do you prefer coding this? Or is there a completely
different way that I'm missing. I like the second way because by not
setting command to null, I know that later in the method it MUST be
set. But I don't like adding the return line after the ExceptionUtils
line.
your code. This can sometimes lead to weird looking code. Consider
the following (silly) example:
Which exception block below do you prefer coding and/or reading? This
is in reference to a class that will generate commonly found and
consistent exceptions in our application. Please give your reasons:
public void sendCommand() {
String command = null;
if (condA) {
command = getDriverCommand();
}
else if (condB) {
command = getUniqueCommand();
}
else {
ExceptionUtils.throwSpecificException(...);
}
sendCommandInternal(command);
}
OR
public void sendCommand() {
String command;
if (condA) {
command = getDriverCommand();
}
else if (condB) {
command = getUniqueCommand();
}
else {
ExceptionUtils.throwSpecificException(...);
return;
}
sendCommandInternal(command);
}
The differences are subtle. In the first block, I initialize the
String command field to be null. If condA or conB are not met, I need
to throw an exception, and I have a helper class that will throw an
exception inside throwSpecificException(...).
In the second block, I do NOT initialize the String command field,
because I know that in the subsequent lines it must be set. If condA
and condB are not met, I am going to throw an exception. But I must
add the "return" statement directly after the ExceptionUtils line
because the class will not compile if I don't: Java complains that on
the line sendCommandInternal(command) that "the variable command may
not be initialized."
Which way do you prefer coding this? Or is there a completely
different way that I'm missing. I like the second way because by not
setting command to null, I know that later in the method it MUST be
set. But I don't like adding the return line after the ExceptionUtils
line.