# What code convetion do you use?

## Recommended Posts

riruilo    218
Hi! What code convetion do you use in your company? Or do you create a new one? (I'm not talking about when I code alone) I will apreciate if you write any link. Thanks a lot.

##### Share on other sites
djz    215
Quote:
 Original post by ricardo_ruiz_lopezHi!What code convetion do you use in your company? Or do you create a new one?(I'm not talking about when I code alone)I will apreciate if you write any link.Thanks a lot.

I try and emulate this as much as possible for C#. I don't follow it completely, but for the most part, emulating the standards set by MSDN example code can do you no harm.

My company has no official coding standard, unfortunately.

##### Share on other sites
Codeka    1239
We follow the MSDN one pretty closely. It doesn't really make much sense to invent a new one when so much of your code makes such heavy use of the standard libarary, it'd look a little out of place, really.

Edit: Oh sorry, it's not specific to just C#... I don't know why I read that into the OP :) For C++ I tend to use a standard library/boost coding style, but that's not in a company environment. In my last job, we wrote C++ similarly to how we do C# now (though with a 'C' prefix for class names... a strange practice that I do not understand to this day, probably borrowed from ATL/MFC).

[Edited by - Codeka on February 8, 2010 6:07:34 PM]

##### Share on other sites
Promit    13246
I follow the C# recommended spec pretty much verbatim, in both C++ and C#. Other languages, I sorta make it up as I go >_<

##### Share on other sites
Hodgman    51328
Apps Hungarian (not Systems Hungarian).

There was actually a big email-fight at work today regarding standards, resulting in this:

##### Share on other sites
Telastyn    3777
My work uses the default MSVS settings, PascalCase for classes/publics, camelCase for locals/params/privates, comment only the peculiar cases... pretty standard stuff. For C# and java anyways. C++ is pretty standard C++ notation.

For my personal projects I change the default settings to not put { on a newline. I also will always use braces for conditionals, and always parenthesize a return value.

And oh, how I await the day that IDE autoformatting is good enough to let everyone do whatever they want and then reformat it to some convention for check-in.

##### Share on other sites
Saruman    4339
When we started up the company I didn't want to spend a lot of time on style so we used the Google C++ style Guide with very little modifications. Also all of the C# code is following the C# recommended spec.

##### Share on other sites
mutex    1111
Braces on new line, spaces instead of tabs, etc., the typical MS C# style. The specifics don't matter too much since it's just formatting; what's important is that you pick a standard and apply it consistently.

What's considerably more important are things like naming, exception usage, and general code design. For this I highly recommend Framework Design Guidelines by Krzysztof Cwalina and Brad Abrams. Despite its name I find it highly applicable to non-framework code. The chapter on naming in particular is hugely important and useful.

##### Share on other sites
Oberon_Command    6087
I'll generally use whatever style is most common in the language I'm using.

My bracing in C++ is different from some styles in that it puts braces on newlines for bracing levels deeper than two levels or sections that are deeper than one level and are <10 lines long. Eg:

// C#public class Foo{   public static void Main()   {      int someVariable = 0;      Console.WriteLine(someVariable);   }}// Javapublic class Thing {   public void bar () {      System.out.println("Foobar");   }}// C++class something{public:   something() {      // this is only a couple of lines      foo();      bar();   }   void derp()    {       if (/* test here */)          foo();       else          bar();   }};// still C++void free_function(){   if (/* test */)   {       if (/* another test */) {         // something         // something      } else {         // something else         // something else         // something else      }   } else {      // something else   }}; Lisp(defun factorial-demo (x)   (if (= x 1)     1     (* n (factorial-demo (- n 1)))))// Scalaobject Hello extends Application {   def foo = System.out.println("Foo");   def bar (x: int) = {     if (x == 5) {       System.out.println("bar");     } else {       System.out.println(x);     }   }}

##### Share on other sites
riruilo    218
Thanks a lot, very interesting.

I found a lot of info here:
http://en.wikipedia.org/wiki/Programming_style

All opinions are welcome.

By the way, does anyone work in a company without them? What do you think?

Thanks.

##### Share on other sites
riruilo    218
Oberon_Command, I see you use SCALA, have you ever create a server with that? What do you think about SCALA language?

Thanks.

##### Share on other sites
Telastyn    3777
All companies have coding styles. Not many have it formalized. As mentioned before, consistency is key. Programmers working together will naturally trend towards some compromise.

##### Share on other sites
Oberon_Command    6087
Quote:
 Original post by ricardo_ruiz_lopezOberon_Command, I see you use SCALA, have you ever create a server with that? What do you think about SCALA language?Thanks.

I haven't used it for much yet - I think the most complex thing I've written in it was a SmallTalk parser. I think it has potential, though.

##### Share on other sites
XTAL256    106
Quote:
 Original post by TelastynAnd oh, how I await the day that IDE autoformatting is good enough to let everyone do whatever they want and then reformat it to some convention for check-in.

Surely there are auto-formatters that can do that now. If it can format code to a specific convention then it should be able to format from one to another.

I use an ancient Smalltalk IDE at work and even it has auto-formatting. Theoretically, we could each use our own coding style (or at least, one that was supported by the auto-formatter) and it would reformat whenever anyone viewed the code in their IDE.

##### Share on other sites
Quote:
Original post by djz
Quote:
 Original post by ricardo_ruiz_lopezHi!What code convetion do you use in your company? Or do you create a new one?(I'm not talking about when I code alone)I will apreciate if you write any link.Thanks a lot.

I try and emulate this as much as possible for C#. I don't follow it completely, but for the most part, emulating the standards set by MSDN example code can do you no harm.

My company has no official coding standard, unfortunately.

Good blog post, I pretty much implement those conventions aswell. I'm very picky when it comes to coding styles.

##### Share on other sites
Telastyn    3777
Quote:
Original post by XTAL256
Quote:
 Original post by TelastynAnd oh, how I await the day that IDE autoformatting is good enough to let everyone do whatever they want and then reformat it to some convention for check-in.

Surely there are auto-formatters that can do that now. If it can format code to a specific convention then it should be able to format from one to another.

I use an ancient Smalltalk IDE at work and even it has auto-formatting. Theoretically, we could each use our own coding style (or at least, one that was supported by the auto-formatter) and it would reformat whenever anyone viewed the code in their IDE.

None that I am aware of. At least none that do it auto-magically for checkin. You could probably set something up manually, but that rather defeats the purpose, and source control repos are finicky enough to make the entire exercise worthless unless the auto-formatter can return the code to precisely what it was.

##### Share on other sites
XTAL256    106
Quote:
 Original post by TelastynNone that I am aware of. At least none that do it auto-magically for checkin. You could probably set something up manually, but that rather defeats the purpose, and source control repos are finicky enough to make the entire exercise worthless unless the auto-formatter can return the code to precisely what it was.

I guess a language like C++ would be a bit more complicated to format. Smalltalk is a fairly simple language in the sense that it has very few keywords and no specific control structures like if's or while's. So it would probably be a lot easier to auto-format than something like C++.

##### Share on other sites
riruilo    218
About autoformatting, I guess there are some topics as naming conventions (m_elapsed_time, ElapsedTime, intElapsedTime, m_ElapsedTime) that cannot be easily managed, in other words, programmers must follow these conventions, not a program.
What do you think?

##### Share on other sites
Telastyn    3777
Well some of that might be able to get auto-formatted, but there is no programmatic replacement for a programmer using good names.

##### Share on other sites
rip-off    10979
The same one as the rest of the file. If not apparent, the same style as the rest of the project. If not apparent, maybe this is a new project (typically a couple of lines of bash), default to whatever I feel like, strongly influenced by the languages "native" style.

Quote:
 And oh, how I await the day that IDE autoformatting is good enough to let everyone do whatever they want and then reformat it to some convention for check-in.

I'd love it.

##### Share on other sites
Zipster    2365
I was trying to see if Perforce supported such a feature, where you could modify code before it was submitted using a custom app/script (in a similar vein to its RCS keyword expansion), but alas there are no triggers that let you modify files before they're submitted. You can, however, fail the submission of a changelist if any files doesn't meet the coding standards/guidelines/conventions. But that's a little Draconian for my tastes :) You could also just send out an embarrassing e-mail!

##### Share on other sites
Fenrisulvur    186
I drew upon this a while ago, from memory.