Posted 27 February 2014 - 12:13 PM
Code is meant to be compiled, and code is meant to be read.
Do what you must to communicate with the compiler. Then do what you can to communicate effectively with the programmers.
L.Spiro mentioned both of them already. Always remember your audience.
The code must compile and generate the right functionality. This comes first. When the language dictates an ordering, you follow. Some languages require that certain declarations and definitions appear in top-down order. Some optimizations require that things be grouped in a specific way (e.g. individual member variables grouped largest to smallest, then large objects like arrays come last) and so these also take precedence.
The code will be read by other people. Custom and experience show that clear communication often has the most critical information first in an inverted pyramidal style, with the least urgent (but often most informative) details near the bottom for those who want to dig deeper. Organize and arrange the code such that people who will read the code can get the most benefit with the minimal work. That includes:
* Probably placing a comment at the very top of the file with the most critical details (often this includes copyright information, the most critical detail to businesses, and then big bold warnings and comments to programmers about a summary or critical notes)
* Probably placing the public interface first, the protected interface second, and the private implementation details last
* Probably placing the most-referenced material at the top or otherwise in a highly visible location.
* Probably clustering functionality by topic
* Probably clustering things that belong in pairs or groups
* Probably clustering things that will be used a sequence
* Probably following the same conventions as other parts of the code base
* Probably the implementation details go at the end, as they are not meant for communication to other programmers
Every chunk of code is going to be different, so write accordingly.
Let's say you are building some fancy new container class. At the top of the file you will probably have some useful comments, perhaps with a usage sample or the reason to use this fancy container. When it comes to the code, you would likely start out with data types and enumerations (because the language requires it). Then it is typical to list constructors, destructors, assigners and copiers. After that you might have various clusters of functionality: individual element access functions, range and iterator access functions, capacity and informational functions, broader utility functions, and so on.
When you are creating a new game object, and there are dozens or hundreds of other game objects, and you have a template to follow of a specific ordering, then by all means use that common template.
You might decide that because most of the code readers are going to be implementing child classes, you could have a grouping of functions and inside that group to mix both public and protected methods, and even throw in some protected and (if the class uses them) public variables that go with the cluster of functionality.