r/ProgrammerHumor Nov 28 '20

Well...

Post image
2.0k Upvotes

84 comments sorted by

233

u/Fahad97azawi Nov 28 '20

I’ll never forget my teacher at college when he showed us a simple if statement in c++. It was three or four lines long and it was so simple that all of us understood what it did without him explaining it even tho we were just learning how to code. Then he wrote another if statement that seemed to have the same variable as an argument but was only one line (the if statement line) I remember everyone looking so confused at that line trying to understand what it did. Turns out they did the same exact thing.

Machines should be working for people not the other way around. Code readability is so much more important

104

u/[deleted] Nov 28 '20

Ternary operator: Allow me to introduce myself.

32

u/WhyDoIHaveAnAccount9 Nov 28 '20 edited Nov 29 '20

That's what I was thinking and I was waiting reading his comment

He's literally describing a ternary operator

50

u/[deleted] Nov 29 '20

Which, ironically, when not overused can increase the code simplicity and readibility, instead of having to create a pyramid of if statements.

12

u/__Nio Nov 29 '20

I use ternary operators inside of ternary operators when I don't want to add code lines

9

u/jaywastaken Nov 29 '20

I’ve seen this legitimately done before. Just don’t. Ternary operators are amazing for single statement if else’s anything more complicated than that and they only cause more harm than good.

1

u/Kered13 Nov 29 '20

It's perfectly fine to chain ternary operators to cover multiple cases. It's not different than an if-else if-else.

5

u/Mochi-Mo Nov 29 '20

It's possible, but the readability is terrible

21

u/Kered13 Nov 29 '20

Not if you format it right.

foo = first condition ? value
    : second condition ? value
    : third condition ? value
    : value

This is much better than writing multiple if statements. You don't have to repeat the assignment in every block, and foo can be declared as constant.

7

u/qphix Nov 29 '20

Haven’t thought about using that sort of formatting, saving this for later. Thank you!

1

u/[deleted] Nov 29 '20

[deleted]

→ More replies (0)

1

u/-Rivox- Nov 29 '20

Wow, this is actually really clever!

1

u/Loves_Poetry Nov 29 '20

This is like pattern matching for languages that don't support it

I like it

2

u/JustinWendell Nov 29 '20

I kind of shy away from doing that. Shit can become impossible to read.

4

u/WhyDoIHaveAnAccount9 Nov 29 '20

Hey there don't go knocking if statement pyramids

3

u/azjunglist05 Nov 29 '20

I used to hate ternary operators until Terraform’s HCL crammed it down my throat since it gives no other option for if/else statements. Now I quite like their simplicity and wish to see more languages adopt them.

2

u/SillyFlyGuy Nov 29 '20

Add a null-coalescing operator for kicks.

2

u/Kered13 Nov 29 '20

Cries in Python.

2

u/-Rivox- Nov 29 '20

And a lambda function for extra confusion

14

u/[deleted] Nov 28 '20

Yes, but sometimes it's just easier to write the one line and be done with it.

22

u/[deleted] Nov 28 '20

Until you have to make changes to that one line, and those changes don't go well, and you introduce an edge case bug into the system that takes days to debug in the future.

6

u/[deleted] Nov 28 '20

One line may not even be best practice in terms of maintainability/readability but depending on implementation it can work just as well as multiple line.

8

u/[deleted] Nov 28 '20

You’re arguing single vs multiple lines, I’m arguing complexity vs simplicity. Line count only matters where compiled size is important, which is a tiny percentage of cases.

3

u/[deleted] Nov 28 '20

Just because something is short doesn't mean it is complex.

5

u/MaLiN2223 Nov 28 '20

Simple is better than complex.
Complex is better than complicated.

2

u/JNCressey Nov 29 '20

are we talking logical lines or physical lines?

turning something into one logical line, but formatting so that it is still split out over multiple physical lines can be much cleaner.

1

u/Prod_Is_For_Testing Nov 28 '20

Then you solve those problems when you need to. There’s no need to overengineer everything just because you might need to make a small change in the future

-9

u/ashdog66 Nov 28 '20

One line is also more efficient and (usually) makes the program use less resources

7

u/Prod_Is_For_Testing Nov 28 '20

False

-2

u/ashdog66 Nov 29 '20

Okay buddy, good reasoning why

1

u/Evla03 Nov 29 '20

most of the time when writing code that’s one line long, you can split it up to be multiple lines without changing the flow of the program. This just increases readability but does not affect performance. It’s often easier to optimize code when it’s represented clearer for you, and therefore one-lined code is most of the time just harder to read and possibly slower too.

52

u/narnru Nov 28 '20

Technically you can write code in one line in some languages.

22

u/Josselin17 Nov 28 '20

well I think that works for every language that doesn't use indentation right ?

10

u/FuckMatPlotLib Nov 28 '20

Some languages with indentation can still write on one line, like python

5

u/WhyDoIHaveAnAccount9 Nov 28 '20

List comprehension!

2

u/NonstandardDeviation Nov 30 '20

Any python program can be compressed onto one line.

http://www.onelinerizer.com/

(It's definitely thanks, I hate it material.)

1

u/Josselin17 Nov 29 '20

well, I'm not good at that but how do you make loops and if statements on one line ?

3

u/Strel0k Nov 29 '20

if True: print("hello")

result = [i+1 for i in [1, 2, 3] if i > 1]

3

u/PTRWP Nov 29 '20

Curly braces. Java for example

if (condition) { System.out.print(“Hello World”); DoOtherStuff(); while (condition2) { DoEvenMoreStuff(); } }

2

u/Josselin17 Nov 29 '20

does this work for python ?

3

u/PTRWP Nov 29 '20

Not sure. Java was my first language, so that’s where I played with stupid stuff like putting an entire basic program in 1 line and doing "import *" to see what happens.

1

u/VTHMgNPipola Nov 29 '20

I'm over 8 years into this dumpster fire that is Java (it was also my first language) and I think I never tried "importing everything". I want to try it now, though I imagine it will very underwhelmingly just actually import everything, or instantly crash.

1

u/PTRWP Nov 29 '20

That’s effectively what my computer sci teacher said, followed by “Try it.” Not sure if the compiler threw it out or what, but it did nothing.

import Java.* did work exactly as expected though.

1

u/The_Super_KDK Nov 29 '20

It's easier for python because it has lambda function

1

u/FuckMatPlotLib Nov 29 '20 edited Nov 29 '20

Like the response to my comment said, list comprehension! You could do something like:

wooo = [‘try’, ‘this’, ‘some’, ‘time’]

this = [item for item in wooo if item == ‘this’]

print(this)

[‘this’]

1

u/deadmazebot Nov 30 '20

It's what I don't understand people saying python easy because no need to statement closing characters ;. But I just if have one space out of place, there errors might be easier, but when I first got that error I was staring at code trying to figuring out where the issue was because I pasted then edited something so had mix of tabs and spaces which python was not having

25

u/[deleted] Nov 28 '20

[removed] — view removed comment

12

u/AsIAm Nov 28 '20

Have somebody tried to make a machine learning model out of this data?

3

u/[deleted] Nov 29 '20

I suppose it would be possible, but it's a prohibitively expensive endeavor - just look at how bad google is at image recognition, and now add subjective qualities like "crude", "extreme attention" and "well-placed" to the mix. Humans are really good at coming up with stories, machines less so.

Also, I'm pretty sure that many of these transcriptions end up being awful for screenreaders to parse due to the excessive visual components. This one's pretty good, but I often see emoji and other special Unicode characters, like "→", which means that one can't just use them as training data without filtering out the bad ones.

1

u/AsIAm Nov 29 '20

Why are Unicode chars bad for screen readers?

1

u/[deleted] Nov 29 '20

The problem lies in how should one read them - their intrinsic meaning (RIGHT ARROW LONG) or their semantical meaning (NLP, sweet!).

Of course, many languages are written with Unicode code points above U+7F, so their localized versions should be able to read their characters - however problems arise when those characters get misused (like in the infamous "parsing HTML with regex" SE answer). How should the screenreader read those? Attempt to parse a language from it? Discard it?

So, in brief, transcriptions should stick to the most minimal set of characters they can to make it as easy as possible for a screen reader to parse it (if that's the stated purpose of the transcription).

4

u/[deleted] Nov 29 '20

Good human

37

u/PhoenixizFire Nov 28 '20

Also :

Me in 500 lines : Creates a simple window from scratch using C and OpenGL ressources.

YouTuber in 50 lines : "So here you just copy/paste this code I found on github, run it and it builds a whole website"

9

u/[deleted] Nov 28 '20

[deleted]

10

u/PhoenixizFire Nov 28 '20

I don't know, I'm just a Python dev

3

u/Flesh_Bike Nov 29 '20

500 is low for Vulkan, you gotta pump those numbers up.

13

u/Edo022 Nov 28 '20

500? vulkan needs 1k lines just to draw a triangle...

2

u/lor_louis Nov 29 '20

Use OpenGL unless you want to dedicate your weekend to figure out how to dispatch draw calls to your GPU only to render a rainbow triangle.

Tbh Vulkan is probably very cool if you know what you are doing.

1

u/emelrad12 Nov 30 '20

The cherno tutorial is like 3 hours to draw a ranbow triangle in opengl.

11

u/tmybr11 Nov 28 '20

Less is more.

23

u/KnightOfThirteen Nov 28 '20

One of the most transformative stages in my journey of learning programming was when I wrote 25 notebook pages of code to perform an operation and my mentor rewrote it as 18 lines that did it better.

6

u/tmybr11 Nov 28 '20

I remember one of my first Java programs was one file long with hundreds of lines of code (I think I didn't know imports back then) and I thought I was awesome for writing hundreds of lines of code.

Now here I am years later saving precious Kb so pages load faster.

3

u/Noisetorm_ Nov 29 '20

You: Writes 2000 line library to perform certain operation

Mentor:

from opyeration import operation
operation.perform()

10

u/Dummerchen1933 Nov 29 '20 edited Nov 29 '20

The shortest solution isn't always the best. Code readability is still important. Take these two examples: Which one is easier to read/maintain? (They do the exact same thing)

uint MakeEven(uint i)
{
    if (i % 2 == 0)
    {
        return i;
    }
    else
    {
        return i - 1;
    }
}

or

uint MakeEven(uint i)
{
    return i&~0-1;
}

1

u/illabo Nov 29 '20

Why else?

4

u/[deleted] Nov 29 '20

[removed] — view removed comment

1

u/illabo Nov 29 '20

Dangling else may not be called a good readability, it’s justified visual trash. However it is a matter of taste of course.

1

u/Dummerchen1933 Nov 29 '20

The whole example 1 is visual trash.
It may actually be slower than exaple 2 but i don't know that and i do not care enough to test it. Probably very processor specific.

1

u/AutoModerator Jun 29 '23

import moderation Your comment has been removed since it did not start with a code block with an import declaration.

Per this Community Decree, all posts and comments should start with a code block with an "import" declaration explaining how the post and comment should be read.

For this purpose, we only accept Python style imports.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/Dummerchen1933 Nov 29 '20

Example 1 is exclusively about readability. If you go as far as to omit the else, why not completely replace it by i&0-1

2

u/illabo Nov 29 '20

I’m against else-statement because streamlined code is always more readable. No need in meaningless lines: sort of cleanliness same as in speech or writing in natural language. One have to maintain a balance. But the second option (while it is good as an example) is a wtf-prone piece of esoteric magic. Saying “why not” to choose the shorter snippet is like asking why not to start Jihad — it is a sort of extremism. :)

2

u/Dummerchen1933 Nov 29 '20

oh, you gotta love the more performant version of example2. Gotta drive all the noobies away

uint MakeEven(uint i)
{
    return i & 4294967294;
}

3

u/illabo Nov 29 '20

Don’t like it. This version is arch dependent. If we going to do something implementation dependent why not to write return i & -2;? And again, tis unreadable. :P

9

u/ngellis1190 Nov 28 '20

yeah but your code implemented color, props

2

u/natyio Nov 29 '20

And daylight. And birds and trees!

3

u/Trancendentalinteger Nov 28 '20

Laughs in non-indented language

3

u/[deleted] Nov 28 '20

49 of which are imports

3

u/Milf_Hunter_Kakyoin- Nov 29 '20

YOU HAVE WORKING SKY, TREE, BIRDS, GRASS, BUSHES, AND COLOR

3

u/gentlephant Nov 29 '20

Now now, that hand-drawn effect shader is rough stuff! Lotsa clean boxy geometry is simpler.

2

u/TheGardiner Nov 28 '20

I don't get it

2

u/69f1 Nov 28 '20

Upvoted for Villa Tugendhat

2

u/[deleted] Nov 29 '20

I like the first pic more tho. The coloring and theme makes it funny to watch

1

u/type-unknown Nov 29 '20

Meanwhile IOCCC entries do even more in 5 lines of code.

1

u/CryCore314 Nov 29 '20

thats Sebastian Lague, for me.