title: Implementing multiple overloaded functions vs doing inside checking/validation for enforcing business logic
body: <p>This is a general issue, but i will demonstrate it using the following problem:</p>
  <p>I want to model airCrafts. now, for every aircraft there is one capability that is loaded with: attackCapability, IntelligenceCapability and BdaCapability. Not all aircraft can be loaded with all capabilities - every airCraft has its own potential capabilities that is supports and can be loaded with.</p>
  <p>I want to implement this business logic with composition:
  every aircraft object will hold a private member of type Capability (every capability will be implemented by a class that inherits from this abstract class/interface).</p>
  <p>Now - I want to design a code that will enforce the business logic - i.e. will not allow any programmer to load an aircraft with an ability it doesn't support.</p>
  <p>I have two options for doing this:</p>
  <ol>
  <li>implement several overloaded version of the <code>SetCapability()</code> function - each one with the type of capability i want to support. For example:</li>
  </ol>
  <pre><code>public void SetCapability(AttackCapability capability);
  public void SetCapability(BdaCapability capability);
  </code></pre>
  <p>That way the user can't load the aircraft with forbidden capability - and if he/she will try doing so, there will be a compilation erorr - i.e. that IDE will show some error message.</p>
  <ol start="2">
  <li>implement one function: public void SetCapability(Capability capability)
  and doing some checking inside it. something like this:</li>
  </ol>
  <pre><code>public void SetCapability(Capability capability){
      if(capability.getType() != typeOf(AttackCapability) || capability.getType() != typeOf(BdaCapability){
             throw new InvalidOperationException();
      }
      _capability = capability;
  }
  </code></pre>
  <p>the disdvantage here is that misuse of the user will be identified only at runtime instead at compiletime (much harder to identify and more bug prone), but as opposed to the previous option - it doesn't make you write several overloaded function which may cause the class to look heavy, strange and less readable for the inexperienced programmer.
  (code reusability is not a big issues, because we always can implement private function like: private void SetCapabiltiy(Capability capability); which do the setting logic and every public overloaded SetCapability function will call it).</p>
  <p>I feel that both option have their advantages and disadvantages as i described above.</p>
  <p>I tend to prefer the first one, because it is more safe and hermeric - but it frequently causes my code to look &quot;ugly&quot; and some programmers may interprete it as duplicated code and don't understand the point...</p>
  <p>I would like to hear your opinion for this issues, and maybe you have some better designs for this problem.</p>