this post was submitted on 08 Dec 2023
618 points (96.4% liked)

Programmer Humor

32411 readers
251 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 49 points 11 months ago* (last edited 11 months ago) (14 children)

Ruby:

a || b

(no return as last line is returned implicitly, no semicolon)

EDIT: As pointed out in the comments, this is not strictly equivalent, as it will return b if a is false as well as if it's nil (these are the only two falsy values in Ruby).

[–] [email protected] 22 points 11 months ago (6 children)

Python:

return a or b

i like it because it reads like a sentence so it somewhat makes sense

and you can make it more comprehensive if you want to:

return a if a is not None else b

[–] [email protected] 7 points 11 months ago (1 children)

I personally dislike this because when you read "or" you expect some boolean result not a random object :/

[–] [email protected] 4 points 11 months ago

there's always the second option for you

load more comments (4 replies)
load more comments (11 replies)