news.volyx.in

Push ifs up and fors down (matklad.github.io)

662 points by celeritascelery · 1011 days ago · 295 comments on HN

Article summary

The article discusses two rules of thumb for coding: pushing if conditions up to the caller and pushing for loops down to operate on batches of objects. This can lead to fewer checks, centralized control flow, and improved performance. The article also notes that these rules can be composed together for even better results. The goal is to make code more efficient and easier to understand.

Main themes

  • coding best practices
  • performance optimization
  • maintainability
  • rules of thumb
  • context-dependent decision making
  • expertise and experience

What commenters say

  • Following rules of thumb without considering context can lead to poor coding decisions.
  • Rules of thumb can be helpful for junior programmers, but experts know when to apply them and when to break them.
  • Premature optimization can sacrifice readability and maintainability for potentially negligible performance gains.
  • Compilers and CPUs can often optimize code, but they cannot account for domain knowledge or high-level inefficiencies.
  • Code should be written with maintainability and flexibility in mind, rather than just performance.
  • Heuristics and rules of thumb can be useful, but they should be applied with nuance and consideration of the specific problem.
  • Experts develop their own heuristics and rules of thumb through experience and practice, rather than relying on shortcuts or general advice.