Skip to content

Reduce the number of magic string constants in code #669

@andrewlee94

Description

@andrewlee94

From a comment in #620:

@dangunter:

There are lots of magic string constants like "mw". imho, most of these should be defined as constants. I tend to create a class (e.g., > Constants) and define class variables with same/similar names, so:

param_dict = {"mw": base_units["mass"]/base_units["amount"]}
would become

class Constants:
mw = "mw" #: this is also an opportunity
mass = "mass" #: to comment on what these
amount = "amount" #: actually 'mean'.

param_dict = {Constants.mw: base_units[Constants.mass]/base_units[Constants.amount]}

If the wordiness bothers you, you could alias the class to a short abbreviation like _C

In the snippet of code above, there are actually two sets of magic string constants and I think that if we were to implement thus we would want to create separate classes for each:

  1. A set of standard names for properties (probably part of property_base.py)
  2. A set of recognized units of measurement (probably part of property_meta.py)

Do others have thoughts on this, and how important do we think this is?

Metadata

Metadata

Labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions