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)

Implementation:

  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: 3990

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{
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?


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

Sponsor:

#2 Paradigm Shifter   Crossbones+   -  Reputation: 5441

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: 21198

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' 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] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#4 FLeBlanc   Crossbones+   -  Reputation: 3125

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: 2122

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

 

-Josh


--www.physicaluncertainty.com
--linkedin
--irc.freenode.net#gdnet


#6 Paradigm Shifter   Crossbones+   -  Reputation: 5441

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: 14426

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.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#8 Shaquil   Members   -  Reputation: 815

Posted 26 February 2014 - 06:51 PM

I expected a very different thread given the title.



#9 slicer4ever   Crossbones+   -  Reputation: 3990

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: 4825

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: 9305

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.


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#12 Krohm   Crossbones+   -  Reputation: 3261

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.



#13 TheChubu   Crossbones+   -  Reputation: 4827

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.


"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator


#14 haegarr   Crossbones+   -  Reputation: 4604

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: 284

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: 22792

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 write about assorted stuff.


#17 Ravyne   GDNet+   -  Reputation: 8187

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



#18 TheComet   Crossbones+   -  Reputation: 1645

Posted 27 February 2014 - 06:16 PM

That's a very personal question
YOUR_OPINION >/dev/null

#19 ChaosEngine   Crossbones+   -  Reputation: 2506

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: 5038

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.



PARTNERS