Forums

Code Organization Question

I asked a question on this forum back in April and two users were incredibly helpful (giles and another kind fellow). I implemented your suggestions and my 'script' has been accomplishing what I intended it to. However, as I look to expand my knowledge/skill set of OOPL's and Python I wish to add more robust functionality to my program. I had written a test case for the Selenium RC web driving framework and the majority of this code resided under a single function. Now, after adding more capability this one function has gotten quite long and with multi-tiered branching indentations to boot. For instance, I am thinking of having a function called 'untilTable()' that has the code which refreshes a resource page [.aspx] until an event triggers a table to be generated on the page. Then, upon that condition being satisfied the script is to call another function named 'rowCount()'. 'rowCount()' gets and stores the amount of rows in the table. Without going on too much further, does anyone have any general guidelines as to when one should start or end a function? I.e. How much should you try to accomplish within one function before calling another function? Any help will be appreciated, thank you.

In general functions should do a single thing. You can easily combine a sequence of functions, and you have more flexibility in the combinations if each does just one unified thing.

For instance, suppose you have a function that creates a sentence, and prints it. If that is all you have, you are out of luck if you want to do something different with the sentence string. A better way is to have a function that just creates the sentence, and returns it for whatever further use you want. Printing is one possibility, but no longer the only possibility.

I'm tempted to write something real abstract here like: A function should contain what is necessary and no more.

I forget who talks about simplifying down our problems until they can be simplified no more, but that is the general idea in academia, Of course the real world is a bit less lofty. The big point is to keep code re-use as high as possible. You don't want your code full of doing the same thing over and over manually.

~HTH

@a2j Spot on - a function should do just one thing, preferably in less than 50 lines, and do it well!

@Philonius: The semantics can help: e.g. if one thinks of writing a function to connect to an FTP host and upload a file, the clue is in the word 'and' - use (at least) two functions.

As an old OOPS pro (Smaltalk, Delphi, C#, Python, even VBA), I would also say that using OOPS principles helps enormously, i.e. appropriate class design.

HTH Jim

This does help. I know the question was a bit rudimentary and most likely has been answered elsewhere---even though I exhausted several searches. Thanks for the responses gentlemen!

@Philonius: Are your efforts here on PA going well? We're here if you need direction!

@Philonius: Many, many discussions on function length in Google, e.g. http://programmers.stackexchange.com/questions/133404/what-is-the-ideal-length-of-a-method

The Code Complete book is always well regarded, but there are loads more on good 'Software Engineering' etc.

back to lurking...

How old is Code Complete now?

Okay, not fair to ask you...I looked it up at the above link and it says: published in 1993 by Microsoft Press.

Of course there is the second edition from 2004. I'm not sure how much was updated though. I know so many people who rave on that book, but for some reason I could never get myself through it the couple of times I picked it up. Wrong time/place maybe...

For anyone interested in looking further into it, the respective ISBN's are:

Showing my age, as usual!

And I still point to the Gang of Four book from time to time...

Anybody got new-ish alternatives pls?

I'm not saying Code Complete is bad. Just saying I never connected with it. I've never heard anyone say anything but kudos for it!

Of course all someone really needs to do is look at -my- code - but my legal department wouldn't allow it ;----)

Finally, I got my response from your legal department...they are allowing me to review your code through the end of this year. Let's get busy!!

Hah! just sacked them all...