A system for ordering methods

Hi Guys,

I’ve been using Yii for a bit and have recently begun thinking that it might be best to set in stone the order in which I put functions into my classes, particularly my models, to make everything easier to find. I contemplated Alphabetical ordering, but you really want related functions to be beside eachother, such-as getters and setters, beforeSave and afterSave, and so on. Right now the rough order in which I put them in is:


Class Table Extends CActiveRecord {

	/* Static Properties */

	/* Properties */

	/* Getters/Setters */

	public static function model() {}

	public function tableName() {}

	public function rules() {}

	public function relations() {}

	/* Transformations/Logic */

	public function attributeLabels() {}

	public function behaviors() {}

	public function search() {}

	public function scopes() {}

	/* Alternate search functions */

}

Now, this is OK, it’s mostly based on the order that the stock code generation does things, but there are some things I don’t like. You really want the rules to be near what they apply to (The getters/setters and instance properties), and for the relations (which are accessed as properties) to also be near the properties. Of course, standard practice says that the properties be defined before functions at the top of the class. The tableName and model methods will generally remain fixed until the end of time, so putting them at the end might get them out of the way. This gives us:


Class Table Extends CActiveRecord {

	/* Static Properties */

	/* Properties */

	/* Getters/Setters */

	public function rules() {}

	public function relations() {}

	public function attributeLabels() {}

	public function behaviors() {}

	/* Transformations/Logic */

	public function search() {}

	public function scopes() {}

	/* Alternate search/find functions */

	public function tableName() {}

	public static function model() {}

}

Does that seem logical? Do any of you have similar schemes you’d be willing to share?

Above all, I think that:

  1. its a matter of taste.

  2. Any order or style of code should serve the purpose of lowering the burden of finding what you need, skimming over code to recall, and in general - enable max attention to the coding act or otherwise developing tasks, rather to "find yourself" in the depth of your code.

Three other comments:

  1. I personally like properties before methods.

  2. You forgot class constants in your recipe :slight_smile: . I put them even before properties.

  3. Bloody use a decent IDE! :) There’s no replacement to Netbeans or Eclipse code navigation capabilities (or other IDEs in that scale). I tend to prefer keyboard and have comfortable shortcuts which saves lots of time,for jumping to methods (in any class) or locating files, classes, etc. Its very useful.

Boaz.