When I work on some programming project (usually command line application in Python with many switches), I'm usually creating about 30 and more functions. Most of the functions are in one file (except some helpers that I utilize in more projects).

Some of the functions are called on particular switch (like -p or --print) but many functions do some helper computations, print operations or database operations because I don't want to main functions be too large.

When I have an idea for a new functionality I often put new functions randomly to the file. Should I think more about it and place it to some particular place? Are there some methods for this?


5 Answers 5


These days, with IDE support for navigating stack traces and finding callers, I find it almost doesn't matter at all which order my methods come in. There is a slight benefit to having a small helper function near the place where it's called, particularly when it's called only in one place, but it's much more important to break your module into small functions in the first place than to order them sensibly. Trying to find the 'optimal' arrangement is almost certainly not worth your (pretty expensive, I hope) time. (Note also that automatically generated APIs are usually sorted alphabetically anyway, so whatever you do within your module doesn't affect others, either.)

  • OK. It seems that the ordering is not important for readability because of "jumps to definition".
    – xralf
    Commented Mar 5, 2012 at 8:57
  • Navigating through the code is not the only reason to arrange your code. Otherwise you could throw a lot of good coding style through the window as well. Note that everyone doesn't always use your favourite IDE.
    – skyking
    Commented May 30, 2019 at 7:51

I prefer the Clean Code version. If you start reading a function, each new self-implemented function call should be directly under it. For example like so:

func Foo()

func func1()

func funcWithFuncs()

func funcA()

func funcB()

func func2()

That way, you can easily track which function is where even with functions that have more functions. Also, it doesn't require a call stack and running the program to easily find where everything is. Of course, this is a "taste" thing, but I haven't found Clean Code to be wrong on this point yet.

In contrast, I hate it when functions are scrambled because I DON'T KNOW WHEN TO STOP SCROLLING; I might miss it entirely, which happens more often than I want with larger code files.

  • Yes, scrambled functions (if one function is called many times) are problem in this ordering.
    – xralf
    Commented Mar 5, 2012 at 8:56
  • Oof. I googled this question because I started writing in C99 Commented Apr 3, 2019 at 4:29
  • 2
    Amazing answer. Do you if this pattern to declare functions have a name? It highly improves the readability. Commented Aug 6, 2020 at 20:10

Maybe it's force of habit from C coding 20+ years ago, but, no matter what the language, I usually let my small helper functions float to the top.

At the very least, I like to have every function declared before it is called, in order to obviate the need for forward declaration/function prototypes.

I guess that makes me the exact opposite of @IAE (+1 to him anyway ;-), although I do sometimes exactly mirror his approach, having helpers directly above the function which calls them.

If helpers are specific to one function and the language allows nested functions, there is much to be said for nesting the helpers inside of the function which utilizes them.

  • 2
    I wonder if there's a program out there that automatically reorders the functions based on function dependencies lol Commented Apr 3, 2019 at 4:34
  • 1
    You could ask on our Software Recommendations sister site
    – Mawg
    Commented Apr 28, 2020 at 8:52

In an Object Oriented language it is important that you can scan a class's data and public interface easily. On a code file level that means you put your encapsulated data first, immediately followed by constructors and public members. Anything protected or private is only interesting when you have to dig into the code rather than just understand and use it, so the protected and private parts (haha) go way down (haha). You can collapse those and keep a nice picture of the class on a functional level.

I am a C# developer using StyleCop, a tool that helps you maintain a consistent order with the above stated reasons in mind. I imagine there will be similar tools for Python too.


I prefer the iterative refactoring approach. While we are developing the application usually our focus would be on the functionalities. Once, there is a code we could look at it and refactor it to make it more readable.

To answer your question on approach (methods for deciding where to place a code) - Ask the questions that you will ask while refactoring for the new functionality or method that you are adding to the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.