Followers 0

Top Or Bottom?

38 posts in this topic

Ok, so i've seen alot of people write classes diffrent ways.  some prefer the interface at the top, and the implementation at the bottom, some prefer it the other way.

i.e: Top:

class Foo{
private:
int m_Member;
public:

void SetMember(int value);

int GetMember(void);
}:


vs: Bottom:

class Foo{
public:
void SetMember(int value);

int GetMember(void);
private:
int m_Member;
};


so, i can understand the point of getting to the interface faster, but personally i like having the variables at the top.  anywho, what are your reasons, what do you like?

0

Share on other sites

I'm a bottom man. Public interface first.

0

Share on other sites

I prefer the public interface at the top of the class, and the private implementation details at the bottom (or preferably in an entirely different file) - I want my focus to be on the interface or I might start abusing my internal knowledge of the implementation. Ideally, my classes should be a black-box. How they work inside shouldn't matter to the user of the class (in the general case).

Edited by Servant of the Lord
0

Share on other sites

I like bottoms, too. Also, implementation should be at the end of the file.

0

Share on other sites

I like big bottoms and I cannot lie. Actually, I prefer small bottoms. And I really don't mind the top or the bottom -- if it is clean and consistent I think it is good. If it is all mixed up, const-incorrect, with lots of comment doodles a furious wroth descends upon me and likely to refactor with extreme prejudice ;-)

-Josh

0

Share on other sites

Yeah don't want too much doodling above and around the bottom (arse antlers, or tramp stamp comments).

0

Share on other sites

It’s not really that subjective.

Public always goes on top because when people look at your class they only care about what methods they can call and there is no reason, regardless of personal preference, to make them scroll down random lengths that vary depending on the file to find it.

20 video-game guys live in a house with 1 rainbow-loving girl.

Even though the girl is in charge of stacking all the magazines in a drawer, regardless of her own preference to put her rainbow magazines on top, it is still correct to put the game magazines on top and hers on bottom, since more people actually care about the game magazines.

There are also laws regarding the order of enumerations (first), typedefs/structs (second), and methods (third).  Enumerations can’t refer to types or functions/methods.  Structures and typedefs can refer to enumerations but not functions/methods.  Functions/methods can refer to any of the above.

L. Spiro

Edited by L. Spiro
0

Share on other sites

I expected a very different thread given the title.

0

Share on other sites

I expected a very different thread given the title.

hey, maybe your coding style falls under the same....aspect? =-P

0

Share on other sites
Hmmm... my C# style does not fit into either category neatly.

I do:

- Fields
- Properties
- Constructors
- Methods
- Explicit interface implementations

(I don't sort any of them by access modifier)
0

Share on other sites

Hmmm... my C# style does not fit into either category neatly.

I do:

- Fields
- Properties
- Constructors
- Methods
- Explicit interface implementations

(I don't sort any of them by access modifier)

Mine is similar to this, except I sometimes put private helper methods before the constructors if there's only a couple, I'm not very consistent when it comes to that.

0

Share on other sites

I try... I really try to put public stuff first. But I don't have the time to and old habits are hard to die. Ok, maybe I don't even try.

0

Share on other sites

The way you worded thinks made me really confused so I'm not quite sure what I voted.

0

Share on other sites

Things useable for the masses comes first in general. But I also think that a decent documentation derived from the source code (using i.e. Doxygen) is even a better choice compared to the source code itself, and generating such documentation can leave out private stuff. So you would need to look into source code only in special cases.

0

Share on other sites

Clearly, you use your generated documentation or your IDE's functionality so it doesn't matter :)

0

Share on other sites
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.

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

Share on other sites

Interface at top. Others have laid the case out plainly.

I've mentioned in another thread somewhere that I actually consider class/struct and their only differences being whether they are public or private access by default (and similar defaults WRT inheritance) to be one of C++'s mostly inconsequential but regrettable-none-the-less warts. I would have preferred a stronger difference between structs and classes, such that the distinction was actually meaningful. IMO, classes should have been made basically as they are with the exception of default public acess, but structs should not have been extended to allow different access levels (and as a result, no need for supporting arbitrary member functions or virtuals) and they should have been extended only with constructors/destructors -- enough to support RAII and enable exception-safe use of structs. But the ship has sailed, and I'm sure the reasons for things being the way they are had nothing to do with language purity and everything to do with making it less burdensome to leverage and extend existing C code in C++ programs.

Anyhow, when I declare a class, public: is nearly always the first line. The exceptions I find in my own code to this rule, is that I sometimes create private typedefs first when they either are used to simplify the internals of public inline member functions, or when they are dependent on template arguments of the class (because I like to be able to see the template parameters and typed dependent on them on the same screen).

0

Share on other sites
That's a very personal question
0

Share on other sites

1. Whatever your team convention is
2. Interface at the top

0

Share on other sites

I like placing the private stuff first in classes, since that is the one thing that makes sense. Putting the public interface (or anything public) first is nonsensical.

A class is a struct where the members default to being private rather than public (in layman's wording, the standard's wording is slightly more elegant).

Therefore, if you write class and immediately follow with public: you are being nonsensical. You're the madman who puts salt on his bananas and throws them away because he doesn't like salted banana. I try not to write nonsensical code, if I can help it.

If one wants the public members first, one should write struct and declare the non-public members private. Of course nobody does that... so it's private stuff first

Edited by samoth
0

Share on other sites

This could've been a very scary topic.. Or maybe it is already???

0

Share on other sites

You mean if you prefer your privates on the top or bottom?

0

Share on other sites

Bottom for classes (first divided in public, protected and private, each of one in "bottom style" ie ctors, functions, inner classes/datastructures, consts, variables... static things before non static things)

Top for data structures (yes, sometimes I add some little functions to structures)

I like placing the private stuff first in classes, since that is the one thing that makes sense. Putting the public interface (or anything public) first is nonsensical.

A class is a struct where the members default to being private rather than public (in layman's wording, the standard's wording is slightly more elegant).

Therefore, if you write class and immediately follow with public: you are being nonsensical. You're the madman who puts salt on his bananas and throws them away because he doesn't like salted banana. I try not to write nonsensical code, if I can help it.

If one wants the public members first, one should write struct and declare the non-public members private. Of course nobody does that... so it's private stuff first

In C++ private as default class access modifier was a style choice with a solid reason, and not salt on banana:

Before starting work on C with Classes, I worked with operating systems. The notions of protection from the Cambridge CAP computer and similar systems – rather than any work in programming languages – inspired the C++ protection mechanisms. The class is the unit of protection and the fundamental rule is that you cannot grant yourself access to a class; only the declarations placed in the class declaration (supposedly by its owner) can grant access. By default, all information is private.

Edited by Alessio1989
0

Share on other sites

I do top because of the class being private by default. My method is to do private members on top, protected if I have any, public methods, followed by any operator overloading I'm doing and the constructors/destructors.

0

Share on other sites
I generally put the interface at the top and private members and classes at the bottom because users of the class(including myself) shouldn't be bothered by the implementation so why make them wade through it before they get to what they need? Even when it comes to the interface, I put methods that will be used the most at the top.

That the default access in classes is private has never been a concern to me. I've always thought of it more as a reminder that member variables in classes should be private.

My head doesn't explode when others do the opposite way though.
0

Create an account

Register a new account