C++ Programming Style Guidelines

Pages: 12
I was currious about how other people design their code, so here is an exelent web page that gives some general rules about coding styles ->

http://geosoft.no/development/cppstyle.html#Layout%20of%20the%20Recommendations


If you scroll down you'll find references to more web pages talking about this.
I found that very useful and interesting to know, hope you'll like it too :)

That one is almost entirely useless, except a couple of links in its reference part (TMH's and Doxygen) are worth mentioning.

For useful guides, check out Sutter/Alexandrescu's Coding Standards or Lakos's Large-Scale C++ (granted, Lakos book is showing its age - his more recent presentations are more useful)
Last edited on
Yes, I've just read the references of Doxygen, these are much better..

Thank for mentioning the books, I'll try to read them one day :)
Style isn't that important as long as you're doing it consistently not like Microsoft's C API's.
closed account (zb0S216C)
I write code the way I feel most comfortable with. I follow my own conventions. I'm not a fan of "recommended coding standards".

Wazzak
Last edited on
@hanst99,
I think the way they name functions is okay, but I'm with you on their type and variable naming. All WinAPI code is made significantly less readable because of it.
I've never seen people add a _ suffix to all of their private member variables, that seems pretty annoying and useless to me.
Yep, the justification is weak too. If you want to be explicit about it, you can always use this->
I do camel case for regular variables:
1
2
int thisIsANumber;
char aLetter;

For constants I do:
1
2
const int NUMBER = 34;
const double THIS_IS_A_NUMBER = 3.14;

For defines I do:
1
2
#ifndef FILENAME_H
#define FILENAME_H 

I think I use normal conventions. Though I sometimes do alternate with personal preference and Hungarian notation (well not strict):
1
2
int iNumber;
char cLetter;
I alternate between this_is_a_name and thisIsAName depending on language, mood and the angle between mars and venus as observed from the moon at a time depending on the moon phase. But I think it's more important that the names convey meaning rather than the style preferences of the author.
linux style is good ,

Hungarian looks ugly
linux style is good


Way too cryptic acronyms and abbreviations for my taste, but I guess you get used to that if you bother.
I like this style of variable, class and type naming ->

Ie.

variables sVar static variable
gVar global variable
mVar member variable
VAR enum constants
rVar reference
pVar pointer
mpVar member pointer ...

typedefs
size_t unsigned long long
ushot_t unsigned short int
ulong_t unsigned long....

classes
CObject a class
IObject interface of a class
ESomething a enum class

functions GetParent() Compute() Initialize()

etc... etc...


I like hungrain notation very very much, but not any more because Microsot use it, And because $$$MS Has the most ugly API on this world, so I've decided to adopt my own coding convention and f* the $MS.

still MS has the best IDE, and that the only good IMO.

See this duscution:
Can anyone convince me that the Win32 API is not as ugly as it seems?
http://www.dreamincode.net/forums/topic/205730-can-anyone-convince-me-that-the-win32-api-is-not-as-ugly-as-it-seems/
Can anyone convince me that the Win32 API is not as ugly as it seems?

It's not as ugly as it seems. It is worse, I made the mistake of trying to learn Win32 API when I first started into game programming for Win32 API/DirectX and quickly abandoned it.
A few months back, there was a post on the real Hungarian notation, not what it became. It's very useful if used properly. Anyone still have the link to that topic?

Personally, I've just switched to only using self-typedef'd variable types, to signify meaning through type (e.g. COST vs. "int"/"double"). Secondly, I try to use only unsigned types, and convey "sign" through the type name. Lastly, all types (typedef'd as well as classes) are in full caps, but this is just for readability: easier to spot in function declarations and it's easier to spot typos as the mark-up of Visual Studio is faster than IntelliSense. (Not bold? You made a typo!)

I like hungrain notation very very much, but not any more because Microsot use it,


That's the dumbest thing I ever heard.
@Gaminic you're probably referring to http://www.joelonsoftware.com/articles/Wrong.html
That's the one! Thanks, Cubbi!
Hi, I'm still a beginner to C++ (my older sibling is in college and I use his textbook)

I thought this would be the appropriate topic to ask my question regarding how to implement functions (the style). I admit to watching some videos (which I only use as a supplement because sometimes it makes it easier to watch someone write code), but I'm confused to what is appropriate for laying out functions.

The two videos I was watching here...
http://www.youtube.com/watch?v=3pAWJ40I1QE (functions w/out parameters)
http://www.youtube.com/watch?v=wwNFo95S75A (functions w/ parameters)

... says you have to do what is called declaring functions above your main function with comments like this (sorry if the below looks bad, never used CODE tags before):

1
2
3
4
5
int myFunc(int, string);
// myFunc - description of function
// @param int - description of first datatype
// @param string - description of second datatype
// @return int - description of what is being returned 


... and to have the body of the function below the main function like this

1
2
3
4
5
int myFunc(int num1, string newString) {

// body of code

}


My brother's textbook doesn't talk about function declarations or comments, only shows us how to define functions above the main function. What standard should I follow?

Also, in the second video, the guy only put the datatypes of the parameters in the declarations, but placed both the datatypes and the variable names in the definitions. Why is that?
Well if that is what your brother's text books are saying then I say quit reading them and read the tutorials on this site.

http://cplusplus.com/doc/tutorial/
PDF Format: http://cplusplus.com/files/tutorial.pdf

Also when you have new questions you really should post them to a new thread either in the Beginner thread or the General C++ forum here.

Your problem is minor, but it gets more complex as you can put the function prototypes in a header file (.h) and the implementation in the source file (.cpp) . To keep it simple though:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
using namespace std;

void myFunc(int, string); // prototype (can also be int num1, string newString too

int main()
{
      myFunc(4, "Hello World!");
      return 0;
}

void myFunc(int num1, string newString)
{
      for (int i = 0; i < num1; i++)
      {
            cout << newString << endl;
      }
}



Hello World!
Hello World!
Hello World!
Hello World!
Last edited on by closed account z6A9GNh0
have the body of the function below the main function
...
shows us how to define functions above the main function. What standard should I follow?


Follow rule number 0 of Sutter/Alexandrescu's Coding Standards book. Don't sweat the small stuff.

(incidentally, in the corporate standard I'm following, the cpp file with the main function is not supposed to have any other functions at all)
Pages: 12