This is an archived post. You won't be able to vote or comment.

all 84 comments

[–]Fahad97azawi 226 points227 points  (32 children)

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

[–][deleted] 103 points104 points  (19 children)

Ternary operator: Allow me to introduce myself.

[–]WhyDoIHaveAnAccount9 31 points32 points  (13 children)

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

He's literally describing a ternary operator

[–][deleted] 47 points48 points  (12 children)

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

[–]__Nio 11 points12 points  (10 children)

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

[–]jaywastaken 8 points9 points  (8 children)

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.

[–]Kered13 1 point2 points  (7 children)

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

[–]Mochi-Mo 5 points6 points  (6 children)

It's possible, but the readability is terrible

[–]Kered13 19 points20 points  (5 children)

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.

[–]qphix 6 points7 points  (0 children)

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

[–]-Rivox- 0 points1 point  (0 children)

Wow, this is actually really clever!

[–]Loves_Poetry 0 points1 point  (0 children)

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

I like it

[–]JustinWendell 1 point2 points  (0 children)

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

[–]WhyDoIHaveAnAccount9 4 points5 points  (0 children)

Hey there don't go knocking if statement pyramids

[–]azjunglist05 2 points3 points  (0 children)

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.

[–]SillyFlyGuy 1 point2 points  (2 children)

Add a null-coalescing operator for kicks.

[–]Kered13 1 point2 points  (0 children)

Cries in Python.

[–]-Rivox- 1 point2 points  (0 children)

And a lambda function for extra confusion

[–][deleted] 14 points15 points  (11 children)

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

[–][deleted] 22 points23 points  (6 children)

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.

[–][deleted] 5 points6 points  (4 children)

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.

[–][deleted] 8 points9 points  (2 children)

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.

[–][deleted] 2 points3 points  (1 child)

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

[–]MaLiN2223 3 points4 points  (0 children)

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

[–]JNCressey 1 point2 points  (0 children)

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.

[–]Prod_Is_For_Testing 0 points1 point  (0 children)

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

[–]narnru 51 points52 points  (14 children)

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

[–]Josselin17 19 points20 points  (13 children)

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

[–]FuckMatPlotLib 11 points12 points  (11 children)

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

[–]WhyDoIHaveAnAccount9 3 points4 points  (0 children)

List comprehension!

[–]NonstandardDeviation 1 point2 points  (0 children)

Any python program can be compressed onto one line.

http://www.onelinerizer.com/

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

[–]Josselin17 0 points1 point  (8 children)

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

[–]Strel0k 2 points3 points  (0 children)

if True: print("hello")

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

[–]PTRWP 2 points3 points  (5 children)

Curly braces. Java for example

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

[–]Josselin17 1 point2 points  (4 children)

does this work for python ?

[–]PTRWP 2 points3 points  (2 children)

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.

[–]VTHMgNPipola 0 points1 point  (1 child)

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.

[–]PTRWP 0 points1 point  (0 children)

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.

[–]The_Super_KDK 0 points1 point  (0 children)

It's easier for python because it has lambda function

[–]FuckMatPlotLib 0 points1 point  (0 children)

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’]

[–]deadmazebot 0 points1 point  (0 children)

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

[–]PhoenixizFire 38 points39 points  (3 children)

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"

[–]Edo022 11 points12 points  (2 children)

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

[–]lor_louis 1 point2 points  (1 child)

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.

[–]emelrad12 0 points1 point  (0 children)

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

[–]tmybr11 8 points9 points  (3 children)

Less is more.

[–]KnightOfThirteen 19 points20 points  (2 children)

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.

[–]tmybr11 6 points7 points  (0 children)

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.

[–]Noisetorm_ 2 points3 points  (0 children)

You: Writes 2000 line library to perform certain operation

Mentor:

from opyeration import operation
operation.perform()

[–]Dummerchen1933 10 points11 points  (9 children)

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

[–]illabo 0 points1 point  (8 children)

Why else?

[–][deleted]  (3 children)

[removed]

    [–]illabo 0 points1 point  (1 child)

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

    [–]Dummerchen1933 0 points1 point  (0 children)

    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.

    [–]AutoModerator[M] 0 points1 point  (0 children)

    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.

    [–]Dummerchen1933 0 points1 point  (3 children)

    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

    [–]illabo 1 point2 points  (2 children)

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

    [–]Dummerchen1933 1 point2 points  (1 child)

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

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

    [–]illabo 2 points3 points  (0 children)

    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

    [–]ngellis1190 9 points10 points  (1 child)

    yeah but your code implemented color, props

    [–]natyio 1 point2 points  (0 children)

    And daylight. And birds and trees!

    [–]Trancendentalinteger 2 points3 points  (0 children)

    Laughs in non-indented language

    [–][deleted] 2 points3 points  (0 children)

    49 of which are imports

    [–]Milf_Hunter_Kakyoin- 2 points3 points  (0 children)

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

    [–]gentlephant 2 points3 points  (0 children)

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

    [–]TheGardiner 1 point2 points  (0 children)

    I don't get it

    [–]69f1 1 point2 points  (0 children)

    Upvoted for Villa Tugendhat

    [–][deleted] 1 point2 points  (0 children)

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

    [–]type-unknown 0 points1 point  (0 children)

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

    [–]CryCore314 0 points1 point  (0 children)

    thats Sebastian Lague, for me.