Jump to content

  • Log In with Google      Sign In   
  • Create Account

Top Or Bottom?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
42 replies to this topic

Poll: Top Or Bottom? (77 member(s) have cast votes)


  1. Top (31 votes [40.26%])

    Percentage of vote: 40.26%

  2. Bottom (46 votes [59.74%])

    Percentage of vote: 59.74%

Vote Guests cannot vote

#1 slicer4ever   Crossbones+   -  Reputation: 5890

Posted 26 February 2014 - 04:08 PM

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{
    int m_Member;
    void SetMember(int value);
    int GetMember(void);

vs: Bottom:


class Foo{
    void SetMember(int value);
    int GetMember(void);
    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?

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#2 Paradigm Shifter   Crossbones+   -  Reputation: 5832

Posted 26 February 2014 - 04:11 PM

I'm a bottom man. Public interface first.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#3 Servant of the Lord   Crossbones+   -  Reputation: 32506

Posted 26 February 2014 - 04:13 PM

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, 26 February 2014 - 04:25 PM.

It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' rather than copy+pasting it all the time.

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal | [Fly with me on Twitter

#4 FLeBlanc   Crossbones+   -  Reputation: 3129

Posted 26 February 2014 - 04:22 PM

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

#5 jjd   Crossbones+   -  Reputation: 2140

Posted 26 February 2014 - 04:29 PM

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 ;-)




#6 Paradigm Shifter   Crossbones+   -  Reputation: 5832

Posted 26 February 2014 - 05:08 PM

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

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#7 L. Spiro   Crossbones+   -  Reputation: 24023

Posted 26 February 2014 - 05:11 PM

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, 26 February 2014 - 05:13 PM.

#8 Shaquil   Members   -  Reputation: 818

Posted 26 February 2014 - 06:51 PM

I expected a very different thread given the title.

#9 slicer4ever   Crossbones+   -  Reputation: 5890

Posted 26 February 2014 - 07:57 PM

I expected a very different thread given the title.


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

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#10 Nypyren   Crossbones+   -  Reputation: 10069

Posted 26 February 2014 - 08:21 PM

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)

#11 Bacterius   Crossbones+   -  Reputation: 12906

Posted 26 February 2014 - 10:09 PM

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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

#12 MaxDZ8   Crossbones+   -  Reputation: 4945

Posted 26 February 2014 - 11:20 PM

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.

Previously "Krohm"

#13 TheChubu   Crossbones+   -  Reputation: 8497

Posted 26 February 2014 - 11:30 PM

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



My journals: dustArtemis ECS framework and Making a Terrain Generator

#14 haegarr   Crossbones+   -  Reputation: 7039

Posted 27 February 2014 - 02:21 AM

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.

#15 JDX_John   Members   -  Reputation: 292

Posted 27 February 2014 - 04:34 AM

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

www.simulatedmedicine.com - medical simulation software

Looking to find experienced Ogre & shader developers/artists. PM me or contact through website with a contact email address if interested.

#16 frob   Moderators   -  Reputation: 39001

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.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

#17 Ravyne   GDNet+   -  Reputation: 13447

Posted 27 February 2014 - 02:09 PM

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

throw table_exception("(ノ ゜Д゜)ノ ︵ ┻━┻");

#18 TheComet   Crossbones+   -  Reputation: 2531

Posted 27 February 2014 - 06:16 PM

That's a very personal question

"Windows 10 doesn't only include spyware, it is designed as spyware" -- Gaius Publius, The Big Picture RT Interview

"[...] we will access, disclose and preserve personal data, including your content (such as the content of your emails, other private communications or files in private folders), when we have a good faith belief that doing so is necessary" -- Windows 10 Privacy Statement

#19 ChaosEngine   Crossbones+   -  Reputation: 4291

Posted 02 March 2014 - 02:39 PM

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

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

#20 samoth   Crossbones+   -  Reputation: 8333

Posted 04 March 2014 - 07:28 PM

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 smile.png

Edited by samoth, 04 March 2014 - 07:29 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.