PDA

View Full Version : What's your prefered program layout?



Pages : [1] 2

Brian
02-26-2002, 12:11 PM
Some people do:


#include <stdio.h>

int main(void) {
printf("Hello, world,");
if(1) {
printf(" and goodbye.");
}
}


but I do:



#include <stdio.h>

int main(void)
{
printf("Hello, world,");
if(1)
{
printf(" and goodbye.");
}
}


I don't see why the hell they do the first one, because it is near impossible to keep track of the brackets. Are they stupid or do they have a reason?

Also, assembly can be used from within C[++] programs, and therefore we should have an assembly board. We need one.

Oh, this is my 500th post. I have no life.

Barjor
02-26-2002, 12:56 PM
I agree with you brian. To bad that both K & R and Stroustrup is doing it the wrong way.

Brian
02-26-2002, 01:00 PM
Maybe it's a pre-ANSI whitespace thingy, and "old school" programmers haven't changed.

ober
02-26-2002, 01:10 PM
I personally like:


#include <stdio.h>
int main(void){printf("Hello, world,"); if(1){printf(" and goodbye.");}}

Prelude
02-26-2002, 02:12 PM
>Are they stupid or do they have a reason?
:confused: Well, since I use a variation of K&R I can safely say that there is a reason. Too much vertical white space makes a program look messy, and since code SHOULD be indented properly there's no need to line up brackets, just line up the indention.

Here's my preferred style


#include <stdio.h>
#include <stdlib.h>

int main ( void )
{
printf ( "Hello world" );
if ( 1 ) {
printf ( ", and goodbye!\n" );
}
return EXIT_SUCCESS;
}

However, I prefer not to use braces in my if statements or loops if there is only one statement in the body. My functions have the starting and ending braces on their own line but everything else with braces has the opening brace one space after the statement and the closing brace on its own line.

I find the 'every brace on its own line' method better for people who are starting out to get a feel for the structure of programs, but eventually a style like that will feel more cumbersome than variations of K&R, which is why K&R tends to be more common among more experienced coders. An exception that I see is the indented braces:


#include <stdio.h>
#include <stdlib.h>

int main (void)
{
printf ("Hello world");
if (1)
{
printf (", and goodbye\n");
}
return 0;
}

For some reason I find this style very hard to follow, despite indention, but how you write your programs is a matter of personal preference unless your employer gives specific instructions on formatting.

-Prelude

stupid_mutt
02-26-2002, 02:19 PM
#include <waga.h>

int main()
{

printf("Your mom");

return 0;
}

Govtcheez
02-26-2002, 02:37 PM
int message[]={0x40,0x08,0x1D,0x07,0x00,0x03,-0x4F,0x57,-0x08,0x03,-0x06,-0x08,-0x43,-0x17};
main(){int i,*j,*t;for(i-=(!i+i+!!i+~i+i),j=message;i<13;i+=-(i+~i)){t=j;++j;*j+=*t;putc(*j,stdout);};};

That's better :)

Brian
02-26-2002, 02:37 PM
You spelt "mum" incorrectly.

ggs
02-26-2002, 02:50 PM
hey govt, you kept my hello world :D

Govtcheez
02-26-2002, 02:55 PM
> hey govt, you kept my hello world

I found it on an old post this afternoon... Actually, I was thinking about it when I saw this thread :D

Salem
02-26-2002, 03:03 PM
< points at Prelude's answer and agrees >

The indentation by itself should tell you the flow of the program - the braces only matter to the compiler, and it really doesn't care about indentation and {} placement.

To that extent, I prefer to place them where they have the least visual impact, because ultimately, they are only of use to the compiler, and (if your indentation is correct), they add nothing to program understanding.

You only have to look at all the badly posted code on this board to see this - sure all the {} are correct, but with it all indented to the left side of the screen, actually figuring out what the code does is a real challenge. The placement of the {} doesn't help at all, and you still have to format it to stand any real chance of understanding it.

And to save you getting in a mess, consider one of these approaches.
1. Get a syntax-aware editor which automatically includes and indents the }, each time you type a {.

2. At the very least, get an editor which does {} matching for you.

3. If you don't have 1., get into the habit of typing the } immediately after typing the {. Sure its a few extra cursor movements, but it beats trying to find out how many } you need after typing a page of nested code.

Oh, and K&R evolved when all code was written on 80*24 terminals, and it was a waste of screen space to have {} on lines by themselves.

phantom
02-26-2002, 06:01 PM
A bad habit I have is ->

#include <stdio.h>

int main(void)
{
printf("Hello, world,");
if(1) {printf(" and goodbye.");}
}

For some unknown reason if I only have one command in between brackets I make it one line. With comments of course :D

novacain
02-26-2002, 10:17 PM
Depends on where and what you started on.

I write in WIN32 and have long nested functions.
If I can I will leave out all the chicken lips I can.



if(!bMoreThanOneLine) DoThatVoodooYouDoSoWell(bThing);


I get legacy code or complex maths functions from others who started on punch cards. They tend to declare one letter variables to save time. Very hard to understand no matter the indentation.



for (i=0; i<ne; i++)
{
(*(C+i)) = (double *) malloc (nn*sizeof(double));
if ((*(C+i)) == NULL) return (OUT_OF_MEM);

for (j=0; j<nn; j++) (*((*(C+i))+j)) = 0.0;
for (j=0; j<ne; j++)
{
(*(*(C+j))) += s*sm;
(*((*(C+j))+ne)) += (s*y*sm);
s *= x;
}

for (j=1; j<ne; j++)
{
(*((*(C+ne-1))+j)) += s*sm;
s *= x;
}
}

phantom
02-27-2002, 12:29 AM
I get legacy code or complex maths functions from others who started on punch cards. They tend to declare one letter variables to save time. Very hard to understand no matter the indentation.

Is it ever commented? Or do you get it excactly as you wrote it?

novacain
02-27-2002, 12:40 AM
Thats it. As I got it.

That bit is only a part of the function to take a line on a greyscale image, convert to a start point, length and array of coef (so smaller to pass around network). Later to be 'reconstituted' and drawn to a screen or saved to file and compared to later captures. (we do digital video image processing inc OCR on moving objects)

No comments in punch cards, no comments ever. Thing is, I don't realy have to understand it until it goes wrong, I just don't like including things in my programs I don't understand, and trying to fix them later.

phantom
02-27-2002, 01:03 AM
Thats it. As I got it.

Eeeeeeeeeeeeeek! If I saw that code the first thing I would do is reach for the rum and coke, then try to comment it!

last year I did a major project for my course and I started programming in June. Even though I used variable names and did basic commenting I still got lost in my own code by november :(

adrianxw
02-27-2002, 02:52 AM
Numerous studies have shown that Allman, (or variations), of Allman format have the highest readability level.

One series of tests, (the one's I used to convince my company to change formats!), gave a series of programmers a series of functions written in various formats, and averaged the time taken to deliver a correct description of what the function was doing. After many many trials, Allman was a clear winner.

The Allman format encompasses more than just the positioning of the braces, but in that respect, it is...



if (expression)
{
actions;
}
else
{
actions;
}


... type thing, not a K&R.

phantom
02-27-2002, 05:00 AM
Numerous studies have shown that Allman, (or variations), of Allman format have the highest readability level.

Do you have a link to that method? and a link to the study would be appreciated. I have never been taught properly so I think it would be a good read.

adrianxw
02-27-2002, 05:18 AM
I can probably dig them up, but would guess a well worded search at google for example would be revealling. Allman is one of the best known formats, (along with K&R and Whitesmith).

One reason for doing it the other way is that you can get more lines on the screen/page. Frankly, with todays technology, I don't believe that argument holds up any more. I may have thought differently if I was using C 25 years ago when we worked with 300 baud teletype terminals, if we were lucky.

phantom
02-27-2002, 05:36 AM
if we were lucky.
Kids today though, they wouldn't beleive you!

Just going through google now, I put allman in and it gave me 219 000 matches. Have now refined my search :D

Got it now, thanks for the info.

stupid_mutt
02-27-2002, 12:15 PM
Brian, wtf is mum?

Brian
02-27-2002, 12:26 PM
Originally posted by stupid_mutt
Brian, wtf is mum?

The correct spelling of the shortened version of Mother.

Govtcheez
02-27-2002, 12:30 PM
> The correct spelling of the shortened version of Mother.

Correct, of course, if you live in Britain. If you live in the US, it's mom.

Brian
02-27-2002, 12:37 PM
No, it's our language, we invented it. Stick to it or get your own.
:D

[That mum/mom post was actually a troll. Don't feed me.]

Govtcheez
02-27-2002, 12:39 PM
^&^ & ^&^& ^& &^^ ^&^^&&^&, ^&^ ^& &^& ^&^^ &^& &^...

But I have my own language, but no one will use it :)

Anyways, it's not like English wasn't just cannibalized from a bunch of other languages in the first place...

Govtcheez
02-27-2002, 12:40 PM
> [That mum/mom post was actually a troll. Don't feed me.]

Damn you! Editing after I post! :p :p :p

Brian
02-27-2002, 12:41 PM
Well, actually when I finished editing, your post was still non existant.

Imperito
02-27-2002, 01:13 PM
Well I use this one:



int main () {
//whatever
return 0;
}


However, I use a neat IDE that can fold a closing curly brace up to it's opening curly brace, so



while (!stop) {
//Many lines of code
}


becomes



+ while (!stop) {}


And clicking the plus expands it out again, but double clicking the while folds it back in. So whenever I don't feel like scrolling past something that I know works, I just fold it up!

Govtcheez
02-27-2002, 01:18 PM
What IDE is that, Imperito?

Imperito
02-27-2002, 01:37 PM
That would be jGrasp.

eng.auburn.edu/grasp/

Govtcheez
02-27-2002, 01:39 PM
Looks cool, except:

> Versions of GRASP for Java, Windows, Linux, and various Unix Systems are available

Do they think Java's an OS?

Brian
02-27-2002, 02:02 PM
Originally posted by Govtcheez
Looks cool, except:

> Versions of GRASP for Java, Windows, Linux, and various Unix Systems are available

Do they think Java's an OS?

Java is an OS, isn't it?

ober
02-27-2002, 02:06 PM
Originally posted by Brian


Java is an OS, isn't it?

PLEASE tell me you are kidding... PLEASE!!?!?!?

Brian
02-27-2002, 02:34 PM
JavaOS

ober
02-27-2002, 02:39 PM
no crap....:eek:

http://www.sun.com/smi/Press/sunflash/9605/sunflash.960529.11819.html

Never heard of it before..

Brian
02-27-2002, 03:06 PM
See the post called Operating Systems.

phantom
02-27-2002, 04:53 PM
[CODE]
int main () {
//whatever
return 0;
}
CODE]
The problem there is if you have many loops inside each other, if you have four opening brackets before the first closing bracket then things can get quite confusing.

for(loop)
{
for(loop)
{
//Do this
if(I die)
{
//do I goto heaven or hell?
{
//I goto heaven
}
else
{
cout<<"Sucked in pal!"
}
}
//Am I wounded?
{
//Scream out for mummy (Australian spelling so get nicked guv :p )
}
}
else
{
//Start running
}
}

James

Sekti
02-27-2002, 05:15 PM
In C++ I do this

#include <iostream.h>

int main(int argc, char* argv[])
{

return 0;
}

phantom
02-27-2002, 05:23 PM
int main(int argc, char* argv[])
I have rarely seen this and could anyone explain what and why there is int argc and char* argv[] ?

Brian
02-27-2002, 05:40 PM
argc is the number of command line arguments and *argv[] is a character array containing the programs name and command line arguments.

argv[0] = the program's path
argv[1] = the first argument
and so on

Unregistered
02-27-2002, 06:41 PM
Well, nowadays, there are four most popular techniques of coding that are acceptable, encouraged and used in developing softwares:

the first technique is the K&R that looks like this:

if (cond) {
stmt1;
stmt2;
...
}


the second technique is called the Allman technique that goes like this:


if (cond)
{
stmt1;
stmt2;
...
}


the third would look like this:

if (cond)
{
stmt1;
stmt2;
...
}

and is called Whitesmith.



Finally, the fourth most popular technique is the following and is known as Alternative:

if (cond) {
stmt1;
stm2;
...
}



The kernighan and Ritchie style(#1) is the most widely used style since it uses slightly less vertical space than other methods. On the other hand many, like Brian, feel that it is more burdensome to match up braces with this technique.

#2 and #3, the Allman and the Whitesmith styles are often preferred by programmers specially those who have done programming in pascal. The brace matchin is easy but extra vertical space is used.

finally #4 is a very popular alternative to standard D&R formatting.



What is important, Brian, is consistency. You pick any one of the acceptable styles and make sure you are consistent.

Remember, a good programer is the one who "Writes programs for people first, computers second". So make sure your code is readable.

tnx,
ben




p.s. if my codes dont look the way they should, then please let me know how to use the special editor. I will post the 4 techniques again in that case. thanks

Unregistered
02-27-2002, 06:43 PM
yes, unfortunately they didnt, so please let me know where to get the editor that you guys have been using.

thanks again.
ben

adrianxw
02-28-2002, 02:29 AM
To have code displayed as it should be, use code/endcode markers. I suspect if I draw them, they will be interpreted so I will describe them!!! An opening code marker consists of the word code placed inside square brackets, the end code marker is /code in square brackets.

Generally:

A number of people have asked me to dig up the studies I referred too above. This I will try to do. I recall at least two of them were conducted at US universities. I had all this stuff in a file, which it would seem, I have "misplaced" or possibly has been "borrowed" by one of my colleagues. I would have thought a google search would find it though - that is what I'll be doing if I can't find the reports!

ihsir
02-28-2002, 03:58 AM
i code

if(cond)
{
cout<<"LINE1";
cout<<"LINE2";
}
else
{
cout<<"LINE3";
cout<<"LINE4";
}


except in switch-case statements where i do

switch(var)
{
case 1:
cout<<"HIT TAB AND START SECT 2";
break
case 1:
cout<<"HIT TAB AND START SECT 2";
break
}


sometimes in multiple if-else i do

if(cond1)
{

if(cond2)
{

if(cond3)
{
}

}

}

bracket matching can give serious headaches...

Unregistered
02-28-2002, 06:17 AM
Thanks to adrian, I am able to show you the four techniques once again, (I hope it works i mean...)


#1) K&R



if (cond) {
stmt1;
stmt2;
...
}




#2) Allman



if (cond)
{
stmt1;
stmt2;
...
}



#3) Whitesmith



if (cond)
{
stmt1;
stmt2;
....
}



#4) Alternative



if (cond) {
stmt1;
stmt2;
...
}


I hope it works this time!
tnx,
ben

Unregistered
02-28-2002, 06:18 AM
Thanks to adrian, I am able to show you the four techniques once again, (I hope it works i mean...)


#1) K&R



if (cond) {
stmt1;
stmt2;
...
}




#2) Allman



if (cond)
{
stmt1;
stmt2;
...
}



#3) Whitesmith



if (cond)
{
stmt1;
stmt2;
....
}



#4) Alternative



if (cond) {
stmt1;
stmt2;
...
}


tnx,
ben

phantom
02-28-2002, 06:19 AM
I would have thought a google search would find it though - that is what I'll be doing if I can't find the reports!

I did a search on google and this was a good article I found.

http://www.tjhsst.edu/compsci/cpp/intro/style.html

dbaryl
02-28-2002, 11:55 AM
I personally prefer the Allman format, with a slight variation at times. It's just that I've different resolutions and things like that, so sometimes you just have to make you code be more readable with what you got... luckily we need not worry about that now :)

Does anyone know a good IDE / editor that does the indentation well (allman style)?

Prelude
02-28-2002, 12:21 PM
>Does anyone know a good IDE / editor that does the indentation well (allman style)?
MSVC++ has an auto indention to the point where it drives me crazy. You can change the indention (it defaults to 4 spaces and I prefer 2), but you have to do so every time you open a project. It also seems to default to an allman style if you place the braces on their own lines.

-Prelude

dbaryl
02-28-2002, 01:15 PM
The line: Does anyone know a[n] good IDE / editor that does the indentation well (allman style)?

...should have read like this:

Does anyone know an 'OK' FREE editor that does the indentation well (allman style)?