Why put private fields and methods at the top of class?

It probably comes from the days of C, when all variables had to be defined at the top, as part of the initialisation. Also, the default access specifier is private, so it can save you a superfluous private: later on in your class definition.


It stems from the days when you had to declare everything - that includes functions as well as variables - before you could use them.

The internal (private) variables and functions went at the top of the file and the external (public) functions went at the bottom of the file. This meant that the public functions could reference the internal functions. If you had recursion you would have to forward reference the function by declaring it's profile.

When languages allowed the code to span several files you had to put public function and variable declarations into header files so that they could be included in the other files in the project - or indeed other projects.

Tags:

Class Design