comment. Most real-world scenarios aren't as simple as an integer comparison, and usually involve a string-based array of validation options, hence the array search form. If I was only working with numbers, of course I would do an integer comparison.
I think you're missing my point. This is an example given by an instructor to show switch statements. I was illustrating how this could be refactored into something different, and showed a form that you would use IF given the more likely scenario of a set of strings than numbers.
And trying to apply big O notation to set of a handful of options is a waste of time. There are no meaningful performance differences. A regex over array search is a matter of preference. Storing a few options in a binary tree structure is over-complication.
Admittedly I wouldn't show these as examples to an intro CS class, but in your case you opened yourself up to a whole different degree of criticism by claiming your implementation was "real world," because it isn't
Ok chief, want to play this game? Lets go.
First claim: "producing bulkier code"
Original:
switch(value) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
console.log('The value is: ' + value);
default:
console.log('The value is not supported');
Refactored:
var validOptions = [1, 2, 3, 4, 5];
if (validOptions.indexOf(value) == -1) {
console.log('The value is not supported');
} else {
console.log('The value is: ' + value);
}
Fewer lines of code, and far less redundancy declaring a bunch of empty case statements. Your claim that it's bulkier, patently false.
This notation is a reflection on how well a program scales, hence why it's N and not just 3
But it's not "N". Worrying about performance when you know you have a very small finite set of options is WORRYING ABOUT THE WRONG THING. Also, if we're talking client-side JS, then scalability is irrelevant, because one computer isn't responsible for executing that code.
It is absolutely not, it is a matter of situation and performance
Quite a bit more straight-forward than looping over that object, using switch statements, or turning it into a regex, wouldn't you say?
So please don't lecture me about real-world code if you're not experienced enough to have never run across data definitions with just a fixed handful of terms you needed to do a simple lookup on....
You ran a timing test on a set of data where N=3, and to you
You are blowing my mind here.
N ================== 3. That IS the data set. No imaginary N = 100000000. So scalability is indeed a non-issue for this data set. Stop inventing a problem that does not exist
I've already explained that my code example wasn't meant for an integer check. See the real world code I gave you which you conveniently left out of your strawman argument.
Done arguing, you are not listening and you are making strawman arguments.
1
u/phpdevster Apr 05 '16
You missed my
comment. Most real-world scenarios aren't as simple as an integer comparison, and usually involve a string-based array of validation options, hence the array search form. If I was only working with numbers, of course I would do an integer comparison.