Skip to content

Magic numbers #85

@mattagape

Description

@mattagape

Some of the Kaska code, e.g. watercloudmodel.py, and possibly other files, have what are termed "magic numbers". These are numbers which make the program work, but are not obvious why they have been chosen.

Here is an example from watercloudmodel.py's cost() function:

V1 = x[6:(6+n_obs)]
V2 = x[(6+n_obs):(6+2*n_obs)]
s = x[(6+2*n_obs):]

Here, the numbers 6 and 2 have been used, but it isn't clear why.

A second problem with magic numbers are that they could easily be mistyped, e.g. one of the 6 could have been typed as 5 or 7, and such an error could slip through.

A better approach is to use numerical constants which have meaningful names. So in the above example we might say:

meaningfully_named_constant_1 = 6
meaningfully_named_constant_2 = 2
V1 = x[meaningfully_named_constant_1:(meaningfully_named_constant_1 + n_obs)]
V2 = x[(meaningfully_named_constant_1 + n_obs):(meaningfully_named_constant_1 +  meaningfully_named_constant_2*n_obs)]
s = x[(meaningfully_named_constant_1 + meaningfully_named_constant_2*n_obs):]

where clearly better names would be chosen for the constants.

This approach (1) helps us see why particular numerical values are used, and (2) means we are less likely to mis-type the number. However, on this second point, I would say my suggested constant names above are two similar (the 1 at the end could be mixed up with a 2), but you get the idea.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions