How would you adhere to the principle of "Tell, do not ask" (hereinafter referred to as the "principle") in the following simple scenario? In the Tetris game, I have the Board, BlockGrid, and Piece classes related to the following example:
public class Board { private var fallingPiece:Piece; private var blockGrid:BlockGrid; ... public function moveFallingPiece(xDirection:int, yDirection:int):void { blockGrid.movePiece(fallingPiece, xDirection, yDirection); } }
Once fallPiece is placed in the bottom row of the BlockGrid, it should no longer be "fallPiece." Am I right in that I do not violate the principle as follows?
if(blockGrid.getPiecePosition(piece).y == 0) { fallingPiece = null; }
But is it really different from this, which, I think, clearly violates the principle?
public function moveFallingPiece(xDirection:int, yDirection:int):void { if(blockGrid.getPiecePosition(piece).y > 0) { blockGrid.movePiece(fallingPiece, xDirection, yDirection); } else { fallingPiece = null; } }
I do not suppose that I correctly designed these class relationships to work with this principle. If this is what I am missing, consult an alternative design.
EDIT, Suggested Solution:
I went with answers suggesting "team feedback" through events. The board advises BlockGrid to move the piece. The BlockGrid movePiece method dispatches MOVED_TO or MOVE_FAILED events, depending on the result that the Council can listen to and use to determine if the piece has stopped falling. Please feel free to report this decision.
public class Board { ... public function Board() { ... blockGrid.addEventListener(PieceMoveEvent.MOVE_FAILED, onPieceMoveFailed); ... } public function moveFallingPiece(xDirection:int, yDirection:int):void { blockGrid.movePiece(fallingPiece, xDirection, yDirection); } public function onPieceMoveFailed(event:MovePieceEvent):void { if(event.instance == currentlyFallingPiece && event.fromPosition.y != event.toPosition.y) { currentlyFallingPiece = null; } }