What kind of coding styles do you adopt?
edit: DAMMIT my poll is meant to have checkboxes, not radio buttons.
Printable View
What kind of coding styles do you adopt?
edit: DAMMIT my poll is meant to have checkboxes, not radio buttons.
I don't really understand your poll. For instance, you could have brackets on their own line and use the hungarian naming convention.
Hence his comment of:
Which was done a good amount of time before your post.Quote:
DAMMIT my poll is meant to have checkboxes, not radio buttons
Well, I'm not quite sure what category mine is:
Code:// if / functions / classes / structs / loops:
int blah(float one, char two) { //space between ) and {, but on same line, ", " seperating each variable
statement; // instead of a space for an indent, I use tabs
}
Allman all the way
i can't really complete it without checkboxes... sometimes i put everything on one line, otherwise brackets get their own line...also, I don't know what some of them mean...Code:int className::retVal(){ return returnedval; }
int className::calcVal()
{
val+=2;
return val;
}
I can't stand brackets having their own line, it looks messy to me :)
:confused: That's weird. Completely opposite to most people. The only reason for having them on the same line is to save space and be able to see more lines at one time. Giving them their own line gives a clearer picture of the code block.Quote:
Originally posted by glUser3f
I can't stand brackets having their own line, it looks messy to me :)
>Giving them their own line gives a clearer picture of the code block.
Because indention is horrible for that purpose. :D We should inform Python users before any more damage is done. :p
Having brackets on their own lines in combo with proper indentation makes it a lot easier to match up brackets.
Now who uses spaces instead of indents? I do...now
uh... I said "clearer", as in better than just indenting.Quote:
Because indention is horrible for that purpose.
aka: in addition to, as well as, also, with, adjacent to, etc...
It just defines the block more clearly.Code:if(blahblahblah){
//stuff
//is second line part of a block? or what?
.
.
.
yup. A company I worked for had that in their coding standards so I adopted it.Quote:
Now who uses spaces instead of indents? I do...now
edit: fortunately visual studios has a setting that makes tab insert spaces instead of tab characters. That has made my life easier.
Yay someone else agrees!!!Quote:
Originally posted by glUser3f
I can't stand brackets having their own line, it looks messy to me :)
Allman non Hungarian. The professionals choice.
Hmm, I prefer to put empty lines where I need them, not because I just started another loop.Quote:
Originally posted by FillYourBrain
:confused: That's weird. Completely opposite to most people. The only reason for having them on the same line is to save space and be able to see more lines at one time. Giving them their own line gives a clearer picture of the code block.
And matching brackets is just as easy:
This is just my opinion, maybe bacause the fist book I ever read about programming used the coding style, don't know.Code:- for (int i = 0; i < SIZE; i++) {
| - if (done) {
| | break;
| - }
- }
>uh... I said "clearer", as in better than just indenting.
Joke...funny...laugh. One of the biggest problems I see is that most people around here don't have a sense of humor when it comes to computers. You guys are missing out on the best part. :)
>//is second line part of a block? or what?
Well, simply by looking up and to the right a bit you'll see an opening brace and have your question answered. :D
but the question is about positioning. looking up and to the right is not in line with the line of code in question. Having it up there does nothing more than hide it.
>looking up and to the right is not in line with the line of code in question.
Can you honestly say that this is a problem? The only way I can imagine that it would be is if you're using a line based editor that only lets you see one line at a time, or if the line in question is really long and the brace is way out in the ionosphere where nobody could possibly see it. The latter is obviously too long and should be made shorter, the former is so uncommon it's scary. :)
Last I checked, the eye move operation was very cheap. :D
I never said it was a problem. Only that the start of a block having the same indent as the rest of the block makes it easier and faster to recognize as a block. We're talking about coding style here, not armageddon.
Kernel style is the best. http://pantransit.reptiles.org/prog/CodingStyle.html
Ya know, despite my deeply held beliefs regarding how code should be formatted, I'm still going to have to code however my professor or employer wants me to. Sad but true.
>looking up and to the right is not in line with the line of code in question.
No need to look up to the right, just look down, I don't type stuff like:
it's always:Code:if (var)
doSomething();
The first is error-prone.Code:if (var) {
doSomething();
}
As for Hungarian notaion, I use it for scope, not for variable types:
m for member
s for static
g for global (I rarely use globals though)
I can't stand stuff like:
class CFont;
int nCount;
LPSTR lpszName;
grr just thinking of those examples ...
well in spite of the outspoken minority here, it looks like Allman is most popular as I expected. Whitesmiths even got a vote. Curious. My company uses that. I'm not a big fan of it, but its better than K&R
I also think hungarian notation is retarded. You should know what all your variables are anyway, and you should only have a few of them per function.
hungarian notation is indeed retarded. Knowing what all your variables are might be asking a bit much though. A few per function, yes. I agree. But there is the matter of globals, statics and members. I like the m_ notation as well as the g_ and s_. When it comes to types though I think that it is just stupid.
I don't know what the different style names are. I'll give some examples and see what you guys think.
[code]
// Functions
int main(int argc, char** argv)
{
DoSomething(true, 5, 0.6f);
int MyVariable;
for (int i=0; i<5; i++)
{
KillACat();
}
return 0;
}
which is why you avoid globals.Quote:
Originally posted by FillYourBrain
hungarian notation is indeed retarded. Knowing what all your variables are might be asking a bit much though. A few per function, yes. I agree. But there is the matter of globals, statics and members. I like the m_ notation as well as the g_ and s_. When it comes to types though I think that it is just stupid.
/me hugs globals
globals rock especially in games. makes things easier where unneeded encapusulation and $$$$ where it should be make things harder.
I only declare classes global. Everything else I use is either inside a function or in the classes. I try to stay away from variables stored in functions if it's not a waste of RAM
Behold: the one true coding style!
If this unique coding style needs a name, call it Unregisteredianoregisteredian.Code:#include <iostream>
using
namespace std;
int main
() {
cout << "Hello "
<< "world?"
<< endl;
return 0; }
Maybe I'm biased because its part of my company's coding standards, but I really like using Hungarian.Quote:
Originally posted by Brian
I also think hungarian notation is retarded. You should know what all your variables are anyway, and you should only have a few of them per function.
When you have millions of lines of code, most of it from other people, knowing the variable type just by looking at it really helps. Even a well named variable, like userSelectedDisplayType, could be many different things - a string, int, long, enum, class, etc. I agree that Microsoft takes this to an extreme, but the idea is still a good one.
Also, I follow the startWithLowerCase andEachAdditionalWord getsCapitalized style, but it just feels wrong that one word doesn't get the capital letter. I'd much rather have iStartWithLowerCase, bAndEachAdditionalWord, or sGetsCapitalized.
Maybe its not necessary in smaller or more beautifully written code libraries, but for me hungarian is definitely preferred.
You should burn for your horrible style.Quote:
Originally posted by UnregdRegd
Behold: the one true coding style!
I think the Allman style looks messy because it spreads things out too much.
I use Hungarian notation all the time, but only on certain variables. I never use it on the standard variables like integer, string, etc. But any uncommon variable, i'll put a prefix on.
>Behold: the one true coding style!
You are my god! If I used C++ more often I'd adopt your style, but I don't, so I won't...sorry. :p
In all honesty, I prefer K&R bracing for everything in my own projects, but if I ever need to match a project style policy I'll do so. I have no problem reading or writing any of the other bracing styles.
I prefer my variables to fit on one line.Quote:
Originally posted by jlou
Also, I follow the startWithLowerCase andEachAdditionalWord getsCapitalized style, but it just feels wrong that one word doesn't get the capital letter. I'd much rather have iStartWithLowerCase, bAndEachAdditionalWord, or sGetsCapitalized.
I prefer the K&R style, but using indent 2, not 4 or even 8. *brrr*
I try to keep everything as simple as possible. Most coding styles want to make things clear but instead, they make everything more complex.
Bad examples (in my opinion) are:
- useless initialization (static int i = 0)
- reinvention of the stdlib (MyMemCpy)
- most typedefs (typedef unsigned int uint)
- useless #defines (#define LOCAL(x) static x)
- functions defined but never used
- inline assembler for non time-consuming functions
- data structures (queues, stacks) implemented via pointers where a simple array would do
- deeply nested functions (call stack > 20)
and, especially: Extreme OOP! I believe that everything is a bunch of computer memory, and not that "everything is an object". ;-) There is a Win32-version of the (excellent!) game "Abuse" out there. Take a look at it and you know what I mean...
How is initalization a bad thing?Quote:
- useless initialization (static int i = 0)
I dislike Hungarian notation. In principle, it's a great idea, but in general (there will of course be exceptions), the scope of any variable should be small enough, that it is not difficult to determine from its context what it is and what it does.
I always put braces on their own lines.
My capitalization scheme depends on the language. In C++, I keep everything lowercase and separate words with underscores. In Java, classes start with a capital, variables and functions with a lowercase letter. Each new word gets a capital letter.
Most styles I can live with. There are a few things that really get on my nerves, though:
- Excesive useless macros
- The useless typedefs (though I find them useful in templatized classes)
- "Static classes" with all private constructors, assignment operators, etc. They should be namespaces.
- Lack of coherent OOD design (i.e. don't do too much or too little).
Initialization is not a bad thing, but _useless_ initialization is. Just because it is nothing good for. Some people often assign variables an initial value that they never use. Like this:Quote:
Originally posted by Thantos
How is initalization a bad thing?
Hey, I want my code to have meaning! ;-)Code:int a = 0;
int b = 0;
float c = .0;
char *d = NULL;
(BTW: The compiler already sets static variables to 0.)
It depends on the type of program I guess. A larger OOP program in C++ would be more of an organized with what most books teach. Smaller ones I tend to cheat a bunch and put 2 to 3 things a line.
I've always been taught to never ever relay (spelling?) on the compiler to initalize anything.Quote:
(BTW: The compiler already sets static variables to 0.)
if you ever intend on entering a second thread static and global should be avoided where possible so most of your variables will NOT be initialized by the compiler.
I think Hungarian notation is a disease you catch when you read MSDN. When I first started out I thought null-terminated strings were prefixed "sz", then MS use that and also "lp" for them, which could be a pointer to anything. :mad:
Do you think all MS programmers are as well clued-up on notation as they think they are?
Also, Visual C++ likes to force me into Whitesmiths whenever I do "switch(...)" and start a new line. Weird.
Whitesmiths can be set up in the tools/options area. "indent opening brace" and "indent closing brace" Are you sure you aren't set up that way? (My company does that == annoying)