Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Interesting question! </p> <p>From the info that you have supplied, it seems to me that <code>setstate</code> (or simply <code>set</code>, if you are setting other things than states) would be fine, though <code>ensure</code> is good if you want to really emphasize the redundancy of an <code>if</code>.</p> <p>To me it is however perfectly intuitive that setting a state does not throw an exception, or require an <code>if</code>. Think of setting the state of any other variable:</p> <p>In C:</p> <pre><code>int i; i = 5; // Would you expect this to throw an exception if i was already 5? // Would you write if (i != 5) i = 5; // ? </code></pre> <p>Also it only takes about one sentence to document this behaviour:</p> <blockquote> <p>The function does nothing if the current state equals the requested state.</p> </blockquote> <p>EDIT: Actually, thinking about it, if it is really important to you (for some reason) that the user is not confused about this, I would in fact pick <code>ensure</code> (or some other non-standard name). Why? Because as a user, a name like that would make me scratch my head a bit and look up the documentation ("This is more than just an ordinary set-function, apparently"). </p> <p>EDIT 2: Only you know how you design your programs, and which function name fits in best. From what you are saying, it seems like your setting functions sometimes throw exceptions, and you need to name a setting function that doesn't - e.g. <code>set_missile_target</code>. If that is the case, I think you should consider the <code>set_if</code>, <code>set_when</code>, <code>set_cond</code> or <code>cond_set</code> names. Which one would kind of depend on the rest of your code. I would also add that one line of documentation (or two, if you're generous), which clarifies the whole thing. </p> <p>For example:</p> <pre><code> // Sets missile target if current target is not already the requested target, // in which case it does nothing. No exceptions are thrown. function cond_set_missile_target () or function cond_set_MissileTarget () or function condSet_MissileTarget () or function condSetMissileTarget () </code></pre> <p><code>ensure</code> is not so bad, but to me it implies only that there is additional logic required to set the state (e.g. multiple states tied together, or other complications). It helps to make the user avoid adding unnecessary <code>if</code>s, but it does not help much with the exception issue. I would expect an <code>ensure</code> function to throw an exception sooner than a <code>set</code> function, since the <code>ensure</code> function clearly has more responsibilities for, well, ensuring that this setting operation is in fact done right. </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