3

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

Stefan
  • 10,010
  • 7
  • 61
  • 117
  • if `black` can do it set it as the Python formatter for VSC – rioV8 Jul 04 '23 at 10:35
  • Unfortunately, it cannot. I already use black with PyCharm. – Stefan Jul 04 '23 at 10:35
  • why not write the formatter, or a new command, that does this, you will find it is not trivial to write, be aware of decorators, and local functions – rioV8 Jul 04 '23 at 10:38
  • Then the interim answer seems to be, that there is no tooling support for the "step down rule" because it would be too complex to implement. In that case it might make no sense following that rule .... because the chances are high that the order will be lost anyway when working with multiple persons in a team (?) – Stefan Jul 04 '23 at 10:50
  • 1
    why the close votes (tool recommendation)? looks like a legitimate question to me. From my quick ctrl+f, this question post never at any point even asked for an extension. – starball Jul 04 '23 at 18:49
  • JetBrains IntelliJ for Java supports this. – Wouter Lievens Jul 06 '23 at 17:36

2 Answers2

0

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

Stefan
  • 10,010
  • 7
  • 61
  • 117
  • "Seem" is the operative word here. I wouldn't even trust ChatGPT to return semantically equivalent code, let alone code with individual methods sorted in any sensical way. – chepner Jul 06 '23 at 18:31
  • Still looking for an IDE integration/automation. – Stefan Aug 04 '23 at 08:46
0

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

Stefan
  • 10,010
  • 7
  • 61
  • 117
  • This is not an actual answer but a comment/workaround. Still looking for a solution to rearrange the code. – Stefan Jul 10 '23 at 07:03