• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
slicer4ever

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

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

0

Share this post


Link to post
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.

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

Share this post


Link to post
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 this post


Link to post
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 smile.png

Edited by samoth
0

Share this post


Link to post
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 smile.png

 

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.

 

p14 http://www.stroustrup.com/hopl2.pdf

Edited by Alessio1989
0

Share this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0