2

I need to do the code review of fellow members.

One of the member always defined the helper functions at the bottom and use them at top e,g

class Sample {

public function SendFile(){
file = $this-convertFile($file);
}

public function convertFile(){
//bla bla
}

}

I sometimes dont know if he has custom defined this function of it is inherited from base class or some builin php function . when i reach bottom then i find it was custom defined

2
  • Are you looking for php specific coding guidelines? Dec 12 '13 at 7:35
  • 1
    I know there are recommendations in C# for making code easier to read: members are prefixed with this., static members are prefixed with ClassName., inherited members are prefixed with base.. Could a solution to your problem involve explicit prefixes rather than function declaration location?
    – MetaFight
    Dec 12 '13 at 10:57
3

Generally, the most important things should come first, with less important things later. In a class this means that the members and the constructor should come first, followed by public methods that make up the API of the class. After that, (private) helpers are listed. Putting the less important bits at the top would be probably more confusing.

I don't think your problem is that files are structured in a sensible way, but that you are trying to understand code top-down in a linear fashion. Code does not execute in a linear fashion, so this a linear approach is bound to fail.

  • Use code folding in your editor to get an overview of all definitions.
  • Use automatically extracted documentation to get an overview of a class before reading the code.
  • Is a method defined in this class or a base class? This can be looked up through a quick search.
2
  • 1
    The problem with the most important things should come first is that important is a very subjective term. As long as the language/compiler supports functions being declared after their use, the relative placement of the declaration and use is just personal preference and style.
    – MetaFight
    Dec 12 '13 at 10:56
  • @MetaFight Certainly, importance is a subjective metric. In OOP languages, the cadence constructors–public API–private helpers can be recommended, and is geared towards quick understanding by unfamiliar people. Another possibility is to organize methods by topics, with helpers methods immediately following the methods where they are used. The position in the call stack is a proxy indicator for importance. In functional languages (esp. ML-descendants) I am used to seeing the helpers at the top. This is useful when reading the whole file, but makes a top-down bird-eye view more difficult.
    – amon
    Dec 12 '13 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.