Is there a standard? How does everyone else do it?
For variables I do this order:
const
public static
private static
public
protected
private
From top to bottom, constants are visible to the most amount of other code, while private variables are visible to no other code, and the others are ordered accordingly. It makes sense but it's based on nothing other than personal preference.
For methods, I put all static methods first, followed by the constructor, then other magic methods, then the rest of the class methods which I order by the order they're likely to be used in. E.g. and kinf of "set" function will go before a get, and if there's a function which gets called at the end, e.g. an output() function I'll put it at the end of the class definition. I also put related functions, or functions which call other functions within the class next to one another.
This is all just personal preference and I was wondering how others do it.
for variables, i put constants first, the rest are either alphabetized or sorted by length of variable name. i often try to line up everything into columns for no reason other than it's easier to read for me. for methods, the constructor(s) is first. static methods are last, sometimes spaced down a few extra lines to give it some distance from the object code, because to me they're not a part of the object like the rest of the methods are. also, sometimes i'll try to put public methods before private methods, grouping them together, but this doesn't always work because i like to put private helper methods right next to the method that uses it.
Bookmarks