News Score: Score the News, Sort the News, Rewrite the Headlines

Push Ifs Up And Fors Down

Push Ifs Up And Fors Down Nov 15, 2023 A short note on two related rules of thumb. If there’s an if condition inside a function, consider if it could be moved to the caller instead: // GOOD fn frobnicate(walrus: Walrus) { ... } // BAD fn frobnicate(walrus: Option<Walrus>) { let walrus = match walrus { Some(it) => it, None => return, }; ... } As in the example above, this often comes up with preconditions: a function might check precondition inside and “do nothing” if it doesn’t hold, or it could...

Read more at matklad.github.io

© News Score  score the news, sort the news, rewrite the headlines