Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

range testing

 
Jinny Morris
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21379
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Checking range requirements is part of the requirements. How you check the range is not.
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic