views:

384

answers:

7

Where to read up on (the best-practice in) source code formatting, for individual languages i.e. what are the conventions for naming variables etc., for indentation and where to place braces, how and where to include comments, etc.

For example for C Kernighan and Ritchie's book The C Programming Language and Linus Torvalds' Linux kernel coding style guide or Google's C++ Style Guide and for Perl at least perlstyle - Perl style guide.

Edit: Perhaps I should have asked "How to get a good start with coding styles".

+3  A: 

I wouldn't give too much on such style-guides. They are a good start, but in the end you or your team will develop own coding-styles.

EDIT: Rob Kam made a comment, that he want to find out more about these 'starting-conventions'.

That's a good point. But mostly you will join existing projects, open-source-projects or commercial, and the people working on it already have a code-convention. In a company probably exists already a code convention. If you start a new project, start with the conventions of another project you did before.

If you really start a new project with a new language you didn't used before ... then you might start with conventions of another language that is somehow similar (i.e. for a new oo-language you can use conventions from Java or Smalltalk) and adjust your conventions as you gain experience for the new language. As I started programming in Java I used my Pascal-coding-conventions. Some of the pascal-conventions didn't work very good for Java, but with the time I dropped some old conventions and developed new.

S.Lott had the idea to look into an open-source-project written in the language of your choice. That's a good idea to get some suggestions about good conventions. Srikanth had the good idea to ask for good conventions here at Stackoverflow. A very good idea, because with the time Stackoverflow will become a good reference for coding-conventions this way.

Mnementh
Agreed. For almost any language there's no such thing as a best-practice code layout: various options are equally good. The purpose of a style guide is to stop a few evil things, and ensure that the team's code looks fairly consistent. Not to make the formatting "right" in any objective sense.
Steve Jessop
It's the good start part that I'd like to find out more about.
Rob Kam
I edited my answer, to give also some hints about these start-parts. Hope this helps.
Mnementh
A: 

Hello and good day for everyone

Well i must say that it depends It depends on if you work for a company or if you work for you ;)

If you work for a company you mus accept the naming style and other rules

and for you, you only need to establish a style that everybody including yourself can instantly understand what and how

the brackets and others is your way

but as you said there are individual languages so they have own rules

and like Mnementh said every team have a way to coding maybe stricted or free way

Thats all with no more.... bye bye

yeradis
+1  A: 

What is better than a book is to locate open-source code in your preferred language.

Rather than a book -- the work of a few authors and editors -- working source code is a community project that meets highly-evolved community standards.

When asked by client to help with coding standards, I prefer to find a good Open Source project and provide a short standards document that says "Make it look like xxx". A good example that I use for Java style is the Apache Ant project.

Everyone has their favorite open source example project. Read some real-world, working code, and pick a favorite. Emulate that project.

S.Lott
+1  A: 

I would use Google for it. For example, to search for Java Coding Guidelines, I will use this query or post in Stack Overflow and ask what are the recommeded coding standard guidelines for Java. It's difficult to point to a single source for all the languages.

I would suggest that you choose one for your company/project and stick with it and make sure it's a good standard.

artknish
A: 

I would really recommend to read uncle bob's Clean Code if you are concerned about code style issues.

JtR
+1  A: 
Bill the Lizard
PMD and Checkstyle are very good tools to enforce a code-convention, but they are not so good to create your own conventions. In my opinion these tools are the second step.
Mnementh
They can be customized to reflect the set of conventions that you choose. I was suggesting that the OP check out these tools to see what conventions are commonly used.
Bill the Lizard
Hmm, in Checkstyle you have no preconfigured convention. But I think somewhere is circulating a checkstyle-rulefile with the Sun-Coding-conventions. In PMD however, the basic-ruleset is a good start. But it covers only some basic stuff.
Mnementh
Yeah, I was looking for that for checkstyle too. I use PMD, myself. Only a fraction of the available rules are in the default ruleset, but it is a good start.
Bill the Lizard
A: 

There is the C and C++ style guides, a short page of links to style guides, coding rules, and discussions from USENET, maintained by Christopher Lott.

Rob Kam