User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion

Reply
Thread Tools Display Modes
Unread 21 Dec 2002, 21:06   #1
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
Coding syntax

In the [php] vs [CODE] thread a little discussion about syntax started.
So, what do you think about how to write readable code?


IMO '{' and '}' characters should be put on a seperate line...

NO:
Code:
int main(){
    printf("BIG NO NO");
}
YES:
Code:
int main()
{
    printf("YEH!!!!");
}
The latter gives a much better view of the overall structure of the code IMO.

Another bad thing IMO (although I make that mistake quite often) is declaring variables mid-code...

NO:
Code:
int main()
{
    int i;

    for (i=0; i < 12; i++)
    {
        //do summat
    }

    char bla[40];       //declaration mid code, oh NOES!!!!!
    sprintf(bla,"value: %d",i);
    printf(bla);
}
YES:
Code:
int main()
{
    int i;
    char bla[40];

    for (i=0; i < 12; i++)
    {
        //do summat
    }

    sprintf(bla,"value: %d",i);
    printf(bla);
}
This gives a better view on all variables used in the peice of code and is easier for reference. You don't have too look all over the code when you want to know what type the variable was.

The last thing I can think of this quickly is the usage of spaces and tabs between operators. I mostly try to align the definition of variables with tabs or spaces and always use spaces between the operators.

NO:
Code:
int main()
{
    int i=0;
    float bla=1.3f;

    i=i+12;       //no spaces between operators

    for(;i>0;i--)   //certain lack of spaces....
    {
        //do something
    }
}
YES:
Code:
int main()
{
    int   i   = 0;
    float bla = 1.3f;

    i = i + 12;

    for (;i > 0; i--)
    {
        //stuff
    }
}
This gives a much better view on the variables IMO and makes it much better to read.

That's all I could think of that quickly... This is my view on readable code. I bet others have a whole different view...
__________________
"Yay"
Structural Integrity is offline   Reply With Quote
Unread 21 Dec 2002, 21:12   #2
Flavius
 
Join Date: Jan 2002
Posts: 421
Flavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet society
NO - i cant stand wasting line spacing just to put a {or }
YES - agreed
NO - if the variable names are very diff in size, all the work u have done goes to waste and u have to go back and change the number of tabs in order to put them all lined up

example:
Code:
int	ThisIsABigVariableName	=	5;
char	Short	=	'a';
Flavius is offline   Reply With Quote
Unread 21 Dec 2002, 21:13   #3
NEWSBOT3
NEWSBOT
 
Join Date: Dec 2000
Location: The enby cave!
Posts: 4,872
NEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriend
Re: Coding syntax

Quote:
Originally posted by Structural Integrity
The thing with spaces is stupid and wastes time.
all the rest i agree with.
__________________
[20:27:47] <nodrog-aawy> **** i think my housemate just caught me masturbating
[11:25:32] <idimmu> you are a little piggy arent you
[13:17:00] <KaneED> i'm so closet i'm like narnia
__________________
Pretty parks and funky scrap metal things here
NEWSBOT3 is offline   Reply With Quote
Unread 21 Dec 2002, 21:31   #4
MT
/dev/zero
Retired Mod
 
MT's Avatar
 
Join Date: May 2000
Posts: 415
MT is an unknown quantity at this point
Re: Re: Coding syntax

Quote:
Originally posted by NEWSBOT3
The thing with spaces is stupid and wastes time.
all the rest i agree with.
You think people put the spaces in themselves? funny!

CODE BEAUTIFIERS EFFESS!

It must be personal preference, I cant follow code when its done on the "{ on next line" thingy.
__________________
#linux : Home of Genius

<idimmu> ok i was chained to a desk with this oriental dude
MT is offline   Reply With Quote
Unread 21 Dec 2002, 21:51   #5
Nodrog
Registered User
 
Join Date: Jun 2000
Posts: 8,476
Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Code:
int main() {
    int i;
    char bla[40];

    for (i = 0; i < 12; i++){//do summat}

    sprintf(bla,"value: %d",i);
    printf(bla);
}
4 spaces in a tab, brackets on the same line as the statement beginning, and braces round everything, even single statement if/while blocks.

Any other way is wrong.



Also, people who use && and || for assignments and statement execution rather than ? and : make me want to break things.

int a = (b||7) is WRONG

int a = (!b)?b:7 is RIGHT

The same applies to controlling statement execution using logical operators rather than conditionals.

Last edited by Nodrog; 21 Dec 2002 at 21:59.
Nodrog is offline   Reply With Quote
Unread 21 Dec 2002, 21:55   #6
zenopus
Xenoc
 
Join Date: Feb 2001
Location: Great Britain
Posts: 297
zenopus is an unknown quantity at this point
Coding style

Well, I know there's K&R and ANSI style C - most people/companies/projects use a hybrid style. Went and searched on the net - found this link refreshingly entertaining.

Considerations like coding standard is very language specific - few would attempt to apply K&R C style to perl.
zenopus is offline   Reply With Quote
Unread 21 Dec 2002, 23:31   #7
MT
/dev/zero
Retired Mod
 
MT's Avatar
 
Join Date: May 2000
Posts: 415
MT is an unknown quantity at this point
personally i use 2 character indents. NEVER EVER EVER EVER EVER USE TAB CHARACTERS. Set your editor to use x spaces when you press the tab key. Nod, I tend to disagree with requiring braces for single line blocks.

Code:
if (x)
  callFuncXIsTrue();
else
  callFuncXIsFalse();
If its single line. Its a few less keypresses, and thats always good.

Most people regard use fo the ternary operator as norty too, but I tend to like it
__________________
#linux : Home of Genius

<idimmu> ok i was chained to a desk with this oriental dude
MT is offline   Reply With Quote
Unread 21 Dec 2002, 23:36   #8
grkn
Shadow of the past
 
Join Date: Apr 2001
Posts: 39
grkn is an unknown quantity at this point
Re: Coding syntax

Quote:
Originally posted by Structural Integrity
IMO '{' and '}' characters should be put on a seperate line...
That debate is kinda like an ever raging holy war ...
And as such things require radical opinions (), anyone writing {} on seperate lines should be shot, hung and burned.
__________________
karrde, Retired head of "Myrkrs roid liberation and smuggling Inc."

One OS to rule them all, One Passport to find them,
One OS to bring them all, And with the EULA bind them.
In the land of Redmond where the windows lie.


"...to leave the Elysium the dead had to drink from and travel beyond the River of Oblivion, Lethe, to once again return to the mortal realm..."
grkn is offline   Reply With Quote
Unread 21 Dec 2002, 23:36   #9
MT
/dev/zero
Retired Mod
 
MT's Avatar
 
Join Date: May 2000
Posts: 415
MT is an unknown quantity at this point
Re: Coding style

Quote:
Originally posted by zenopus
... found this link ...
Thou shalt not dispute K&R.
__________________
#linux : Home of Genius

<idimmu> ok i was chained to a desk with this oriental dude
MT is offline   Reply With Quote
Unread 22 Dec 2002, 01:15   #10
Raging.Retard
Street Tramp
 
Raging.Retard's Avatar
 
Join Date: Apr 2000
Location: Street Gutter
Posts: 341
Raging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant future
Hungarion notation for all my vars.

http://msdn.microsoft.com/library/de...hunganotat.asp
__________________
Chimney Pots.
Raging.Retard is offline   Reply With Quote
Unread 22 Dec 2002, 01:29   #11
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
I'm one of the '2 space indent' heretics but only so I can still see the sixth level of a nested loop
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
Gayle29uk is offline   Reply With Quote
Unread 22 Dec 2002, 01:31   #12
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Code:
#include <stdio.h>
#include <stdlib.h>

int
main ()
{
  int i;
  
  for (i = 0; i < 10; i++)
    if (printf ("i = %d\n", i) < 0)
      {
        perror ("printf");
        return (EXIT_FAILURE);
      }
  
  return (EXIT_SUCCESS);
}
Lots and lots of space, default indent style (GNU coding standard), function names at the start of the line for easy grepping.

In C of course, you can only declare variables at the start of a block. In C++ it's often useful to declare stuff halfway through, and if you're calling a constructor, might save some time in the case where you might return before you initialise something. And declaring an int in your for loop can clear things up. for (int i = 0; ...)

Not that any of it is important, since I'll rarely start a new C project from scratch.
__________________
#linux
queball is offline   Reply With Quote
Unread 22 Dec 2002, 01:31   #13
meglamaniac
Born Sinful
 
meglamaniac's Avatar
 
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
I cannot stand code where the opening { is the next line down.
My reasoning is best explained by example:
Code:
main {
   int x=0,y=0;
   while (;x<8;x++) {
      while (;y<8;y++) {
      }
      y=0;
   }
}
While that code does nothing much, it is very clear what brackets relate to what loop.
Want to find out what the } you're looking at closes? 'Draw' a vertical line up until it meets a conditional statement or a function/type definition - that's what it closes. There's no need to put the opening { on the next line - it serves no informative purpose, it wastes space, and it makes the code looks messy.

Thats just my opinion of course, but that's how I work.

__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.

Last edited by meglamaniac; 22 Dec 2002 at 11:10.
meglamaniac is offline   Reply With Quote
Unread 22 Dec 2002, 01:45   #14
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Re: Coding syntax

Quote:
Originally posted by Structural Integrity

IMO '{' and '}' characters should be put on a seperate line...
Since you asked in the other thread here's my personal vocabulary, other people might use different words:

{ or } is a curly brace or just brace
{ Left/Open Curly Brace
} Right/Close Curly Brace

( Left/Open Round Bracket / Left/Open Parenthesis
) Right/Close Round Bracket / Right/Close Parenthesis

[ Left/Open Square Bracket
] Right/Close Square Bracket

And "bracket" by itself usually means ( or ), but if I'm doing Tcl or something it might mean [ or ] too.
queball is offline   Reply With Quote
Unread 22 Dec 2002, 02:00   #15
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
I never explain my code to other people so I don't have to worry about the nomenclature I use to describe it Possibly because I (technically) nest things too deep and explaining 6 levels (or more) of nesting doesn't strike me as fun
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
Gayle29uk is offline   Reply With Quote
Unread 22 Dec 2002, 09:11   #16
Intervention
In fond memory of SB
 
Join Date: Dec 2002
Location: LIGHTNING BOLT!!
Posts: 393
Intervention is an unknown quantity at this point
My methods are much alike SI's. I'm also quite fond of the tab key, but that is just how I've learnt. I never actually read much code when I first started learning so no other style was imposod on me - this is how I've evolved my writing myself.


Code:
int main()
    {
    cout << "hello, Margaret" << endl;
    
    for(int i=0; i <= 10; i++)
        {
        cout << i << ". moo" << endl;
        }

    return 0;   
    }
This way allows you to see quickly which braces are associated with what. The opening brace follows the line after the thing that requires it - one indent in. All other code is then in line with this indent.
__________________
Peace, Love and Linux

--------------
My Artwork
Intervention is offline   Reply With Quote
Unread 22 Dec 2002, 11:02   #17
meglamaniac
Born Sinful
 
meglamaniac's Avatar
 
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Forgot to mention last night (I was doing quite well to say I'd just come back from the pub heh).
The main reason I keep the braces on the same indentation as the statement they relate to is because, so far as I'm concerened, the braces should be treated as a part of the statement itself - this they should occupy the same indentation.
It just makes obvious sense to me.

Oh well, no doubt we'll all have to read the various different ways of laying it out at various points.



[edit]
Just had a rummage about. This code is a simple selection sort function I used in a db program I had to write for coursework, and shows pretty much how I do things (I removed the comments to make it fit on the board).
Code:
void sort_by_id(void) {
	int j,k,smallest;
	struct record temp;
	for (j=0; j<nstudents-1; j++) {
		smallest = j;
		for (k=j+1; k<nstudents; k++) {
			if (student[k].id < student[smallest].id) smallest=k;
		}
		temp = student[j];
		student[j] = student[smallest];
		student[smallest] = temp;
	}
}
__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.

Last edited by meglamaniac; 22 Dec 2002 at 11:09.
meglamaniac is offline   Reply With Quote
Unread 22 Dec 2002, 13:57   #18
Slidey
Registered User
 
Join Date: Mar 2001
Posts: 205
Slidey is an unknown quantity at this point
i know i do things differently for every coding project i do but i try and adhere to this:

Code:
int function(int arg1, int arg2)
{
    for(x=0;x<9;x++) {
        printf("%i\n", x);
    }
}
with functions the braces are on the next line, for anything else they're on the same line
__________________
#linux - home of idiots

#impulsed - home of genius..?
Slidey is offline   Reply With Quote
Unread 22 Dec 2002, 17:35   #19
Idi
Guest
 
Posts: n/a
Quote:
Originally posted by Nodrog

Also, people who use && and || for assignments and statement execution rather than ? and : make me want to break things.

int a = (b||7) is WRONG

int a = (!b)?b:7 is RIGHT

The same applies to controlling statement execution using logical operators rather than conditionals.
Are you insane?
  Reply With Quote
Unread 22 Dec 2002, 18:04   #20
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
Quote:
Originally posted by Slidey
with functions the braces are on the next line, for anything else they're on the same line
That's the K&R syntax as described in the link Zenopus posted... I find it a tad odd though, why not have the same notation for every block?
Also something I found awfull in that document was the following:

Code:
if (bla) {
    //stuff
} else {        //WTF?!
    //other stuff
}
I'm very fond of using a lot of whitespaces and newlines, so this is something I can't grasp... I don't find this readable at all. I think that I need quite some time to unravel code written like this.

Also, I've never seen '||' or '&&' being used for operator assignments, it took me a few seconds before I understood what was happening in Nods example. I haven't seen people who use ': ?' a lot either.
Structural Integrity is offline   Reply With Quote
Unread 22 Dec 2002, 19:30   #21
Slidey
Registered User
 
Join Date: Mar 2001
Posts: 205
Slidey is an unknown quantity at this point
maybe thats where i got it from. i own that book (although i cant find it !)
__________________
#linux - home of idiots

#impulsed - home of genius..?
Slidey is offline   Reply With Quote
Unread 23 Dec 2002, 01:26   #22
Epcylon
Registered User
 
Join Date: Apr 2000
Location: Oslo, Norway
Posts: 78
Epcylon is a glorious beacon of lightEpcylon is a glorious beacon of lightEpcylon is a glorious beacon of lightEpcylon is a glorious beacon of lightEpcylon is a glorious beacon of light
Code:
def function(something, somewhat):
    if something == somewhat:
        print "Weee!"
        print "humf"
    else:
        print "Booo!"
        print "humf"
When I do code in an inferiour language where braces are needed:

Code:
void function(int something, int somewhat) {
    if (something == somewhat) {
        printf("Weee!\n");
        printf("humf\n");
    } else {
        printf("Booo!\n");
        printf("humf\n");
    }
}
__________________
Epcylon
[R1]: noob | [R2]: B8S/ICD | [R3-5]: ICD | [R6]: HR | [R7-9.5]: HR/NoS |
[R10]: HR RecOff | [R10.5]: HR RO -> HR HC -> HR pe0n | [R11]: HR pe0n -> Leave of Absence |
[R12]: HR free-pe0n | [R13-]: HR pe0n
Epcylon is offline   Reply With Quote
Unread 23 Dec 2002, 08:07   #23
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
Quote:
Originally posted by Epcylon
When I do code in an inferiour language where braces are needed:
I could be wrong about the language in the first code block but please tell me you didn't seriously just call C inferior to python...
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
Gayle29uk is offline   Reply With Quote
Unread 23 Dec 2002, 08:10   #24
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Quote:
Originally posted by Gayle28uk
I could be wrong about the language in the first code block but please tell me you didn't seriously just call C inferior to python...
Good morning Gayle
queball is offline   Reply With Quote
Unread 23 Dec 2002, 08:20   #25
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
Quote:
Originally posted by queball
Good morning Gayle
Good morning, just defending my favourite language from the vicious slurs or the great unwashed
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
Gayle29uk is offline   Reply With Quote
Unread 23 Dec 2002, 15:21   #26
Iniluki
Baron Samedi
 
Join Date: May 2001
Location: I'm not idimmu
Posts: 62
Iniluki has a spectacular aura aboutIniluki has a spectacular aura aboutIniluki has a spectacular aura about
I agree with all your points except the last one.


Code:
void main()
{
   int i =5;
   int answer =0;

   answer= 2*1;
}
This (above) looks easier to read than the example (below).

Code:
void main()
{
  int i = 5;
  int answer = 0;

  answer = 2 * 1;

}
Thats just me though!
__________________

Victims, aren't we all?
Iniluki is offline   Reply With Quote
Unread 23 Dec 2002, 15:27   #27
Iniluki
Baron Samedi
 
Join Date: May 2001
Location: I'm not idimmu
Posts: 62
Iniluki has a spectacular aura aboutIniluki has a spectacular aura aboutIniluki has a spectacular aura about
Re: Re: Coding syntax

Quote:
Originally posted by NEWSBOT3
The thing with spaces is stupid and wastes time.
all the rest i agree with.
No it aids readability. What if there's a problem with something you coded a few weeks ago, the easier it is to read through the quicker the problem is going to get solved.

One thing that does f*ck me right off, is people giving variable's stupid names. Their names should reflect what they do!


And a special hell should be reserved for my Software engineering tutor, who seems to think its goof practise to lay out a function prototype like this..

Code:
boolean      getarg(int      n,
						  tstring  s,
						  int      maxs)
{
			int          i;
			boolean      valid;

			printf("\n");
			printf("enter argument %1d : ", n);

			valid = getline(s, stdin, MAXSTR);

			/*  **FIX** to remove newline character from second last
				 character position.
				 i.e.  ch,ch,ch,...,ch,newline,endstr          */

			i = 0;
			while ((s[i] != NEWLINE) && (i < maxs))
					i = i + 1;

			s[i] = ENDSTR;

			return (((valid) && (s[0] != ENDSTR)));
Trying to work out wtf was going on, when I had a hangover, took me a while.
__________________

Victims, aren't we all?
Iniluki is offline   Reply With Quote
Unread 23 Dec 2002, 16:01   #28
djbass
mmm.. pills
 
djbass's Avatar
 
Join Date: Apr 2000
Location: Australia
Posts: 2,152
djbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond repute
It really does just come down to both personal preference, and which school of thought you belong to.

My coding style is very simular to what SI describes, and though sometimes it can be tedious to dot every i and cross every t I feel its worth the effort (though there are certain formatting rules taken care of by MSVC which helps a lot).

=[DJ Bass]=
__________________
CSS : the result of letting artists design something only an engineer should touch.
djbass is offline   Reply With Quote
Unread 29 Dec 2002, 20:09   #29
Azaghal
Registered User
 
Join Date: Jul 2000
Posts: 49
Azaghal is an unknown quantity at this point
I know, TheSocket is a crap name, but if it's in a Class 'Client' it is straightfoward...
Code:
void Client::GetFromServer (void) {

	if (recv(theSocket,&singlechar,1,0) > 0) {

		if (singlechar == '\n') {
			recvbuffer[recvlen] = '\0';
			parse_line(recvbuffer);
			recvbuffer[0] = '\0';
			recvlen       =   0 ;
		}
		else {
			recvbuffer[recvlen] = singlechar;
			recvlen++;
		}
	}

}
That's how I like it, clear usage of Tabs. I can't stand people using spaces instead of tabs. Just as some people can't stand tabs.
Azaghal is offline   Reply With Quote
Unread 30 Dec 2002, 14:19   #30
Nodrog
Registered User
 
Join Date: Jun 2000
Posts: 8,476
Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Quote:
Originally posted by Azaghal
[i]I know, TheSocket is a crap name
It's not as bad as 'singlechar'.
Nodrog is offline   Reply With Quote
Unread 1 Jan 2003, 18:13   #31
Azaghal
Registered User
 
Join Date: Jul 2000
Posts: 49
Azaghal is an unknown quantity at this point
Quote:
Originally posted by Nodrog
It's not as bad as 'singlechar'.
'singlechar' is here quite descriptive of what it is... a temp single char.

'tmp' or 'c' would perhaps have been better, but here it shows my explicit intention of reading into a single char and not a string.
Azaghal is offline   Reply With Quote
Unread 2 Jan 2003, 01:33   #32
W
Gubbish
 
Join Date: Sep 2000
Location: #FoW
Posts: 2,323
W is a jewel in the roughW is a jewel in the roughW is a jewel in the rough
Code:
void runl(int l){
  char a,b=0;
  for(int t=0,c;t<l;t++){
    a=getchar();
    c=t+255;
    if(!a)b=a;
    while(a==b&&t<l&&t<c){
      a=getchar();
      t++;
    }
    c-=255;
    if(t>c){
      putchar(0);
      putchar(t-c);
    }
    putchar(a);
    b=a;
  }
}
An example of the cleanest code I'll ever make unless I get paid for having the code cleaner. The intendations will be done with real tabs, not spaces, and the context and name of the function, along with what input and output it uses is enough to understand what it does, and the declaration and useage of variables explain what they mean much better than having to type a 5-10 letter name each time you refer to it. The letters used, the complete lack of spaces, and where I put my curlies and intendations are all things I've simply grown used to. If I was coding purely for myself, never having to show the code to anyone, I would remove alot of newlines (and attached intendations), putting two-statement blocks on the same line as the if/while they're conditional on etc, since I personally find that more readable. And quite frankly, I don't see the issue with spaces. In what way does they aid readability? It's not like the letter are printed on top of eachother without them.
__________________
Gubble gubble gubble gubble
W is offline   Reply With Quote
Unread 2 Jan 2003, 17:24   #33
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
Quote:
Originally posted by W
And quite frankly, I don't see the issue with spaces. In what way does they aid readability? It's not like the letter are printed on top of eachother without them.
I find using spaces quite logical as in normal writing you also use spaces to seperate words.
I see an operator, constant and variable name as a "word" and thus I find that they should be seperated with a space. I helps me read code as if it were normal text.
Structural Integrity is offline   Reply With Quote
Unread 3 Jan 2003, 02:22   #34
Intervention
In fond memory of SB
 
Join Date: Dec 2002
Location: LIGHTNING BOLT!!
Posts: 393
Intervention is an unknown quantity at this point
Quote:
Originally posted by Structural Integrity
I find using spaces quite logical as in normal writing you also use spaces to seperate words.
I see an operator, constant and variable name as a "word" and thus I find that they should be seperated with a space. I helps me read code as if it were normal text.

I agree with you completely there.

In my opinion, spaces are essential to have a quick view of what's happening.
__________________
Peace, Love and Linux

--------------
My Artwork
Intervention is offline   Reply With Quote
Unread 3 Jan 2003, 06:34   #35
Jester
Pedantic hypocrite
 
Jester's Avatar
 
Join Date: Jan 2001
Location: Back and to the left
Posts: 1,488
Jester has a reputation beyond reputeJester has a reputation beyond reputeJester has a reputation beyond reputeJester has a reputation beyond reputeJester has a reputation beyond reputeJester has a reputation beyond reputeJester has a reputation beyond reputeJester has a reputation beyond reputeJester has a reputation beyond reputeJester has a reputation beyond reputeJester has a reputation beyond repute
I code like a monkey. At best I try to make sure I stay consistent within a project.

Jester
__________________
I always wanted to be a dancer, but I could never get the shit off my shoes
.......
Jester is offline   Reply With Quote
Unread 4 Jan 2003, 23:39   #36
Pariah
Guest
 
Posts: n/a
curly-braces on same line as whatever initiates the block, closing brace on same line as the else which follows.
Two spaces for indention.
For one-liners I put if (asdf) { doQwert }, and in any language not perl, I'd skip the braces. (adding a linebreak before doQwert if I was in a good mood)
Spaces between the three args to for-loops.

I think that narrows it down



And as someone else said, this is a hopeless thing to discuss. It's like discussing which color is best suited for your kitchen.



Code:
while (my $arg = pop) {  ## Getting all the arguments

  if ($arg =~ /^\-/) {                                      ## Arg start with -, indicating an option
    my @option = split /[^wath]*/, $arg;
    for (my $i=1; $i<@option; $i++) { $option{$option[$i]} = 1; }

  } elsif (-f "$arg") {                                     ## Arg is a file
    push @files, $arg;

  } else {                                                  ## No idea what arg is
    print STDERR "Ignoring argument '$arg'. Neither file nor option?\n";
  }
}
  Reply With Quote
Unread 5 Jan 2003, 00:45   #37
W
Gubbish
 
Join Date: Sep 2000
Location: #FoW
Posts: 2,323
W is a jewel in the roughW is a jewel in the roughW is a jewel in the rough
Quote:
Originally posted by Structural Integrity
I find using spaces quite logical as in normal writing you also use spaces to seperate words.
I see an operator, constant and variable name as a "word" and thus I find that they should be seperated with a space. I helps me read code as if it were normal text.
In normal text you don't need to have the period separated from the last letter of a sentence with a space to be able to tell them apart. I use the same aproach when coding. if(fish==banana) really is readable, since neither the () nor the == can be mistaken for letters, and are generally full of whitespace.


PS, I do the }else{ and }elseif(){ thing too
__________________
Gubble gubble gubble gubble
W is offline   Reply With Quote
Unread 5 Jan 2003, 01:44   #38
Nodrog
Registered User
 
Join Date: Jun 2000
Posts: 8,476
Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Quote:
Originally posted by W
In normal text you don't need to have the period separated from the last letter of a sentence with a space to be able to tell them apart. I use the same aproach when coding. if(fish==banana) really is readable, since neither the () nor the == can be mistaken for letters, and are generally full of whitespace.


PS, I do the }else{ and }elseif(){ thing too
Thats true,but you wouldnt write like this(would you)because,space around whitespace does make it easier to read(in my opinion)when you have something on both sides of the'punctuation mark'like this.However,your mileage may differ.
Nodrog is offline   Reply With Quote
Unread 5 Jan 2003, 09:47   #39
Cyp
∞+♪˛
 
Join Date: Nov 2000
Location: :uo!te]oŻ|
Posts: 428
Cyp is an unknown quantity at this point
Main thing needed to write readable code is probably a lack of comments. Nothing worse than spending 15 minutes trying to work out what a line of code is supposed to do, before realizing that it is a comment, and then spending 15 minutes trying to work out what the next line is supposed to do, before realizing that it is also a comment.

Quote:
Originally posted by W
Code:
void runl(int l){
  char a,b=0;
  for(int t=0,c;t<l;t++){
    a=getchar();
    c=t+255;
    if(!a)b=a;
    while(a==b&&t<l&&t<c){
      a=getchar();
      t++;
    }
    c-=255;
    if(t>c){
      putchar(0);
      putchar(t-c);
    }
    putchar(a);
    b=a;
  }
}
...
Looks easier to read than most people's code... I'd probably put 5 more spaces in there, but better a bit too few spaces that a bit too many spaces... And looks like files will be a bit more compact after running the code...
__________________
Structural Integrity for Creator - since he'll probably make PA turn 3D.
Wikipedia forum
Note to self - Don't write Chinese letters with bold and italics...
<!--Last incarnation: Nov 2000-->
Cyp is offline   Reply With Quote
Unread 9 Jan 2003, 13:21   #40
MOS
Registered User
 
Join Date: Jan 2003
Location: Australia
Posts: 30
MOS is an unknown quantity at this point
Re: Coding syntax

Quote:
Originally posted by Structural Integrity

Code:
int main()
{
    int   i   = 0;
    float bla = 1.3f;

    i = i + 12;

    for (;i > 0; i--)
    {
        //stuff
    }
}
Your for loop decalartion is extreamly annoying
If I want to now what your forloop is doing i should be able to find out exacly what is happening in the header. I shouldn't have to look else where in your code to find what i is when the loop starts.

As for your points
NO - only first year students and pedantic lectures like the other method - forcing people to scroll more makes code harder to read.

YES - The variable has scope through the hole code block so it should be declared at the point it gains scope at the start.

YES - but only because I like neat code - this not a major issue.
__________________
If I knew what I was doing then it wouldn't be called research.

Last edited by MOS; 9 Jan 2003 at 13:37.
MOS is offline   Reply With Quote
Unread 9 Jan 2003, 19:52   #41
Idi
Guest
 
Posts: n/a
Here is a snippet from Gorecki, (released under the GPL so do what thou wilt!)

Code:
void Engine::play() { //play
	if (!got_row) {
		get_next_track();
		std::cout << "hmmmmm !\n";
	}
	if (got_row == true) {
		std::cout << "GET ROW = TRUE !\n";
		if (monkey_media_mixer_get_state(mixer) == MONKEY_MEDIA_MIXER_STATE_PLAYING) { //pause player
			monkey_media_mixer_set_state (mixer, MONKEY_MEDIA_MIXER_STATE_PAUSED);
			gorecki->set_play_image();
		} else if (monkey_media_mixer_get_state(mixer) == MONKEY_MEDIA_MIXER_STATE_PAUSED) { //contiune track
			monkey_media_mixer_set_state (mixer, MONKEY_MEDIA_MIXER_STATE_PLAYING);
			gorecki->set_pause_image();
		} else { //else play new track
		  play_track();
		}
	}
}
I code as it happens, but thats fairly average for me I guess, I've noticed though that the more I rewrite a routine, the more rem statements it gets!
  Reply With Quote
Unread 10 Jan 2003, 00:18   #42
pablissimo
Henry Kelly
 
pablissimo's Avatar
 
Join Date: Apr 2000
Posts: 7,374
pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
2 space indent as I always end up scrolling, and I find declaring variables mid-code is much better than declaring at the beginning of a function.

Say you have a long function with lots of operations going on, I'd rather have any temporary vars declared immediately before they're used (within reason) than having it all at the top, if only so I'm not scrolling up and down the page when debugging.
pablissimo is offline   Reply With Quote
Unread 10 Jan 2003, 02:14   #43
MOS
Registered User
 
Join Date: Jan 2003
Location: Australia
Posts: 30
MOS is an unknown quantity at this point
Quote:
Originally posted by pablissimo
Say you have a long function with lots of operations going on, I'd rather have any temporary vars declared immediately before they're used (within reason) than having it all at the top, if only so I'm not scrolling up and down the page when debugging.
Yes that is a fair enough example of when the rule can be broken, as long as the temp variable is only used in the next 2 or 3 lines of code if it used further down, say 20 or 30 lines, then it can be hard to find, and this all too often what people end up doing. They declare some temp variable to be used as a parameter and then end up using it again and not moving the declaration to the top of the function.
__________________
If I knew what I was doing then it wouldn't be called research.
MOS is offline   Reply With Quote
Unread 10 Jan 2003, 02:29   #44
W
Gubbish
 
Join Date: Sep 2000
Location: #FoW
Posts: 2,323
W is a jewel in the roughW is a jewel in the roughW is a jewel in the rough
Quote:
Originally posted by MOS
Yes that is a fair enough example of when the rule can be broken, as long as the temp variable is only used in the next 2 or 3 lines of code if it used further down, say 20 or 30 lines, then it can be hard to find, and this all too often what people end up doing. They declare some temp variable to be used as a parameter and then end up using it again and not moving the declaration to the top of the function.
Eh no... If you do that in most sane programming languages you get an out of scope error. What you coding in, basic?
__________________
Gubble gubble gubble gubble
W is offline   Reply With Quote
Unread 10 Jan 2003, 02:45   #45
MOS
Registered User
 
Join Date: Jan 2003
Location: Australia
Posts: 30
MOS is an unknown quantity at this point
Quote:
Originally posted by W
Eh no... If you do that in most sane programming languages you get an out of scope error. What you coding in, basic?

huh - Im obviously assuming im still in the same scope. and as far as language take your pick C/C++, Java, Visual Basic, Pascal etc they are all the same in regard to what I am talking about.
__________________
If I knew what I was doing then it wouldn't be called research.
MOS is offline   Reply With Quote
Unread 10 Jan 2003, 11:16   #46
pablissimo
Henry Kelly
 
pablissimo's Avatar
 
Join Date: Apr 2000
Posts: 7,374
pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Quote:
Originally posted by W
Eh no... If you do that in most sane programming languages you get an out of scope error. What you coding in, basic?
What programming languages give that error? I've never heard of that problem in C, C++ or Java, VB doesn't mind, TCL is happy enough...
pablissimo is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Forum Jump


All times are GMT +1. The time now is 11:57.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018