this post was submitted on 12 Sep 2025
140 points (89.8% liked)

Lemmy Shitpost

34477 readers
3844 users here now

Welcome to Lemmy Shitpost. Here you can shitpost to your hearts content.

Anything and everything goes. Memes, Jokes, Vents and Banter. Though we still have to comply with lemmy.world instance rules. So behave!


Rules:

1. Be Respectful


Refrain from using harmful language pertaining to a protected characteristic: e.g. race, gender, sexuality, disability or religion.

Refrain from being argumentative when responding or commenting to posts/replies. Personal attacks are not welcome here.

...


2. No Illegal Content


Content that violates the law. Any post/comment found to be in breach of common law will be removed and given to the authorities if required.

That means:

-No promoting violence/threats against any individuals

-No CSA content or Revenge Porn

-No sharing private/personal information (Doxxing)

...


3. No Spam


Posting the same post, no matter the intent is against the rules.

-If you have posted content, please refrain from re-posting said content within this community.

-Do not spam posts with intent to harass, annoy, bully, advertise, scam or harm this community.

-No posting Scams/Advertisements/Phishing Links/IP Grabbers

-No Bots, Bots will be banned from the community.

...


4. No Porn/ExplicitContent


-Do not post explicit content. Lemmy.World is not the instance for NSFW content.

-Do not post Gore or Shock Content.

...


5. No Enciting Harassment,Brigading, Doxxing or Witch Hunts


-Do not Brigade other Communities

-No calls to action against other communities/users within Lemmy or outside of Lemmy.

-No Witch Hunts against users/communities.

-No content that harasses members within or outside of the community.

...


6. NSFW should be behind NSFW tags.


-Content that is NSFW should be behind NSFW tags.

-Content that might be distressing should be kept behind NSFW tags.

...

If you see content that is a breach of the rules, please flag and report the comment and a moderator will take action where they can.


Also check out:

Partnered Communities:

1.Memes

2.Lemmy Review

3.Mildly Infuriating

4.Lemmy Be Wholesome

5.No Stupid Questions

6.You Should Know

7.Comedy Heaven

8.Credible Defense

9.Ten Forward

10.LinuxMemes (Linux themed memes)


Reach out to

All communities included on the sidebar are to be made in compliance with the instance rules. Striker

founded 2 years ago
MODERATORS
 
top 44 comments
sorted by: hot top controversial new old
[–] UnfortunateShort@lemmy.world 1 points 12 hours ago

Easily solved by using Rust and have literally anything evaluate your expression and return whatever

[–] benni@lemmy.world 3 points 19 hours ago

I love ternary for assigning to constants.

[–] 9point6@lemmy.world 23 points 1 day ago* (last edited 1 day ago)

Control structure conditional:

  • verbose
  • boring
  • may result to nothing

Ternary expression:

  • terse
  • all action
  • always leads to a result
[–] infinitesunrise@slrpnk.net 30 points 1 day ago (3 children)

For real though I actually find them incredibly useful for creating clean and readable code. I wish Lua 5.1 had a ternary syntax.

[–] p_consti@lemmy.world 7 points 1 day ago (1 children)

Ternary, and inline switch (match expressions), as found in functional languages

[–] infinitesunrise@slrpnk.net 6 points 1 day ago

Oh god yea, replicating switch functionality with a huge column of elifs is so gross.

[–] nimpnin@sopuli.xyz 2 points 1 day ago

I've survived 11 years of programming without ternary operators and prefer to keep it that way

[–] lime@feddit.nu 1 points 1 day ago* (last edited 1 day ago) (2 children)

luas operators are all text in order to be readable. more symbols makes code less readable.

if you want a one line operation that gives a default result, use or: a = b or c is equivalent to if b then a = b else a = c end.

[–] kuberoot@discuss.tchncs.de 2 points 21 hours ago (1 children)

The issue with Lua's and/or in this context is that they don't work if false or nil are valid values. In a and b or c, if b = false, the result is always c.

I also love null-related operators like ?? and ?. for this, since they explicitly check for null, letting you handle any non-null values for optional/default values. The syntax can get a bit cursed, like maybeNull?.maybeMethod?.(args) in JS, but I still prefer that to writing out multiple field accesses in an if condition... And arguably the code is only less readable if you aren't acclimated to it.

All that said I do really appreciate Lua's simplicity, as a language that provides tooling to create the features you want instead of building them into the language, though I wish it had some conventional regex instead of its own patterns.

[–] lime@feddit.nu 1 points 19 hours ago

i despise doing null checks as operators, because everyone does them differently. python's a is not None is immediately obvious and you don't have to think about chaining rules.

[–] infinitesunrise@slrpnk.net 2 points 1 day ago* (last edited 1 day ago) (1 children)

OK true, technically speaking it is indeed more readable, I guess I really meant that it takes far longer to read. I do admire Lua's barebones simplicity. Thank you for the "or" tip, I've used it a few times before but I often forget about it.

[–] lime@feddit.nu 1 points 1 day ago

always remember that code is read more than it is written. complex lines need to be deciphered, simple lines don't. especially fun with symbols that have nonlocal effects like rusts ?.

[–] BeigeAgenda@lemmy.ca 22 points 1 day ago* (last edited 1 day ago) (2 children)

Don't you just love the readability

 a =  a > b ? (b > c ? (a < d ? c : a) : d) : (b < c ? a : d )
[–] kryptonianCodeMonkey@lemmy.world 5 points 20 hours ago

Weird example. 3 nested conditionals is not the typical use case for a ternary, and 2 of the 5 branches result in a pointless a=a assignment. I agree this is bad code, but it's just as bad and hard to parss in a normal if-else structure too:

if (a>b) {
    if (b>c) {
        if (a<d) {
            a=c;
        }
        else {
            a=a;
        }
    }
    else {
        a=d;
    }
}
else {
    if (b<c) {
        a=a;
    }
    else {
        a=d;
    }
}

In another situation, though, it's perfectly readable to have a much more typical ternary use case like:

a = c > d ? c : d

And a pair of parentheses never hurt readability either:

a = (c > d) ? c : d

[–] subignition@fedia.io 8 points 1 day ago

this is way more nested ternary operators than I would ever use (which I understand is for the sake of example) but if you rearrange them so that the simplest statements are in the true branches, and use indentation, you can make it at least a little more readable

a = a <= b ? 
    (b < c ? a : d)
    : b <= c ?
        d
        : (a < d ? c : a);
[–] PeriodicallyPedantic@lemmy.ca 17 points 1 day ago (1 children)

Bah

Ternary is just a compressed if-elseif-else chain with a guaranteed assignment.
If you format it like a sane person, or like you would an if/else chain, then it's way easier to read than if/else chains.

[–] guber@lemmy.blahaj.zone 4 points 1 day ago (1 children)

if else chain? believe of or not, straight to jail.

[–] PeriodicallyPedantic@lemmy.ca 6 points 1 day ago (1 children)

Hey, when you gotta pick a value from a bunch of options, it's either if/elseif/else, ternary, switch/case, or a map/dict.

Ternary generally has the easiest to read format of the options, unless you put it all on one line like a crazy person.

[–] guber@lemmy.blahaj.zone 3 points 1 day ago (2 children)

me personally, i prefer switch case statements for many-value selection, but if ternary works for you, go ham (as long as you don't happen to be the guy who's code I keep having to scrub lol)

[–] PeriodicallyPedantic@lemmy.ca 1 points 14 hours ago* (last edited 12 hours ago)

Switch is good if you only need to compare equals when selecting a value.
Although some languages make it way more powerful, like python match.
but I generally dislike python despite of this, and I generally dislike switch because the syntax and formatting is just too unlike the rest of the languages.

Generally I prefer the clear brevity of:

var foo=
    x>100 ? bar :
    x>50 ? baz :
    x>10 ? qux :
    quux;

Over

var foo;
if(x>100) {
    foo=bar;
} else if(x>50) {
    foo=baz;
} else if(x>10) {
    foo=qux;
} else {
    foo=quux;
}

Which doesn't really get any better if you remove the optional (but recommended) braces.
Heck, I even prefer ternary over some variations of switch for equals conditionals, like the one in Java:

var foo;
switch(x) {
case 100:
    foo=bar;
    break;
case 50:
    foo=baz;
    break;
case 10:
    foo=qux;
    break;
default:
    foo=quux;
}

But some languages do switch better than others (like python as previously mentioned), so there are certainly cases where that'd probably be preferable even to me.

[–] thebestaquaman@lemmy.world 3 points 1 day ago

If there's more than two branches in the decision tree I'll default to a if/else or switch/case except if I want to initialise a const to a conditional value, which is one of the places I praise the lord for ternaries.

[–] groctel@lemmy.world 7 points 1 day ago* (last edited 1 day ago) (1 children)

At my previous workplace we had a C macro that was something like

#define CheckWhatever(x__, true__, false__) \
    whatever(x) ? (true__) : (false__)

I don't remember this shit, so I'm just paraphrasing cursed C. The question one would ask is... why? Well, because you also want to do

#define CheckWhatever2(x__, true__, false__) \
    CheckWhatever((x__ ##1), (true__), (false__)) \
    CheckWhatever((x__ ##2), (true__), (false__))

And, of course

#define CheckWhatever3(x__, true__, false__) \
    CheckWhatever2((x__ ##1), (true__), (false__)) \
    CheckWhatever2((x__ ##2), (true__), (false__))

Long story short, someone wanted to CheckWhatever6 inside another macro. While debugging code old enough to vote, my editor suggested expanding the macro, which expanded to ~1400 lines for a single ternary operator chain. Fun times!

[–] guber@lemmy.blahaj.zone 3 points 1 day ago (2 children)

yeah... yikes. c is a beautiful language but thing like these are why macros may be it's largest blemish. hope that codebase doesn't keep planes flying!

[–] groctel@lemmy.world 1 points 20 hours ago (1 children)
[–] thebestaquaman@lemmy.world 2 points 1 day ago* (last edited 22 hours ago)

For all its faults, I think what makes C beautiful is that it gives you complete freedom do be an absolute idiot.

Whenever I decide to hack something together with an arcane macro, I feel like an animal being released back into the wild, with the (pre-)compiler yelling "Be free! Explore the mysteries of our incomprehensible world!"

[–] four@lemmy.zip 6 points 1 day ago (4 children)
x = if y > 5 { "foo" } else { "bar" }

This is just superior to anything else

[–] kryptonianCodeMonkey@lemmy.world 2 points 20 hours ago (2 children)

In what language is that valid syntax?

[–] four@lemmy.zip 2 points 13 hours ago

This is Rust syntax, but there's similar syntax in Haskell

[–] calcopiritus@lemmy.world 3 points 20 hours ago

This is valid rust. I don't know if there are more languages with this feature

[–] thebestaquaman@lemmy.world 2 points 1 day ago (2 children)

I honestly can't see how this is more readable than

x = (y > 5) ? "foo" : "bar"

I get that it's a syntax that needs to be learned, but it's just so clean and concise!

[–] calcopiritus@lemmy.world 3 points 20 hours ago

Because it can be done for multiple lines too. And you can do else-if too. Also, "if" and "else" is more recognizable than "?" and ":"

x = if y > 5 {
    println!("Y was over 5");
    z + 5
} else if y < 0 {
    handle_negative_y(y);
    z - y
} else {
    println!("<WARN> unexpected value for y"}
    0
}
[–] four@lemmy.zip 1 points 1 day ago (2 children)

What I like about using if and else for that is that you're already using those keywords for branching in other parts of the code.

Though my least favorite is probably Python's:

x = "foo" if y > 5 else "bar"

It just seems backwards to me

[–] UnfortunateShort@lemmy.world 1 points 12 hours ago* (last edited 12 hours ago)

Because Python wants you to read it like English:

x is "foo" if y is greater than 5, else it is "bar"

[–] thebestaquaman@lemmy.world 1 points 22 hours ago* (last edited 22 hours ago) (1 children)

While Python's version does feel a bit backwards, it's at least consistent with how list comprehensions are set up. They can also feel a bit "backwards" imo, especially when they include conditionals.

[–] four@lemmy.zip 1 points 21 hours ago (1 children)

List comprehension is another thing I don't like about Python :)

There's more of those, but one thing I do like about Python is that I get paid for writing it, so I try not to complain too much

[–] kryptonianCodeMonkey@lemmy.world 3 points 20 hours ago

I love list comprehension. Best part of the language, imo. To each their own.

[–] groctel@lemmy.world 3 points 1 day ago

Never forget your roots

(setq x (if (> y 5) :foo :bar))
[–] humanspiral@lemmy.ca 2 points 1 day ago

('bar',:'foo')&({~ 5&< )

[–] Amir@lemmy.ml 5 points 1 day ago

All my homies love ternary

[–] bluespin@lemmy.world 4 points 1 day ago

you would love jsx/tsx with react

[–] QuantumTickle@lemmy.zip 4 points 1 day ago

"Brought to you by the Go gang"

[–] dsilverz@calckey.world 2 points 1 day ago

@guber@lemmy.blahaj.zone

lua -e "print('Lua: ' .. ('awkward_look_monkey_meme' and '👀' or '😐'))"
python -c "print('Python: ' + (not 'awkward_look_monkey_meme' and '👀' or '😐'))"
[–] black_flag@lemmy.dbzer0.com 2 points 1 day ago

A lot of languages have more intuitive ternary syntax than C