-
February 6th, 2009, 12:01 PM
#1
[RESOLVED] Coding standards (rules)
Hello guys!
If it's alright, I was wondering if some of you could share with me the policy on the conding standards of the company you guys work for. Example would be...
1. Use typedef for all data types
2. Use the following block style[code]void foo() {
...
}
3. Capitalize the first letter of a function
4. No Hungarian notation
5. And so forth...
I've read the general coding standards
but I'm far more interested in hearing from all the working professionals here,
so that in the future when I get a job, I'd be better prepared.
As always, thanks!
-
February 6th, 2009, 12:06 PM
#2
Re: Coding standards (rules)
Here's a pretty good one. It covers a lot of things, so it's easy to compare it to your own style.
-
February 6th, 2009, 12:08 PM
#3
Re: Coding standards (rules)
Have you read C++ Coding Standards by Sutter and Alexandrescu? There is alot more to be considered than just stylistic guidelines.
-
February 6th, 2009, 12:36 PM
#4
Re: Coding standards (rules)
My company doesn't have a single monolithic set of coding standards, but there are guidelines within some specific projects.
Personally when I'm working with existing code I just follow the extant style, and when I'm writing my own I impose my preferred style. Really not a fan of your #2 incidentally.
-
February 6th, 2009, 12:49 PM
#5
Re: Coding standards (rules)
IMPO, Braces should be on their own line 99.5% of the time. The other 0.5% it may be acceptable to put the entire "construct" on one line:
Code:
void foo() { return 1; }
This is typical when there are many of the same time sequentially in the file...
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
February 6th, 2009, 01:11 PM
#6
Re: Coding standards (rules)
Originally Posted by potatoCode
2. Use the following block style[code]void foo() {
...
}
As always, thanks!
Words cannot describe how annoying I find that style of brace use.
Code:
void foo()
{
//code
}
is so much easier to see.
I use hungarian notation frequently.
I'd add that white space is important
Don't write multiple statements on a single line
Don't include superflous comments
Use meaningful variable and function names
Last edited by GCDEF; February 6th, 2009 at 01:14 PM.
-
February 6th, 2009, 01:17 PM
#7
Re: Coding standards (rules)
The most important thing is: Think through every function you add to an interface completely. If knowing how to use it requires any understanding of the internal implementation, you're doing it wrong.
-
February 6th, 2009, 01:25 PM
#8
Re: Coding standards (rules)
Originally Posted by GCDEF
I use hungarian notation frequently.
This is one I find very interesting. Consider how many class names there are and the FLA's (First Letter Acronyms) and other abbreviations regulrly clash.
For example what your use you for variables of the following types:
Code:
std::vector<int>
std::vector<double>
std::vector<vector<double>
std::vector<std::pair<int,double>>
std::vector<std::pair<doublie,int>>
I would find svspidData and svspdiData to be very confusing to refer to the last two types...
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
February 6th, 2009, 01:32 PM
#9
Re: Coding standards (rules)
Originally Posted by TheCPUWizard
For example what your use you for variables of the following types:
I think that it depends on which variant of Hungarian notation is used. Joel Spolsky points out in Making Wrong Code Look Wrong that Simonyi's original Hungarian notation concept was concerned with the semantics of the variables, not their types as declared in the program. I tend to be more sympathetic towards this "Apps Hungarian", although I still do not understand why such an encoding is really necessary when spelling out the word would be easier to read for those uninitiated to the abbreviations.
-
February 6th, 2009, 01:38 PM
#10
Re: Coding standards (rules)
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
February 6th, 2009, 01:46 PM
#11
Re: Coding standards (rules)
Originally Posted by laserlight
I think that it depends on which variant of Hungarian notation is used. Joel Spolsky points out in Making Wrong Code Look Wrong that Simonyi's original Hungarian notation concept was concerned with the semantics of the variables, not their types as declared in the program. I tend to be more sympathetic towards this "Apps Hungarian", although I still do not understand why such an encoding is really necessary when spelling out the word would be easier to read for those uninitiated to the abbreviations.
Even semantic usage gets a bit complex to say the least...
I find that the vast majority of the time simply hacing the class name in ProperCase a local variable with the exact same name in camelCase is sufficient.
For "Collection" classes, I tent to end with the Collection Type (CarList, StateMap, etc) variables can then simple be plural)
(psuedo code)
Code:
CarList cars;
foreach (Car car in cars)
{
}
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
February 6th, 2009, 02:04 PM
#12
Re: Coding standards (rules)
Originally Posted by Russco
Yuck!
Why??
I know in some cases its forced on us if you program in MFC or win32 but even then I'd only use it the absolute minimum you can get away with.
Habit I guess. Mainly just for PODs.
-
February 6th, 2009, 03:05 PM
#13
Re: Coding standards (rules)
Originally Posted by TheCPUWizard
IMPO, Braces should be on their own line 99.5% of the time.
Originally Posted by GCDEF
Code:
void foo()
{
//code
}
So you also write
Code:
if (a==b)
{
foo();
}
else
{
bar();
}
?
And you find that easy to read?
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
February 6th, 2009, 03:10 PM
#14
Re: Coding standards (rules)
Personally I usually omit braces entirely when it's a single-line statement. Either that or use ? notation.
-
February 6th, 2009, 03:12 PM
#15
Re: Coding standards (rules)
Take a look at this. Evidently, Bjarne helped develop some coding standards.
http://www.research.att.com/~bs/bs_faq2.html
I used to use hungarian notation a bit. It is flexible. I like the idea of including scope into variable names to avoid confusion between locals and members. Globals should be within a namespace at the very least which helps to clarify when using the '::' operator to indicate where the variable is located. It gets a bit ridiculous when you try to include type info into the variable name.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|