40

I've seen this de facto standard in many places in many languages, but I've never understood it - why put your private fields and methods at the top of a class declaration? Metaphorically it seems like private things should be located at the bottom (hidden) and everything public should be at the top, so that when you read through the class top to bottom you first see the public interface then the inner workings.

What is the reasoning behind this?

Just to clarify, I don't mean the practice of declaring all members at the top of the class, but of putting private members/methods at the top of a class declaration, before anything public.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
JimDaniel
  • 12,513
  • 8
  • 61
  • 67

11 Answers11

32

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 its 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ChrisF
  • 134,786
  • 31
  • 255
  • 325
13

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Mark Ingram
  • 71,849
  • 51
  • 176
  • 230
  • Why not use the `struct` keyword in the first place? – Florian Feb 27 '19 at 18:35
  • "Why not use the struct keyword in the first place?" Because it's called `class` everywhere, for example, someone may forward-declare like "`class YourApi;`" and get an error saying "Was re-declared as `class`, while `YourApi` is `struct`". – Top-Master Dec 20 '22 at 15:12
  • Saving milli-seconds of typing "`public:`", by using a `struct` (instead of `class`), is not worth hours of re-compile, which many API users endure (and that just because they type `class` by default). – Top-Master Dec 20 '22 at 15:21
  • @Top-Master `class` and `struct` are interchangeable in forward-declarations, it's perfectly allowed to forward-declare something as a `class` and then define it as a `struct`. – Donald Duck Dec 23 '22 at 17:13
  • 1
    @DonaldDuck say that to the MSVC compiler. – Top-Master Dec 24 '22 at 09:17
  • @Florian "Why not use the struct keyword in the first place?" Just because you could, doesn't mean you should. Just because to the compiler they are not that different, to many developers they have additional meaning: class is for real/copmlex object types (not in a mathematical sense;), while structs used for basic data structures, POD types. If it has private or protected fields or used in inheritance it probably should be a class. If you describe field layout for a protocol or file format it could be a struct (preferably one compatible with C) and have another class use that struct. – Steven Spark May 12 '23 at 12:50
10

Personally, when I am trying to read somebody else's code and understand it, I like having the context that the private fields provide. It gives me a quick glimpse at the type of things they are doing and what I can expect to see. If it is done right, it can give a nice preview, almost like a table of contents to the rest of the code.

Erich Mirabal
  • 9,860
  • 3
  • 34
  • 39
  • 4
    This is a good point, but I think the public methods are closer to a table of contents than the variables are. – byxor Dec 19 '16 at 17:20
  • This answer is only true if we replace the words "private variable" with "public method" in our mind ;-) – Top-Master Dec 21 '22 at 07:23
6

I agree that it probably comes out of C, but it also serves a practical function.

In all things you want to learn in the order that such knowledge is required, such as learning how to add before learning how to multiply.

In the case of code, your public functions will generally reference internal private ones, and your private functions are less likely to reference the public ones (although there certainly is overlap.) So, understanding what private variables you have and what they are used for will be useful when trying to understand what the public methods are doing with those variables.

In this fashion it is also useful to understand what the private methods do before you read about the public methods which are calling them.

By and large, I believe it is a style that comes from C and earlier languages, but because it is functionally more useful where it is, I don't see any benefit in switching styles.

And, as always, breaking consistency is never a good idea unless there's an incredibly compelling reason to.

DevinB
  • 8,231
  • 9
  • 44
  • 54
  • A co-worker and I came to the same conclusion, after the debate which triggered this question. This was the only logical reason we could come up with. – JimDaniel Apr 22 '09 at 18:27
3

Two reasons.

If they are all together at the top, they are easy to find, and easy to inspect when creating a new thing to see what naming conventions are at work and what names are and are not available.

In the C language, and in JavaScript, all references in code must be declared and/or defined above where they are referenced, because everything referenced must be already known. (Yes, I know, JavaScript only sort of.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dkretz
  • 37,399
  • 13
  • 80
  • 138
3

With respect to fields, it's pretty common to make all fields private (encapsulation and all that). And they're small. Since they're small, it's nice, and possible, to put them all together, and the top of a class is a nice, stable place to put them. I don't know if this is the historical justification for it - I think the C language roots probably explains that better - but it could be a reason that we keep them there.

For my own part, I tend to put private methods at the bottom, for the reasons you suggest.

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
3

In OOP there are two things, data and behavior.

The private members represent the data and the public methods represent the behavior.

You can think about behavior only in the light of the data it represents.

So that's why I would like to keep my data elements at the top.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RN.
  • 997
  • 4
  • 14
  • 31
1

I guess it's habit. You have to declare functions and variables before you can use them so this is consistent.

On the other hand, it's the stuff you don't really want other people to be reading so it belongs at the bottom.

Jimmy J
  • 1,953
  • 1
  • 14
  • 20
  • 1
    Depending on the programming language you are using, class level functions and variables do not have to be declared before you use them. Both Java and C# work this way. – sgriffinusa Apr 22 '09 at 18:00
1

I thought the de facto standard was public methods first!

At least that is the way I write my classes. It helps me choosing the data structures for my methods.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Naveen
  • 74,600
  • 47
  • 176
  • 233
0

Because of languages like where you have, as ChrisF says, such as interpretive languages, with Python being a prominent one.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eiyrioü von Kauyf
  • 4,481
  • 7
  • 32
  • 41
-1

It's not like it's a secret that they're there. Are you afraid that someone will learn about the inner workings? If they have the source, they can learn everything they need to know about your class.

Putting them in with the others makes it easier to develop that class. All of your class variables are in one place, and so are your methods.

belgariontheking
  • 1,351
  • 1
  • 10
  • 15
  • It's not about _fully_ hiding the private variables, it's about letting the reader see the more important behaviour first. – byxor Dec 19 '16 at 17:21