Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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
add comment

2 Answers

up vote 4 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
add comment

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()
    func1()
    funcWithFuncs()
    func2

func func1()
    stuff

func funcWithFuncs()
    funcA()
    funcB()

func funcA()
    stuff

func funcB()
    stuff

func func2()
    stuff

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
add comment

Your Answer

 
discard

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.