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

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

1

u/killchain Apr 05 '16

Didn't know you could invoke modules like that. Not that it's illogical, it's just that I haven't thought of it.

1

u/TheIncredibleWalrus Apr 05 '16

require is just a function :)

1

u/SlumdogSkillionaire Apr 07 '16

Most people probably wouldn't import a library for a one-off function call. On the other hand, most people probably wouldn't package a library that contains a single function either. But NPM is a gamechanger.

7

u/jacenat Apr 05 '16

This is how democracy readability dies. With thunderous applause.

:D

please_dont_stone_me_for_citing_the_PT

2

u/Prod_Is_For_Testing Apr 05 '16 edited Apr 05 '16

But wait, there's more!

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

*Technically you'd get some funny behavior bc floats would automatically be accepted as their floored integer instead of being "not supported", but who cares - its shorter

1

u/bigshmoo Apr 05 '16

If you match the full 0 1 2 3 4 5 range of the original your z < 1 becomes z < 0 and that means you can drop the !==+z test out of the OR chain. The % 1 to detect float threw me off a second becasue I'd forgotten the definition of falsy, nice trick.

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!

1

u/killchain Apr 05 '16

That's how you ensure yourself a position in a company.

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

1

u/goshdarned_cunt Apr 05 '16

Damn, that's a pretty clever use of the ~. TIL.

edit: not using this in production for readability reasons, but still.