skip to Main Content

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

https://www.jetbrains.com/help/resharper/File_and_Type_Layout.html#configuring-file-and-type-layout-rules-visually

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

https://www.jetbrains.com/help/pycharm/viewing-structure-and-hierarchy-of-the-source-code.html#ws_build_hierarchy

  • Coding standards

Python PEP 8 and vertical distance for function definitions

2

Answers


  1. Chosen as BEST ANSWER

    AI models like ChatGPT seem to be able to sort functions by their usage. I just used the tutorial at https://learn.deeplearning.ai/chatgpt-building-system to try following prompt:

    message = '''
    Sort the functions of the following class by their usage and 
    return a new version of the class:
    
    class A:
        def main(self):
            self._function_1()
            self._function_2()
            self._function_3()
    
        def _function_3(self):
            print("Hi 3!")
    
        def _function_2(self):
            print("Hi 2!")
    
        def _function_1(self):
            print("Hi 1!")
    
    '''
        
    response = get_completion(message)
    print(response)
    

    and the correct result was

    class A:
        def main(self):
            self._function_1()
            self._function_2()
            self._function_3()
    
        def _function_1(self):
            print("Hi 1!")
    
        def _function_2(self):
            print("Hi 2!")
    
        def _function_3(self):
            print("Hi 3!")
    

    Unfortunately, I am currently not allowed to use ChatGPT from withon my IDE. Thereofore, I am still looking for a "classical" solution that can be included in PyCharm or VsCodium without sending data to a remote server.

    I guess, that tools like GitHub Copilot or Refact.ai will allow to sort the functions from within the IDE, soon (or already do so).

    https://marketplace.visualstudio.com/items?itemName=smallcloud.codify


  2. As an alternative to actually rearranging the code, showing the Call Hierarchy (Ctrl + Alt + H in PyCharm) for Callees might be helpful.

    The Call Hierarchy is available without starting the debugger (once the indexing has been finished).

    In order to navigate to a called function, one can expand a called function and click on its first entry (="grand child") in the Call Hierarchy View.

    (Navigation is of course also possible with Ctrl + click on a function inside the editor.)

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search