Is this right?
Example code:
void foo(E param){
if(param == null){
// do this
}else{
// do that
}
//...
}
First of all I read some posts where people where discussing about allowing variables to be null adds complexity to the software Best explanation for languages without null so I would like to make and effort and forget about this to try to answer this question.
The thought (My thought)
As a beginner my thought might be wrong but i Would like you to help me clear this up.
I think checking if a variable is null is correct depending on the context in which that variable is, let me explain with an example:
Bad use:
Our project have the Command pattern implemented and we have a set of instructions. Then we write in the console an instruction and we have to interpret it. We have an Interpreter Class which tries to parse that so if the interpreter cant find the appropiate instruction it returns null.
Code example:
public class Interpreter {
public static final String LINE_SEPARATOR = System.getProperty("line.separator");
private static final List<Instruction> listInstructions = loadInstructions();
/**
* Generates a new instruction according to the user input
* @param line - A string with the user input
* @return The instruction read from the given line. If the instruction is not correct, then it returns null.
*/
public static Instruction generateInstruction(String line) throws WrongInstructionFormatException
{
Instruction instru = null;
Iterator<Instruction> it = listInstructions.iterator();
int i = 0;
while(it.hasNext() && i <9 && instru == null)
{
try
{
instru = it.next().parse(line);
}
catch(WrongInstructionFormatException e)
{
}
finally
{
i++;
}
}
return instru;
}
//...
public class MainClass
{
/**
* MainFunction
*/
public void mainFuction()
{
Instruction instru;
while(true)
{
instru = Interpreter.generateInstruction(orden);
if(instru == null) <------
// Error.
else
instru.execute();
}
}
}
Well in this case I think we are implementing this wrong because the interpreter must not return null but an exception because in my opinion a variable in this context could never be null. I hope I'm being clear, if it is not then I would like to clarify later with your comments
Correct use: (EDITED)
The main class of our project has a set of attributes from which both can be instantiated or not. Depending on whether we have instantiated one, both or neither there will be a set of functions to perform one, two or no action.
public class MainClass extends Observable
{
private A a;
private B b; // This attribute allows you to start making a log in a txt file
private C c; // This attribute allows you to start making a log in a DB
public MainClass(A a){ //....}
public MainClass(A a, B b) { // .....}
public MainClass(A a, C c) { // .....}
public MainClass(A a, B b, C c){ //.....}
public void log(String update)
{
if(this.b != null) <-----
// Save the update in the txt file
if(this.c != null) <-----
// Save the update in a db row
}
}
Well, why is this right? I think is right because the attributes may have null value and not as in the previous case.
With this begins the discussion and I would like it to help other people if they had the same doubt.