New Year Resolution: Code Cleanup
I enjoyed Ethan Banks' post on New Year's Thoughts: Start with Documentation, so I thought I'd write about what I'm doing this week: code cleanup. Over the last couple of years I've written a decent amount of code to automate mundane network management tasks. As quick one-off hacks have turned into things that I actually depend on, I've noticed a lot of ugliness that I want to fix.
Everything here is assuming Python as the language of choice:
Everything here is assuming Python as the language of choice:
- For scripts that send email, check to make sure the list of mail receivers is up-to-date.
- Look for those nasty embedded IP addresses and replace them with DNS names.
- Change from old-style open(FILE)/close(FILE) constructs to with open(FILE) as f constructs.
- Get rid of "pickles" for persistent structured data storage. Pickles are a form of native Python object serialization that are quick and convenient, but have a lot of potential problems. I've mostly used Python's native SQLite3 library to store data in simple persistent databases, but occasionally I just use plain text files.
- Look for repetitive code in the main script logic and try to move it into functions or classes where possible. For example, I had several scripts that were building email reports via clunky string concatenation, so I created a Report class that knows how to append to itself and do simple formatting.
- Remove unused module imports (that were usually there for debugging).
- Standardize module imports, declaration of constants, etc.
- Add more comments!
- Remove old code that was commented out for whatever reason.
- Look for places where I was creating huge lists in memory and try to figure out a way to reduce memory consumption with generators.
- Change to new-style string formatting.
- Write some tests, particularly for log parsers.
- Migrate from optparse to argparse for handling CLI flags.
- Migrate to Python 3.