Skip to content

Commit 5ff136b

Browse files
authored
Merge pull request #13 from gonuke/more_style
Added some more simple style guidance
2 parents 861800a + 6449212 commit 5ff136b

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

README.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,37 @@ if __name__ == "__main__":
4646

4747
### Variable naming
4848
* Generally, choose nouns for variables and verbs for methods
49+
* The most common approach to multi-word variables is "snake case": variables
50+
are in lower case with words separated by underscore
4951
* Clear variable and method names can reduce the need for comments
52+
* Carefully determine whether temporary variables are helpful. If you only use
53+
it once, consider if that line of code can be combined with the place it is
54+
used.
55+
* Avoid **Magic Numbers** - numerical constants without a clear purpose
56+
* provide numerical constants with a variable to describe their purpose
57+
* these can be physical constants (`AVOGADRO = 6.02e23`), unit conversions
58+
(`EV2MEV = 1e-6`), or vector indices (`z = 2`), among others
59+
* they are not generally needed for things like squaring a quantity or
60+
dividing by some integer that arises from algebra
61+
62+
### Data structure design
63+
* Take advantage of python's rich data structures and related methods
64+
* `dictionaries` are a preferred way to bind data together in a way that is
65+
clear, rather than parallel lists that are indexed in parallel
66+
* `numpy` arrays are frequently better choices than python lists for numerical
67+
data
68+
* when looping over iterables (e.g. lists, dictionaries, numpy arrays, etc),
69+
avoid an indexing variable if possible
70+
* `zip()` may be useful for iterating through multiple iterables with parallel
71+
indexing
72+
* `enumerate()` allows you to autogenerate an indexing variable, but make sure you need that index
73+
* iterating over a `range()` is probably a last resort, try iterating directly over
74+
the iterable or using `zip`
75+
* consider [list
76+
comprehensions](https://www.w3schools.com/python/python_lists_comprehension.asp)
77+
for simple operations
78+
* when using a loop variable, consider the same general guidance on variable naming;
79+
avoid overly simple loop variables, e.g. `(i,j,k)`, that have no semantic meaning
5080

5181
### Comments
5282
* Include a docstring in every method
@@ -55,7 +85,23 @@ if __name__ == "__main__":
5585

5686
### Modularity
5787
* If you have cut & paste code in two different places, it probably should be a
58-
method
88+
method/function (or in some cases a loop)
5989
* Even very short methods can be valuable if the method name makes the code more
6090
readable
6191
* Ideally, methods should be no longer than one screen worth of lines
92+
* Practice **Separation of Concerns**:
93+
* a single method should have a single purpose that is clear from the name
94+
* avoid any combination of reading, using and writing data in the same
95+
method
96+
97+
## Checklist
98+
99+
Before submitting a PR, ask yourself: "Have I...."
100+
* [ ] modularized my code into methods that each have a clear and
101+
singular purpose?
102+
* [ ] included a docstring for every method?
103+
* [ ] replaced all magic numbers with variables?
104+
* [ ] used method names (verbs) and variable names (nouns) that make the
105+
code clearly readable?
106+
* [ ] removed instances of copy/paste code or nearly identical sections of code?
107+
* [ ] made good data structure design choices?

0 commit comments

Comments
 (0)