Codestyle aka Coding Guidelines

Use common sense

These are conventions which someone try to follow when writing code for me, the projects which i'm involved in or generally, when altering, patching, enhancing or updating code i wrote. They are this way mainly for reasons of taste, however, sticking to a common set of formatting rules also makes it slightly easier to read through a projects sources. If you want to do something which depends on documents i wrote, did briefing of or simply spoke of implementation possibilitys, please try to follow these rules. In my opinion it's faster for a coder to read common formatted code and find bug's inthere instead of identifying a projects member by it's "code slang" and fall back to this members specific style and errors. It's like a natural language.

As such i don't suggest that you follow these rules slavishly, in certain cases it is OK (and in fact favorable) to stray from them.

Hugging braces

Braces in your code should look like the following example:

if( int i = 0; i < t; i++ ) {
	[...]
}
else {
	[...]
}
 
class Dummy() {
	[...]
}
 

Did you see the {}'s on that?

Use Tabs, not Spaces

Says it all, really.
We are online and filesize counts. Even for the PHP interpreter. Use 4-Char Tabs and source looks good.

Whitespaces

Lets start with some basics.

Conventional operators surrounded by a space character

a = (b + c) * d;
 

Language reserved words separated from opening parentheses by a white space

while( true ) {
 

Commas followed by a white space

function someFunction( a, b, c );
int d, e;
 

Semicolons followed by a space character, if there is more on line

for( int a = 0; b++; c < d ) { &hellip;
doSomething(e); doSomething(f);	// This is probably bad style anyway
 

When declaring class inheritance and in a ? construct, colons should be surrounded by white space

class BusWheel : public RubberInflatable {
(isNight) ? colorMeDark() : colorMeBright();
 

Switch / Case constructs

switch( cmd ) {
 
	case k_SaveCmd:
		this->save();
		break;
 
	case k_LoadCmd:
		this->_file = (char*) data;
 
	case k_PlayCmd:
		this->close();
		this->play( this->_file );
		break;
 
	default:
		Dialog::handleCommand( sender, cmd, data );
}
 

Naming

Constants

Basically, you have to choices:
define( 'SOME_KLUDGY_CONSTANT_NAME', true );
 
or
define( 'k_SomeKludgyConstantName', true ); // notice the k_ prefix		
 

Global Elements

g_ prefixed global elements make it easyier to identify static classes/singletons or variables.
$g_commandPipeline->sendCommand( receiver, SOME_COMMAND, data, $this );
 
or
g_engine.timer.getElapsed();
 

Classes

Mixed case starting with upper case
class MyClass() {
 

Class members

Class members are a bit more complicated.
Following tries to close the gap between different programming languages and object scopes.

a public member / method:
char*	somePublicVariable;
 
void somePublicMethod() {
	return true;
}
 
NOTE: public members/methods are written like they are. No prefixes.
a private member / method:
char*	_somePrivateVariable;
 
void _somePrivateMethod() {
	return true;
}
 
NOTE: private members/methods are written with a '_' prefix.
a protected member / method:
char*	__someProtectedVariable;
 
void __someProtectedMethod() {
	return true;
}
 
NOTE: protected members/methods are written with a '__' prefix.

Class methods

mixed case, starting with lowercase.
void thisIsMyFancyMethod();
 
NOTE: Like above. Private/protected methods should be also prefixed with '_' to be easier identified within 'semi object oriented' environments like Javascript or PHP

Language specific functionality

Needs to be done. Currently i like to write about C++ typedefs and template naming conventions

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.
  • No HTML tags allowed.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.
© 2010 khype.org.. Drupal theme by Kiwi Themes.