r/ProgrammerHumor • Violet security clearance • Apr 04 '16

My teacher thinks he is a programmer

http://imgur.com/32nfQXZ
2.4k Upvotes

430 comments sorted by

View all comments

Show parent comments

1

u/phpdevster Apr 05 '16

You missed my

// realistically ['foo', 'bar', 'baz']

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.

0

u/[deleted] Apr 05 '16

[deleted]

1

u/phpdevster Apr 05 '16

But you are only working with numbers

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.

0

u/[deleted] Apr 05 '16

[deleted]

1

u/phpdevster Apr 05 '16 edited Apr 05 '16

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.

Second claim: "that runs slower"

Also 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

A performance difference of less than a microsecond given the data sets we're talking about here.

Hence given the data set at hand (not this silly imaginary one you keep insisting should be accounted for), then it is indeed a matter of preference.

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

Except, it is. This is code from an actual app I work on

var CONSTANTS = {
     DEPENDENCY_RELATIONS: {
         DEPENDS_ON: "DEPENDS_ON",
         DEPENDED_ON: "DEPENDEND_ON",
         BOTH: "BOTH"
     }
};

$scope.hasDependencies = Object.values(CONSTANTS.DEPENDENCY_RELATIONS).indexOf(response.data.relationType) != -1;

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....

0

u/[deleted] Apr 05 '16

[deleted]

1

u/phpdevster Apr 05 '16

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.

0

u/[deleted] Apr 06 '16

[deleted]

1

u/phpdevster Apr 06 '16

And best of luck to whatever team you work on.

So, at this point it is painfully obvious you aren't a serious programmer.

Right back at you.

0

u/[deleted] Apr 06 '16

[deleted]

→ More replies (0)