PDA

View Full Version : Coding style?



Brian
10-19-2003, 02:48 PM
What kind of coding styles do you adopt?

edit: DAMMIT my poll is meant to have checkboxes, not radio buttons.

MrWizard
10-19-2003, 03:57 PM
I don't really understand your poll. For instance, you could have brackets on their own line and use the hungarian naming convention.

Thantos
10-19-2003, 04:04 PM
Hence his comment of:

DAMMIT my poll is meant to have checkboxes, not radio buttons

Which was done a good amount of time before your post.

Lurker
10-19-2003, 04:06 PM
Well, I'm not quite sure what category mine is:


// 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
}

Magos
10-19-2003, 05:20 PM
Allman all the way

major_small
10-19-2003, 07:25 PM
i can't really complete it without checkboxes... sometimes i put everything on one line, otherwise brackets get their own line...
int className::retVal(){ return returnedval; }
int className::calcVal()
{
val+=2;
return val;
}also, I don't know what some of them mean...

glUser3f
10-20-2003, 07:23 AM
I can't stand brackets having their own line, it looks messy to me :)

FillYourBrain
10-20-2003, 07:34 AM
Originally posted by glUser3f
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.

twm
10-20-2003, 07:40 AM
>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

Thantos
10-20-2003, 07:43 AM
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

FillYourBrain
10-20-2003, 07:46 AM
Because indention is horrible for that purpose. uh... I said "clearer", as in better than just indenting.

aka: in addition to, as well as, also, with, adjacent to, etc...



if(blahblahblah){
//stuff
//is second line part of a block? or what?
.
.
.
It just defines the block more clearly.

FillYourBrain
10-20-2003, 07:47 AM
Now who uses spaces instead of indents? I do...now yup. A company I worked for had that in their coding standards so I adopted it.

edit: fortunately visual studios has a setting that makes tab insert spaces instead of tab characters. That has made my life easier.

Lurker
10-20-2003, 08:08 AM
Originally posted by glUser3f
I can't stand brackets having their own line, it looks messy to me :)

Yay someone else agrees!!!

adrianxw
10-20-2003, 08:18 AM
Allman non Hungarian. The professionals choice.

glUser3f
10-20-2003, 08:21 AM
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.

Hmm, I prefer to put empty lines where I need them, not because I just started another loop.
And matching brackets is just as easy:


- for (int i = 0; i < SIZE; i++) {
| - if (done) {
| | break;
| - }
- }


This is just my opinion, maybe bacause the fist book I ever read about programming used the coding style, don't know.

twm
10-20-2003, 08:30 AM
>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

FillYourBrain
10-20-2003, 08:53 AM
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.

twm
10-20-2003, 09:03 AM
>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

FillYourBrain
10-20-2003, 09:08 AM
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.

Brian
10-20-2003, 09:41 AM
Kernel style is the best. http://pantransit.reptiles.org/prog/CodingStyle.html

joshdick
10-20-2003, 11:40 AM
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.

glUser3f
10-20-2003, 12:27 PM
>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:


if (var)
doSomething();

it's always:


if (var) {
doSomething();
}

The first is error-prone.

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 ...

FillYourBrain
10-20-2003, 12:39 PM
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

Brian
10-20-2003, 01:41 PM
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.

FillYourBrain
10-20-2003, 01:57 PM
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.

frenchfry164
10-20-2003, 02:42 PM
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;
}

Brian
10-20-2003, 02:44 PM
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.

which is why you avoid globals.

Speedy5
10-20-2003, 02:56 PM
/me hugs globals

globals rock especially in games. makes things easier where unneeded encapusulation and $$$$ where it should be make things harder.

frenchfry164
10-20-2003, 02:59 PM
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

UnregdRegd
10-20-2003, 03:02 PM
Behold: the one true coding style!


#include <iostream>
using
namespace std;

int main
() {
cout << "Hello "
<< "world?"
<< endl;

return 0; }


If this unique coding style needs a name, call it Unregisteredianoregisteredian.

jlou
10-20-2003, 05:46 PM
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.Maybe I'm biased because its part of my company's coding standards, but I really like using Hungarian.

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.

Lurker
10-20-2003, 05:53 PM
Originally posted by UnregdRegd
Behold: the one true coding style!

You should burn for your horrible style.

KurtSurge
10-20-2003, 05:55 PM
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.

twm
10-20-2003, 07:17 PM
>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.

Brian
10-21-2003, 01:45 AM
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 my variables to fit on one line.

rpet
10-21-2003, 08:58 PM
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...

Thantos
10-21-2003, 09:40 PM
- useless initialization (static int i = 0)
How is initalization a bad thing?

Zach L.
10-21-2003, 10:19 PM
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).

rpet
10-22-2003, 04:37 AM
Originally posted by Thantos
How is initalization a bad thing?

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:


int a = 0;
int b = 0;
float c = .0;
char *d = NULL;

Hey, I want my code to have meaning! ;-)

(BTW: The compiler already sets static variables to 0.)

ZakkWylde969
10-22-2003, 05:32 AM
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.

Thantos
10-22-2003, 07:38 AM
(BTW: The compiler already sets static variables to 0.)
I've always been taught to never ever relay (spelling?) on the compiler to initalize anything.

FillYourBrain
10-22-2003, 08:26 AM
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.

SMurf
10-22-2003, 10:43 AM
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.

FillYourBrain
10-22-2003, 11:26 AM
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)