Results 1 to 2 of 2
  1. #1
    diablo84 is offline Member
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

    Question Negative one for illegal/default values

    Hi:
    What is the general consensus on passing of negative one (an impossible value) to a method to denote that a default value must be used or that parameter should be ignored? I have seen it done in many places, but is it considered an acceptable practice? When should/shouldn't it be used?
    Example:

    Java Code:
    public void doSomething (int param1, int param2, int param3) {
        if (param1 != -1)
            _myObject.x = param1;
        if (param2 != -1)
            _myObject.y = param2;
        ...
        ...
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Personally I think -1 is used because it is not an acceptable number to call an array index. Negative one is generally accepted as "infinity" in computer coding for legacy reasons, so it has kind of, more or less, become the "undefined" number. Since everything is zero- or one-based (arrays, Strings, basic counting, string lengths, etc.), negative one will hardly ever be a valid value.

    You can use it wherever you see fit, really. Just try to think of it like this: "Can -1 be an acceptable value in this parameter? If yes, then it cannot be a default value. If not, then it can be."

    Much luck.

Similar Threads

  1. Default values of stored procedures
    By vinayakshukre in forum JDBC
    Replies: 2
    Last Post: 01-13-2009, 01:05 PM
  2. Printing default Swing values
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 12:09 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •