If can write it in a single line of code I'm gonna do it

@vaporeon_ const max = (a, b, c) => a < b ? b < c ? c : b : a < c ? c : a;

@wallhackio Personally I would've split it into 2 lines to make it easier to read, but it's also fine like this 👍

Does your code often need to compare specifically 3 items?

Follow

@vaporeon_ I would never actually use this for a serious project I was just amusing myself by writing it in a single indecipherable line

This specific thing I was doing needed it. Except then I realized later actually I didn't need it lol so I got rid of it

@wallhackio > I would never actually use this for a serious project

Why not?

@vaporeon_ I would write it more clearly if it were something serious

@wallhackio By adding parentheses? Or splitting it across 2 lines?

@vaporeon_ if I were serious I would write it like
const max = (a, b, c) => a < b
? b < c
? c
: b
: a < c
? c
: a;

or I would use an if else for the "outer" condition so there is no nested ternary

@wallhackio That's too many lines, in my opinion :psyduck:

I would've written it like this:

const max = (a, b, c) => a < b ? (b < c ? c : b)                                
: (a < c ? c : a);

@wallhackio Let's do a poll to decide: Is this code easy to read and understand?

const max = (a, b, c) => a < b ? (b < c ? c : b)                                
: (a < c ? c : a);

@wallhackio (I hope there aren't any problems because of me editing the poll for clarity...)

@vaporeon_ @wallhackio it depends what you mean by "easy"

does it take a lot of effort to understand? no

but it does take some effort in a way that most single lines of code don't. if i come across this line while reading, i'm coming to a screeching halt while i slow down to inspect it closely for a moment

and even then, while it's not too bad to figure out what you think it does, if i have to verify its correctness? i'm going to have to spend some time and brainpower running through a couple cases to convince myself it does, in fact, do what you think it does

@monorail @vaporeon_ @wallhackio it's basically the equivalent of a sentence with too many parantheticals and/or em dashes

Sign in to participate in the conversation
📟🐱 GlitchCat

A small, community‐oriented Mastodon‐compatible Fediverse (GlitchSoc) instance managed as a joint venture between the cat and KIBI families.