a) The book Clean Code
from Robert C. Martin suggests to sort functions according to the "step down rule":
We want the code to read like a top-down narrative. We want every function to be followed by those at the next level of abstraction so that we can read the program, descending one level of abstraction at a time as we read down the list of functions. I call this The Step- down Rule.
b) PyCharm allows to show functions in alphabetical order in the Structure View. Also see Can PyCharm sort methods alphabetically? Therefore, there is no need to manually sort functions in alphabetical order and I can use the order of functions in a class for a different purpose.
c) Until now I manually sort them by their usage/abstraction-level/call stack. (I also put static and public functions on top and functions starting with "_" below. I put properties below functions.) However, some colleagues in my team are not aware of that order or might follow a different order.
d) Instead of manually sorting functions by their usage/abstraction-level/call stack, I would prefer a tool similar to black
(unfortunately, black does not sort functions), doing that formatting/sorting work automatically for us on save actions. (The order of the corresponding unit-tests should be the same as the order of the functions.)
=> How can I achieve that in PyCharm or VsCodium? Is there some code/plugin I could start with, already knowing about the usage order/calling tree?
=> Do you have rules in your team regarding the order of the functions (without enforcing them by tooling)? Or do you just put them in arbitrary order?
Related:
- Edit code programmatically
Parse a .py file, read the AST, modify it, then write back the modified source code
https://libcst.readthedocs.io/en/latest/tutorial.html
https://github.com/python-rope/rope
https://redbaron.readthedocs.io/en/latest/
- IDE features
How to tell PyCharm to put generated functions below the current function instead of above?
Can PyCharm sort methods alphabetically?
https://youtrack.jetbrains.com/issue/PY-13453/Ability-to-rearrange-python-code
How to reorder methods in PyCharm
Simple way to reorder methods of a Java class in IntelliJ?
https://github.com/psf/black/issues/3029
https://plugins.jetbrains.com/plugin/9450-code-blocks-sorter
https://github.com/osimek1/intellij-code-blocks-sorter/issues/15
Call Hierarchy in Visual Studio Code
- Coding standards