Tuesday 30 September 2014

Bad Python

Don't create constants for a string literal if it is only used once. You aren't doing Java anymore.

Dont add __init__.py unless you need that dir to be a package.

If you access a property or dictionary reference 3 times or more pull it out into a variable.

Wednesday 3 September 2014

Bad database

SQL:

Don't use select * (start) when dealing with an SQL database. It is bad practice. 

Transactions:
Some python code I made a horrible error with one time:

try:
    with self._rw_engine.begin() as connection:
        rs = connection.execute("SQL")
        myobj.a = rs.get("a")
        myobj.b = rs.get("b")
        myobj.c = rs.get("c")
        # myobj loaded
except Exception:
    log.exception('Unhandled exception:')
    raise
else:
    myobj.load_more_from_db()

What is that exception doing there? Surely it is pointless to log it and reraise, why not remove it and put the load_more_from_db call at '# myobj loaded' comment.
Well myobj is created in a transaction and load_more_from_db will use a different transaction which led to a deadlock on certain tests. So watch your scope.