views:

104

answers:

7

Assume you have a variety of number or int based variables that you want to be initialized to some default value. But using 0 could be problematic because 0 is meaningful and could have side affects.

Are there any conventions around this?

I have been working in Actionscript lately and have a variety of value objects with optional parameters so for most variables I set null but for numbers or ints I can't use null. An example:

package com.website.app.model.vo
{   
    public class MyValueObject
    {
     public function MyValueObject (
                                            _id:String=null, 
                                            _amount:Number=0,
                                            _isPurchased:Boolean=false
                                      )
     { // Constructor
      if( _id != null )           this.id          = _id;
      if( _amount != 0 )          this.amount      = _amount;
      if( _isPurchased != false ) this.isPurchased = _isPurchased;
     }

     public var id:String;
     public var amount:Number;
         public var isPurchased:Boolean;
    }
}

The difficulty is that using 0 in the above code might be problematic if the value is not ever changed from its initial value. It is easy to detect if a variable has a null value. But detecting 0 may not be so easy because 0 might be a legitimate value. I want to set a default value to make the parameter optional but I also want to later detect in my code if the value was changed from its default without hard to debug side affects.

I suppose I could use something like -1 for a value. I was wondering if there are any well known coding conventions for this kind of thing? I suppose it depends on the nature of the variable and the data.

This is first my stack overflow question. Hopefully the gist of my question makes sense.

+1  A: 

The value -1 is often traditionally used as an "out of range" or "invalid" value to indicate failure or non-initialised data. Then again, that goes right down the pan if -1 is a semantically valid value for the variable...or you're using an unsigned type.

Rob
+1  A: 

You seem to like null (and for a good reason), so why not just use it throughout?

Pavel Minaev
Actionscript doesn't accept null for int or number typed parameters. But in reading this question:http://stackoverflow.com/questions/1003100/why-cant-typed-optional-arguments-have-a-default-of-nullit looks like NaN is an equivalent null value for numbers. So I think that answers my question. But I am curious what conventions exist in other languages.
Gordon Potter
+3  A: 

A lot of debuggers will use 0xdeadbeef for initializing registers. I always get a chuckle when I see that.

But, in all honesty, your question contains its own answer - use a value that your variable is not ever expected to become. It doesn't matter what the value is.

paxdiablo
0xdeadbeef, LOL. Interesting, will keep an eye out for that one. Thanks for the response. This is the kind of stuff I was looking for. And I agree usage of a non expected number makes sense.
Gordon Potter
+1  A: 

Since you asked in a comment I'll talk a little bit about C and C++. For efficiency reasons local variables and allocated memory are not initialized by default. But debug builds often do this to help catch errors. A common value used is 0xcdcdcdcd which is reasonably unlikely. It has the high bit set and is either a rather large unsigned or rather large negative signed number. As a pointer address it is odd which will cause an alignment exception if used on anything but a char (but not on X86). It has no special meaning as a 32 bit floating point number so it isn't a perfect choice.

Occasionally you'll see a partially aligned value in a variable such as 0xcdcd0000 or 0x0000cdcd. These can be treated as suspcious at the very least.

Sometimes different values will be used depending on the allocation area of library. That gives you a clue where a bad value may have originated (i.e., it itself wasn't initialized but it was copied from an unititialized value).

The ideal value would be invalid no matter what alignment you read from memory and is invalid over all primitive types. It also should look suspicious to a human so even if they do not know the convention they can suspect something is a foot. That's why 0xdeadbeef can be a good choice because the (hex viewing) programmer will recognize that as the work of a human and not random chance. Note also that it is odd and has the high bit set so it has that going for it.

George Phillips
+1  A: 

In ActionScript you can only assign Number.NaN to variables that are typed Number, not int or uint.

That being said, because AS3 does not support named arguments you can always look at the arguments array (it's a built-in array that all functions have, unless you use the ...rest construct). If that array's length is less than the position of your numeric argument you know it wasn't passed in.

Branden Hall
Interesting point the arguments array I will look into that. Sounds very useful. Thanks for the tip.
Gordon Potter
+1  A: 

I often use a maximum value for this. As you say, zero often is a valid value. Generally max-int, while theoretically valid, is safe to exclude. But not always; be careful.

ejgottl
+1  A: 

I like 0xD15EA5ED, it's similar to 0xDEADBEEF but is usually more accurate when debugging.

KPexEA