News for hackers. Stuff that matters.
Re:Push ifs up and fors down by Tainnor on Thursday November 16, 2023 @01:03AM (#38286253)
The more experience I get, the more I feel that too many programmers worry about making things pretty "in the small", but not enough care about proper design of an entire codebase.

I'll admit that I like a concise and well-crafted function just as much as many others, but articles like this one are probably the things that can lead to the kind of unproductive bikeshedding that is sometimes experienced in PRs and other discussions. I don't care that much about whether your function is messy - or about where you put your ifs and fors (or if you use maps and filters instead), as long as the function is properly named, has a good interface (including expressive types), a clear purpose, is properly documented, doesn't make excessive use of side effects etc.

Re:Push ifs up and fors down by Leo_Germond on Thursday November 16, 2023 @01:20AM (#38286308)
The advice about if up is not bikeshedding though, it is the exact kind of architectural choice you're saying one should decide on. Don't believe me ? Well imagine you have inputs, where should you validate them ? According to this rule of thumb it's at the topmost level, when they are received. Well that seems super sensible, and it's typically something that helps with understanding the code (rather than checking them at thw very last moment). Also for proofs that's technically necessary to allow the preconditions to "percolate up", which has the same effect of moving the if up.

So the first advice is definitely not bike shedding, the second one I'm not so clear though ;)

Re:Push ifs up and fors down by Tainnor on Thursday November 16, 2023 @01:24AM (#38286329)
But the article wasn't about "validate inputs at the boundaries" (which is a semantic concern), it was about "push your ifs up" (which is a syntactic concern).

I agree that in the provided example, those two seem to somewhat coincide (although it's hard to say, given that the author makes an effort to give their functions names such as "frobnicate" that don't indicate their purpose), but in the general case that doesn't have to be true.

Re:Push ifs up and fors down by mike_hock on Thursday November 16, 2023 @01:54AM (#38286474)
Are we reading the same article? There is zero concern for syntax.

The first point is literally what you said. Write functions with clear interfaces. If your function doesn't handle None, make its argument type reflect that (Walrus instead of Option<Walrus>).

The second point is about performance. Hoist branches out of loops, and process batches so any constant cost is amortized over the batch. Is that even controversial?

> the author makes an effort to give their functions names such as "frobnicate"

Yes, and that's good, because it keeps the focus on his actual points rather than the minutiae of a contrived example.

Re:Push ifs up and fors down by Tainnor on Thursday November 16, 2023 @06:40AM (#38288349)
> Are we reading the same article? There is zero concern for syntax.

By "syntactical" I mean "concerned with the surface level appearance of low-level code structure".

> Yes, and that's good, because it keeps the focus on his actual points rather than the minutiae of a contrived example.

Only if you think that guidelines about how to write code should be devoid of the context of the problem you're trying to solve. I happen to think that this is mostly wrong.

Re:Push ifs up and fors down by mike_hock on Thursday November 16, 2023 @03:46PM (#38295232)
Yes, I think it's possible to have discussions on an abstract level and not everything has to be a case study.
Re:Push ifs up and fors down by pjc50 on Thursday November 16, 2023 @08:07AM (#38289067)
"If" is semantic!
Re:Push ifs up and fors down by Tainnor on Thursday November 16, 2023 @08:40AM (#38289367)
Its location in the codebase isn't.
Re:Push ifs up and fors down by patmorgan23 on Thursday November 16, 2023 @09:40AM (#38290057)
If it changes the behavior of the program it is.
Re:Push ifs up and fors down by Tainnor on Thursday November 16, 2023 @10:10AM (#38290499)
Well, let's consider the three examples the author gives for "pushing ifs up":

The first one isn't even a proper example because they write "push the if to the caller", but doesn't actually show the caller. So the good and bad examples aren't even straightforwardly comparable. For the second example, calling g() has exactly the same behaviour as calling f(), and for the third example, the behaviour also remains identical with or without the enum.

I don't see how this is any more than just pushing code around without having more context about these methods and who calls them.

Re:Push ifs up and fors down by mpweiher on Thursday November 16, 2023 @02:05AM (#38286531)
> it is the exact kind of architectural choice you're saying one should decide on.

While I can't speak to what the OP had in mind, architectural concerns are definitely not inside a function. Even connecting individual functions/procedures barely registers at the smallest, most granular scale of architecture.

Of course, our programming languages for the most part don't let us express architectural concerns...so here we are.

Re:Push ifs up and fors down by Tainnor on Thursday November 16, 2023 @06:27AM (#38288262)
Yes, this is what I was talking about. Debating which function should include an "if" condition seems rather immaterial to me.
Re:Push ifs up and fors down by tangjurine on Thursday November 16, 2023 @01:11AM (#38286278)
Like there's the stuff that can be refactored because the changes are local, or the places to change are clear.

And then the stuff that is a lot harder to refactor because you would need to change code in a lot of places, and the places that need to be changed aren't clear. That's probably more the proper design of a codebase stuff.

Re:Push ifs up and fors down by kubb on Thursday November 16, 2023 @01:53AM (#38286467)
Programming language design also tends to focus on the small (e.g. optimizing the number of language keywords).
Re:Push ifs up and fors down by pjmlp on Thursday November 16, 2023 @02:04AM (#38286524)
To go along the same line of thought, too many think about the beatuy of the flower, instead of the whole florest.

The amount of hours occasionally wasted discussing this in PR, that add zero value to what the customer actually cares, e.g. does pressing the button print or not their monthly invoice.

Sure there is a balance to be had between completly unmaintainable code, and a piece of art to expose in the Louvre, however too many focus on the latter, instead of what is the customer getting out of their beautiful flower.

Re:Push ifs up and fors down by valand on Friday November 17, 2023 @05:51AM (#38301837)
Often the people who overfocus on the beauty of the flower are those detached too far from the customers, too many indirections that hide failures and pressure
Re:Push ifs up and fors down by mpweiher on Thursday November 16, 2023 @02:08AM (#38286545)
> ... but not enough care about proper design of an entire codebase.

One reason is that we don't have ways of meaningfully expressing components much larger than functions or that connect differently from functions in our programming languages. At most we can aggregate functions together. After that we're on our own.

Re:Push ifs up and fors down by teo_zero on Thursday November 16, 2023 @03:02AM (#38286857)
Poor choice of words in TFA and a cursory read by a reader generated this misunderstanding. "Up" means "to the caller" for the author, but was probably understood as "towards the beginning (of the function or file)".
Re:Push ifs up and fors down by Woshiwuja on Thursday November 16, 2023 @05:16AM (#38287749)
From the title i would have guessed "Use more ifs and less fors" and it didnt make sense at the beginning
Re:Push ifs up and fors down by Tainnor on Thursday November 16, 2023 @06:29AM (#38288275)
No, I did understand the point the author was making. I just think it's a weak one.
Re:Push ifs up and fors down by joelthelion on Thursday November 16, 2023 @03:10AM (#38286903)
> The more experience I get, the more I feel that too many programmers worry about making things pretty "in the small", but not enough care about proper design of an entire codebase.

I've seen the reverse a lot, too. People who enjoy designing over-engineered cathedrals and can't be bothered to think about the low-level efficiency of their algorithms.

Re:Push ifs up and fors down by antupis on Thursday November 16, 2023 @03:34AM (#38287005)
I think this tells more about company culture than the people themselves, some companies like small things, and others like cathedrals.
Re:Push ifs up and fors down by maeln on Thursday November 16, 2023 @03:25AM (#38286968)
In general, I feel like too many developer get caught up in dogma, "best-practices" that are effectively often unrealistic, and tend to adopt extremist way of thinking about the big and / or the small picture. A lot of the "dev culture" feel like it lacks a sense of pragmatism.
Re:Push ifs up and fors down by berkes on Thursday November 16, 2023 @06:42AM (#38288367)
> People who enjoy designing over-engineered cathedrals and can't be bothered to think about the low-level efficiency of their algorithms.

When that's deliberate, I'd say it's good.

Quite often we need to optimize for maintenance. Or for high churn in developers. For readability. Adaptability. And so on. Each optimization is a trade-off. It's not always "performance vs readability", but quite often it is.

If we spend time and effort on the low-level efficiency of algorithms, AND that comes at the cost of readability, adaptability, maintainability or any other -ility, we should very deliberate make a choice: does performance matter more than X. The answer is suprisingly often "no". And it's one of the many reasons why today's software is slow, bloated, battery-eating and unstable.

Well, the times when that choice is not made deliberately are probably more to blame.

Re:Push ifs up and fors down by Tainnor on Thursday November 16, 2023 @06:52AM (#38288434)
"Over-engineered cathedrals" aren't what I mean by "good, proper design".

I hate huge, messy meta-frameworks where everything happens implicitly and you can debug nothing. But I hate piles of ad-hoc code where everything calls everything and you can't change a single thing without breaking 2000 other things just as well. You usually don't need complicated code constructs to organise your code well - but you do need to think.

Designing maintainable code is a difficult skill and one that involves careful thinking about tradeoffs. Unfortunately, mostly we as an industry never seem to be able to have time for it.

Re:Push ifs up and fors down by valand on Thursday November 16, 2023 @05:04AM (#38287658)
> but articles like this one are probably the things that can lead to the kind of unproductive bikeshedding that is sometimes experienced in PRs and other discussions

I'd counterargue that if these trivial principles are established beforehand, unproductive bikeshedding of this type will happen less on PRs and other discussions.

> as long as the function is properly named, has a good interface (including expressive types), a clear purpose, is properly documented, doesn't make excessive use of side effects etc.

In performance-sensitive software where DOD thrives, more care needs to be put into these "in the small" things because that is how compiler optimization works. Then, it changes the rule of the game: statement semantics become crucial, self-documenting code makes more sense, in-code comments are for reasoning exclusively, and "documentation" becomes "specification and user manual".

Re:Push ifs up and fors down by t8sr on Thursday November 16, 2023 @07:17AM (#38288634)
I work in security, and get to interact with a lot of area tech lead and architect types (L7-L9 in FAANG terms). I have found that the people who most concern themselves with “proper design” are generally about 5-10 years into their career and trying to prevent juniors from shooting themselves in the foot, but don’t yet have a proper appreciation for the cost of complexity and how code changes over the long run. Conversely, after you’ve been doing this for 20+ years you learn to value simplicity over most other technical factors.

Questions like “how early do we branch” and “what is the actual job this has to do” end up being the ones whose answers tend to have the most long-term value. Questions about abstractions and encapsulation tend to lead to the type of argument you describe. And the “the big picture” people are the ones with the most security issues in their code, because they don’t really understand what the properly architected codebase is really doing.

Re:Push ifs up and fors down by Tainnor on Thursday November 16, 2023 @08:44AM (#38289406)
> Questions like [...] “what is the actual job this has to do” end up being the ones whose answers tend to have the most long-term value.

How is that not "big picture", proper design and abstraction/encapsulation?

You seem to be arguing against a strawman. I didn't write "complexity". But if you want to fix security issues in your database access patterns, you better not have SQL distributed across your whole codebase.

Re:Push ifs up and fors down by t8sr on Thursday November 16, 2023 @04:34PM (#38295884)
I don't think I am. You seem to disagree that things like where to branch are worth talking about, yet you haven't given any examples of what you mean by the big picture that is worth talking about. In this thread, you repeatedly tell people they don't understand what you're saying - so what are you saying?

Re: SQL, maybe? Maybe not? It's a lot easier to do bad things to a database if you let an ORM generate the query. Writing SQL directly is not an anti-pattern, as long as you parametrize the query. But then again, maybe an ORM is not what you're suggesting - once again, you're saying what you don't like, and others are left to draw conclusions about what you think is good style.

Re:Push ifs up and fors down by Tainnor on Thursday November 16, 2023 @07:55PM (#38297860)
1. Yes, I happen to think that the difference between this:

  fn f() {
    if foo && bar {
      if foo {

      } else {

      }
    }
  }
and this:

  fn g() {
    if foo && bar {
      h()
    }
  }

  fn h() {
    if foo {

    } else {

    }
  }
is not worth talking about.

2. If we're talking about things like validate user input at the boundaries (and if you're a library developer, "user input" might refer to what the library users will pass in to your code), then I think that is a valid and useful guideline. If we talk about "don't spend time executing a bunch of code when you can exit early", then I'll also agree, although in most contexts, this doesn't really matter at the granularity of functions - but it does matter that you e.g. reject invalid input early on in your request handling (assuming a web app here) and not start processing it before deciding that you don't need to (with the notable exception of password validation, which should always be constant time).

3. What I mean by "big picture" is things like clearly delineating which parts of the codebase are responsible for what - for example, which parts of the code contain logic related to the database, which parts are related to business logic, which parts to interfacing with external APIs, which parts about handling responses. It's things such as "functional core, imperative shell" (Gary Bernhard) or other means to try to manage side effects. It's deciding whether to write blocking or non-blocking code, and in the latter case, how exactly to go about it (instead of making a total mess by mixing both styles with a lot of back and forth). It's about deciding on how you want to structure your test suite so it gives you the maximum amount of confidence.

4. I think ORMs provide mostly false confidence and outside of prototype scenarios, I wouldn't personally use them anymore - especially not ones like Hibernate and ActiveRecord - but, alas, it's not always my decision. Their biggest flaw is IMHO that they pollute your domain model (and sometimes even your presentation logic - looking at you, ActiveRecord) with database logic, just for the convenience of being able to call "save()" directly on an object. I don't terribly mind writing raw SQL (as long as you properly parameterize inputs), although I think that generally, type-safe query builders with a 1:1 relationship to SQL (such as jOOQ) are preferable. I do think that all of your SQL-related logic in your application shouldn't be scattered randomly across 10,000 files - it should be in a dedicated place (a number of files grouped in a common folder/package/module, maybe) so that if you need to review certain aspects of it (related to security or performance, e.g. avoiding N+1 queries), you don't have to hunt that logic down throughout your whole application.

Re:Push ifs up and fors down by xtracto on Thursday November 16, 2023 @10:55AM (#38291088)
This resonates with my experience. Apparently now the "inversion of control", "dependency injection" patterns are in vogue. So my Sr. Developers are building new code with all that crap. So much that when you want to make a simple change, you have to hunt down 20 files of `xxxCreator`s, `xxxHandler`s and `xxxProvider`s that are glorified functions invoked by `.execute` or `.call`.

And this is in TypeScript ... we were laughing about that kind of complexity in Java 20 years ago... in Slashdot.

Re:Push ifs up and fors down by valand on Friday November 17, 2023 @05:45AM (#38301795)
I always deplore this shallow influencing of design patterns without proper examination of whether said design pattern fits or is even correctly described by the popularizer. People fail to realize that inversion of control is necessary ONLY if some controls need to be inverted.

To be fair, "inversion of control" is a great concept, but unfortunately told in a very specific way across time so the meaning now has changed into an inferior form of the original.

Re:Push ifs up and fors down by Tainnor on Friday November 17, 2023 @04:39PM (#38310803)
I do personally think it's preferable for components to have their dependencies injected in some way, and that it's better to have initialisation logic (especially related to heavy weight components) in one place.

That doesn't mean that you need some huge ass meta-framework for it though. Just have your main function assemble the components together, done.

Re:Push ifs up and fors down by valand on Friday November 17, 2023 @09:22PM (#38314194)
Even this is an over simplification of inversion of control.

Dependency injection is one form, but even a library that exposes parameters is also a form of inversion of control.

The degree of control is what varies, whether it is a mere flag (boolean), a variant directive (complex struct), a turing complete construct (pointer of instantiated function) or anything in between.

What I am saying is that this concept has been reduced as it is popularized and it isn't doing justice to the original powerful concept.