So, I figured I'd start a discussion on coding style. Now, I would have made this into a poll, but there's simply too many independent choices one can make when committing to a coding style, be it self-enforced or through some sort of prewritten guidelines - variable notation, indendation, brackets, number notation etc.
Personally, I've found my own style has evolved drastically over the past few years - less and less with time, as I become more confident in what I do and why I do it. I later found a style guide that fit most of how I already it, apart from a few things (like pointer *'s, and switch case colon whitespace).
Without further ado, a fictional snippet of C++ code as I would write it - disregard the contents, as the style is all that matters in this discussion.
I'm including ternary operators for fun and giggles, Byron.
Code: Select all
#ifndef MAIN_C
#define MAIN_C
#define PI (3.14159f)
#define DEG(rad) ((float)rad * (360.0f / (2.0f * PI)))
#include <cstdio>
#include "glbase.h"
#include "main.h"
enum IntroversionGames {
UPLINK,
DARWINIA,
DEFCON,
MULTIWINIA,
SUBVERSION,
CHRONOMETER,
GAMES,
};
class Trout {
public:
void smash();
int getSmashes();
private:
bool smashed_;
int smashes_;
};
Trout trout;
// main rendering loop
void draw(int width, int height) {
// determine colour based on mouse button(s) pressed
glColor3f(mouse.right ? 0.5f : 0.25f,
mouse.left ? 0.5f : 0.25f,
mouse.middle ? 0.75f : 0.375f);
// print number of smashed trout, only if this trout is smashed
// else a nonsensical statement involving plankton is printed
if (trout == smashed) {
int trouts = trout.getSmashes();
int notUsed;
printf("%d %s(s) %s\n", trouts, "trout", "smashed");
// print nonsensical statement involving plankton
// also do some redundant calculations
} else {
float x;
float y = 1.0f;
float *z = &x;
x = y + (float)trout.getSmashes();
printf("NONSENSICAL STATEMENT INVOLVING PLANKTON");
}
// the game! is it out?
switch (game) {
case SUBVERSION:
// fallthrough
case CHRONOMETER:
printf("COMING ... SOMETIME!\n");
break;
case MULTIWINIA:
printf("COMING SOON!\n");
break;
case UPLINK:
// fallthrough
case DEFCON:
// fallthrough
case DARWINIA:
printf("ALREADY OUT!\n");
}
}
#endif /* MAIN_C */
Now, I really wish the bbcode [code] had syntax highlighting. Oh well, it's pasted here. I'll discuss my choices a little later in the thread, if it kicks off.
Discuss. (Obviously)