Push ifs up and fors down

The author provides two rules of thumb in this short note. The first rule suggests that if there is an “if” condition inside a function, it should be considered whether it can be moved to the caller instead. The author explains that by pushing the “if” condition up to the caller, it can result in fewer checks overall and centralize control flow in a single function, making it easier to notice redundancies and dead conditions. The second rule advises pushing “for” loops down and introducing the concept of a batch of objects to improve performance. The author highlights that this approach can amortize startup costs and allow for flexible processing orders. The author concludes by emphasizing the benefits of adhering to these rules and suggests that they can be applied at both micro and macro levels.

https://matklad.github.io/2023/11/15/push-ifs-up-and-fors-down.html

To top