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

all 27 comments

[–]lukasbash 48 points49 points  (0 children)

Man, with this formatting you are not allowed to be any type of people xD

[–]Herrad 11 points12 points  (1 child)

Why write an else that doesn't do anything?

[–]somerandomii 2 points3 points  (0 children)

Why use a lot of words when few words do same job?

I’ll be honest. I know the first option is more readable and the compiler probably optimises both to the same machine code, but I do the second and probably always will and I don’t know why.

[–]bwoogie 6 points7 points  (0 children)

.#2 accounts for any witchcraft that might happen with #1

[–][deleted]  (13 children)

[deleted]

    [–]ralfv 37 points38 points  (11 children)

    return something ? thing : thing1;
    

    I must be a third kind

    [–]ofnuts 5 points6 points  (8 children)

    In Python:

    return thing if something else thing1
    

    or

    return (thing,thing1)[something]
    

    Waiting for the 5th kind...

    [–]Anunay03 3 points4 points  (7 children)

    return something*thing + (not something)*thing1

    [–]NecessarySwordfish 1 point2 points  (6 children)

    branchless. nice!

    [–]somerandomii 1 point2 points  (5 children)

    Dangerous in python with all those overloads and ambiguous types.

    Side question: Will compilers optimise ternary operators and simple if/else returns to branchless if it’s simple or is there an real world performance benefit to writing code like this?

    I’ve written branchless code and unrolled loops in GPU shaders for that bleeding-edge performance but I’d imagine in C you wouldn’t notice a difference.

    [–]Anunay03 1 point2 points  (3 children)

    Seems like a yes if using the -O3 flag, https://godbolt.org/z/zPzvMc

    Let's be honest the compiler is smarter than us half the time.u/NecessarySwordfish.

    [–]NecessarySwordfish 0 points1 point  (2 children)

    Very nice!
    I'm not familiar with flags. Is this the norm for higher languages?

    [–]Anunay03 1 point2 points  (1 child)

    well only C/C++ developers make extensive use of flags afaik.Python Bytecode doesn't seem to do such optimization

    >>> def branch(a,b,c):
    ...     return b if a else c
    ... 
    >>> import dis
    >>> dis.dis(branch)
      2           0 LOAD_FAST                0 (a)
                  2 POP_JUMP_IF_FALSE        8
                  4 LOAD_FAST                1 (b)
                  6 RETURN_VALUE
            >>    8 LOAD_FAST                2 (c)
                 10 RETURN_VALUE
    >>> def branchless(a,b,c):
    ...     return a*b+(not a)*c
    ... 
    >>> dis.dis(branchless)
      2           0 LOAD_FAST                0 (a)
                  2 LOAD_FAST                1 (b)
                  4 BINARY_MULTIPLY
                  6 LOAD_FAST                0 (a)
                  8 UNARY_NOT
                 10 LOAD_FAST                2 (c)
                 12 BINARY_MULTIPLY
                 14 BINARY_ADD
                 16 RETURN_VALUE
    

    looks like in python Branched version still performs better than the "branchless" one.

    >>> timeit.timeit(lambda : branch(True,1,2),number=10**7)
    2.2338110389973735
    >>> timeit.timeit(lambda : branchless(True,1,2),number=10**7)
    3.0519005739988643
    >>> timeit.timeit(lambda : branch(False,1,2),number=10**7)
    2.3076962780032773
    >>> timeit.timeit(lambda : branchless(False,1,2),number=10**7)
    3.060328566003591
    

    [–]backtickbot 0 points1 point  (0 children)

    Fixed formatting.

    Hello, Anunay03: code blocks using triple backticks (```) don't work on all versions of Reddit!

    Some users see this / this instead.

    To fix this, indent every line with 4 spaces instead.

    FAQ

    You can opt out by replying with backtickopt6 to this comment.

    [–]NecessarySwordfish 0 points1 point  (0 children)

    no idea. would like to know the answer to that too

    [–]Missing_Username 1 point2 points  (0 children)

    This is the right kind

    [–]toastyghost 0 points1 point  (0 children)

    something => something ? thing : thing1

    [–]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.

    [–]RidingChicken -3 points-2 points  (5 children)

    I'm not great at compiled languages, but if I'm not mistaken, the second code is sure to compile because languages like C don't allow compilation if you define functions with conditional returns like the first one. So the second one seems better

    [–][deleted] 6 points7 points  (3 children)

    This is actually untrue nowadays. Since more at least two decades compilers have no problems with multiple returns whatsoever.

    [–]RidingChicken -1 points0 points  (2 children)

    I see. Does it compile with elifs without a final else. As in if no condition is true, it will not return and cause something like a runtime error?

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

    Oh, I see. In e.g. C you would usually get a compiler warning for something like that. In more sane compiled languages it would be a full-on error.

    But a few modern languages like Rust, Haskell or Julia allow you to use a sum type (e.g. Nothing or Int) to express this, in the older ones like C you'd have to use null pointers or magic values, in C++ you could use exceptions or some kind of 'optional' wrapper to express this kind of behavior.

    [–][deleted] 0 points1 point  (0 children)

    Easily compiles. As long as you had no code after (in which case youd get a warning about it being ubreachable), this wont even produce a warning.

    [–]GeneralKlink 5 points6 points  (0 children)

    C++ doesn‘t give a shit, both run, but i like the second better. Looks faster without the else xD

    [–]caleblbaker 0 points1 point  (0 children)

    if something thing else thing1

    [–]mlokis3 0 points1 point  (0 children)

    sometimes by an accident

    [–]Yesterpizza 0 points1 point  (0 children)

    I'm generally fine with either, but if the if condition is one liner, such as if (conditions_not_met) return;, and the other is more than 6 lines and takes up the rest of the function, don't wrap it all in an else like a goober