Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to organize code? [closed]

I'm not talking about how to indent here. I'm looking for suggestions about the best way of organizing the chunks of code in a source file.

Do you arrange methods alphabetically? In the order you wrote them? Thematically? In some kind of 'didactic' order?

What organizing principles do you follow? Why?

like image 506
Piers Cawley Avatar asked Sep 16 '08 14:09

Piers Cawley


2 Answers

i normally order by the following

  1. constructors
  2. destructors
  3. getters
  4. setters
  5. any 'magic' methods
  6. methods for changing the persisted state of reciever (save() etc)
  7. behaviors
  8. public helper methods
  9. private/protected helper methods
  10. anything else (although if there is anything else its normally a sign that some refactoring is necessary)
like image 106
Marc Gear Avatar answered Oct 19 '22 15:10

Marc Gear


I tend to use following pattern:

  • public static final variables
  • static functions, static blocks
  • variables
  • constructors
  • functions that do something related to logic
  • getters and setters (are uninteresing mostly so there is no need to read them)

I'm have no pattern of including local classes, and mostly I put them on top of first method that uses them.

I don't like separating methods depending on access level. If some public method uses some private method they will be close to one another.

like image 32
jb. Avatar answered Oct 19 '22 15:10

jb.