Saturday, March 9, 2019

A Reminder: Best Coding Practices

You are human. You will make mistakes. You are human. You will forget things. 

Maybe my best advice is just to recite those four short sentences out loud every time you open Stata.  But what does your fallibility imply for how you should code? Tal Gross has some excellent rules of thumb. I will summarize them below in case the link ever stops working.
  1. Use sensible names for variables and dofiles. For example, instead of calling a new variable "sex", call it "female". 
  2. Comment everything! //You won't regret it! 
  3. Make code readable. Put spaces before and after "+" and never ever put anything after a { or }. Go to the next line immediately. 
  4. Create sections with ***************. 
  5. Make code portable by making appropriate use of folders. 
  6. Check your work. No, this doesn't mean reading lines and lines of code over and over again. It means things like summarizing variables right after creating them. Anything suspicious? 
  7. Use a template. 
  8. Preserve source data. Never ever change the original data source ever. Create new data files. 
  9. Don't repeat yourself. Speaking of repeating myself, many of these tips are sounding familiar. I think I have blogged about this before. Yes, I have! Read here. That's Ok. These tips are worth repeating. Lines of code are not!

No comments:

Post a Comment