Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes, I'd say something isn't designed right there. It's hard to say since you don't show how <code>FallingPiece</code> is used or how it fits in the system, but it doesn't make sense to me why it needs to get a <code>currentPiece</code> in its constructor.</p> <p>Since it looks like <code>currentPiece</code> can change over time, it seems like you're using a single instance of <code>FallingPiece</code> as a singleton container for the current falling piece. In that case, you must be getting the second, third and so on pieces some way. Why not simply do the same for the first piece?</p> <p>You say that when a piece hits the bottom, an event is raised that triggers the generation of a new piece to start falling. Shouldn't you just fire that event yourself to start the first piece?</p> <p><strong>In general:</strong></p> <p>The general reason (as I see it) that work with dependencies shouldn't be done in the constructor is that the construction of an object should be purely about setting up the class to be able to do what it needs to do. Having the classes actually <em>do</em> what they do should be accomplished through calls to the object's API after it is created. In the case of <code>FallingPiece</code>, once it has an <code>IPieceGenerator</code> it has the ability to do everything it needs to do. Actually doing that (starting a piece falling) should be done by calling a method on its API or (as seems to be the case here) firing an event that it will respond to.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload