Skip to content

Coding guidelines

DaceyTom2 edited this page Oct 20, 2020 · 20 revisions

Welcome to the openfisca-canada-dts Coding Guidelines wiki!

Enum standards

  • Enums live with their variables, unless they are common throughout the project
  • Class names camel case
  • Enum in all caps
  • Value human readable
  • Enums used for the OF enum approach all include unknown as an option
  • Enum is the standard approach for benefit finder over booleans and strings

Clarification: The IncomeStatus enum can live with the income_status variable in the same .py file because they are a 1 to 1 relationship. However, BooleanEnum should not live with cerb_is_eligible because many places will use the BooleanEnum

example

class IncomeStatus(Enum):
   ALL_INCOME = "lost all income"
   SOME_INCOME = "lost some income"
   UNCHANGED_INCOME = "lost no income"
   UNKNOWN = "unknown"

Variable standards

Variable name standards

  • Variables are named using snake case
  • Top level eligibility variables are named is_eligible prefixed with benefit named followed by two underscores
  • Regular variables should have specific and clear names for their purpose
Variable name examples
class benefit_name__is_eligible(Variable):
class this_is_a_clear_variable_to_demonstrate_snake_case(Variable)

Variable example

https://openfisca.org/doc/openfisca-python-api/variables.html

class benefit_name__is_eligible(Variable):
    value_type = Enum
    entity = Person
    definition_period = MONTH
    label = u"is person eligible for benefit"
    reference = "tbd"
        def formula(persons, period, parameters):
            return persons('this_is_a_clear_variable_to_demonstrate_snake_case', period)

Folder structure standard

  • Folders are named using snake case
  • Folders are to follow an intuitive pattern to allow variables to be easily reused
Folder naming example
-> benefit_eligiblity
---> emergency_relief
-----> cerb__is_eligible.py
-----> crb__is_eligibility.py
---> emplyoment
---> student
-> country
---> age
----->country_age.py
---> country_population
---> provinces
-----> province_population
-> person
---> age
-----> person_age.py

Unit test standard

  • Tests folder structure should mirror variables
  • Unit tests are written in a specific way to utilise the OpenFisca framework using yaml
  • Minimum 80% coverage
yaml unit test example

https://openfisca.org/doc/openfisca-python-api/openfisca_test.html

    - name: "Example test"
      period: 2020-09
      absolute_error_margin: 0
      input:
        example_input: true
      output:
        example_output_is_eligible: true

Reform

To be defined...