Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>UPD</strong> (from my comment): It is certainly not a good practice to pick some magic number to indicate value absence, unless you can be completely sure this magic number would not appear as a result of some arithmetics (which generally is true). So the best approach here is either to use a wider type than <code>int</code> (e.g. <code>Number</code>), or pass some flag (<code>Boolean</code> value) to function, as well as <code>int</code>, to indicate actual value absence. Choice should depend on whether you want to emphasize the importance of fact that passed value is integral.</p> <hr> <p>If you feel comfortable with expanding argument type, then you should use <code>NaN</code> and do this:</p> <pre><code>function setMyValue (myVar : Number = NaN) : void { if ( isNaN(myVar) ) { /* Generate a value for myVar */ } else { /* Use the supplied value for myVar */ } } </code></pre> <p>That is both more technically accurate and it is also more stable. It will also allow you to strongly type at compile-time and would not cause weird bugs as in case of using <code>Object</code>.</p> <p>Note that this will not work with <code>int</code> as one of the answers suggests. <code>int</code> has <em>only</em> numerical values, and <code>NaN</code> will get coerced to 0. You will need to use <code>Number</code> type. </p> <p>If you're worried about performance - don't be. Using <code>Number</code> instead of <code>int</code> or <code>uint</code> is generally OK.</p> <hr> <p><strong>UPD:</strong> If you want to explicitly mark that argument should be integral just use additional flag and exact type - it would be much clearer.</p> <pre><code>function setMyValue (isIntValueSupplied : Boolean = false, intValue : int = 0) : void { if ( isIntValueSupplied ) { /* Generate or use a default value of intValue */ } else { /* Use the supplied value for intValue */ } } </code></pre> <hr> <p>Another option is to define your own type with additional value, e.g. <code>MaybeInt</code>, which holds integer value and a flag (whether it was actually initialized or not). That way you would clearly signal that value should be integral or <em>no-value</em>. However, this approach may be a bit bulky.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COThis is certainly getting closer, as at least I'll know that it's a number coming through the param (which I can then re-cast to an int/uint), but it goes to much the same problem as my concern with dirkgently's response. Is it really cleaner to typecast to the wrong data type? Perhaps I'm simply asking for something that can't be done in a truly clean way. BTW, this has nothing to do with performance per se. I'm simply trying to establish a best practice approach to this issue. Thanks for the help!
      singulars
    2. CO@Wikiup: It is certainly not a good practice to pick some magic number to indicate value absence, unless you can be completely sure this magic number would not appear as a result of some arithmetics (which generally is true). So the best approach here is either to use a wider type than `int` (e.g. `Number`), or pass some flag (`Boolean` value) to function, as well as `int`, to indicate actual value absence. Choice should depend on whether you want to emphasize the importance of fact that passed value is integral.
      singulars
    3. COI think your comment provides a far better "best practice" solution than your original answer. I agree that magic numbers are seldom a good idea (that was, after all, the point of my original posting). function setMyValue(useSuppliedValue:Boolean = false, suppliedValue:int = 0), while wordier, seems to provide a less ambiguous format for the method; this strikes me as a pretty solid answer. If you want to edit your answer and write it up as such, I'll happily check 'answer' and credit you with the rep points. :c) Many thanks!
      singulars
 

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