acockworkorange ,

You get one level at the get go because everything is in a function. So just two levels of indentation? A pretty basic if.. for..if nesting has to be refactored? Into what? Goto? Should I sprinkle return statements all over the place?

Y’all gotta understand that Linus is often kind of an ass.

reverendsteveii ,

rules aren't there to be enforced, they're there so that when you break them you take a second to think about why.

dejected_warp_core ,

One nit: whatever IDE is displaying single-character surrogates for == and != needs to stop. In a world where one could literally type those Unicode symbols in, and break a build, I think everyone is better off seeing the actual syntax.

JK_Flip_Flop ,

I love ligatures but much prefer the ones that preserve the proper width of all the characters for this exact reason

baod_rate ,

are there ligatures for monospace fonts that don't preserve the width of the characters?

PlexSheep ,

I think it's a lineature. FiraCide does that for example, and I like it very much. My compiler and lsp will tell me if there is a bad char there. Besides, the linea tires take the same space as two regular characters, so you can tell the difference.

It's not the 90s anymore. My editor can look nice.

darkpanda ,

Ligature, not lineature.

PlexSheep ,

Oops, good to know.

Oinks ,
@Oinks@lemmy.blahaj.zone avatar

If your build fails because you can't track down the literal in the code I would recommend just looking at the compiler error. I understand the concerns about == vs = more but the vast majority of LSPs (and some compilers) will catch that too.

I have also yet to see any IDE enable ligatures by default, at least VS Code and the JetBrains suite both require opting into them even though their default fonts support them.

savedbythezsh ,

In a world where your IDE and maybe also compiler should warn you about using unicode literals in source code, that's not much of a concern.

VSCode (and I'm sure other modern IDEs, but haven't tested) will call out if you're using a Unicode char that could be confused with a source code symbol (e.g. i and ℹ️, which renders in some fonts as a styled lowercase i without color). I'm sure it does the same on the long equals sign.

Any compiler will complain (usually these days with a decent error message) if someone somehow accidentally inserts an invalid Unicode character instead of typing ==.

Juice ,

Broad generalizations aren't for the people who make them, they're for the suckers who consistently fall for them

Kimano ,

My personal code readability axe to grind is nested complex ternary operators.

Every now and then I'll see something like this

return (checkFormatType(currentObject.type==TYPES.static||currentObject type==TYPES.dynamic?TYPES.mutable:TYPES.immutable)?create format("MUTABLE"):getFormat(currentObject));

And I have a fucking conniption because just move that shit into a variable before the return. I get it when sometimes you just need to resolve something inline, but a huge amount of the time that ternary can be extracted to a variable before the ternary, or just rewrite the function to take multiple types and resolve it in the function.

lud ,

That example looks like the PowerShell equivalent of piping complex things around 20 times before actually doing something with the results.

dejected_warp_core , (edited )

In a one-liner competition, sure.

In my codebase? I'd pull a "let's linger after standup about your PR" and have the coder sweat through a 10 minute soapbox about nothing before laying down the law.

Kimano ,

Yeah, the annoying thing is the people who I generally have found to be the worst about stuff like this are old school Senior C developers, who still program like it's 1987 and we have to fit everything into 4K of RAM.

Fortunately there's nothing like that in my code base, I just run into stuff like that periodically when I'm digging around in other team's server code looking for something.

Skullgrid ,
@Skullgrid@lemmy.world avatar

no but bro, the code complexity tool says that this scope has 14 complexity instead of 13, we gotta cram it in a single ternary for code legibility

Nighed ,
@Nighed@sffa.community avatar

This posts entire comment chain is an interesting example of people that have extensive knowledge in completely different areas of programming to me. And have some concepts I had never heard/thought of.

ZILtoid1991 ,

Why is multiple levels of indentation bad?

IDK, but if the reason is "to break stuff into multiple functions", then I'm not necessarily writing yet another single-use function just to avoid writing a comment, especially in time critical applications. Did that with a text parser that could get text formatting from a specifically written XML file, but mainly due to it being way less time critical, and had a lot of reused code via templates.

theherk ,

Like with everything, context matters. Sometimes it can indicate poorly structured control flow, other times inefficient loop nesting. But many times it is just somebody’s preference for guard clauses. As long as the intent is clear, there are no efficiency problems, and it is possible to reach the fewest branches necessary, I see no issues.

zea_64 ,

Indentation implies there's some control structure causing it. Too many control structures nested gets hard to mentally keep track of. 3 is arbitrary, but in general more indentation => harder to understand, which is bad.

frezik ,

It's important to remember that Linus is primarily writing about C code formatting. C doesn't have things that tend to create more deeply nested structures, such as a formal class syntax, or nested functions.

Going too deep is still bad--as zea notes, it's an indication of control structures run amok--but the exact number is dependent on the language and the context.

diviledabit ,

And you never include the switch-case-block indentation levels.

dejected_warp_core ,

Honestly I don't mind the indentation since C isn't going to give us many ways to address this with as little code.

That said, with compilers that are good at inlining trivial functions, I really do appreciate the "it does what it says on the tin" approach to using functions on things like this. Even if they're only used once. Comments would help too.

The logic in these if statements is inscrutable on a cold read like this. To me, that's a maintenance risk; imagine seeing a snippet this size on a PR. Having functions that name what the hell is going on could only help.

RustyNova , (edited )

While I totally agree with that philosophy, it heavily depends on the language.

For Rust, my philosophy is more like this:

  • Impl + fn body don't count, as well as async blocks if they span the whole function
  • do not nest more than one if statement. You probably better using guard clauses or matches
  • do not put loops into an if statement.
  • do not nest loops unless clearly shown to be (X, Y) indexing
  • method chaining is free
  • do not nest closures, unless the nested closure doesn't have a {} block
  • do not use mod unless it's test for the current module. No I don't want to Star Wars scroll your 1000 line file. Split it.
magikmw ,

I'ma gonna steal this.

meteokr ,
@meteokr@community.adiquaints.moe avatar

I don't know enough Rust to understand by what you mean by the last one. My understanding was that mod name was just declaring the module that this file depends on. Could you explain what I should do instead? Since your other statements I totally agree with, I should probably agree with the last one.

Killing_Spark ,

mod name declares that the module should be compiled and reachable as a submodule of the current module. This assumes that you have a file or directory of the name in the right place. This is what you should do.

You can also declare a module like this: mod name {...} where you just put the content in the block. The two are functionally equivalent, from the compilers perspective.

meteokr ,
@meteokr@community.adiquaints.moe avatar

I don't understand how to follow this bullet point that I was replying to.

do not use mod unless it’s test for the current module. No I don’t want to Star Wars scroll your 1000 line file. Split it.

I already know what mod does in a basic sense, I wanted to know what the commenter meant by this.

Killing_Spark ,

This point advocates against the use of mod with content in a file unless it is used for a testing module. A common pattern is to have the unit tests for a module inside the main module file. Tests in rust are just specially tagged functions. To avoid compilation costs in non-test builds and false unused code warnings you can put all test related code in a submodule and tag that module with #[cfg(test)]. That way the module will only be included and compiled if the crate is being compiled to run tests.

The Star wars thing refers to scrolling long text files similar to the intro of the starwars movies where a long text is scrolled for the viewer.

meteokr ,
@meteokr@community.adiquaints.moe avatar

Oh so its just referring to writing the mod's code in the same file the mod is declared in being bad form? That seems very reasonable; since the point of a module is code separation so it makes sense to always put it in its own file. Good, I'm already doing that at least!

calcopiritus ,

Why have an async block spanning the whole function when you can mark the function as async? That's 1 less level of indentation. Also, this quite is unusable for rust. A single match statement inside a function inside an impl is already 4 levels of indentation.

RustyNova ,

Those async blocks exist when doing async in traits.

And I never said I respected the 4 level of indentations. That's exactly the point of those rules. Keep it lowly indented but still readable

Doods ,

A single match statement inside a function inside an impl is already 4 levels of indentation.

How about this?

The preferred way to ease multiple indentation levels in a switch statement is to align the switch and its subordinate case labels in the same column instead of double-indenting the case labels. E.g.:

switch (suffix) {
case 'G':
case 'g':
        mem <<= 30;
        break;
case 'M':
case 'm':
        mem <<= 20;
        break;
case 'K':
case 'k':
        mem <<= 10;
        /* fall through */
default:
        break;
}

I had some luck applying this to match statements. My example:


let x = 5;

match x {
5 => foo(),
3 => bar(),
1 => match baz(x) {
	Ok(_) => foo2(),
	Err(e) => match maybe(e) {
		Ok(_) => bar2(),
		_ => panic!(),
		}
	}
_ => panic!(),
}

Is this acceptable, at least compared to the original switch statement idea?

lseif ,

i personally find this a lot less readable than the switch example. the case keywords at the start of the line quickly signify its meaning, unlike with => after the pattern. though i dont speak for everybody.

Doods ,

How about this one? it more closely mirrors the switch example:

match suffix {
'G' | 'g' => mem -= 30,
'M' | 'm' => mem -= 20,
'K' | 'k' => mem -= 10,
_ => {},
}

How about this other one? it goes as far as cloning the switch example's indentation:

match suffix {
'G' | 'g' => {
	mem -= 30;
        }
'M' | 'm' => {
	mem -= 20;
        }
'K' | 'k' => {
	mem -= 10;
        }
_ => {},
}
lseif ,

the problem is that, while skimming the start of each lines, nothing about 'G' | 'g' tells me that its a branch. i need to spend more time parsing it. mind you, this may simply be a problem with rust's syntax, not just ur formatting.

RustyNova ,

It's a lot less readable imo. As well than a cargo fmt later and it's gone (unless there's a nightly setting for it)

Doods ,

Formatters are off-topic for this, styles come first, formatters are developed later.

My other reply:

How about this one? it more closely mirrors the switch example:

match suffix {
'G' | 'g' => mem -= 30,
'M' | 'm' => mem -= 20,
'K' | 'k' => mem -= 10,
_ => {},
}

How about this other one? it goes as far as cloning the switch example's indentation:

match suffix {
'G' | 'g' => {
  mem -= 30;
       }
'M' | 'm' => {
  mem -= 20;
       }
'K' | 'k' => {
  mem -= 10;
       }
_ => {},
}
folkrav ,

I mean, I use formatters everywhere I can exactly so I don’t have to think about code style. I’ll take a full code base that’s consistent in a style I dislike, over having another subjective debate about which style is prettier or easier to read, any day. So whatever cargo fmt spits out is exactly what I’ll prefer, regardless of what it looks like, if only for mere consistency.

calcopiritus ,

Well, of course you can have few indent levels by just not indenting, I don't think the readability loss is worth it though. If I had give up some indentation, I'd probably not indent the impl {} blocks.

Doods ,

I just got some idea yesterday regarding impl blocks, ready to be my respondent?

I had a big impl block with 4 levels of indentation, so I cut the block, and replaced

impl InputList {
    //snip
}

with mod impl_inputlist; and moved the impl block to a new file, and did not indent anything inside that block.

The advantage this has over just not indenting the impl block in place, is that people will have difficulty distinguishing between what's in the block and what's outside, and that's why the impl was moved to its own exclusive file, impl_inputlist.rs

Maybe I am overstressing indentation. Ss there something wrong with my setup that prevents me from accepting 4-space indentation?

I use:

Editor: Neovide

Font: "FiraCode Nerd Font Mono:h16" (16px fonts are addicintg)

Monitor: 1366x768, 18.5 inch, 10+ years old, frankenstein-ly repaired Samsung monitor.

Distance: I sit at about 40-60 Cm from my monitor.

That leaves me with a 32x99 view of code excluding line numbers and such.

AVincentInSpace ,

what if I need to nest if lets

RustyNova ,

Use a match? Unless it's for guard clauses, a match is fine enough

AVincentInSpace ,

what if i need to if let on the result of another if let

RustyNova ,

Oh, then you use and_then() or something similar.

There's also the possibility to use the guard clauses patern and do let <...> = <...> else {}.

And finally, you can always split into another function.

It's not straight rules. It depends on what makes it more readable for your case.

AVincentInSpace ,

what about if on a boolean followed by an if let

RedditWanderer ,

Only the sith deal in absolutes

Entropywins ,

You must be a sith...

69420 ,

Absolutely.

kureta ,

They might be a sith.

Entropywins ,

Ah shit...

diviledabit ,

Ah bowel movement...

It could be a fart brewing

algernon ,
@algernon@lemmy.ml avatar

Sadly, that's not code Linus wrote. Nor one he merged. (It's from git, copied from rsync, committed by Junio)

riodoro1 ,

You really think someone would do that? Just go on the internet and tell lies?

ngn OP ,
@ngn@lemy.lol avatar
Epzillon ,

Isn't that from 1991 while the quote is from 1995? If we're nitpicking maybe we shouldn't time travel 🤓

lowleveldata ,

Damn it Time Patrol! You can't stop me!

avidamoeba ,
@avidamoeba@lemmy.ca avatar
  • Time Troll
metallic_z3r0 ,

I mean it was 0.01, at that point he was screwed anyway, and he fixed his program.

redcalcium ,

He wouldn't make that statement unless he experienced the horror himself.

Now, if he still does it these days...

nialv7 ,

He barely ever code these days.

rwhitisissle ,

I've heard similar from the worst first year CS students you could ever meet. People talk out their ass without the experience to back up their observations constantly. The indentation thing is a reasonable heuristic that states you are adding too much complexity at specific points in your code that suggests you should isolate core pieces of logic into discrete functions. And while that's broadly reasonable, this often has the downside of you producing code that has a lot of very small, very specific functions that are only ever invoked by other very small, very specific functions. It doesn't make your code easier to read or understand and it arguably leads to scenarios in which your code becomes very disorganized and needlessly opaque purely because you didn't want additional indentation in order to meet some kind of arbitrary formatting guideline you set for yourself. This is something that happens in any language but some languages are more susceptible to it than others. PEP8's line length limit is treated like biblical edict by your more insufferable python developers.

refalo ,

line 152 is the only thing past 3 levels and I'd say that one gets a pass.

laughterlaughter ,

Plus it shows three levels of indentation. Well... there is the extra one created by the compiler directives, but do they really count?

dariusj18 ,

The number one thing that gets in my way of refactoring to function is figuring out what to name the functions takes too long.

victorz ,

Then perhaps the code you are trying to extract doesn't make a clear and cohesive procedure. Maybe include more or less of the code, or rework the code into logical pieces or steps. Write the algorithm in human language first, then implement the steps using functions.

🤷‍♂️ Or fnck it.

Feathercrown , (edited )

Sometimea the smallest logical unit of an algorithm uses more than 3 levels of indentation

skullgiver ,
@skullgiver@popplesburger.hilciferous.nl avatar

Maybe this will be the biggest contribution AI will do for programmers: figure out good names. I should try that some time!

On the other hand, AI will only generate output as good as its input, and most large code projects (including Linux) are terrible at naming things.

dariusj18 ,

I had a similar thought

zea_64 ,

Pick something and change it when inspiration strikes. Sometimes you need a big picture view of something to get the right abstractions or even just name things.

sabreW4K3 ,
@sabreW4K3@lazysoci.al avatar

He obviously meant 13, because 3 is WTF 😭

electricprism ,

SCSS go brrrr

aport ,

Ligatures 🤢🤮🤮

xmunk ,

I fucking hate that If style.

nukul4r ,

You're brave (I don't agree)

DmMacniel ,

What's wrong with Ligatures? It makes reading code a bit more tolerable.

Ephera ,

I mean, I certainly wouldn't give someone else shit for using ligatures, but personally, I don't like them, because:

  • they break with monospacedness. Everything is in a nice grid and you've randomly got these character combinations that needlessly stick out.
  • they sometimes happen in places where they really shouldn't.
  • they hide what the actual characters are. Especially, if go to edit that code, my brain will really struggle for a split-second when there's a '≠', then I delete one character and rather than the whole thing disappearing, I'm left with a '!'.
red ,
@red@sopuli.xyz avatar

Do you also get surprised when you backspace a tab and suddenly it removes more whitespace than 1 characters worth?

Or did you learn it fast and really never think about it?

I think it's more a "getting used to" thing, that once learned, you don't think about, but it makes things more readable.

Ephera ,

Sure, I could get used to it. But it being more readable is not even true for me, because the thing I got used to instead, is that != is the unequals-operator. I see that much more often than .

red ,
@red@sopuli.xyz avatar

Studies show that ligatures improve readability, but I acknowledge that it's likely untrue for outliers.

Ephera ,

For monospace fonts? I've heard of such research for proportional fonts, where ligatures definitely make sense to me. But yeah, I wouldn't assume such research to automatically translate to monospace.

Faresh ,

they break with monospacedness

The IDEs I've used had the ligatures be of the same character width as the original operator.

Ephera ,

Oh, yeah, I meant that it makes two characters into one big one, visually reaching across two or three widths, or just having one of the characters larger than the usual grid, e.g. in := the equals sign reaches into the width of the colon.

This reminds me of a recent Microsoft font¹, so naturally here's a rant about that: They developed a feature, called "texture-healing", which basically allows characters that normally need to cramp into one monospace width, like m or w, to reach into the space of neighboring characters, if those neighboring characters are narrow, like an i.

In theory, not a terrible idea, but then you get this kind of hate crime:
https://lemmy.ml/pictrs/image/155b3c80-ea20-4879-9241-b481cd36c920.png

Obviously, might just be me again, but not having these letters align, just looks so much worse to me.

¹: It's this font: https://monaspace.githubnext.com/

red ,
@red@sopuli.xyz avatar

I mean, we read code more than we write it. You just vomitted over something that increases readability. Maybe a time for a rethink?

skullgiver ,
@skullgiver@popplesburger.hilciferous.nl avatar

Ligatures are great. Don't use them if you don't like them, but don't try to shame people for having different preferences.

The biggest exception to using ligatures is in documentation. I believe Kotlin used (uses?) ligatures in some of its documentation, leaving the reader to figure out if they actually need to type ≠ or if != will suffice. Not a great move, even if the IDE will render the ligatures just fine!

avidamoeba ,
@avidamoeba@lemmy.ca avatar

Uses multiple returns... I'm switching to Windows.

GissaMittJobb ,

What's wrong with multiple returns?

avidamoeba , (edited )
@avidamoeba@lemmy.ca avatar

Maintainability.

You can't read a block of code and as quickly and understand its control flow without reading every line, especially in regards to resource cleanup.

For example say you have:

...
if this:
    something
    or other
    and many more...
    ...
else:
    yet another thing 
    and some more
    ...

do some cleanup
return
...

Say you aren't exactly interested in what happens inside each branch. If you can assume that there's one return at the end of the block, you can see the if and else, you can reason about what values would trigger each branch, you can also see that no matter which branch is executed, the cleanup step will be executed before returning. Straightforward. I don't have to read all the lines of the branches to ensure the cleanup will be executed. If I can't assume a single return, I have to read all those lines too to ensure none of them jumps out of the function skipping the cleanup. Not having to think about such cases reduces the amount of reading needed and it makes reasoning about the block simpler. The bigger the blocks, the more the branches, the stronger the effect. You have one less foot-shotgun to think about. The easier you make it for your brain, the fewer mistakes it's gonna make. For all those days when you haven't slept enough.

E: Oh also refactoring blocks of code out into functions is trivial when you don't have multiple returns. Extracting a block with a return in it breaks the parent control flow and requires changes in the implementation.

E2: Shorter blocks do not obviate this argument. They just make things less bad. But they make almost everything less bad. Shorter blocks and single returns make things even better.

ugo ,

Bad advice. Early return is way easier to parse and comprehend.

if (p1)
{
    if(!p2)
    {
        if(p3 || !p4)
        {
            *pOut = 10;
        }
    }
}

vs

if(!p1) return;
if(p2) return;
if(!p3 && p4) return;

*pOut = 10;

Early out makes the error conditions explicit, which is what one is interested in 90% of the time. After the last if you know that all of the above conditions are false, so you don’t need to keep them in your head.

And this is just a silly example with 3 predicates, imagine how a full function with lots of state looks. You would need to keep the entire decision tree in your head at all times. That’s the opposite of maintainable.

avidamoeba , (edited )
@avidamoeba@lemmy.ca avatar

I'm sure you are capable of rewriting that in 3 lines and a single nested scope followed by a single return. In fact in languages that use exceptions you have to use at least one subscope.

Notice that in my example I didn't even broach the example with error conditions, cause it's trivial to write cleanly either way. Instead I talked about returns inside business logic. You can't unfuck that. 🐞

ugo , (edited )

Since my previous example didn't really have return value, I am changing it slightly. So if I'm reading your suggestion of "rewriting that in 3 lines and a single nested scope followed by a single return", I think you mean it like this?

int retval = 0;

// precondition checks:
if (!p1) retval = -ERROR1;
if (p2) retval = -ERROR2;
if (!p3 && p4) retval = -ERROR3;

// business logic:
if (p1 && !p2 && (p3 || !p4))
{
    retval = 42;
}

// or perhaps would you prefer the business logic check be like this?
if (retval != -ERROR1 && retval != -ERROR2 && retval != -ERROR3)
{
    retval = 42;
}

// or perhaps you'd split the business logic predicate like this? (Assuming the predicates only have a value of 0 or 1)
int ok = p1;
ok &= !p2;
ok &= p3 || !p4;
if (ok)
{
    retval = 42;
}

return retval;

as opposed to this?

// precondition checks:
if(!p1) return -ERROR1;
if(p2) return -ERROR2;
if(!p3 && p4) return -ERROR3;

// business logic:
return 42;

Using a retval has the exact problem that you want to avoid: at the point where we do return retval, we have no idea how retval was manipulated, or if it was set multiple times by different branches.
It's mutable state inside the function, so any line from when the variable is defined to when return retval is hit must now be examined to know why retval has the value that it has.

Not to mention that the business logic then needs to be guarded with some predicate, because we can't early return. And if you need to add another precondition check, you need to add another (but inverted) predicate to the business logic check.

You also mentioned resource leaks, and I find that a more compelling argument for having only a single return. Readability and understandability (both of which directly correlate to maintainability) are undeniably better with early returns. But if you hit an early return after you have allocated resources, you have a resource leak.

Still, there are better solutions to the resource leak problem than to clobber your functions into an unreadable mess. Here's a couple options I can think of.

  1. Don't: allow early returns only before allocating resources via a code standard. Allows many of the benfits of early returns, but could be confusing due to using both early returns and a retval in the business logic
  2. If your language supports it, use RAII
  3. If your language supports it, use defer
  4. You can always write a cleanup function

Example of option 1

// precondition checks
if(!p1) return -ERROR1;
if(p2) return -ERROR2;
if(!p3 && p4) return -ERROR3;

void* pResource = allocResource();
int retval = 0;

// ...
// some business logic, no return allowed
// ...

freeResource(pResource);
return retval; // no leaks

Example of option 2

// same precondition checks with early returns, won't repeat them for brevity

auto Resource = allocResource();

// ...
// some business logic, return allowed, the destructor of Resource will be called when it goes out of scope, freeing the resources. No leaks
// ...

return 42;

Example of option 3

// precondition checks

void* pResource = allocResource();
defer freeResource(pResource);

// ...
// some business logic, return allowed, deferred statements will be executed before return. No leaks
// ...

return 42;

Example of option 4

int freeAndReturn(void* pResource, const int retval)
{
    freeResource(pResource);
    return retval;
}

int doWork()
{
    // precondition checks

    void* pResource = allocResource();

    // ...
    // some business logic, return allowed only in the same form as the following line
    // ...

    return freeAndReturn(pResource, 42);
}
Miaou ,

goto is used in C for this exact kind of early return management. The person you answered to does not maintain code I think

avidamoeba ,
@avidamoeba@lemmy.ca avatar

goto cleanup is not the same as return. I didn't badmouth goto cleanup.

Miaou ,

This is virtually the same thing with a different keyword, I'd like to hear where you (and the down voters) draw the line.

avidamoeba , (edited )
@avidamoeba@lemmy.ca avatar

Not sure why you had to do the inverted predicate check again in your first example. You already have the information encoded in the value of retval. It can be written like this:

int result = 0;
if (!p1) result = -ERROR1;
if (p2) result = -ERROR2;
if (!p3 && p4) result = -ERROR3;
if (result != 0) {
    result = 42;
}

return result;

With a return value you have to add 4 extra lines. This overhead remains constant as you add more checks and more business logic.

Yes all the other suggestions are better than early returns in business logic and would help with leaks. Would be nice if we had RAII outside of C++. I think Rust has it? Haven't done Rust yet.

eager_eagle ,
@eager_eagle@lemmy.world avatar

I hate it when some blame early returns for the lack of maintainability.

Early returns are a great practice when doing argument validation and some precondition checks. They also avoid nested blocks that worsen readability.

What's being described there is a function that tries to do too much and should be broken down. That's the problem, not early returns.

avidamoeba , (edited )
@avidamoeba@lemmy.ca avatar

Early returns are very similar to gotos. One level of nesting to take care of validation is trivial in comparison. You're replacing logical clarity for minimal visual clarity. This is true regardless of the size of the function which shows that the size of the function isn't the determinant. You're not alone in your opinion, clearly, and I'm not going to convince you it's a bad practice but I'll just say what I think about it. 😅 This practice doesn't make it my team's codebase.

eager_eagle ,
@eager_eagle@lemmy.world avatar

You can say any execution flow controls are like gotos - continue, break, exceptions, switch, even ifs are not much more than special cases of gotos.

This is true regardless of the size of the function which shows that the size of the function isn’t the determinant

Logical clarity does tend to worsen as the function grows. In general, it is easier to make sense of a shorter function than a longer one. I don't know how you could even say otherwise.

Early returns are still great for argument validation. The alternative means letting the function execute to the end when it shouldn't, just guarded by if conditions - and these conditions any reader would have to keep in mind.

When a reader comes across an early return, that's a state they can free from their reader memory, as any code below that would be unreachable if that condition was met.

avidamoeba , (edited )
@avidamoeba@lemmy.ca avatar

I never said longer functions are not less clear. I said my argument is valid irrespective of the length of the function which shows that the problems I claim multiple returns bring are independent of function length. 😊

Any validation you can write with a few early returns you can write with an equivalent conditional/s followed by a single nested block under it, followed by a single return. The reader is free to leave if the validation fails nearly the same, they have to glance that the scope ends at the end of the function. Looks at conditional - that's validation, looks at the nested block - everything here runs only after validation, looks after the block - a return. As I mentioned in another comment, validation is a trivial case to do either way. Returns inside business logic past validation is where the problematic bugs of this class show up which requires more thorough reading to avoid.

If you gave me a PR with early returns only during validation, I probably won't ask you to rewrite it. If I see them further down, it's not going in.

eager_eagle , (edited )
@eager_eagle@lemmy.world avatar

Any validation you can write with a few early returns you can write with an equivalent conditional/s followed by a single nested block under it, followed by a single return. The reader is free to leave the validation behind just the same.

And that conditional indents your entire function one level - if you have more validation checks, that's one level of indentation per check (or a complicated condition, depends whether you can validate it all in one place). It's pretty much the case the other user illustrated above.

Returns inside business logic past validation is where the problematic bugs of this class show up

That much we agree. But again, this is not an early return issue, putting too much logic in a function is the issue. Rewriting it without early returns won't make it much clearer. Creating other functions to handle different scenarios will.

avidamoeba ,
@avidamoeba@lemmy.ca avatar

Again, if you can write it with conditionals and returns, you can write it with equivalent number of conditionals and a single nested scope. No further scopes are needed. The conditional will even look nearly identically.

PeriodicallyPedantic ,

If your function is so long that keeping track of returns becomes burdensome, the function is too long.

I'm not a fan of returning status codes, but that's a pretty clear example of early return validation where you can't just replace it with a single condition check.
Having a return value that you set in various places and then return at the end is worse than early return.

avidamoeba , (edited )
@avidamoeba@lemmy.ca avatar

I don't think it's worse, I think it's equivalent. Also I don't like the risk of resource leaks which is inherent to multi-returns beyond input validation. And that's true beyond C because memory isn't the only resource that can be leaked.

It's not about how readable the branches are, it's about having to read all of them to ensure you understand the control flow so that you don't leak. Length of functions is a red herring. You want me to read the contents of short blocks to ensure the control flow is correct. I don't want to read the contents of those blocks, other than the conditional and loop statements. Reading short blocks is better than reading long blocks. Reading just the control flow lines is better than reading short blocks.

Miaou ,

And I'm going to make you read those blocks because they are there for a damn reason. What are you even reading at this point if you're not reading the preconditions? That's how you end up dereferencing null pointers, when you have ten nested ifs you can barely see it on your screen

PeriodicallyPedantic ,

You said yourself they're equivalent. You either have to read the blocks in both cases or neither case.

You need to read the blocks to know what gets returned (either early or in a single return). You need to read the blocks to see what resources get created but not released. What are you hoping to achieve by only reading control flow?

At least with an early return you can stop reading.

avidamoeba ,
@avidamoeba@lemmy.ca avatar

I meant assigning a return value then returning it is the sam as rnultiple returns. Anyway.

PeriodicallyPedantic ,

Right. Like I said.

What are you hoping to accomplish by only reading control flow and not the contents of the blocks? You keep raising concerns like not properly releasing resources, but if you don't read the blocks you don't know what resources we're allocated.

I think your argument depends on both having your cake and eating it.

avidamoeba ,
@avidamoeba@lemmy.ca avatar

Yes clearly someone has to read the blocks at least once to ensure they are correct.

In subsequent reads, when I'm interested in the second block out of two, say during a defect analysis, I don't have to read the first one to be sure I'm going to reach the second. I can straight head for the second one and any subsequent stuff I care about. Multiple returns force me to read both blocks. I don't know what else to tell you. To me this is obvious and I think it's probably even provable. I don't know about you but I have to read a lot of existing code and every bit helps. We have pretty strict code style guides for that reason.

PeriodicallyPedantic ,

If you're reading the control flow, and the control flow tells you the first block isn't being entered, then it doesn't matter if the first block contains an early return or not, because it wasn't being entered. If it was being entered then you have to read it anyway to make sure it's not manipulating state or leaking resources.

To use your example: in subsequent reads, when I'm interested in the second block out of n, say during defect analysis, I can head straight to the second block in either case since control flow shows the first block was skipped - but in the case of early return from the second block I can stop reading, but in the case of a single return I need to read the flow for all subsequent n blocks and the business logic of any subsequent blocks that get entered. The early return is a guarantee that all subsequent blocks may be ignored.

To me this is also obvious. I've been doing this for quite a while and 95% of the time, reviewing and debugging code with a single return is far more tedious.

avidamoeba , (edited )
@avidamoeba@lemmy.ca avatar

Clearly I'm not referring to an if/else by saying two blocks. Even in my original example I show the exact issue. You don't understand it. I can't explain it better.

PeriodicallyPedantic ,

Have you stopped to consider why you can't explain it better? Perhaps the reason is because you're wrong.

Your toy example does not show the issue you think it shows. You've moved your cleanup block away from the context of what it's cleaning up, meaning that you've got variables leaking out of their scopes. Your cleanup code is now much more complex and fragile to changes in each of the blocks its cleaning up after.

You tried to use your toy example to show A is better, but then we showed that actually B is just as good. So fix your toy example to show what you actually want to say, because everything you said so far depends on you setting different standards for each scenario.

avidamoeba ,
@avidamoeba@lemmy.ca avatar

Have you stopped to consider why you can't explain it better? Perhaps the reason is because you're wrong.

Yes I have. You've already assumed I'm not too bright more than once and worked from there. There's no point in investing more work on my end. If what I said worked, good. If not, that's fine too.

PeriodicallyPedantic ,

Now that's the pot calling the kettle black.

What work have you even invested? You've just repeatedly restarted your original stance. But sure, whatever.

kubica ,
@kubica@kbin.social avatar

You mean you are early-returning to windows, uh? You can't do that by your own rules.

avidamoeba ,
@avidamoeba@lemmy.ca avatar

Fuck.

skullgiver ,
@skullgiver@popplesburger.hilciferous.nl avatar

Windows isn't any better I'm afraid.

I blame C for forcing users to use goto, conditional returns, or terribly nested code. Language features like defer or even try/catch/finally make it much easier to write readable code with limited returns.

avidamoeba ,
@avidamoeba@lemmy.ca avatar

That was the joke. 😂 I bet Windows is worse.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • [email protected]
  • tech
  • kbinEarth
  • testing
  • interstellar
  • wanderlust
  • All magazines