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.
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:
function load(){
$this->loadCategories();
$this->loadLocations();
}
function loadCategories(){
$this->getCategoryNames();
}
function loadLocations(){
$this->getLocationIDs();
}
function getCategoryNames(){}
function getLocationIDs(){}
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.
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.
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.
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.
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.