Page 1 of 1
Ordering of functions
Posted: Thu Feb 05, 2009 10:54 am
by pickle
Hi folks,
Just a general discussion question: How do you order your functions in libraries or classes? For example, do you put all your directly called functions first, followed by functions that are called internally, like this:
Code: Select all
function load(){
$this->loadCategories();
$this->loadLocations();
}
function loadCategories(){
$this->getCategoryNames();
}
function loadLocations(){
$this->getLocationIDs();
}
function getCategoryNames(){}
function getLocationIDs(){}
Or do you arrange them by purpose like so:
Code: Select all
function load(){
$this->loadCategories();
$this->loadLocations();
}
function loadCategories(){
$this->getCategoryNames();
}
function getCategoryNames(){}
function loadLocations(){
$this->getLocationIDs();
}
function getLocationIDs(){}
Just curious what people's approaches/reasoning is?
Re: Ordering of functions
Posted: Thu Feb 05, 2009 11:42 am
by Christopher
That's an interesting question. It may be one of those things where the conventions people follow are because of a couple of different ideas being applied to one thing. In classes it seem like the convention of putting the constructor first is pretty universal. Then you often see initialization methods that kind of go with the constructor. Then you often see any private methods. Then you often see getters and setters. Then the normal, public methods. And I often see things like _toString() and Iterator methods stuck at the end -- I guess because they are considered outside the realm of the normal class code. I also notice that methods are often grouped by functionality, such as all import methods together and all export methods in a separate group. But because there are several competing reasons for grouping methods, you may find the same kinds of methods grouped in different ways in different classes -- yet both make sense when you browse the source.
Re: Ordering of functions
Posted: Thu Feb 05, 2009 11:48 am
by alex.barylski
Personally I try and list constants, then private member data, then ctor(), then public get/set methods, then initialization.
I'm actually pretty crazy about how everything is laid out in this regard.
To the point where the longest methods (in terms of SLOC) come last, I find it makes the file easier to read.
I also make sure to pair get/set functions like follows:
Code: Select all
getSomething(){ return $this->_something; }
setSomething($something)
{
$this->_something = $something;
return $this;
}
Re: Ordering of functions
Posted: Thu Feb 05, 2009 2:51 pm
by pickle
I usually order mine in a sort of hierarchy: Those that get called directly (Tier 1 so to speak) are listed first. Helper functions (Tier 2) for those initially called functions, are listed after all the Tier 1. Tier 3 are listed after Tier 2, etc.
My question was originally brought on by me wondering how readable my code is. If 100% of everyone else orders their functions in a particular way, I figured I'd adopt that paradigm.
Re: Ordering of functions
Posted: Thu Feb 05, 2009 3:37 pm
by alex.barylski
My question was originally brought on by me wondering how readable my code is. If 100% of everyone else orders their functions in a particular way, I figured I'd adopt that paradigm.
I think this is where personal taste makes a big difference.
I have many many personal conventions which I stick to to expedite development. Literally shaving seconds off past experiences, but seconds add up to minutes, right?
I know that using an object/class I am more likely interested in the public interfaces, which is why I keep constants front and center and public functions come first.
I can see during implementation phase however, why a sequential order is prefered, I would say most of my classes follow a similar order simply by design, not so much planned. I always build a class with most functionality in the ctor() once it reaches 10-20 lines I refactor code into protected methods until a public interface make it's self required. I do this to keep the interface as simple and hollisitic as possible, cohesive I guess.
Cheers,
Alex
Re: Ordering of functions
Posted: Thu Feb 05, 2009 3:44 pm
by pickle
ctor() ??
Re: Ordering of functions
Posted: Thu Feb 05, 2009 3:49 pm
by alex.barylski
Shorthand for constructor() -- common abbreviation in C++

Re: Ordering of functions
Posted: Fri Feb 06, 2009 3:06 pm
by josh
I just put them randomly, I put functions that are coupled heavily next to each other. Then as some point I go back and re-arrange it the same way phpdocumentor / my IDE shows the members ( public members a-z, then protected a-z, finally private methods a-z ), so that when I look at a member in the docs and see 2 members adjacent to it, I can rely on the same 2 members being adjacent int he actual code. This is something I do in between refactorings.
10 years from now this conversation will be meaningless

I imagine code will be stored in a "richer" structure than the flat file.