Advertisement

Top Or Bottom?

Started by February 26, 2014 10:08 PM
37 comments, last by Ectara 10 years, 6 months ago

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

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"

Advertisement

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

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.

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.

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

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("(? ???)? ? ???");

That's a very personal question
"I would try to find halo source code by bungie best fps engine ever created, u see why call of duty loses speed due to its detail." -- GettingNifty

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

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

This topic is closed to new replies.

Advertisement