palpen / grades_sum_stats

Stata program that calculates some summary statistics of class grades
0 stars 0 forks source link

coding tips #1

Open wbuchanan opened 8 years ago

wbuchanan commented 8 years ago

Just came across your repo and thought I'd offer some suggestions that could make your script more stable/flexible and follow typical coding practices in Stata.

  1. Rename grade_sum_stats.do to grade_sum_stats.ado. This will make it easier to pass parameters and for others to use the command.
  2. Include a version number after defining the program (e.g., version 13.1). The syntax for insheet changed a bit in version 14 and it might result in code breaking, while the version command will help to make sure the code is interpretted consistently across versions.
  3. What happens if the user doesn't have outreg2 available on their system? I created a utility for another program I was working on here that will install the program if unavailable (assuming it is available from the ssc). This can avoid problems with that (especially if you ever need to get a new machine or need the code to be portable).
  4. You can get rid of the first loop in the script using
ds, not(type string)
loc grade_vars `r(varlist)'
ds, has(type string)
loc string_vars `r(varlist)'

di as res `"The variables `: subinstr loc grade_vars `" "' `", "', all' are numeric.  And "' _n `"The variables `: subinstr loc string_vars `" "' `", "', all' are string variables"'
  1. If you include either the lower level commands to parse the parameters or the syntax command, you can allow users to pass values a bit more flexibly and consistently. (e.g., if the user is using file paths with embedded strings the insheet command will likely fail when it expands the local macros).
palpen commented 8 years ago

Thank you for these tips! Glad to know someone can actually see what I'm doing here. I've implemented some of them now and will include the rest soon. The script using the ds command to replace the first loop and using the syntax command to parse arguments are very useful.

wbuchanan commented 8 years ago

Not a problem. Every once in a while I try to check out who is posting Stata code on GitHub in the hopes that more of the user community will start using GitHub. I also figure it could be helpful since it doesn't seem like there are many places with a substantial group of Stata users (particularly when it is a space that I'm a bit more familiar with).

palpen commented 8 years ago

Yea, it definitely helps to have a more active user group online for Stata users. Most people in economics use Stata but very few use version control on their code. I think this is reasonable given that most code written by economist in Stata are used for a very specific application or for quick hypothesis testing. Thus, maintainability is not so important.