Why should functions always return the same type?

前端 未结 7 2040
野性不改
野性不改 2020-12-01 03:31

I read somewhere that functions should always return only one type so the following code is considered as bad code:

def x(foo):
 if \'bar\' in foo:
  return          


        
7条回答
  •  情书的邮戳
    2020-12-01 04:08

    Premature optimization is the root of all evil. The minuscule efficiency gains might be important, but not until you've proven that you need them.

    Whatever your language: a function is defined once, but tends to be used at any number of places. Having a consistent return type (not to mention documented pre- and postconditions) means you have to spend more effort defining the function, but you simplify the usage of the function enormously. Guess whether the one-time costs tend to outweigh the repeated savings...?

提交回复
热议问题