Note that there are some explanatory texts on larger screens.

plurals
  1. POPreferred way to "move" an object between lists
    text
    copied!<p>I have two separate lists of entities:</p> <pre><code>class EntityCollection : IList&lt;Entity&gt; { //... } EntityCollection Foo; EntityCollection Bar; </code></pre> <p>I want to implement an operation to move an object <code>Qux</code> that is on list Foo to Bar. What's the best way to implement it?</p> <ul> <li><p>As a <code>MoveTo</code> instance method on <code>EntityCollection</code>:</p> <pre><code>public void MoveTo(EntityCollection to, Entity entity); // Client code Foo.MoveTo(Bar, Qux); </code></pre></li> <li><p>As a <code>MoveFrom</code> instance method on <code>EntityCollection</code>:</p> <pre><code>public void MoveFrom(EntityCollection from, Entity entity); // Client code Bar.MoveFrom(Foo, Qux); </code></pre></li> <li><p>As a static <code>Move</code> method on <code>EntityCollection</code>:</p> <pre><code>public static void Move(Entity entity, EntityCollection from, EntityCollection to); // Client code EntityCollection.Move(Qux, Foo, Bar); </code></pre></li> <li><p>As a <code>Move</code> instance method on the class that holds boths collections:</p> <pre><code>public void Move(Entity entity, EntityCollection from, EntityCollection to); // Client code Holder.Move(Qux, Foo, Bar); </code></pre></li> </ul> <p>Alternatively, and since the entities can only be in one collection at a time, I could have the entities track their location themselves, and implement it on the entity itself:</p> <pre><code> public void MoveTo(EntityCollection to) { if(Location != null) Location.Remove(this); to.Add(this); Location = to; } // Client code Entity e; e.MoveTo(Foo); // Later on... e.MoveTo(Bar); </code></pre> <p>When presented with so many options, I want to know: where does the move method belong? And why?</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