Java - Get/set methods receiving and returning "null"
I'm a beginner in Java. I'm trying, for training purpose, to build myself a chess game application. Within my class Case, that will be used to instanciate all the 64 cases of my board, I write get/set methods to find if there's a Piece occupant in the instances of the case.
I read that returning "null" is a bad practice, so I throw an exception instead to signify that the case is free. But, I wonder how to set the occupant's pointer to "null"; can I simply push "null" as a parameter when I will call this method?
Also, could taking/returning "null" be an acceptable/good practice?
public Piece getOccupant(){
if (this.occup开发者_JS百科ant == null)
throw new IllegalArgumentException(this.occupant + " is Empty");
return this.occupant;
}
public void setOccupant(Piece newOccupant){
this.occupant = newOccupant;
}
Thanks!
[Update]
Thanks to all of your for your comments, ideas, corrections and recommendations. Here is the updated version of my code for this part, and I feel satisfied with it, as it served its purpose (increase my understanding thru practice).
/*
* Modifiers of Occupant
*/
/**
* Used to find if a Piece is located in this Cell
* @return a Piece reference to the occupant. Will send a
* null pointer if cell is empty
*/
public Piece getOccupant(){
return this.occupant;
}
/**
* Used to set a new occupant in the Cell.
* @param newOccupant is a reference to a Piece instance,
* and should be set to null if the cell is emptied, or using
* the method clear().
*/
public void setOccupant(Piece newOccupant){
this.occupant = newOccupant;
}
/**
* Used to verify if a Cell is empty of any occupant
* @return true if cell is empty.
*/
public boolean isEmpty(){
if(this.occupant == null)
return true;
return false;
}
/**
* Free the cell of any occupant, if any were
*/
public void clear(){
this.occupant = null;
}
A space on the board being unoccupied is not exceptional. Its normal and will always be true for the majority of the board. You should not be throwing exceptions here; exceptions should only be thrown for an unexpected event that signify a significant problem with what you are trying to do.
You can certainly pass null to a setter (except for a primitive type like int/long).
It might be better to add some convenience methods, an isEmpty method to your Space class:
public boolean isEmpty(){
if (this.occupant == null)
return true;
return false;
}
and also perhaps a clear method
public void clear() {
this.occupant = null;
}
that way you don't have to test on the nullity of the getter result, and you don't need to pass null to set -- this has the added benefits of being easily testable, and creates a API that is meaningful to your Space class.
If you want to forbid null values, you should do it on the setter method:
public void setOccupant(Piece occupant) {
if (occupant == null) throw new NullPointerException("occupant");
this.occupant = occupant;
}
Note that some people prefer to throw IllegalArgumentException. Either way, the point is to "fail fast" as soon as someone sets a forbidden value.
Having said all of that, a chess board certainly can have empty positions, so allowing null seems to make more sense.
I suggest you read "Effective Java, 2nd Edition" by Josh Bloch.
Where did you read that recommendation? In my opinion, there is absolutely nothing wrong about returning null
, provided that null
conveys some useful information and does not indicate a severe error condition. In this case, it is perfectly normal for a chess cell to not contain a piece, and I would definitely expect getOccupant()
to return null in that case.
If the caller is aware of NULL return values, it's not bad to return NULL values by callee.
Instead of returning null or throwing an exception, you should create a class "Empty", "None", "Void", something like that, that you would assign to all your Case that are empty.
small suggestion no need of if block, you can simplify your code by simply returning the output of expression
public boolean isEmpty(){
return this.occupant == null
}
精彩评论