Hacker News new | ask | show | jobs
by empath75 2506 days ago
in python:

  def getSocketAPIPort(defaultport):
       try:
           return os.environ["socketPort"]
       except KeyError:
           return defaultport
1 comments

Close, but values from os.environ are always string, and not guaranteed to be parsable numbers.
This is a bit clearer:

    def getSocketAPIPort(defaultport): 
      return int(os.getenv('socketPort', defaultport))
Closer, but int can throw a ValueError. The original Haskell code defaulted to the defaultPort if the specified port could not be cast to an int.
I see. Though I think it's better to throw an error. I wouldn't want my app just coming up on default port if I had configured it incorrectly.
It might be in Python, but it's sorta oranges to apples for you to just not do the same thing your Rosetta code is supposed to do.

What's the value of your exercise if you just redefine the problem to get rid of the tricky part?

I’d say the original code was wrong. anyway catching an exception isn’t tricky.