C++ Programming Style Guidelines

Pages: 12
Apr 28, 2012 at 6:02pm
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 :)

Apr 28, 2012 at 6:18pm
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 Apr 28, 2012 at 6:20pm
Apr 28, 2012 at 8:05pm
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 :)
Apr 28, 2012 at 9:48pm
Style isn't that important as long as you're doing it consistently not like Microsoft's C API's.
Apr 28, 2012 at 10:01pm
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 Apr 28, 2012 at 10:01pm
Apr 28, 2012 at 10:27pm
@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.
Apr 28, 2012 at 11:25pm
I've never seen people add a _ suffix to all of their private member variables, that seems pretty annoying and useless to me.
Apr 28, 2012 at 11:35pm
Yep, the justification is weak too. If you want to be explicit about it, you can always use this->
Apr 29, 2012 at 1:17am
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;
Apr 29, 2012 at 1:20am
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.
Apr 29, 2012 at 2:14pm
linux style is good ,

Hungarian looks ugly
Apr 29, 2012 at 2:16pm
linux style is good


Way too cryptic acronyms and abbreviations for my taste, but I guess you get used to that if you bother.
Apr 29, 2012 at 2:37pm
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/
Apr 29, 2012 at 10:07pm
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.
Apr 30, 2012 at 8:46am
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.
Apr 30, 2012 at 10:11am
@Gaminic you're probably referring to http://www.joelonsoftware.com/articles/Wrong.html
Apr 30, 2012 at 10:24am
That's the one! Thanks, Cubbi!
May 1, 2012 at 3:25am
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?
May 1, 2012 at 3:41am
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 May 1, 2012 at 3:54am by closed account z6A9GNh0
May 1, 2012 at 4:07am
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