What's Your Coding Style?

Programming, for all ages and all languages.
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

What's Your Coding Style?

Post by Creature »

Hello,

I am having trouble figuring out a coding style. The truth is, I try to stick to UpperCamelCase for pretty much everything, except parameters for functions are in lower camel case. I'm also not sure what case to use inside functions, I usually tend to use UpperCamelCase there too unless it's a one-character name like 'i'. Besides that, I've encountered lots of problems trying to define my constants; I used defines with capital letters at first (with always some sort of type identifier, such as PAGE_FLAG_PRESENT for paging), but now I keep using enumerations to define constants and namespaces to wrap them and classes. I've also had numerous times that I wasn't really satisfied with my current way of doing things, tried to rewrite it all, and it turned out that I wanted something like the original style anyway.

I was wondering if anyone here has a specific coding style they always stick to when they code something (unless of course, it's a project that has strict style guidelines). Have you had any problems with naming variables and such (conflicting names) that you've encountered using your own style?
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: What's Your Coding Style?

Post by Owen »

I pretty much follow the style of Qt:

Code: Select all

#define MACROS_ALWAYS_UPPER_CASE(_parameters_underscore_prefixed) 
namespace CamelCased {

class CamelCased {
public:
   void methodJavaCased(int asAreParameters)
   int someVal(); // getters have no prefix
   void setSomeVal(); // setters have set prefix
private:
    int m_membersHaveAPrefix;
    static int ms_asDoStatics;

    // if this class is pImpl style, then the p is called m
    CamelCasedPrivate* m;
};

enum MoreCamelCasing {
    ConstantsAreCamelCasedToo = 0,
    // in general, the enumeration values should end with something related to what they're defining
};

const int ExampleOfAConstant = 0;
}

// All function names are javaCased
void function() {
    int variableName; // Variables in functions are javaCased
}
Names for hidden (i.e. implementation detail) objects should be created by a special macro:

Code: Select all

// These two for special corner cases
#define PRJ_CONCAT2(_a, _b) _a##_b
#define PRJ_CONCAT(_a, _b) PRJ_CONCAT2(_a, _b)
#define PRJ_GEN_NAME(_name) PRJ_CONCAT(PRJ_CONCAT(PRJ_CONCAT(_i_PrjName, __LINE__), __FILE__), _name)
They should also be translation unit private when created in the global scope (i.e. defined in an anonymous namespace or declared static) whenever possible
fronty
Member
Member
Posts: 188
Joined: Mon Jan 14, 2008 5:53 am
Location: Helsinki

Re: What's Your Coding Style?

Post by fronty »

I try to follow FreeBSD's style(9).
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: What's Your Coding Style?

Post by gravaera »

Code: Select all

#define MACRO_CATEGORY_SUBCAT_VERBOSE_PURPOSE(__param)   (__param)

//Post-fix classes with capital C. Structs with capital S.
class lowerCamelCaseC {
   friend class creepyClassC;
   public:
      lowerCamelCaseC();
      ~lowerCamelCaseC();

   public:
      type myMethod();

   private:
      int moreLowerCamelCase;
};
I always use lower camel case in C++. In C, I follow the underscore convention. For all types of block statements I do full bracing, even for one liners.

As an addendum, I comment feverishly, and use the following format for comments:

Code: Select all

/** EXPLANATION:
 * This is an explanation block which is placed before any code in the file, after the #includes.
 * ...
 **/

/* General multiline comment.
 * Which spans multiple lines.
 **/

//Single line comment in C and C++
/* Single line comment in ASM. (I use GNU AS) */

;;When using NASM, etc, I form my comments like this, with two semi-colons
The rest of my coding style is extremely explicit, in that I tend to do things like:

Code: Select all

for (simple-expression;
   ((fully-bracketed) || (complex-expression));
   expression
   )
{};

while (expression) {
   //Even for single line block statements, I still place the braces in.
};
Last edited by gravaera on Wed Feb 24, 2010 10:09 am, edited 1 time in total.
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: What's Your Coding Style?

Post by Love4Boobies »

I use the original KNF (Kernel Normal Form) coding style from 4.4BSD-Lite2, which is where FreeBSD's coding style descended from. The difference is that I don't use the obsolete K&R parameter list syntax and I also use single-line comments. (Oh, and I adapt it for C++ but that's obviously not very hard).
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: What's Your Coding Style?

Post by Firestryke31 »

It doesn't cover everything, and it's designed more for easy reading (though I don't know how well I did in that regard) but I've got my coding style in the developer section of my site (link). Some of the "example" code isn't as efficient as it probably could be, but it's meant to demonstrate style, not efficiency.

Also, under Operators, IDK if the pre/postfix inc/decrement operator statement is valid, but once again, it's just to demonstrate style.
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
User avatar
KotuxGuy
Member
Member
Posts: 96
Joined: Wed Nov 25, 2009 1:28 pm
Location: Somewhere within 10ft of my favorite chubby penguin!

Re: What's Your Coding Style?

Post by KotuxGuy »

My coding style:

Code: Select all


#define MACRO_UPPERCASE(paramsCamel)

class CamelClass
{
   methodJavaCase(paramsCamel);

   privateStuffJavaCase;
}

Give a man Linux, you feed the nearest optician ( Been staring at the PC too long again? ).
Give a man OS X, you feed the nearest NVidia outlet ( I need more GPU power!! )
Give a man Windows, you feed the entire Tylenol company ( Self explanatory :D )
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Re: What's Your Coding Style?

Post by earlz »

It depends on language... For C#, the usual CamelCase. For C/++ underscore_style for Ruby, it's the way recommended by the language.
leledumbo
Member
Member
Posts: 103
Joined: Wed Apr 23, 2008 8:46 pm

Re: What's Your Coding Style?

Post by leledumbo »

This one is mine:
Pascal:

Code: Select all

unit MyUnit;

{$mode objfpc}{$H+}

interface

uses
  Unit1,Unit2;

type
  TSomeClass = class
  private
    FProp: LongInt;
  public
    property Prop: LongInt read FProp;
    constructor Create;
    destructor Destroy;
    procedure Proc;
    function Func: LongInt;
  end;

implementation

constructor TSomeClass.Create;
begin
end;

destructor TSomeClass.Destroy;
begin
end;

procedure TSomeClass.Proc;
var
  i: LongInt;
begin
  for i:=1 to 10 do begin
    ...;
  end;
  repeat
    ...
  until ...;
end;

function TSomeClass.Func: LongInt;
begin
  while ... do begin
    ...;
  end;
  case Result of
    0: ...;
    1: ...;
    otherwise ...;
  end; 
end;

end.
C:

Code: Select all

#define A_CONSTANT 255

typedef struct {
  int i;
  char c;
} TMyStruct;

int func() {
  for (int i=0;i<10;i++) {
    ...;
  }
  while (...) {
    ...;
  }
  return 0;
}

void proc() {
  do {
    ...;
  while (...);
  switch (...) {
    case 0:
      ...;
    break;
    case 1:
      ...;
    break;
    default:
      ...;
  }
}
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: What's Your Coding Style?

Post by AJ »

Hi,

I tend to follow this document which is pretty comprehensive and tries to justify the reasons for everything. There are a couple of unusual cases in there (for example, memberVariable_), but I find it makes code very readable. Ultimately, that will always come down to opinion, though.

Cheers,
Adam
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: What's Your Coding Style?

Post by Solar »

AJ wrote:...memberVariable_...
Blindfold? Cigarette?

OK, guys. Aim...



The Pro-POS coding style guide (C++) isn't perfect, and today I'd probably do some things a bit differently, but all in all it's sound.

PDCLib shows my C coding style.
Every good solution is obvious once you've found it.
Synon
Member
Member
Posts: 169
Joined: Sun Sep 06, 2009 3:54 am
Location: Brighton, United Kingdom

Re: What's Your Coding Style?

Post by Synon »

User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: What's Your Coding Style?

Post by NickJohnson »

This is my usual C style:

Code: Select all


// debugging comment (if C++ style supported)
/* regular comment */
/*** important comment ***/
/***** really important comment *****/
/***** SEPARATOR *****/

/*
 * Multiple line comment.
 */

#ifndef SENTRY
#define SENTRY

/* header stuff */

#endif/*SENTRY*/

#define CONSTANT 1
#define MULTI_WORD_CONSTANT 2
#define MACRO(n) ((n) + 42)
#define MULTI_LINE_MACRO(v) \
do { \
 ... \
} while (0);

struct stuff {
 int q;
 struct stuff *next_stuff;
} stuff_t;

int some_function(int a, char *b) {
 int foo;
 stuff_t bar;
 int *z = (void*) b;
 int i;

 switch (b[2]) {
 case 'a':
  ...
  break;
 case 'q':
  ...
  break;
 }

 if (a & (1 << 5)) {
  foo = 42;
 }
 else {
  foo = 66;
 }

 bar.q = 0;
 for (i = 0; i < foo; i++) {
  bar.q += (foo * 5) + i;
 }

 return (z) ? 123 : 321;
}

(all with 4-space tabs)

The best I can classify it would be slightly modified TBS with underscores.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: What's Your Coding Style?

Post by Solar »

Synon wrote:http://en.wikipedia.org/wiki/Indent_style#Variant:_1TBS
Nice summary. Didn't know Wikipedia had such a page.

In short, K&R, 1TBS and ANSI are all three acceptable (and mostly a matter of personal taste, with pros and cons for each of them). I vote for ANSI, though - the most verbose of the three. The others are various degrees of crap, IMNSHO, with the GNU style being the winner in the crap department by a wide margin.

I advise against having tabs (0x09) in your sources, though, except for Makefiles (where you can't go without them). Too often you get inconsistencies introduced by different editing environments. Some retain the tabs, some expand them to spaces. Pretty soon, you've got source where an indent is either a tab, 4 spaces, or 8 spaces, and it wastes your time getting it consistent again that's better spent writing code.

My suggestion is to use 4 spaces for indent.

Actually, in all projects where I got a saying in these things, source is automatically checked (among others) to contain only characters in the 0x20-0x7e range, and commit is rejected if this (or some other) constraint is violated. Saves you all kind of headaches (like code page / UTF-8 concerns...).
Every good solution is obvious once you've found it.
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: What's Your Coding Style?

Post by Firestryke31 »

Solar wrote: Actually, in all projects where I got a saying in these things, source is automatically checked (among others) to contain only characters in the 0x20-0x7e range, and commit is rejected if this (or some other) constraint is violated. Saves you all kind of headaches (like code page / UTF-8 concerns...).
So all of the source is on one line? (0x0A &| 0x0D)
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
Post Reply