Ordering of functions

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Ordering of functions

Post 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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Ordering of functions

Post 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.
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Ordering of functions

Post 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;
}
 
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Ordering of functions

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Ordering of functions

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Ordering of functions

Post by pickle »

ctor() ??
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Ordering of functions

Post by alex.barylski »

Shorthand for constructor() -- common abbreviation in C++ :P
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Ordering of functions

Post 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 :-D I imagine code will be stored in a "richer" structure than the flat file.
Post Reply