I thought I would put together some useful programming tips that I have learned over the years. Most of these are general tips, but they are tailored towards Python.
- Zen of Python. Even if you don’t use Python, these are good ideas to internalize.
- The language documentation (Python’s standard library), StackOverflow, and Google searches are your best friends.
- Utilize modern IDEs (like Spyder for Python) and tab-completion to reduce the number of basic errors.
- Comments are not optional. The general logic of functions and objects should be understandable from the comments. Every block of code logic should have a short comment to aid future changes. If you find a chunk of code confusing now, it will be just as confusing if not worse in the future!
- Use sensible variable names. This cuts down on the number/length of comments.
- Try to adhere to the language standards (Python’s), but don’t obsess over it.
- Set your own consistent standards (Do variable names end in s or not? Do boolean variables have similar style names? Etc).
- When starting a project, do you best to get quickly get up to a basic working prototype. Working but incomplete code is always better than non-working code. Quick coding is aided by the next point…
- Outline your code before starting. My tips for outlining in Python are detailed after this list.
- Write modular code. Common tasks should be made into functions or objects.
- Avoid magic numbers and hard coded values. Better to include a set of named parameters in one section of your code where the basic logic of these variables is explained.
- Avoid multiple inheritance (check out this fun explanation of why this is bad).
- A program should have a standard interface, I like to call it main, and a way to run the standard interface with some default values. In Python, utilize if __name__ == ‘__main__’: to define standard parameters and then call main(parameters). This aids the goal of always having working code, as well as making it easier to interact with different programs.
- Check out these Python tricks (1-23 are the best, rest are more advanced).
Here are details on how I outline code in Python. I try my best to have running code at all times, even if it does absolutely nothing. If it isn’t real code, I leave it as a comment. Therefore, my programming tends to proceed as follows.
- Outline the general logic of the code in comments. Define needed functions, but at first have it take no actual variables (utilize pass to keep it as functioning Python code). In the comments inside a function, list the data type you think it should take in, what it should do, and what it should return.
- If you start to code a function or series of logic, you can safely leave it incomplete by having it raise NotImplementedError.
- Use assert to check any of your assumptions. A custom assert statement will save you lots of time later.
- While the Pythonic way is to utilize duck typing, I still prefer to do some type checking if there is potential for confusion. So I like to utilize things like isinstance or implement checks on attributes.
- Take advantage of your IDE’s additional formatting options. For example, Spyder specially highlights comments that start with TODO: with a little checkmark. Additionally, it supports code blocks and defines them by #%%. This lets you quickly run small chunks of a larger code.
What is the major advantages of coding like this?
- If your code always runs, it allows you to quickly find syntax errors and typos.
- You avoid implementing unused code. It sucks to really work on a code section to only realize later that you didn’t actually need it.
- You spend your time on the standard case and can add certain options or take care of edge cases when the appropriate time arises. Because sometimes that time will never arise…
Anyways, enough advice, start coding!