Structure of Code is Key

Thanks to the way imports and modules are handled in Python, it isrelatively easy to structure a Python project. Easy, here, meansthat you do not have many constraints and that the moduleimporting model is easy to grasp. Therefore, you are left with thepure architectural task of crafting the different parts of yourproject and their interactions.

Easy structuring of a project means it is also easyto do it poorly. Some signs of a poorly structured projectinclude:

  • Multiple and messy circular dependencies: if your classesTable and Chair in furn.py need to import Carpenter fromworkers.py to answer a question such as table.isdoneby(),and if conversely the class Carpenter needs to import Table and Chairto answer the question carpenter.whatdo(), then youhave a circular dependency. In this case you will have to resort tofragile hacks such as using import statements insidemethods or functions.
  • Hidden coupling: each and every change in Table’s implementationbreaks 20 tests in unrelated test cases because it breaks Carpenter’s code,which requires very careful surgery to adapt the change. This meansyou have too many assumptions about Table in Carpenter’s code or thereverse.
  • Heavy usage of global state or context: instead of explicitlypassing (height, width, type, wood) to each other, Tableand Carpenter rely on global variables that can be modifiedand are modified on the fly by different agents. You need toscrutinize all access to these global variables to understand whya rectangular table became a square, and discover that remotetemplate code is also modifying this context, messing withtable dimensions.
  • Spaghetti code: multiple pages of nested if clauses and for loopswith a lot of copy-pasted procedural code and noproper segmentation are known as spaghetti code. Python’smeaningful indentation (one of its most controversial features) makeit very hard to maintain this kind of code. So the good news is thatyou might not see too much of it.
  • Ravioli code is more likely in Python: it consists of hundreds ofsimilar little pieces of logic, often classes or objects, withoutproper structure. If you never can remember if you have to useFurnitureTable, AssetTable or Table, or even TableNew for yourtask at hand, you might be swimming in ravioli code.