Results 1 to 2 of 2
- 08-08-2010, 06:16 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 1
- Rep Power
- 0
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; ... ... }
- 08-08-2010, 07:58 PM #2
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
-
Default values of stored procedures
By vinayakshukre in forum JDBCReplies: 2Last Post: 01-13-2009, 12:05 PM -
Printing default Swing values
By Java Tip in forum Java TipReplies: 0Last Post: 03-12-2008, 11:09 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks