527
you are viewing a single comment's thread
view the rest of the comments
[-] Omega_Haxors@lemmy.ml 4 points 8 months ago* (last edited 8 months ago)

Screw else statements, people who use if-return have 180% more readable code.

[-] SpeakinTelnet@programming.dev 6 points 8 months ago

Because everyone knows a function stops at the if-else. Nothing ever happens afterward.

[-] Omega_Haxors@lemmy.ml 1 points 8 months ago* (last edited 8 months ago)

You're writing extremely bad code if that's the case and you need to refactor. The point of a function is to return a value. Anything else is just there to waste cycles and make the code less readable. You should also never use else statements for arithmetic due to their massive relative overhead. Your processor can do multiple arithmetic operations in the time it takes to process one if statement, and don't get me started on people who demand you use nested if even though switch statements are way faster and leagues more readable.

[-] 1rre@discuss.tchncs.de 8 points 8 months ago

Who's suggesting that people are using if statements for arithmetic?

The only time that you can feasibly replace an if statement with arithmetic is if it's a boolean, but frankly that's an edge case... Also if you're not writing in rust or c or whatever then don't worry as the interpreter will run a huge amount of branches for every line of code (which is what all your nested ifs, switches, gotos, returns etc. will compile down to anyway)

[-] SpeakinTelnet@programming.dev 7 points 8 months ago* (last edited 8 months ago)

I disagree but you do you.

Edit: dammit you edit your comment a lot for someone who claims to know how to write code properly.

[-] Omega_Haxors@lemmy.ml 1 points 8 months ago* (last edited 8 months ago)

Those who are the most wrong have the strongest conviction

EDIT: I make a lot of edits because unlike you, I care about the quality and accuracy of what I write. You're going to spend like an extra 10 minutes tops writing for something that will be read by thousands for years to come. It's basic courtesy.

[-] quicksand@lemm.ee 1 points 8 months ago

...and the self-awareness finally hits

[-] Omega_Haxors@lemmy.ml 1 points 8 months ago* (last edited 8 months ago)

Yeah i'm starting to remember why I quit. In any other industry toxic shit like this goes sinks to the bottom, not the top.

Like seriously, what were you hoping to accomplish with this one? The thread ended yesterday. That's reddit tier douchery.

[-] AVincentInSpace@pawb.social 5 points 8 months ago* (last edited 8 months ago)

god forbid anyone do multiple non-nested if blocks one after another in the same function as the function does multiple different things or (horror of horrors) put an if-else inside a loop

also unless your compiler is completely and utterly brain dead (as is the case with Python, Java, C#, and most other languages that ~~only pretend to be compiled~~ compile to bytecode) a switch and a series of elif statements will compile to the exact same sequence of machine instructions. you can check on godbolt.org if you don't believe me.

modern compilers are insanely smart. as an example, this loop counts the number of 1's in the binary representation of a number:

    while (n)
    {
        n = n & (n - 1);    // clear the least significant bit set
        count++;
    }

LLVM will recognize that this is what you are trying to do and emit a single POPCNT instruction on x86, eliminating the loop entirely.

also how would you even use if statements for arithmetic at all? you aren't thinking of that one joke isEven() function, are you?

[-] Octopus1348@lemy.lol 3 points 8 months ago

It's much more readable when you use else depending on the checks. You can still use return in an else block.

def Allowed()

  if name == "Octopus1348": return True

  elif name == "Bobert": return True

  else:
        return "You are not allowed to use this script."

print(Allowed())

`

this post was submitted on 16 Jan 2024
527 points (100.0% liked)

Programmer Humor

19197 readers
902 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS