That particular example is slightly clearer with early return but only because it’s an artificially simple example. If it were slightly more complicated I can imagine the single exit version being significantly clearer.
About to say the same. Sometimes examples are too simple. Is there no real world code we can examine and refactor?
I wish I had a nickel for every tutorial or doc example that used “foo” or “bar” instead of a real-world(ish) example. I’d be rich and it’s awful and lazy.
I teach this stuff, so I see lots of code written by students that could be refactored to be much nicer, but really, it’s just not that hard to come up with more realistic examples at this level.
For a good example, see Trisha Gee’s series on JetBrains’ site. Posted to Lobsters: https://lobste.rs/s/nzntxj/code_smells_null.