DeltaE / Canada-U.S.-ElecTrade

The Role of U.S.-Canada Electricity Trade in North American Decarbonization Pathways
2 stars 0 forks source link

Un-nest heavily nested for-loops #129

Open Kiel-SFU opened 2 years ago

Kiel-SFU commented 2 years ago

Generally, nesting more than a few for-loops is considered bad practice in Python because it is known to imply that a) there might be a more efficient way of doing it (for example, through itertools), or if not a), then that b) the code is probably difficult to read.

Because Pylint is now enforcing a max line length of 90 in the codebase, it has become more relevant to reduce line length. To make lines less long on average, generally you would do something like this:

for a in ax:
   for b in bx:
      for c in cx:
         for d in dx:
            // do something in d
         // do something in c
      // do something in b
   // do something in a

would become

for a in ax:
   for b in bx:
      loop_over_c()
      // do something in b
   // do something in a

loop_over_c(){
for c in cx:
   for d in dx:
      // do something in d
   // do something in c
}

We should consider looking for places in the code where this sort of change could be applied, such as in capacity_factor.py.