frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

Push Ifs Up and Fors Down

https://matklad.github.io/2023/11/15/push-ifs-up-and-fors-down.html
25•goranmoomin•4h ago

Comments

esafak•37m ago
I agree, except for this example, where the author effectively (after a substitution) prefers the former:

    fn f() -> E {
      if condition {
        E::Foo(x)
      } else {
        E::Bar(y)
      }
    }
    
    fn g(e: E) {
      match e {
        E::Foo(x) => foo(x),
        E::Bar(y) => bar(y)
      }
    }
The latter is not only more readable, but it is safer, because a match statement can ensure all possibilities are covered.
password4321•36m ago
Code complexity scanners⁰ eventually force pushing ifs down. The article recommends the opposite:

By pushing ifs up, you often end up centralizing control flow in a single function, which has a complex branching logic, but all the actual work is delegated to straight line subroutines.

⁰ https://docs.sonarsource.com/sonarqube-server/latest/user-gu...

daxfohl•3m ago
IME this is frequently a local optimum though. "Local" meaning, until some requirement changes or edge case is discovered, where some branching needs to happen outside of the loop. Then, if you've got branching both inside and outside the loop, it gets harder to reason about.

It can be case-dependent. Are you reasonably sure that the condition will only ever effect stuff inside the loop? Then sure, go ahead and put it there. If it's not hard to imagine requirements that would also branch outside of the loop, then it may be better to preemptively design it that way. The code may be more verbose, but frequently easier to follow, and hopefully less likely to turn into spaghetti later on.

(This is why I quit writing Haskell; it tends to make you feel like you want to write the most concise, "locally optimum" logic. But that doesn't express the intent behind the logic so much as the logic itself, which can lead to horrible unrolling of stuff when minor requirements change. At least, that was my experience.)

stevage•28m ago
The author's main concern seems to be optimising performance critical code.
daxfohl•21m ago
I like this a lot. At first, putting ifs inside the fors makes things more concise. But it seems like there's always an edge case or requirement change that eventually requires an if outside the for too. Now you've got ifs on both sides of the for, and you've got to look in multiple places to see what's happening. Or worse, subsequent changes will require updating both places.

So yeah, I agree, pulling conditions up can often be better for long-term maintenance, even if initially it seems like it creates redundancy.

jmull•17m ago
I really don't think there is any general rule of thumb here.

You've really got to have certain contexts before thinking you ought to be pushing ifs up.

I mean generally, you should consider pushing an if up. But you should also consider pushing it down, and leaving it where it is. That is, you're thinking about whether you have a good structure for your code as you write it... aka programming.

I suppose you might say, push common/general/high-level things up, and push implementation details and low-level details down. It seems almost too obvious to say, but I guess it doesn't hurt to back up a little once in a while and think more broadly about your general approach. I guess the author is feeling that ifs are usually about a higher-level concern and loops about a lower-level concern? Maybe that's true? I just don't think it matters, though, because why wouldn't you think about any given if in terms of whether it specifically ought to move up or down?

dcre•8m ago
I took a version of this away from Sandi Metz’s 99 Bottles of OOP. It’s not really my style overall, but the point about moving logic forks up the call stack was very well taken when I was working on a codebase where we had added a ton of flags that got passed down through many layers.

https://sandimetz.com/99bottles

Why Are There So Many 'Alternative Devices' All of a Sudden?

https://www.theatlantic.com/technology/archive/2025/05/alternative-device-fair/682837/
1•fortran77•2m ago•0 comments

Share your AI agents with a simple link

https://nelly.is/news/share-agents-with-a-link
1•gitmagic•3m ago•0 comments

The 80% of UI Design – Typography [video]

https://www.youtube.com/watch?v=9-oefwZ6Z74
2•vinhnx•4m ago•0 comments

Apple's A.I. Ambitions for China Provoke Washington's Resistance

https://www.nytimes.com/2025/05/17/technology/apple-alibaba-ai-tool-china.html
2•mistersquid•5m ago•0 comments

Steepest Descent Density Control for Compact 3D Gaussian Splatting

https://arxiv.org/abs/2505.05587
2•PaulHoule•8m ago•0 comments

Analyzing, Predicting, and Controlling How a Reasoning Model Will Think

https://arxiv.org/abs/2505.10185
1•simonpure•10m ago•0 comments

Show HN: A MCP server to evaluate Python code in WASM VM using RustPython

https://github.com/tuananh/hyper-mcp/tree/main/examples/plugins/eval-py
1•tuananh•11m ago•0 comments

Ask HN: How to Foreshadow?

1•ksec•12m ago•0 comments

Orchestrator: The AI agent operating system

https://www.orchestratoros.com/
1•obitracks•14m ago•0 comments

Coming to a Brain Near You: A Tiny Computer

https://www.wsj.com/tech/brain-implant-musk-als-tbi-neuralink-f733998f
2•bookofjoe•19m ago•1 comments

Show HN: X-RAY – A student-built tool to audit OS behavior via ISO comparison

2•tangtian•21m ago•0 comments

Show HN: Insert-tools – bulk insert with schema check and column match in CH

https://github.com/castengine/insert-tools
1•castengine•22m ago•0 comments

Show HN: SRE Assistant – An AI-powered agent for Kubernetes and AWS operations

https://github.com/serkanh/sre-bot
1•serkanh•23m ago•0 comments

Community Stewardship of Faster CPython

https://discuss.python.org/t/community-stewardship-of-faster-cpython/92153
2•bratao•25m ago•0 comments

The Day Grok Lost Its Mind

https://www.nytimes.com/2025/05/17/opinion/grok-ai-musk-x-south-africa.html
4•mistersquid•27m ago•0 comments

Focus Is Relaxing

https://afarah.info/public/blog/focus/index.html?_r=hn
3•afarah1•27m ago•0 comments

Psychedelics May Shift Brain Power to the Right Hemisphere

https://neurosciencenews.com/psychedelics-right-hemisphere-28946/
2•nabla9•30m ago•1 comments

Windsurf SWE-1 AI coding model

https://swe-1.surf/
2•zoudong376•35m ago•1 comments

NHS rolls out 5-minute 'super-jab' for 15 cancers

https://www.england.nhs.uk/2025/04/nhs-rolls-out-5-minute-super-jab-for-15-cancers/
6•dsego•35m ago•0 comments

Debian 12.11 Released

https://www.debian.org/News/2025/20250517
2•midzer•35m ago•0 comments

FDA Clears First Blood Test to Diagnose Alzheimer's

https://www.bloomberg.com/news/articles/2025-05-16/fda-approves-first-blood-test-to-diagnose-alzheimer-s-disease
4•marc__1•43m ago•0 comments

The Common Lisp Cookbook: Scripting. Command Line Arguments. Executables

https://lispcookbook.github.io/cl-cookbook/scripting.html
3•Tomte•44m ago•0 comments

Quickemu: Quickly create and run optimised Windows, macOS and Linux VMs

https://github.com/quickemu-project/quickemu
3•Tomte•44m ago•0 comments

How Does Retrieval Improve New Learning? (2024)

https://www.learningscientists.org/blog/2024/3/7/how-does-retrieval-improve-new-learning
1•mooreds•45m ago•0 comments

Britain is now the biggest funder of solar-geoengineering research

https://www.economist.com/science-and-technology/2025/05/14/britain-is-now-the-biggest-funder-of-solar-geoengineering-research
1•helsinkiandrew•48m ago•1 comments

Life Is Short (2016)

https://paulgraham.com/vb.html
4•tzury•51m ago•1 comments

'Murderbot' is the best new comedy of 2025

https://text.npr.org/nx-s1-5393659
8•mooreds•52m ago•0 comments

HUD and DOI Open Under Utilized Federal Lands for Affordable Housing [video]

https://www.youtube.com/watch?v=Orp0KfBajJs
3•mooreds•52m ago•0 comments

O2 VoLTE: locating any customer with a phone call

https://mastdatabase.co.uk/blog/2025/05/o2-expose-customer-location-call-4g/
5•kragniz•52m ago•1 comments

The 'deprofessionalization of video games' was on full display at PAX East

https://www.gamedeveloper.com/business/-deprofessionalization-is-bad-for-video-games
5•amichail•53m ago•0 comments