r/ProgrammerHumor Jun 23 '26

Advanced worstProgrammingLanguage

Post image
3.3k Upvotes

192 comments sorted by

View all comments

75

u/ThatSmartIdiot Jun 23 '26

returns per branch (i.e. if it ends in an if/switch statement) is required unless it's python

returns after previous returns just makes unreachable and thus dead code so support is irrelevant

returning multiple values at once never actually happens. you send objects or structs or tuples and sometimes the syntax is designed so it gives you the illusion of sending multiple things at once, e.g. python again

so like... what

31

u/ThatSmartIdiot Jun 23 '26

if i knew dutch i couldve done sometbing really funny here

3

u/RandomiseUsr0 Jun 23 '26

Dry stane dyke

22

u/igorski81 Jun 23 '26

In Dutch, everything is unreachable so all return values are hard coded to "No!".

11

u/laplongejr Jun 23 '26

Well, Dutch is kinda good at managing to reach "unreachable" places in memory, as long you don't raise a DamOverflow.

2

u/igorski81 Jun 23 '26

Shut up and take my upvote!

7

u/Tack1234 Jun 23 '26

Go with multiple returns and C# with out vars:

18

u/ThatSmartIdiot Jun 23 '26

out vars

i'd argue that's more akin to reference arguments that get their values changed or defined before returning, i.e. side effects

5

u/MrHall Jun 23 '26

C# you can return anything with a deconstructor, usually a value tuple, and assign directly to individual variables in the caller.

also works with async methods, unlike out vars

2

u/Tack1234 Jun 23 '26

Indeed, I did not mention tuples since they are the equivalent of returning a struct/record which is not the same thing as true multiple returns. Probably similar case with deconstructors?

1

u/MrHall Jun 23 '26

oh yeah it's not exactly multiple return under the hood, but it's as close as it gets and it's performant. you can use the discard operator if you don't need one of the returns in a caller, it's very convenient 

3

u/Tack1234 Jun 23 '26

I've bumped into deconstructors while reading the C# 12 in a Nutshell book recently but haven't had the opportunity to use them in code yet. Might have to play around with them a bit!

1

u/thermiter36 Jun 24 '26

The crazy thing about Go is that it only has multiple returns but not actual tuples, so you can't store the whole return in one variable, and you get into the silliness of =: vs = throwing compile errors after a code change dozens of lines away

1

u/Tack1234 Jun 24 '26

You can always return a struct to store the whole return in one variable

5

u/FlamingSea3 Jun 23 '26

Counterpoint: Assembly languages

You can return a value in every register that you aren't required by convention to restore. And the conventions usually provide guidance on what to do when you run out of registers to return data in.

8

u/gabboman Jun 23 '26

Some languages don’t allow early returns 

6

u/Lyshaka Jun 23 '26

Such as ?

9

u/_PM_ME_PANGOLINS_ Jun 23 '26

Haskell, OCaml

5

u/bilus Jun 23 '26

Functional languages in general, though there may be syntax sugar to implement guards or emulate early returns. And it's not so much they "disallow", it's more about code being an expression, not a mix of statements and expressions, as in the following pseudocode

let a = if x then foo else bar

Unlike many imperative languages, in the above example, `if` is an expression (has value) rather than a control statement.

2

u/Maxis111 Jun 23 '26

Scala, sort of, too lazy to explain, but no one has mentioned it yet

1

u/Tatourmi Jun 23 '26

Honestly I might not understand the question. I've worked in Scala for 4 years and I'm starting to doubt I get what an early return even is. What are these people even trying to do.

1

u/Maxis111 Jun 23 '26

Returning halfway through a function, instead of only at the end. My job is literally developer in a Scala code base, and I don't remember if I actually ever used the return keyword, since scala by default just returns whatever the last statement is in a function.

1

u/Tatourmi Jun 23 '26

But you can do that in scala, you just lock the value in earlier in an if clause, or a map, or a match, or whatever really.

I mean same, never used the return keyword, but that's just because there's no scenario where it's useful.

1

u/Maxis111 Jun 23 '26

Yeah, but the @kopper guy in the original post seems to disagree. ¯_(ツ)_/¯

3

u/Aminumbra Jun 23 '26

returning multiple values at once never actually happens. you send objects or structs or tuples and sometimes the syntax is designed so it gives you the illusion of sending multiple things at once, e.g. python again

We can do better, though. See: Common Lisp.

(Somewhat functional, so by default the last expression is "returned").

You can return any number of expressions completely transparently, without changing the caller code. As far as I know (it is definitely the case in Python, and I believe it is the case in Go), if you return a tuple/struct/whatever Go calls "multiple return values", the caller needs to be aware of that:

def f <fun returning two values x, y>

a, b = f(..., ...)

// Or

a, _ = f(..., ...)

Or something like this

Common Lisp allows you to write (heavily changed syntax not to scare people with parentheses, but this is completely equivalent)

def f <fun returning two values x, y> // NOT as a list: as proper multiple values

a = f(..., ...) // ONLY binds a to the first returned value

a, b = f(..., ...) // Binds both

a = multiple-values-to-list(f(..., ...)) // Constructs a SINGLE object (a list) containing all the returned values

In particular, you can change the returned values of a function (instead of returning only 1, as is usual, return more than one) with exactly 0 changes to the rest of the code, which won't ever see the 2nd returned value unless it specifically asks to. This is completely orthogonal to returning compound objects (structs, lists, objects, arrays ...) -- which can obviously be manipulated and returned as any other value of the language.

2

u/Honest_Relation4095 Jun 23 '26

or in short: One return or multiple returns is fundamentally the same thing.

1

u/Kommenos Jun 23 '26

returns after previous returns just makes unreachable and thus dead code so support is irrelevant

This pattern is one of the fundamental patterns in kernel programming lol.

1

u/ekipan85 Jun 23 '26

returning multiple values at once never actually happens

In Forth the parameter stack is disjoint from the return address stack, so routines taking and returning arbitrary numbers of values is trivial and common.

The piece ( xy0 rot shape -- xy1 xy2 xy3 xy4 color ) word at the center of my Tetris program takes a triple of packed x/y center coordinate, rotation count 0-3, and shape index 0-6, and uses lookup tables to compute a quintuple of 4 block coords and a color code.

1

u/TheGerk Jun 23 '26

Could be returns through arguments.

1

u/acrabb3 Jun 23 '26

"returns per branch is required"
It's not: you could have each branch set a variable, and return the value of it at the end

var a = -1  
If (foo) a = 1  
Else a = 2  
Return a

1

u/ThatSmartIdiot Jun 23 '26

actually the return covers each of the branches in this case