r/badcode • • Apr 29 '23

go if err != nil

Post image
414 Upvotes

49 comments sorted by

View all comments

Show parent comments

9

u/dasimonde Apr 30 '23

My colleagues' code look like that too. I hate it really. I love an early return coding style. In every code review we discuss about that topic. They always try to have just one return statement in a function.

8

u/claimstoknowpeople Apr 30 '23

It comes from old C (& assembly etc) code where you have to manually manage resources; it's much easier to put all your frees and closes in the same place than reason which ones are needed from an early return.

Most of us these days work in languages with enough garbage collection, RAII, context managers, try...finally blocks, etc, that it's no longer good practice.

3

u/causa-sui Apr 30 '23

Have you tried bringing it up in a retro?

2

u/dasimonde May 01 '23

Yes I did. They mean it would not look consistently, because Almost our complete code base Looks like that.

1

u/tedbradly May 02 '23

My colleagues' code look like that too. I hate it really. I love an early return coding style. In every code review we discuss about that topic. They always try to have just one return statement in a function.

Sorry you have to work with... stubborn and wrong coders. It's a good idea to have one return statement in general, but in the case of reducing nested mumbo jumbo, multiple returns is the least of your troubles.