Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

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?

share|improve this question

4 Answers 4

up vote 5 down vote accepted

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.)

share|improve this answer
OK. It seems that the ordering is not important for readability because of "jumps to definition". –  xralf Mar 5 '12 at 8:57

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.

share|improve this answer
Yes, scrambled functions (if one function is called many times) are problem in this ordering. –  xralf Mar 5 '12 at 8:56

Maybe it's force of habit from C coding 20+ years ago, but, no matter what teh 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.

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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