news.volyx.in

Push Ifs Up and Fors Down (matklad.github.io)

569 points by goranmoomin · 440 days ago · 197 comments on HN

Article summary

The article discusses two coding principles: 'Push Ifs Up' and 'Push Fors Down'. The first principle suggests moving if conditions to the caller function to simplify control flow and reduce bugs, while the second principle recommends operating on batches of objects instead of individual objects for better performance. By following these principles, code can become more efficient, readable, and maintainable. The article provides examples and motivations for these principles, including improved performance and reduced complexity.

Main themes

  • code readability
  • performance optimization
  • coding principles
  • code maintainability
  • complexity metrics
  • parallelization and vectorization
  • separation of concerns

What commenters say

  • Some commenters disagree with the 'Push Ifs Up' principle, arguing that it can lead to less readable code and more complexity in certain cases.
  • Others argue that code scanners and complexity metrics can be useful but should not be followed blindly, as they may not always reflect the best coding practices.
  • The 'Push Fors Down' principle is seen as a way to improve performance by reducing branching and allowing for better parallelization and vectorization.
  • There is a debate about the trade-offs between code readability, maintainability, and performance, with some arguing that simplicity and clarity should be prioritized over performance optimizations.
  • Some commenters suggest that separating decisions from actions can improve code quality and readability, but others argue that this can lead to overly complex code with many small functions.
  • The use of code formatters and scanners is seen as a way to enforce coding standards, but some argue that this can be overly restrictive and stifle creativity.
  • The importance of considering the specific use case and requirements when applying coding principles is emphasized, as what works well in one situation may not work in another.
  • There is a discussion about the role of experience and judgment in coding, with some arguing that senior developers should be able to make their own decisions about code complexity and readability.