Search code examples
javaoopnaming-conventionschess

What will be the most descriptive name for my method that returns a list of chess pieces attacking a square


Edit: Once again, thanks to those who commented and answered. Agreed, not the best question in the world, but I needed a little push to get past this obstacle in my mind. What I have taken with me in particular is that the return type is an important part of the method signature.

One of the important aspects of clean coding is picking good names for your classes, variables and methods.

Following what I have read in literature and online, I would try and pick names that are, firstly, as descriptive (and therefore unambiguous) as possible, and secondly, as concise as possible.

I am for my own amusement and learning writing a chess game in java, and I have stumbled upon a method that I simply can't figure out how to name in a satisfactory way. The method lives on my ISquare interface and is intended to bring me back a list of pieces that are currently attacking that square.

To be fully descriptive the name should indicate that the method returns a collection of pieces, arguably even a list, and that the pieces are attacking this square instance. One could argue that the latter is implied by where the method lives, but I'm not too sure about that.

The most descriptive name I can think of is probably in violation of every single other naming convention, and obviously won't do:

List<IPiece> giveMeTheListOfPiecesThatThisSquareIsUnderAttackBy();

These two alternatives show that the method relates to the current instance, but seem to hint that the result is of a boolean nature:

List<IPiece> isUnderAttackByPieces();
List<IPiece> underAttackByPieces();

The next one is descriptive about the return type, but not explicit about what the pieces are attacking:

List<IPiece> getAttackingPieces();

This one might satisfy my criteria, but intuitively I would say that using the words "This" and "Square" doesn't look very good:

List<IPiece> piecesAttackingThisSquare();

Currently I have settled with underAttackByPieces(), but as described above that doesn't quite nail it.

Any help you can offer will be most appreciated!


Solution

  • I would settle with getAttackingPieces. Since it's a method of ISquare, I think it is clear enough what is under attack. You can be more explicit in the method's Javadoc comment.