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

    Question Negative one for illegal/default values

    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?

    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 is offline Senior Member
    Join Date
    Jun 2010
    Destiny Islands
    Rep Power


    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, 12:05 PM
  2. Printing default Swing values
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 11:09 AM

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