2

I'm working on a chess game and have created a base class for game entities and chess pieces, along with an enum for the state of a square, when returned in a collection of possible movements:

public enum MovementValidationResult { None = 0, Safe = 1, Attackable = 2 }
public abstract class GameEntity {
    public bool Alive { get; set; }
    public Vector2 Position { get; set; }
    public Vector2 Rotation { get; set; }
}
public abstract class ChessPiece : GameEntity {
    protected abstract MovementResult[] GetPossibleMovements(Vector2 requestedPosition);
}

When attempting to move a chess piece, I need to get a collection of possible movements, and present them to the client for visual display. The definition of a possible movement is presently limited to:

  • A Vector2 describing the grid coordinates of the square.
  • An educated guess on the result of moving to said square.

For example, I created a CodePen for a coding challenge a while back, and, while I didn't put much thought into it at the time, it visually displays the potential squares a chess piece can move to, and educated guesses for the result of moving to each of those potential squares:

Screenshot of the aforementioned CodePen.

What I'm having trouble with is creating a name for the data structure that describes the resulting state of a square, and as such, the quality of naming for related methods, variables, etc are all impacted. So far, the best I've come up with is MovementResult and while I believe it clearly conveys the data presented; I also believe it to be slightly misleading due to the fact that this is a possible movement, so it isn't the actual result.

However, is there ever a need to really distinguish between possible and actual in such a simple use-case? Also, another reason I'm hesitant is because it sounds like a better name for the MovementValidationResult enum (defined above).


Is MovementResult an acceptable name in this scenario, or should my data structure be named something else entirely?

Note: I didn't exclude a definition for PossibleMovement because no such definition exists yet, and is actually why I'm here.

6
  • 2
    How do professionals or chess masters refer to these "possible|alterntive" movements?
    – Laiv
    Aug 16, 2021 at 17:18
  • 3
    You don't always have to encode all the information in type names - especially if the type itself is more general in nature than what the instance represents. E.g., the type could be List<Movement>, while the variable name could be possibleMovements. Now, if there's some behavior-rich concept associated with "possible move(ment)s" that you could encode as a type (so that other objects could delegate subtasks to that object), then you could name it according to that concept (something like: MovementRules, AllowedMoves, ... or maybe there's already a chess term you can use?). Aug 16, 2021 at 17:23
  • 1
    Another reason to use a specialized type (even though it may not be behaviorally rich) might be if you want to enforce some constraints on a generic list of moves; you could have methods that require this specialized type as input, and only one way to produce it - via the thing that enforces the constraint (a constructor, a factory method, a validator of some sort). Aug 16, 2021 at 17:24
  • 1
    In Spanish, I have found "Jugadas Candidatas" which definition matches your "allowed movements". I'm unsure about how to translate it into English. It seems to be candidate move
    – Laiv
    Aug 16, 2021 at 17:26
  • 2
    For a single look-ahead move, "Candidate" or "CandidateMove" (as suggested by @Laiv) is a good name. For a series of moves, "Line" would be the proper chessic term. e.g. "In the poisoned pawn line, Black grabs a pawn...". However, it may not be a good term for fellow programmers.
    – user949300
    Aug 16, 2021 at 23:13

3 Answers 3

5

I will try to answer this and not making an opinionated answer in the way. As I commented, I would start by delving a bit into the game glossary.

After a bit, I came to the conclusion that Candidate board as a possible and virtual state of the board is fair enough. Also Candidate move as a possible and valid movement that leads to a Candidate board.

Variant seems a good fit too, given Wikipedia's definition.

In absence of chess experts, you have to find out names that communicate intent. Or in this case, state. If none of the names likes you, you can add a brief introduction with Javadoc. If you use IDE's it's likely you can read these comments anywhere just moving the cursor over the variable type.

However, is there ever a need to really distinguish between possible and actual in such a simple use-case?

Readability matters. Pick a random name that doesn't express satisfactorily what a method does or what a variable is for, leave the app for the next 6 months, be back then and try to understand the code. Find where things happen without debugging. Or leave others to read your code and see what happens.1

Also, another reason I'm hesitant is because it sounds like a better name for the MovementValidationResult enum (defined above).

Well, you are not a robot. You should be capable of finding out a name that expresses best what MovementValidationResult holds. Swap your "developer hat" with the "gamer" or "chess master" hat. What are None, Safe and Attackable? Vulnerability? Exposure? Is there any term in the glossary to express the same idea?


1:Even if nobody ever reads the code. Getting used to making code readable is the kind of skill you want to develop at all costs. Overall if you are to be professional. I think it was Uncle Bob who wrote once that making code hard to understand was just another way to be disrespectful to your coworkers. I couldn't agree more.

3

If this is after considering the whole board and the game history, you are showing that pawns':

legal moves

This is not the same as a data structure that holds the game boards that would result from choosing those moves. Such boards wouldn't let you highlight the GUI.

1

What I'm having trouble with is creating a name for the data structure that describes the resulting state of a square, and as such, the quality of naming for related methods, variables, etc are all impacted. So far, the best I've come up with is MovementResult and while I believe it clearly conveys the data presented; I also believe it to be slightly misleading due to the fact that this is a possible movement, so it isn't the actual result.

I think you've overthinking it. There's a difference between an abstract concept and an instance, but type names don't always reveal that.

For example, suppose your application runs simulations as a basic chess AI. You therefore spawn many simulations, each containing a ChessBoard with Pieces that are moved around.
None of those data structures are the "real" game. They are simulations.

But as far as the logic is concerned, that real/theoretical distinction is irrelevant. It is not up to the ChessBoard class to distinguish whether it's part of a real game or a theoretical one. In either case, the ChessBoard's role is to follow the rules of chess. By intention, you want that behavior to be unchanged regardless of whether it's a simulation or a real game, because if the behavior changes, then your simulation is no longer a representation of a real game of chess.

The better distinction here is made through the name of the property, rather than the name of the type. In other words:

// real
public ChessBoard CurrentGame { get; set; }

// theoretical
public IEnumerable<ChessBoard> Simulations { get; set; }

In reality, you'd probably use a Simulation class with the ChessBoard in it, but I'm intentionally glossing over such specific details here.

It is not the type that reveals how the specific instance is being used. It's the variable/property that reveals this.

The same applies to your case:

// theoretical
public IEnumerable<MovementResult> PossibleMoves { get; set; }

// real
public IEnumerable<MovementResult> MoveHistory { get; set; }

I'd be more inclined to call it a Movement rather than a MovementResult, because by that standard, half your classes could be called ...Result which makes the name rather meaningless; but that is my personal opinion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.