File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Cattle Drive and the fly likes range testing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "range testing" Watch "range testing" New topic
Author

range testing

Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Ladies, Gentlemen and Nit-Pickers -

I have spent the past eternity being "picked" with respect to clarity, readability and simplicity of code. So now I have a "nit": When it's necessary to test a command-line input for range, say the input number must be between 0 and 99 inclusive, it seems to me that the obvious and simplest test would be something like


But instructors' solutions have code like


I seems to me that the instructors' solutions are less "obvious" than the simple "is it in this range" comparison. The range comparison does not require more code, since the input string has to be converted to an integer anyway; in any case, I have been told that more code is just fine as long as it's not more than 3 or 4 (or so) more lines. So what's the story here?

Thanks!

[ January 22, 2008: Message edited by: Jinny Morris ]

[ko]: edited to obfuscate code
[ January 22, 2008: Message edited by: Katrina Owen ]
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1357
    
  17
Hi Ginny,

Since the String is already there, it doesn't add any complexity to use it before it gets converted either.

In this particular case, it is either this, or find a nice, readable way to represent a number with enough digits that most people have to count zeros or nines to figure out just what the number is. There are several nice ways of doing that.

Both the String approach and the numeric approach work well.

I wouldn't have thought of the String approach myself, but I sure was impressed when I saw that solution the first time (okay, so I'm easily impressed)
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Katrina -

Thanks - but - well, but ... I had asked Pauline about this one, & she suggested that I ask on the forum. The trouble with obfuscating the instructor's code in this instance is that I (at least) had to look at the code twice in order to see what it was doing, and had been being "picked" on exactly that point (that if the code is clear, you shouldn't have to look at it twice). So on the one hand, I can see why you did that, but on the other hand, just saying "string stuff" seems like it kind of loses my point. And when the "string stuff" uses a count of digits, I still have to count the digits in my number ...

??
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
There are certainly many ways to approach this issue. It seems that when the original instructor's solution for Say was written, the instructor who wrote it was thinking more about using Strings and String methods. That approach has its pros and cons.

The indexOf() method of the String class is very useful in certain situations, and it is certainly good to be aware of how to use it to test various things. I think that whether or not it is better than the numeric comparison in this particular case is debatable.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
I think that whether or not it is better than the numeric comparison in this particular case is debatable.

That's why I thought this is such a good question (thanks for posting it, Jinny ).

So what do the rest of y'all cowpokes think?
Dick Summerfield
Ranch Hand

Joined: Oct 04, 2007
Posts: 90
Originally posted by Jinny Morris:
it seems to me that the obvious and simplest test would be something like

I can't very well disagree with you there because it's what I wrote too As you say, it seems the obvious approach. Also we get an immediate check on whether the input is numerical which gets deferred in the other approach: (input of "99---" for instance, gives message "value out of range" instead of NumberFormatException). One snag with our method, as someone pointed out, is when the numbers-to-be-validated get big (and I have this image, probably not entirely devoid of sadism , of nitpickers with rolling eyeballs as they try to count how many "nines" there are in front of the "L").
I just tried using a "constant" (are we allowed to say that in Java?) instead of a direct literal - which is no big improvement but maybe it helps readability:

[ January 23, 2008: Message edited by: Dick Summerfield ]
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20494
    ∞

In the world of pair programming, the developer with the keyboard creates what they think is the best solution. If the other developer thinks of a simpler solution, they suggest it. If the developer with the keyboard doesn't agree, eventually the keyboard goes to the other developer and they get to express their opinion in Java.

If the two developers do not agree on which solution is best, the shorter solution is the winner.

In this case .... if y'all are talking about what I think you are talking about ... I think one of the important issues would be whether the number is so big that it cannot be converted to an int, or even a long.

Perhaps a group of interested folks that have completed this assignment could send an e-mail to all interested parties and have a reply-all style discussion of this issue.


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Perhaps a group of interested folks that have completed this assignment could send an e-mail to all interested parties and have a reply-all style discussion of this issue.


No more discussion here? I had assumed this topic wasn't that sensitive, even though it is part of the assignment requirements. Oh well.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Checking range requirements is part of the requirements. How you check the range is not.
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1357
    
  17
Originally posted by paul wheaton:
In this case .... if y'all are talking about what I think you are talking about ... I think one of the important issues would be whether the number is so big that it cannot be converted to an int, or even a long.


Well technically one should be able to stuff this into a long, but if it is out of range, it could probably blow up.

Originally posted by Dick Summerfield:
One snag with our method, as someone pointed out, is when the numbers-to-be-validated get big (and I have this image, probably not entirely devoid of sadism , of nitpickers with rolling eyeballs as they try to count how many "nines" there are in front of the "L").


No kidding - usually I have to move the cursor through the number, counting as I go. (And yes, I check, because it sometimes happens that the range checking checks against the wrong number)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: range testing
 
Similar Threads
How to compare String and int?
creating a method
Can we have two return statement in a method
Junit query
Help with program: Monty Hall Problem