Hacker News new | past | comments | ask | show | jobs | submit login

IMO that's anti-pattern / code smell, in most situations.



As long as we're talking about libraries, yes.

For cli tools, on the other hand, there are definitely legitimate cases for exiting early.


Sure, but even in most of those scenarios, I’d prefer to structure the code differently... perhaps write a main() function that just returns, or other functions... more modular, maintainable, teatable, ... admittedly, in Python you need to use os.exit if you want to return a specific non-zero exit code, but even in that case good code style would be something like

  def main()
    blablabla
    if foo:
      return bar
    ...
    return 8
  
  if __name__ == “__main__”:
    os.exit(main())
Of course that’s just my advice / opinion / preference, other ways are valid as well...


You are right, as for example the tests would behave in an erratic way. It should be allowed to complete.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: