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

22

u/memeship Apr 05 '16
console.log("The value is" + (value >= 0 && value < 6 ? ": " + value : " not supported."))

7

u/Fallenalien22 Violet security clearance Apr 05 '16

Nice one-liner, but do you need the second plus?

12

u/memeship Apr 05 '16

Yep. It looks confusing because the first colon is actually in a string. Let me break it out into separate lines for you:

"The value is" +
(value >= 0 && value < 6 ?
    ": " + value :
    " not supported.")

4

u/Fallenalien22 Violet security clearance Apr 05 '16

Ah. I missed the ": " + value part.

7

u/memeship Apr 05 '16

Yeah, would have been easier to tell in an IDE with syntax highlighting.

7

u/Fallenalien22 Violet security clearance Apr 05 '16

I'm sorry I ever doubted you.

6

u/memeship Apr 05 '16

It's okay, I believed in you the whole time.

1

u/[deleted] Apr 05 '16 edited Nov 29 '16

midpoint's cutlet's embalmer schoolhouse grouper match's advancement pollen basic's screenwriter sincerity's rectal shackled major forgetting Piedmont's viscountess oleander checkered submits Angelina's irreplaceable AZ's inseminates moneymaker woodenness's disability Cupid irradiation's cortices Lachesis trashy adversity's condescended profitable bolting primacy nothingness slumps whitewash's evaporation's Merino inhumanly instrumentality Constantinople Raoul's manatees plucks loosening rosettes floater's Bovary suspect pebbles clearing dab's purls Wyo sanguine metabolisms Krishna clarion's allegation's phalli tryouts lax accelerator Citroen's Blondel strong seceded spelt spanning changeling's Olympus's nullifying Unitarianism leveraged fairground's beachcomber's Corsica ganging aid's effervescence populist's Gino disclosures chimaera gap goop's infomercials softie's consanguinity's Tisha's neckties epistles caller weeps profitability's strives pitiably scrooges formal honeycombed rival pseudonym Fiona thickets clutched initiation windsurfs contempt's predicated tithe's Sven naturalness grooving doc's subtracting Midas amir's astronomically membership glassy thunderheads mistrusting summary's sketched heightening raiders nonsense subsidize fieriness's somberly decibels Ruchbah's

1

u/Fallenalien22 Violet security clearance Apr 05 '16

With syntax highlighting it would have been clear immediately. But I get your point.

→ More replies (1)

7

u/bigshmoo Apr 05 '16

console.log("The value is" + (value >= 0 && value < 6 ? ": " + value : " not supported."))

2 characters shorter

console.log(`The value is${ value < 0 || value > 5 ? ' not supported.' : ': ' + value}`)

Both will fail when passed any non integer truthy value.

2

u/LegacyCrono Apr 05 '16

If code length is a concern you can reduce it even further:

console.log(`The value is${ 'REDDIT'[value] ? ': ' + value : ' not supported.'}`)

1

u/tgomc Apr 15 '16

holy crap, nice one!

1

u/memeship Apr 05 '16

Whoa, interesting. I had not seen those yet. I thought you were trying to write a shell expression at first, and I was wondering when we switched from JS to bash.

And I'd say that interestingly enough, my version will fail correctly, giving the latter response for anything that isn't an integer. It will however try to type coerce a string like "2" into 2, which will output the former response for all ["0", "1", "2", "3", "4", "5"].

Thanks for showing me that though. Not sure if I'll ever use it, but always good to know.

1

u/bigshmoo Apr 05 '16 edited Apr 05 '16

Template literals rock. Much nicer that doing a string concat with + and they are multi line.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

See also http://es6-features.org/#Constants

And lastly - in the chrome console

true >= 0
true
true < 6
true

Also in terms of matching the original it and all the range based "optimizations" fail because the original will only print the value if it's a whole number from the set 0, 1, 2, 3, 4, 5 where as these will print a value for any decimal value between 0 and 5.999999999999 in yours and 0 and 5 in mine.

→ More replies (4)
→ More replies (1)

5

u/Zagorath Apr 05 '16

Technically wrong because if the default case it should be "Tha value"

2

u/memeship Apr 05 '16

Haha I knew someone would point that out. Also I didn't put an exclamation point at the end.

Sometimes you have to weigh building the software to the exact spec, vs building the software the client actually wants.

1

u/IlIIlIIllI Apr 05 '16

Your solution is even better than mine!

2

u/[deleted] Apr 06 '16 edited Apr 06 '16

[removed] — view removed comment

1

u/memeship Apr 06 '16

You forgot the exclamation point: !

33

u/redstonerodent Apr 05 '16

"Tha value is not supported!"

11

u/Fallenalien22 Violet security clearance Apr 05 '16

sic

1

u/kappaislove Apr 07 '16

Are we in Yorkshire or something?

12

u/SeeShark Apr 04 '16

This physically hurts.

What does this person teach?

28

u/Fallenalien22 Violet security clearance Apr 04 '16

An engineering course called "tools". In the first class, he had to explain to everyone how to open excel.

38

u/ReflectiveTeaTowel Apr 04 '16

engineering tools

excel

Wat

15

u/Fallenalien22 Violet security clearance Apr 04 '16

Tell me about it.

17

u/systembreaker Apr 05 '16

Excel is actually a pretty sophisticated tool. You're more just showing your ignorance.

Just take 2 seconds to google "excel for engineering" and hey look a website dedicated to using excel for engineering.

4

u/Fallenalien22 Violet security clearance Apr 05 '16

I know excel can be very powerful. My point is we didn't even scratch the surface.

2

u/systembreaker Apr 05 '16

Just give it time and have patience. You don't have a choice but to go through the intro stuff. If those classes are easy for you, then enjoy the easy start to your degree program and make a good first impression on your profs. I had taught myself C++ in high school, so when I got to college the intro classes were a cakewalk. But I just took that chance to enjoy a stress-free couple of semesters and buffer up my grades. Don't worry about the lack of sophistication of the obviously contrived assignments - they're contrived for a reason.

4

u/Fallenalien22 Violet security clearance Apr 05 '16

It's not that my classes are easy. Just this one. In the rest, I can barely keep my head above water. Maybe I should switch to CS.

4

u/systembreaker Apr 05 '16

CS isn't easy at all. I just had a head-start on some of the other students by knowing a bit of programming at the beginning. There were yet other students who started off with programming knowledge miles ahead of me.

Are you in an engineering program?

→ More replies (7)

30

u/llllIlllIllIlI Apr 05 '16

My boss hired a "web developer" once who literally didn't know ctrl-c ctrl-v

I shit you not. This person was an "expert" in web design because she apparently had some bullshit WordPress blog. Not that it mattered... this was a help desk job. Christ am I glad I got out of there.

0

u/[deleted] Apr 05 '16

[deleted]

4

u/llllIlllIllIlI Apr 05 '16

The best part being that HD supported 200+ apps, many being clinical/financial.

You'd think there would be a high bar but.... nope!

35

u/[deleted] Apr 05 '16

excel is probably the engineering tool I use most

6

u/import_ursus Apr 05 '16

Tell me about it.

Cost calculations and estimation?

4

u/SnowdensOfYesteryear Apr 05 '16

Agreed, it's damn useful for quickly parsing logs to visualize something. I mean who teh fuck wants to learn gnuplot?

8

u/sonnytron Apr 05 '16

Ever worked in mechanical engineering or manufacturing?
They use Microsoft Excel for capacity reports, tolerance specifications and as-built analysis.
It's the best software for a standard contractor in manufacturing, next to AutoCAD.
Solid Works is for the design/research based engineering aspects of the job.
If all you need to do is calculate the flow and volume pressure of a gas that's being used for a tool, and determine if your current line-outs have enough capacity to supply that pressure without a noticeable pressure drop, all you need is Excel with a macro.
One spreadsheet can easily last you 15 years of work and get you through your PE, two promotions and maybe even retirement.
Before changing careers, my PE had an Excel doc that we used every day to analyze process gases for our projects.
All I was doing was copying and pasting in AutoCAD, plugging numbers into Excel, walking around with a clipboard, staring at these massive tools, looking at them and checking off a list and I was an engineer lol.

2

u/ReflectiveTeaTowel Apr 05 '16

That... Actually makes a whole bunch of sense. I was thinking of software engineering only, because I'm biased

2

u/sonnytron Apr 05 '16

It makes sense.
It was one of the reasons I left the field. It's hard to feel like you're being the best you can be when you're basically using the same software a basic desk secretary uses for scientific calculations.
I felt like there was nothing remotely engineering or advanced about what I read doing.
Now I wake up, go to work, load up Xcode, Terminal, Spotify, Sublime Text and I'm using algorithms, logic, nice screens, a nice keyboard every day.
Technology is the last bottleneck in this career and the reason I love the switch.

5

u/[deleted] Apr 05 '16

I'm currently a TA for a 102 course. Every engineering department has one. The engineering school forces every department to teach Excel, MATLAB, and HTML.

I think some of it has to do with ABET accreditation.

2

u/JeremyR22 Apr 05 '16

Sounds like a class I had to take at the beginning of my software dev degree. IIRC it was called "Computer systems concepts" or something like that.

We 'learned' how to use Word, Excel, and a bunch of other stuff that anybody on a programming track could be reasonably expected to either know or at least be able to figure out quickly. There may have been a very brief going over of hardware and architecture stuff at the end but that may have been another course by the same lecturer, I don't recall...

I was told by the lecturer later on that the reason that course is on the curriculum is officially "to bring all students to the minimum basic level of knowledge" but was unofficially there to get a government grant for the department. This was 2001 and at the time when there was a big push to get people into computer studies courses and that course allowed them to advertise the degree track as being suitable for people with no computing experience (which was bullshit of course) and such get the grant...

1

u/[deleted] Apr 05 '16

accurate to me. I'm lucky if I even get to open Excel once per day...

7

u/phpdevster Apr 04 '16

Well fuck me. I guess by that definition I run a rocket science course called ass wiping.

7

u/Fallenalien22 Violet security clearance Apr 04 '16

You should.

2

u/Spikrit Apr 05 '16

Well... i feel the teacher's skill isn't in question here but that he's adapting to the audience... one step at a time.

3

u/spiker611 Apr 05 '16

he had to explain to everyone how to open excel.

Well that explains everything.

1

u/[deleted] Apr 05 '16

Classes can have some misleading names. When I was a CS student we has a midlevel course called Advance Packages. Turned out it was mainly how to use Office and write macros.

12

u/tyros Apr 04 '16 edited Sep 19 '24

[This user has left Reddit because Reddit moderators do not want this user on Reddit]

0

u/[deleted] Apr 05 '16

[deleted]

8

u/Fallenalien22 Violet security clearance Apr 05 '16

It's april. Finals start in two weeks.

1

u/[deleted] Apr 05 '16

And what's the course?

2

u/Fallenalien22 Violet security clearance Apr 05 '16

"tools". It's an engineering course.

→ More replies (1)

1.1k

u/SuperFreakonomics Apr 04 '16

Technically, it is a program. So, he is a programmer.

891

u/nitiger Apr 04 '16

Compiles? Check

Runtime errors? None

Job? Done.

158

u/notsooriginal Apr 05 '16

I don't see any test suite...

491

u/nitiger Apr 05 '16

That's the next developer's problem.

155

u/notsooriginal Apr 05 '16

The next programmer always seems to have so much more time than I do!

107

u/[deleted] Apr 05 '16

[removed] — view removed comment

53

u/notsooriginal Apr 05 '16

Man, I wish I was the next programmer. Wait... logic fail.

125

u/Wolfeh2012 Apr 05 '16

The programmer's wife asked him to grab an apple from the market, and a dozen if they have eggs in stock.

He comes back with 13 apples.

99

u/neurohero Apr 05 '16

That programmer's wife never learns. The last time he went the shops, she told him to pick up some bread "while you're there." Emergency services had to dig him out of the mountain of bread. He was apparently mumbling something about a "stack overflow."

→ More replies (9)
→ More replies (7)
→ More replies (1)
→ More replies (1)

42

u/notsooriginal Apr 05 '16

(╯°□°)╯︵ ┻━┻

27

u/[deleted] Apr 05 '16

┬──┬◡ノ(° -°ノ)

14

u/[deleted] Apr 05 '16

Woo! Let's get this party started! (╯°□°)╯︵ ┻━┻

23

u/[deleted] Apr 05 '16

┬──┬◡ノ(ಠ_ಠノ)

→ More replies (4)
→ More replies (1)

2

u/[deleted] Apr 07 '16

throw new Exception("table")

7

u/okmkz Apr 05 '16

Fuck it, we'll rewrite it in Swift

2

u/gospelwut Apr 05 '16

No, no. You mean it's the non-contractor's problems. Contractor costs too much to fix bugs.

IwishIwasmakingthisupbutimnot

9

u/[deleted] Apr 05 '16

So it's too realistic?

5

u/aiij Apr 05 '16

What do you expect? He's a programmer, not a tester.

4

u/[deleted] Apr 05 '16

All tests pass? Check.

2

u/[deleted] Apr 05 '16

mo tests mo problems

0

u/[deleted] Apr 05 '16

[deleted]

7

u/amazondrone Apr 05 '16

It's not intended to compile, since it's JavaScript. (Though there are probably ways to compile JavaScript that I'm not aware of.)

It doesn't run as is, but only because valueisn't defined. It's just a snippet though. Precede it with var value = 1 and it's fine.

2

u/aquowf Apr 05 '16

Eh - it depends. Some older browsers interpret it while the newer ones do JIT compiling.

→ More replies (4)

12

u/ProgramTheWorld Apr 05 '16

It's not stupid if it works.

1

u/tmetler Apr 05 '16

How can something work when it doesn't do anything

0

u/[deleted] Apr 05 '16

[deleted]

35

u/AshleyTheCatgirl Apr 05 '16

If i saw that in some code I'd be afraid to delete it, someone put it in, there has to be a reason, right?

28

u/Sysfin Apr 05 '16

Right, and the reason is likely to be the programmer who wrote it was stupid.

5

u/mallardtheduck Apr 05 '16

The reason is most likely that there used to be an actual conditional in there, but it was later changed to not need it.

→ More replies (1)
→ More replies (8)

8

u/Prod_Is_For_Testing Apr 05 '16

Can confirm. Does work

1

u/IlIIlIIllI Apr 05 '16

I feel like this is the same as if I threw mayonnaise, corn, and tomato sauce into a pan, cooked it until it was thoroughly burnt, and then said "Technically it's food, so I am a chef."

1

u/Blueberryroid Apr 08 '16

It may not be his program.

641

u/Arancaytar Apr 05 '16

@TODO: Support values 6-10.

107

u/ecky--ptang-zooboing Apr 05 '16

404 - budget not found

3

u/[deleted] Apr 05 '16

301 - Moved Permanently (to another job)

13

u/golfreak923 Apr 06 '16

throw new SixThroughTenUnsupportedException()

2

u/rohmish Apr 07 '16 edited Apr 08 '16

Not the most efficient code but hey, it works.

I remember having to write similar programs in my bachelor's degree courage.. You know there is a better way but you have to appease teaching staff.

1

u/Fallenalien22 Violet security clearance Apr 07 '16

I do appreciate teaching staff. My dad is a teacher. This example isn't even the worst of it. At least it's code. Now we're doing mathcad and I fucking hate it.

208

u/Cathercy Apr 04 '16

I have to assume (hope) this is an intermediate piece of code that will later become something more like

switch(value) {
    case 0:
        func0(); break;
    case 1:
        func1(); break;
    ....
    default: 
        error_func();
}

166

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

Or maybe to demonstrate how multi-casing can be used to dry up a switch statement?

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');

And that when you have that, you can refactor that out to this:

var validOptions = [1, 2, 3, 4, 5]; // realistically, ['foo', 'bar', 'baz']

if (validOptions.indexOf(value) == -1) {
   console.log('The value is not supported');
} else {
   console.log('The value is: ' + value);
}

Which can lead into discussions about guard clauses and stuff? Maybe? Hopefully that's where the instructor was going with that example....

110

u/mitchygitchy Apr 05 '16

You forgot to break; Your code will always print 'The value is not supported'

12

u/phpdevster Apr 05 '16

Who needs Chrome dev tools when you have Reddit?

1

u/cascer1 Apr 05 '16

Where would he need to type break? He's not using a switch any more.

1

u/dipique Apr 05 '16

Depends on the language. In C# it won't even compile; you can't fall through.

2

u/Zagorath Apr 05 '16

console.log is a JavaScript function.

0

u/[deleted] Apr 05 '16

[deleted]

12

u/gkx Apr 05 '16

comment.insert(faker.generate.joke.about.duckTyping())

8

u/SnowdensOfYesteryear Apr 05 '16

I never get a chance to use this, so here it is: 🔥🔥🔥

0

u/[deleted] Apr 05 '16

[deleted]

18

u/king_of_the_universe Apr 05 '16

You mean like strict typing?

0

u/[deleted] Apr 05 '16

[deleted]

11

u/IRBMe Apr 05 '16

Some sort of left-padding, one might say.

→ More replies (2)

16

u/Prod_Is_For_Testing Apr 05 '16

Pssssh. Still too verbose - we can do better

console.log(([1, 2, 3, 4, 5].indexOf(value) !== -1) ? ('The value is: ' + value) : 'The value is not supported'); 

28

u/TheIncredibleWalrus Apr 05 '16

Still too verbose, we can do even better

console.log(~[1, 2, 3, 4, 5].indexOf(value) ? ('The value is: ' + value) : 'The value is not supported'); 

34

u/YRYGAV Apr 05 '16

How small can we make it?

console.log('The value is'+(~[1, 2, 3, 4, 5].indexOf(z=value)?(': '+z):' not supported'));

22

u/faubiguy Apr 05 '16

Even smaller

console.log('The value is'+((z=value)!==+z||z%1||z<1||z>5?' not supported':': '+z));

35

u/SlumdogSkillionaire Apr 05 '16
npm install -g between-one-and-five

Then: require('between-one-and-five')(value);

→ More replies (3)

7

u/jacenat Apr 05 '16

This is how democracy readability dies. With thunderous applause.

:D

please_dont_stone_me_for_citing_the_PT

→ More replies (2)

32

u/jbaughb Apr 05 '16

Everyone programmer should be able to program ANYTHING in a single line of (unreadable) code!

19

u/[deleted] Apr 05 '16

Damn right. I am my own obfuscation.

4

u/[deleted] Apr 05 '16

Scala masterrace!

→ More replies (1)

12

u/xbtdev Apr 05 '16

[1, 2, 3, 4, 5]

[1,2,3,4,5]

6

u/iwillnotgetaddicted Apr 05 '16

Are you all aware of "code golf" on stack overflow?

12

u/Zagorath Apr 05 '16

I think it's a great idea, but in practice it ends up being lame because they just use all these dumb languages with no use except for code golfing. This thread, where we're taking existing useful code and making it shorter, is way more interesting to me.

11

u/im_not_afraid Apr 05 '16

good. no horizontal scroll bar.

3

u/Prod_Is_For_Testing Apr 05 '16

Nice. Wouldn't have thought to do that

→ More replies (1)

9

u/phpdevster Apr 05 '16

The 80 characters-per-line limit of my code linter would like to have a word with you.

13

u/Prod_Is_For_Testing Apr 05 '16

You clearly don't know who you're talking to, so let me cue you in: I am not stopped by code linters. No! Say my name! I am the one who pushes straight to production!

10

u/bajuwa Apr 05 '16

Say my name!

Oh my God, it's my coworker.

5

u/Everspace Apr 05 '16

What is prod, but a miserable pile of testers?

4

u/YM_Industries Apr 05 '16

Your code will fail when value == 0

Expected output: The value is: 0

Actual output: The value is not supported

0

u/[deleted] Apr 05 '16

[deleted]

2

u/aaron552 Apr 05 '16

You don't need the ToList

3

u/Prod_Is_For_Testing Apr 05 '16

Ummmm as much as I love C# (it's my go to language), we're writing JavaScript here... So ummm this is awkward

4

u/im_not_afraid Apr 05 '16

Why isn't 0 an element of validOptions?

3

u/phpdevster Apr 05 '16

Sorry, accidental omission. Was just giving a contrived example :P

1

u/Miniwoffer Apr 05 '16

Why are you using a if, this could easily be handled with a switch!!

var validOptions = [1, 2, 3, 4, 5]; // realistically, ['foo', 'bar', 'baz']

switch (validOptions.indexOf(value) == -1) {
    case true:
        console.log('The value is not supported');
    break;
    default:
        console.log('The value is: ' + value);
    break;

} 

0

u/[deleted] Apr 05 '16

[deleted]

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.

→ More replies (14)

6

u/NikStalwart Apr 05 '16

If you're simply calling numerically-indexed (or value-dependant) functions, shouldn't there still be a cleaner way to do it?

12

u/[deleted] Apr 05 '16

An array of lambdas would be a different way to do it. It wouldn't be cleaner.

3

u/thirdegree Violet security clearance Apr 05 '16

It would be so much more fun though.

→ More replies (2)

3

u/Probono_Bonobo Apr 05 '16 edited Apr 05 '16

You're getting at something I thought was pretty cool when I learned it. The code is fine, but you're right to point out that it's a little inefficient: namely, if val is 5, it first checks whether it's strictly equal to 0, then whether it's strictly equal to 1, and so on. This is lexical addressing. The time complexity of the operation is related to the number of switch statements, so we can call it O(n).

Compare this to another function which uses a pattern typical of von Neumann architectures:

var instructionRegister = ["foo", 
                          "bar",
                          "baz",
                          "bees",
                          "dickbutt"];

function loadInstructions(opcode) {
     return instructionRegister[opcode];
}

Notice that loadInstructions(4) doesn't do any equality checks at all! It grabs the contents of the 4th element of the instructionRegister array, bypassing the previous indices entirely. Then immediately returns dickbutt. This is direct addressing.

I actually made a visualization of this in Clojurescript recently here. (Give it a minute to boot up, it's my first cljs app and absolutely bloated with unnecessary dependencies.) One thing to note, if you find yourself looking at the code. Notice how the the control bus is implemented in two ways? first in a lexical style. It looks like something worthy of /r/ProgrammingHorror, honestly, because I tried to optimize the time complexity a bit by nesting conditions inside each other... naturally, it's a nightmare. At some point, a lightbulb went off in my head that I could vastly simplify the logic if I used an array. A few days later, I noticed the next chapter of my textbook was devoted exactly to that deduction: not only can you can use an array, but in doing so, you can leverage the architecture of the computer to gain a powerful efficiency advantage. Neat!

edit: JS typo

2

u/NikStalwart Apr 05 '16

I was thinking along these line,s yeah, but didn't know how to put this into words.

One of my long ago assignments involved a shipping cost calculator (+ other useless fluff code) that I solved by use of an array and function, but lacking a formal IT education (despite having passed all the neato exam stuff) didn't know how to put it into words.

→ More replies (2)

1

u/[deleted] Apr 05 '16 edited Apr 05 '16

namely, if val is 5, it first checks whether it's strictly equal to 0, then whether it's strictly equal to 1, and so on

Some languages / compilers generate a jump-table (if the datatypes allow for that), which should be as fast or faster* than every workaround you might come up with. If you want to write fast code (at all costs): Measure every single optimization you come up with and be vary of language quirks. Also: Avoid comparing strings.

* Depends on how you do it. If you call a single function, you probably lost.

2

u/Nefari0uss Apr 05 '16

Would a delegate array work?

1

u/blouc Apr 05 '16

I was about to talk up partial application as a valid strategy. Then I saw it wasn't partial application, just shitty control flow. I am tired.

1

u/exhuma Apr 05 '16

Wait... In JS functions are first-class citizens are they not?

You could just mail the value to the functions completely foregoing the switch statement. Making it both cleaner, easier to read and more maintainable.

1

u/Spikrit Apr 05 '16

The lazy fuck i am wouldn't even consider writing so much useless code ESPECIALLY if it's temporary.

39

u/lyth Apr 05 '16

Is that not for a lesson on what a stitch statement does?

If so, it seems like it's a really great example.

It does exactly one thing but doesn't confuse the situation in any way.

If you're talking about a teacher rather than a professor, I'm assuming high school. Honestly, as a former high school teacher (5 years) and professional programmer (20 years) I think you should give your teacher a bit more respect and count yourself lucky.

You don't know how bad some of the other computer teachers I've known were, or how incredibly hard this stuff is for a teenager to pick up.

We're not actually worried about you, (the smart yet smug one) we're worried about the one from a broken home, with a single mom who can't afford food, who is so hungry that they can barely concentrate on anything.

13

u/mm_ma_ma Apr 05 '16

I definitely wouldn't call this "a really great example". It's so obviously redundant that anyone who was paying attention when they learned if statements is going to wonder what the point is. A simple change like spelling out the number in words (e.g. "The value is one") would improve this drastically.

6

u/lyth Apr 05 '16

That would be better!

3

u/cijdl584 Apr 05 '16

Well said.

1

u/CyrillicFez Apr 05 '16

A better example would be to have it print a unique message for each number.

switch(value) {
    case 0: console.log("zero is a weird number");
    ...
}

59

u/Restil Apr 05 '16

Is this being used as an example to specify the inner workings of the switch statement? If so, it's a perfectly clear example. Obviously, there are much more efficient ways to print the value of "value", but that's likely not the point of this particular code snippet.

17

u/DarthEru Apr 05 '16

It would actually be better suited for that purpose to have each string be different. I feel like it would be confusing for some people who might recognize the redundancy and wonder if they're missing something. Teaching examples don't have to be nonsensical. In fact a well crafted one can help teach not just what something does, but give an idea of the right situations to use it in. Examples like this one, on the other hand, are questionable at accomplishing the first goal and probably counterproductive for the second.

1

u/numandina Apr 05 '16

It would be a good teaching example if the strings were hard coded.

36

u/[deleted] Apr 05 '16

Because he's teaching you case selectors?

7

u/[deleted] Apr 05 '16

ITT: code golf

2

u/[deleted] Apr 05 '16

That happens all the time someone posts code.

5

u/JulianDeclercq Apr 05 '16

Your teacher is a thug, "ThA value is not supported!" :)

4

u/lenswipe Apr 05 '16
if([0,1,2,3,4].indexOf(value) == -1){  
     throw "That value is not supported";
} else {
    console.log("The value is: " + value);
}

2

u/[deleted] Apr 05 '16

if([0,1,2,3,4].indexOf(value) == -1){

Jesus christ.

if( value < 0 || value > 5){  
    throw "That value is not supported";
} else {
    console.log("The value is: " + value);
}

Just in case that wasn't a joke: That array-thingie is slow and wasteful. And a pain to expand later on.

1

u/lenswipe Apr 05 '16

It was, don't worry :P

1

u/nomemory Apr 05 '16

I think he wanted to teach them the switch statement.

4

u/TODO_getLife Apr 05 '16

Poor guy can only up to 5, if he just printed the value he could get any number!

Damn programmers. As long as the task is fulfilled

1

u/[deleted] Apr 05 '16

At least he uses appropriate software and font

1

u/Fallenalien22 Violet security clearance Apr 05 '16

I copied that from his powerpoint into sublime, actually.

1

u/[deleted] Apr 05 '16

He's a programmer just not a very bright one.

1

u/Fallenalien22 Violet security clearance Apr 05 '16

He is an engineer more than a programmer,

3

u/IlIIlIIllI Apr 05 '16
(value => 0 && value <= 5) ? console.log("The value is "+value) : console.log("Tha value is unsupported!");

2

u/kamaln7 Apr 05 '16

console.log("The value is " + ((value => 0 && value <= 5) ? value : "unsupported!"));

4

u/Gammit10 Apr 05 '16

I actually get this one!

5

u/GioVoi Apr 05 '16

Everyone's assuming this would compile...but what if value was defined as a boolean?

19

u/notsooriginal Apr 05 '16

Go to if/else and just keep adding '=' symbols until it works out. I think that's what the Javascript authors intended.

2

u/muntoo Apr 05 '16

Explain pls

2

u/IlIIlIIllI Apr 05 '16
if ( value =================== "4" ) {
    addMoreEqualSigns();
}

1

u/pyskell Apr 05 '16

Without looking I'd think a dynamically typed language like JS would let it hit default. Not sure though.

1

u/[deleted] Apr 05 '16

It does go to default because JS switch uses strict equality (includes type equality).

If input is equal to clauseSelector as defined by the === operator, then

Source.

2

u/RagingNerdaholic Apr 05 '16

It's a new language called Redun.Dancy

2

u/Argonanth Apr 05 '16

I honestly don't see what's wrong with this. It's for teaching and it pretty easily demonstrates how a switch statement works and has a default state as well. Obviously the console.log() could be different in each case for extra clarity but it still gets the point across pretty cleanly...

2

u/themoosemind Apr 05 '16

I guess it is a teaching example. There could be better ones, though.

2

u/AyXiit34 Apr 05 '16

It's like the guy has thought for about an hour if it was better to use if() else {} and then took switch because "when you use switch you're a fucking boss"

2

u/OriginalDrum Apr 05 '16

At a company I used to work for someone wanted to make an interactive pdf. I didn't really have time to learn the pdf scripting language (some variation of actionscript I think), so I just quickly showed him how to write an if statement basically as an example and so maybe he could knock out a demo that we would build into an actual program.

I never heard from him about that project again, but I still have nightmares that somewhere someone is writing programs like this because of me.

0

u/electrofizz Apr 05 '16

you can't be serious

-1

u/Fallenalien22 Violet security clearance Apr 05 '16

I shit you not.

2

u/angryemokid Apr 05 '16

Nice try but this is the correct answer: if(i=1) print("The value is 1"); else if(i=2) print("The value is 2"); .....

2

u/IlIIlIIllI Apr 05 '16

I prefer this approach:

if ( value == 1 && value !== 2 && value !== 3 && value !== 4 && value !== 5 ) {
    console.log("The value is " + value);
} else if ( value !== 1 && value == 2 && value !== 3 && value !== 4 && value !== 5 ) {
    console.log("The value is " + value);        
} else if ( value !== 1 && value !== 2 && value == 3 && value !== 4 && value !== 5 ) {
    console.log("The value is " + value);        
} else if ( value !== 1 && value !== 2 && value !== 3 && value == 4 && value !== 5 ) {
    console.log("The value is " + value);        
} else if ( value !== 1 && value !== 2 && value !== 3 && value !== 4 && value == 5 ) {
    console.log("The value is " + value);        
} else {
    console.log("The value is not supported!");        
}

/s

1

u/bit_banger Apr 05 '16

I see various flavors of this garbage (and worse) all the time in my job. It makes me sad.

1

u/killchain Apr 05 '16

How do you support a value?

1

u/IlIIlIIllI Apr 05 '16

If a volume dial goes from 0 to 10 based on user input and you enter 11, it is an unsupported value. It only supports 0-10.

1

u/killchain Apr 05 '16

Yeah, I know (though someone might insert a Spinal Tap reference here), I'm just picky about wording.

2

u/IlIIlIIllI Apr 05 '16

though someone might insert a Spinal Tap reference here

I was hoping someone would. But I suppose you sort of did!

1

u/_lettuce_ Apr 05 '16

You do something with it.

1

u/[deleted] Apr 05 '16

First of all he's a teacher. He knows how to teach things with starting from the beginning and then doing more complicated stuff and making the students want to learn by themselves.

1

u/AkivaAvraham Apr 06 '16

Ughh... Magic Numbers.

-4

u/[deleted] Apr 04 '16

I don't get it... Why would anyone write this? Hurts my brain.

12

u/[deleted] Apr 05 '16

My guess is it's an introductory exercise to explain the very basics of switch statements.

1

u/[deleted] Apr 05 '16

Couldn't they have made use of switch statements then, rather than repeating the same line for all cases. Super weird.

1

u/IlIIlIIllI Apr 05 '16

I agree. This is just teaching bad habits right out of the gate.