rap-community-of-practice icon indicating copy to clipboard operation
rap-community-of-practice copied to clipboard

Clean code guidance

Open SamHollings opened this issue 2 years ago • 2 comments

some teams want to use clean code - we need guidance on the best way to approach this for analytical code, why you would want to do it, and what to watch out for.

SamHollings avatar May 25 '22 14:05 SamHollings

Some topics that could come up with regard to this (specifically with a python focus)

  • Tidy data https://r4ds.had.co.nz/tidy-data.html
  • How long should a function be? (Single responsibility principle)
  • Variable naming
  • Extracting magic numbers to a separate variable
# don't do this
if df_by_respondent["q23"] == 4:
    pass

# do do this
ANSWERED_NOT_APPLICABLE = 4
if df_by_respondent["q23"] == ANSWERED_NOT_APPLICABLE:
    pass
  • The principles of self documenting code
  • Type hints?
  • Don't repeat yourself
  • Using a params.py file
  • How long should a file be?
  • Test driven development

adam-carruthers avatar May 25 '22 14:05 adam-carruthers

This is in our backlog (https://nhsd-jira.digital.nhs.uk/browse/NV-1305)

SamHollings avatar Jul 12 '22 09:07 SamHollings