Programming‎ > ‎Python‎ > ‎

22-Effective Coding


1. Do not throw exceptions across the functions, they are killer non-debug-able.

The foo4() function when catches the exception, there is no clue where the exception is thrown.
Such exception cascading would be nightmare in production.

Another problem is leaked handle since exception handler is not present within function, instead out of scope of function.

Do try\except within a function and function should return bool to represent success\failure.

def foo():
    
    tmp = None
    
    tmp['hello'] = 'xyz'
    
    print(str(tmp['hello']))
    
  
    
    
def foo2():
  foo()
  
def foo3():
  foo2()
  
def foo4():
  try:
    
    foo3()
  except Exception as e:
    print("e : " + str(e))
    
  
foo4()


Run:
Python 3.5.2 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
   
e : 'NoneType' object does not support item assignment
   





Comments