By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

(Programming) Various Syntax styles

P: n/a
There is a LARGE number of syntax styles in most (if not all)
programming languages.

For example, one syntax style (my current one):

....
int main()
{
for (int i = 0; i < 50; i++)
{
cout << "Hello World!\n";
j = 2*i+1; //some weird calculation
}

return 0;
}

But I am thinking of a change, a change to a style more widely used in
programming languages. If I seem not to like it, I will return to my
current one... The new style is:

int main() {
for (int i = 0; i < 50; i++) {
cout << "Hello World!\n";
j := 2*i+1;
}

return 0;
}

I seem not to be really used to it now (cuz it's just the beginning).
I was thinking to change because in many places I saw the second
style, so I suppose it's better (looking).

WHAT IS YOUR FAVORABLE SYTLE???????????????? give an example if
necessary
Jul 22 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a

"Chris Mantoulidis" <cm****@yahoo.com> wrote in message
news:a8**************************@posting.google.c om...
There is a LARGE number of syntax styles in most (if not all)
programming languages.

For example, one syntax style (my current one):

...
int main()
{
for (int i = 0; i < 50; i++)
{
cout << "Hello World!\n";
j = 2*i+1; file://some weird calculation
}

return 0;
}

But I am thinking of a change, a change to a style more widely used in
programming languages. If I seem not to like it, I will return to my
current one... The new style is:

int main() {
for (int i = 0; i < 50; i++) {
cout << "Hello World!\n";
j := 2*i+1;
}

return 0;
}

I seem not to be really used to it now (cuz it's just the beginning).
I was thinking to change because in many places I saw the second
style, so I suppose it's better (looking).

WHAT IS YOUR FAVORABLE SYTLE???????????????? give an example if
necessary


There is no "best" coding style. Which coding style you choose is not
important, as long as it is consistent. It also helps if you use the same
coding style as everyone else around you. Of all the places I have worked
the first coding style prefered. Since I'm used to that style, code
formatted that way is easier to read for me than the second style. I expect
it to be the other way around for people used to the second style.

If you work alone choose whatever you like best. If you work in a group use
the coding style everyone else uses. Other than that I see no reason to
switch styles.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl

Jul 22 '05 #2

P: n/a
On Thu, 11 Dec 2003 06:17:18 -0800, Chris Mantoulidis wrote:
I seem not to be really used to it now (cuz it's just the beginning).
I was thinking to change because in many places I saw the second
style, so I suppose it's better (looking).


Use the style you prefer when you work alone. Use the common style when
you work in a group.

At a minimum be consistent within one source file.

Tabs for indentation is evil, but not as evil as mixing tabs and spaces.

--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up

Jul 22 '05 #3

P: n/a
Chris Mantoulidis wrote:
There is a LARGE number of syntax styles in most (if not all)
programming languages.

For example, one syntax style (my current one):

...
int main()
{
for (int i = 0; i < 50; i++)
{
cout << "Hello World!\n";
j = 2*i+1; //some weird calculation
}

return 0;
}


with your example my funny coding style is:

int main(){for(int i=0;i<50;i++){cout<<"Hello World!\n";j=2*i+1;
//some weird calculation
}return 0;}

It's obtained by the following C program:

/************************ File: cz.c **********************
It has been proved that the number of errors in a C or C++ program
is proportional to the number of lines into the source files.
This program dramaticaly reduce the number of errors,
reducing (up to 99%) the number of lines in your sources.
OBVIOUSLY a source code that is run by this program:
is semantic equivalent to the original
but it is less readable !
Try all forms:
cz cz.c cz_short.c
cz -256 cz.c cz_small.c
cz -30000 -X cz.c cz_atom.c
cz -0 cz.c cz_long.c
to obtain:
cz_short.c ( 200 lines long)
cz_small.c ( 118 lines long)
cz_atom.c ( 27 lines long)
cz_long.c (2746 lines long)
from:
cz.c ( 639 lines long)
Author: Dario Email: da***@despammed.com
************************************************** **********//*
Version History:
1.0 Dec 6 1994
Initial version
1.1 Dec 7 1994
1. Correct from:
sscanf(argv[i]+1, "%ld", &maxOutLen)
to:
sscanf(argv[i]+1, "%d", &maxOutLen)
2. Correct from:
static char * operator[] = {...
to:
static char * operatorString[] = {...
3. Operators added to operatorString:
%%%% ... <> <% %> <: :> %%
4. Introduced the function isIdientifierLetter
5. Changed from:
*n = *n+1;
to:
(*n)++;
6. Correct from
#if defined(__STDC__) || defined(__cpluscplus)
to:
#if defined(__STDC__) || defined(__cplusplus)
7. Many casts added to the result of memory allocators
8. Correct from:
static forceNewLine(void)
to:
static void forceNewLine(void)
1.2 Dec 12 1994
1. Recognize as preprocessor line a line starting with %%
2. Now may be also compiled by pre-ANSI-C compiler
1.3 Aug 04 1997
1. Third paramter of strncmp is a size_t
2. Many int casted to char
3. Changed the default for maxOutLen from 80 to 70
*/
#if defined(__STDC__) || defined(__cplusplus)
#define VOID void
#define FUNC_1(t1,v1) (t1 v1)
#define FUNC_2(t1,v1,t2,v2) (t1 v1, t2 v2)
#define FUNC_3(t1,v1,t2,v2,t3,v3) (t1 v1, t2 v2, t3 v3)
#define TRIG_BACK_SLASH "?" "?" "/" /* trigraph for \ */
#define TRIG_POUND_SIGN "?" "?" "=" /* trigraph for # */
#else
#define VOID
#define FUNC_1(t1,v1) (v1) t1 v1;
#define FUNC_2(t1,v1,t2,v2) (v1, v2) t1 v1; t2 v2;
#define FUNC_3(t1,v1,t2,v2,t3,v3) (v1, v2, v3) t1 v1; t2 v2; t3 v3;
#define TRIG_BACK_SLASH "??/" /* trigraph for \ */
#define TRIG_POUND_SIGN "??=" /* trigraph for # */
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MYstrncmp(a1,a2,a3) strncmp(a1,a2,(size_t)(a3))
static FILE*inF;static FILE*outF;static char*inLine=0;static char*
lastToken=0;static char*prevToken=0;static int outCounter=0;static
int inComment=0;static int removeComment=0;static int maxOutLen=70-1
;static char*operatorString[]={/* sorted by lenght! *//* 4 */"%%%%",
/* 3 */"...","->*","<<=",">>=",
#if defined(__STDC__) || defined(__cplusplus)
"?""?""(",/* trigraph for "[" */"?""?"")",/* trigraph for "]" */"?"
"?""!",/* trigraph for "|" */"?""?""'",/* trigraph for "^" */"?""?"
"<",/* trigraph for "{" */"?""?"">",/* trigraph for "}" */"?""?""-",
/* trigraph for "~" */
#else
"??(",/* trigraph for "[" */"??)",/* trigraph for "]" */"??!",
/* trigraph for "|" */"??'",/* trigraph for "^" */"??<",
/* trigraph for "{" */"??>",/* trigraph for "}" */"??-",
/* trigraph for "~" */
#endif
/* 2 */"->","++","--",".*","<<",">>","<=",">=","==","!=","&&","||" ,
"*=","/=","%=","+=","-=","&=","^=","|=","::","##","<>","<%","%>",
"<:",":>","%%",/* 1 */"!","%","^","&","*","(",")","-","+","=","{",
"}","|","~","[","]",/* "\\", */";",/* "\'", */":",/* "\"", */"<",">"
,"?",",",".","/","#",/* 0 */0,};static void clean(VOID){if(inLine){
free((void*)inLine);inLine=0;}if(prevToken){free(( void*)prevToken);
prevToken=0;}if(lastToken){free((void*)lastToken); lastToken=0;}if(
inF&&inF!=stdin){(void)fclose(inF);inF=0;}if(outF& &outF!=stdout){(
void)fclose(outF);outF=0;}}static int getOpLen FUNC_1(char*,p){int i
;unsigned int len;for(i=0;operatorString[i];i++){len=strlen(
operatorString[i]);if(strlen(p)>=len&&!MYstrncmp(p,operatorString[i]
,len))return len;}return 0;}static void byebye(VOID){clean();exit(-1
);}static void outOfMemory(VOID){(void)fprintf(stderr,"%s\n",
"Out of memory!");byebye();}static int endLine FUNC_1(char*,line){
int len=strlen(line);if(len==0||line[len-1]!='\n')return 0;if(len>=2
&&line[len-2]=='\\'){line[len-2]=(char)0;return 0;}if(len>=4&&!
MYstrncmp(line+len-4,TRIG_BACK_SLASH,3)){line[len-4]=(char)0;return 0
;}return 1;}static void addCharToLine FUNC_2(char,c,char**,line){int
len=strlen(*line);*line=(char*)realloc((void*)*lin e,(size_t)(len+1+1
));if(!*line)outOfMemory();(*line)[len+0]=c;(*line)[len+1]=(char)0;}
static void readLine(VOID){int c;int len;if(inLine){free((void*)
inLine);inLine=0;}inLine=(char*)malloc((size_t)1); if(!inLine)
outOfMemory();*inLine=(char)0;while(!endLine(inLin e)){c=fgetc(inF);
if(c==EOF)break;addCharToLine((char)c,&inLine);}le n=strlen(inLine);
if(len)inLine[len-1]=(char)0;}static void forceNewLine(VOID){if(
outCounter){(void)fprintf(outF,"\n");outCounter=0; }}static int
isIdentifierLetter FUNC_1(char,c){return c!=0&&(isalnum((int)c)||c==
'_'||c=='$');}static int needSpace(VOID){int result;char*p;if(!
prevToken||!outCounter)return 0;if(isIdentifierLetter(prevToken[
strlen(prevToken)-1])&&isIdentifierLetter(lastToken[0]))return 1;p=(
char*)malloc((size_t)(strlen(prevToken)+strlen(las tToken)+1));if(!p)
outOfMemory();(void)sprintf(p,"%s%s",prevToken,las tToken);result=(
getOpLen(prevToken)!=getOpLen(p));free((void*)p);r eturn result;}
static void putLastToken(VOID){int len;int isComment;int need;len=
strlen(lastToken);isComment=inComment||((len>=2)&& (!MYstrncmp(
lastToken,"//",2)||!MYstrncmp(lastToken,"/*",2)||!MYstrncmp(
lastToken+len-2,"*/",2)));if(isComment&&removeComment)return;if(
outCounter+1+len>maxOutLen)forceNewLine();need=nee dSpace();(void)
fprintf(outF,"%s%s",need?" ":"",lastToken);outCounter+=need+len;if(
inComment||(isComment&&len>=2&&!MYstrncmp(lastToke n,"//",2)))
forceNewLine();}static void preProcessorLine FUNC_1(char*,p){
forceNewLine();(void)fprintf(outF,"%s",p);outCount er+=strlen(p);
forceNewLine();}static void setToken FUNC_3(char*,p,int,s,int,e){
/*
The token is between index s (includeed)
and e (excluded) of p string.
*/int len;int i;if(prevToken){free((void*)prevToken);
/* prevToken = 0; */}prevToken=lastToken;/* lastToken = 0; */
len=e-s;lastToken=(char*)malloc((size_t)(len+1));if(!las tToken)
outOfMemory();for(i=0;i<len;i++)lastToken[i]=p[s+i];lastToken[len]=(
char)0;}static int get1Comment FUNC_2(char*,p,int*,n){int s=*n;while
(p[*n])(*n)++;setToken(p,s,*n);return 1;}static int getNComment
FUNC_2(char*,p,int*,n){int s=*n;(*n)+=2;inComment=1;while(p[*n])if(p
[*n]=='*'&&p[*n+1]=='/'){(*n)+=2;inComment=0;break;}else(*n)++;
setToken(p,s,*n);return 1;}static int getString FUNC_2(char*,p,int*,
n){int start;int s=*n;if(p[*n]=='L')(*n)++;start=p[*n];(*n)++;while(
p[*n]&&p[*n]!=start){if(strlen(p+*n)>=2&&p[*n]=='\\')(*n)+=2;else if
(strlen(p+*n)>=4&&!MYstrncmp(p+*n,TRIG_BACK_SLASH, 3))(*n)+=4;else(*n
)++;}if(p[*n]==start)(*n)++;setToken(p,s,*n);return 1;}static int
getNumber FUNC_2(char*,p,int*,n){/*
This functions recognize a superset of valid C or C++ numbers,
but this is not a problem in this program.
*/int s=*n;while(isIdentifierLetter(p[*n])||p[*n]=='+'||p[*n]=='-'
||p[*n]=='.')(*n)++;setToken(p,s,*n);return 1;}static int
getIdentifier FUNC_2(char*,p,int*,n){int s=*n;while(
isIdentifierLetter(p[*n]))(*n)++;setToken(p,s,*n);return 1;}static
int getOperator FUNC_3(char*,p,int*,n,int,len){int s=*n;(*n)+=len;
setToken(p,s,*n);return 1;}static int getToken FUNC_2(char*,p,int*,n
){int len;while(p[*n]){if(p[*n]=='/'&&p[*n+1]=='/')return get1Comment
(p,n);if(p[*n]=='/'&&p[*n+1]=='*')return getNComment(p,n);if(isdigit
((int)p[*n])||(p[*n]=='.'&&isdigit((int)p[*n+1])))return getNumber(p
,n);if(isIdentifierLetter(p[*n]))return getIdentifier(p,n);if(p[*n]
=='"'||(p[*n]=='L'&&p[*n+1]=='"')||p[*n]=='\''||(p[*n]=='L'&&p[*n+1]
=='\''))return getString(p,n);if(!isspace((int)p[*n])){len=getOpLen(
p+*n);return getOperator(p,n,len?len:1);}(*n)++;}return 0;}static
void cLine FUNC_1(char*,p){int n;char*e;if(inComment){e=strstr(p,
"*/");if(e){e+=2;setToken(p,0,e-p);inComment=0;}else setToken(p,0,(
int)strlen(p));putLastToken();}else e=p;if(e){n=e-p;while(getToken(p
,&n))putLastToken();}}static void analizeLine(VOID){char*p=inLine;
while(isspace((int)*p))p++;if(!inComment&&(*p=='#' ||(p[0]=='%'&&p[1]
=='%')||(strlen(p)>=3&&!MYstrncmp(p,TRIG_POUND_SIG N,3))))
preProcessorLine(inLine);else cLine(inLine);}static void engine(VOID
){while(!feof(inF)){readLine();analizeLine();}forc eNewLine();}static
void usage FUNC_1(char*,argv0){(void)fprintf(stderr,
"Usage: %s [options] [infile [outfile]]\n",argv0);(void)fprintf(
stderr,"where each argument may be:\n");(void)fprintf(stderr,
" -n compact C or C++ code in lines n characters long
(default 70)\n"
);(void)fprintf(stderr," -X remove comments\n");(void)
fprintf(stderr," -C preserve comments (default)\n");
(void)fprintf(stderr,
" infile C or C++ source file (default standard input)\n");
(void)fprintf(stderr,
" outfile C or C++ output file (default standard output)\n")
;byebye();}static void runArguments FUNC_2(int,argc,char**,argv){
FILE*f;int i;if(argc<=1)usage(argv[0]);for(i=1;i<argc;i++)if(inF==
stdin&&!strcmp(argv[i],"-X"))removeComment=1;else if(inF==stdin&&!
strcmp(argv[i],"-C"))removeComment=0;else if(inF==stdin&&argv[i][0]
=='-'){if(argv[i][1]==0)maxOutLen=70;else if(sscanf(argv[i]+1,"%d",&
maxOutLen)!=1||maxOutLen<0)usage(argv[0]);maxOutLen--;if(maxOutLen<0
)maxOutLen=1;}else{if(inF!=stdin&&outF!=stdout)usa ge(argv[0]);f=
fopen(argv[i],(inF==stdin)?"r":"w");if(!f){(void)fprintf(stderr ,
"Unable to open file %s\n",argv[i]);byebye();}if(inF==stdin)inF=f;
else outF=f;}}int main FUNC_2(int,argc,char**,argv){inF=stdin;outF=
stdout;runArguments(argc,argv);engine();clean();re turn 0;}
Jul 22 '05 #4

P: n/a

"Chris Mantoulidis" <cm****@yahoo.com> wrote in message
news:a8**************************@posting.google.c om...
There is a LARGE number of syntax styles in most (if not all)
programming languages.

For example, one syntax style (my current one):

...
int main()
{
for (int i = 0; i < 50; i++)
{
cout << "Hello World!\n";
j = 2*i+1; //some weird calculation
}

return 0;
}

But I am thinking of a change, a change to a style more widely used in
programming languages. If I seem not to like it, I will return to my
current one... The new style is:

int main() {
for (int i = 0; i < 50; i++) {
cout << "Hello World!\n";
j := 2*i+1;
}

return 0;
}


I would not exactly put this on par with moving to Alaska, losing 200
pounds, or having a sex change operation. It really doesn't make much
difference.
Jul 22 '05 #5

P: n/a

"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote in message
news:pa***************************@spam.for.me.inv alid...

Tabs for indentation is evil...


I agree, but why do you say that exactly?
Jul 22 '05 #6

P: n/a
>
There is a LARGE number of syntax styles in most (if not all)
programming languages.

For example, one syntax style (my current one):

...
int main()
{
for (int i = 0; i < 50; i++)
{
cout << "Hello World!\n";
j = 2*i+1; //some weird calculation
}

return 0;
}
with your example my funny coding style is:

int main(){for(int i=0;i<50;i++){cout<<"Hello World!\n";j=2*i+1;
//some weird calculation
}return 0;}

It's obtained by the following C program:

/************************ File: cz.c **********************
It has been proved that the number of errors in a C or C++ program
is proportional to the number of lines into the source files.
This program dramaticaly reduce the number of errors,
reducing (up to 99%) the number of lines in your sources.
OBVIOUSLY a source code that is run by this program:
is semantic equivalent to the original
but it is less readable !
Try all forms:
cz cz.c cz_short.c
cz -256 cz.c cz_small.c
cz -30000 -X cz.c cz_atom.c
cz -0 cz.c cz_long.c
to obtain:
cz_short.c ( 200 lines long)
cz_small.c ( 118 lines long)
cz_atom.c ( 27 lines long)
cz_long.c (2746 lines long)
from:
cz.c ( 639 lines long)
Author: Dario Email: da***@despammed.com
************************************************* ***********//*
Version History:
1.0 Dec 6 1994
Initial version
1.1 Dec 7 1994
1. Correct from:
sscanf(argv[i]+1, "%ld", &maxOutLen)
to:
sscanf(argv[i]+1, "%d", &maxOutLen)
2. Correct from:
static char * operator[] = {...
to:
static char * operatorString[] = {...
3. Operators added to operatorString:
%%%% ... <> <% %> <: :> %%
4. Introduced the function isIdientifierLetter
5. Changed from:
*n = *n+1;
to:
(*n)++;
6. Correct from
#if defined(__STDC__) || defined(__cpluscplus)
to:
#if defined(__STDC__) || defined(__cplusplus)
7. Many casts added to the result of memory allocators
8. Correct from:
static forceNewLine(void)
to:
static void forceNewLine(void)
1.2 Dec 12 1994
1. Recognize as preprocessor line a line starting with %%
2. Now may be also compiled by pre-ANSI-C compiler
1.3 Aug 04 1997
1. Third paramter of strncmp is a size_t
2. Many int casted to char
3. Changed the default for maxOutLen from 80 to 70
*/
#if defined(__STDC__) || defined(__cplusplus)
#define VOID void
#define FUNC_1(t1,v1) (t1 v1)
#define FUNC_2(t1,v1,t2,v2) (t1 v1, t2 v2)
#define FUNC_3(t1,v1,t2,v2,t3,v3) (t1 v1, t2 v2, t3 v3)
#define TRIG_BACK_SLASH "?" "?" "/" /* trigraph for \

*/#define TRIG_POUND_SIGN "?" "?" "=" /* trigraph for # */#else
#define VOID
#define FUNC_1(t1,v1) (v1) t1 v1;
#define FUNC_2(t1,v1,t2,v2) (v1, v2) t1 v1; t2 v2;
#define FUNC_3(t1,v1,t2,v2,t3,v3) (v1, v2, v3) t1 v1; t2 v2; t3 v3;
#define TRIG_BACK_SLASH "??/" /* trigraph for \ */
#define TRIG_POUND_SIGN "??=" /* trigraph for # */
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MYstrncmp(a1,a2,a3) strncmp(a1,a2,(size_t)(a3))
static FILE*inF;static FILE*outF;static char*inLine=0;static char*
lastToken=0;static char*prevToken=0;static int outCounter=0;static
int inComment=0;static int removeComment=0;static int maxOutLen=70-1
;static char*operatorString[]={/* sorted by lenght! *//* 4 */"%%%%",
/* 3 */"...","->*","<<=",">>=",
#if defined(__STDC__) || defined(__cplusplus)
"?""?""(",/* trigraph for "[" */"?""?"")",/* trigraph for "]" */"?"
"?""!",/* trigraph for "|" */"?""?""'",/* trigraph for "^" */"?""?"
"<",/* trigraph for "{" */"?""?"">",/* trigraph for "}" */"?""?""-",
/* trigraph for "~" */
#else
"??(",/* trigraph for "[" */"??)",/* trigraph for "]" */"??!",
/* trigraph for "|" */"??'",/* trigraph for "^" */"??<",
/* trigraph for "{" */"??>",/* trigraph for "}" */"??-",
/* trigraph for "~" */
#endif
/* 2 */"->","++","--",".*","<<",">>","<=",">=","==","!=","&&","||" ,
"*=","/=","%=","+=","-=","&=","^=","|=","::","##","<>","<%","%>",
"<:",":>","%%",/* 1 */"!","%","^","&","*","(",")","-","+","=","{",
"}","|","~","[","]",/* "\\", */";",/* "\'", */":",/* "\"", */"<",">"
,"?",",",".","/","#",/* 0 */0,};static void clean(VOID){if(inLine){
free((void*)inLine);inLine=0;}if(prevToken){free( (void*)prevToken);
prevToken=0;}if(lastToken){free((void*)lastToken) ;lastToken=0;}if(
inF&&inF!=stdin){(void)fclose(inF);inF=0;}if(outF &&outF!=stdout){(
void)fclose(outF);outF=0;}}static int getOpLen FUNC_1(char*,p){int i
;unsigned int len;for(i=0;operatorString[i];i++){len=strlen(
operatorString[i]);if(strlen(p)>=len&&!MYstrncmp(p,operatorString[i]
,len))return len;}return 0;}static void byebye(VOID){clean();exit(-1
);}static void outOfMemory(VOID){(void)fprintf(stderr,"%s\n",
"Out of memory!");byebye();}static int endLine FUNC_1(char*,line){
int len=strlen(line);if(len==0||line[len-1]!='\n')return 0;if(len>=2
&&line[len-2]=='\\'){line[len-2]=(char)0;return 0;}if(len>=4&&!
MYstrncmp(line+len-4,TRIG_BACK_SLASH,3)){line[len-4]=(char)0;return 0
;}return 1;}static void addCharToLine FUNC_2(char,c,char**,line){int
len=strlen(*line);*line=(char*)realloc((void*)*li ne,(size_t)(len+1+1
));if(!*line)outOfMemory();(*line)[len+0]=c;(*line)[len+1]=(char)0;}
static void readLine(VOID){int c;int len;if(inLine){free((void*)
inLine);inLine=0;}inLine=(char*)malloc((size_t)1) ;if(!inLine)
outOfMemory();*inLine=(char)0;while(!endLine(inLi ne)){c=fgetc(inF);
if(c==EOF)break;addCharToLine((char)c,&inLine);}l en=strlen(inLine);
if(len)inLine[len-1]=(char)0;}static void forceNewLine(VOID){if(
outCounter){(void)fprintf(outF,"\n");outCounter=0 ;}}static int
isIdentifierLetter FUNC_1(char,c){return c!=0&&(isalnum((int)c)||c==
'_'||c=='$');}static int needSpace(VOID){int result;char*p;if(!
prevToken||!outCounter)return 0;if(isIdentifierLetter(prevToken[
strlen(prevToken)-1])&&isIdentifierLetter(lastToken[0]))return 1;p=(
char*)malloc((size_t)(strlen(prevToken)+strlen(la stToken)+1));if(!p)
outOfMemory();(void)sprintf(p,"%s%s",prevToken,la stToken);result=(
getOpLen(prevToken)!=getOpLen(p));free((void*)p); return result;}
static void putLastToken(VOID){int len;int isComment;int need;len=
strlen(lastToken);isComment=inComment||((len>=2)& &(!MYstrncmp(
lastToken,"//",2)||!MYstrncmp(lastToken,"/*",2)||!MYstrncmp(
lastToken+len-2,"*/",2)));if(isComment&&removeComment)return;if(
outCounter+1+len>maxOutLen)forceNewLine();need=ne edSpace();(void)
fprintf(outF,"%s%s",need?" ":"",lastToken);outCounter+=need+len;if(
inComment||(isComment&&len>=2&&!MYstrncmp(lastTok en,"//",2)))
forceNewLine();}static void preProcessorLine FUNC_1(char*,p){
forceNewLine();(void)fprintf(outF,"%s",p);outCoun ter+=strlen(p);
forceNewLine();}static void setToken FUNC_3(char*,p,int,s,int,e){
/*
The token is between index s (includeed)
and e (excluded) of p string.
*/int len;int i;if(prevToken){free((void*)prevToken);
/* prevToken = 0; */}prevToken=lastToken;/* lastToken = 0; */
len=e-s;lastToken=(char*)malloc((size_t)(len+1));if(!las tToken)
outOfMemory();for(i=0;i<len;i++)lastToken[i]=p[s+i];lastToken[len]=(
char)0;}static int get1Comment FUNC_2(char*,p,int*,n){int s=*n;while
(p[*n])(*n)++;setToken(p,s,*n);return 1;}static int getNComment
FUNC_2(char*,p,int*,n){int s=*n;(*n)+=2;inComment=1;while(p[*n])if(p
[*n]=='*'&&p[*n+1]=='/'){(*n)+=2;inComment=0;break;}else(*n)++;
setToken(p,s,*n);return 1;}static int getString FUNC_2(char*,p,int*,
n){int start;int s=*n;if(p[*n]=='L')(*n)++;start=p[*n];(*n)++;while(
p[*n]&&p[*n]!=start){if(strlen(p+*n)>=2&&p[*n]=='\\')(*n)+=2;else if
(strlen(p+*n)>=4&&!MYstrncmp(p+*n,TRIG_BACK_SLASH ,3))(*n)+=4;else(*n
)++;}if(p[*n]==start)(*n)++;setToken(p,s,*n);return 1;}static int
getNumber FUNC_2(char*,p,int*,n){/*
This functions recognize a superset of valid C or C++ numbers,
but this is not a problem in this program.
*/int s=*n;while(isIdentifierLetter(p[*n])||p[*n]=='+'||p[*n]=='-'
||p[*n]=='.')(*n)++;setToken(p,s,*n);return 1;}static int
getIdentifier FUNC_2(char*,p,int*,n){int s=*n;while(
isIdentifierLetter(p[*n]))(*n)++;setToken(p,s,*n);return 1;}static
int getOperator FUNC_3(char*,p,int*,n,int,len){int s=*n;(*n)+=len;
setToken(p,s,*n);return 1;}static int getToken FUNC_2(char*,p,int*,n
){int len;while(p[*n]){if(p[*n]=='/'&&p[*n+1]=='/')return get1Comment
(p,n);if(p[*n]=='/'&&p[*n+1]=='*')return getNComment(p,n);if(isdigit
((int)p[*n])||(p[*n]=='.'&&isdigit((int)p[*n+1])))return getNumber(p
,n);if(isIdentifierLetter(p[*n]))return getIdentifier(p,n);if(p[*n]
=='"'||(p[*n]=='L'&&p[*n+1]=='"')||p[*n]=='\''||(p[*n]=='L'&&p[*n+1]
=='\''))return getString(p,n);if(!isspace((int)p[*n])){len=getOpLen(
p+*n);return getOperator(p,n,len?len:1);}(*n)++;}return 0;}static
void cLine FUNC_1(char*,p){int n;char*e;if(inComment){e=strstr(p,
"*/");if(e){e+=2;setToken(p,0,e-p);inComment=0;}else setToken(p,0,(
int)strlen(p));putLastToken();}else e=p;if(e){n=e-p;while(getToken(p
,&n))putLastToken();}}static void analizeLine(VOID){char*p=inLine;
while(isspace((int)*p))p++;if(!inComment&&(*p=='# '||(p[0]=='%'&&p[1]
=='%')||(strlen(p)>=3&&!MYstrncmp(p,TRIG_POUND_SI GN,3))))
preProcessorLine(inLine);else cLine(inLine);}static void engine(VOID
){while(!feof(inF)){readLine();analizeLine();}for ceNewLine();}static
void usage FUNC_1(char*,argv0){(void)fprintf(stderr,
"Usage: %s [options] [infile [outfile]]\n",argv0);(void)fprintf(
stderr,"where each argument may be:\n");(void)fprintf(stderr,
" -n compact C or C++ code in lines n characters long
(default 70)\n"
);(void)fprintf(stderr," -X remove comments\n");(void)
fprintf(stderr," -C preserve comments (default)\n");
(void)fprintf(stderr,
" infile C or C++ source file (default standard input)\n");
(void)fprintf(stderr,
" outfile C or C++ output file (default standard output)\n")
;byebye();}static void runArguments FUNC_2(int,argc,char**,argv){
FILE*f;int i;if(argc<=1)usage(argv[0]);for(i=1;i<argc;i++)if(inF==
stdin&&!strcmp(argv[i],"-X"))removeComment=1;else if(inF==stdin&&!
strcmp(argv[i],"-C"))removeComment=0;else if(inF==stdin&&argv[i][0]
=='-'){if(argv[i][1]==0)maxOutLen=70;else if(sscanf(argv[i]+1,"%d",&
maxOutLen)!=1||maxOutLen<0)usage(argv[0]);maxOutLen--;if(maxOutLen<0
)maxOutLen=1;}else{if(inF!=stdin&&outF!=stdout)us age(argv[0]);f=
fopen(argv[i],(inF==stdin)?"r":"w");if(!f){(void)fprintf(stderr ,
"Unable to open file %s\n",argv[i]);byebye();}if(inF==stdin)inF=f;
else outF=f;}}int main FUNC_2(int,argc,char**,argv){inF=stdin;outF=
stdout;runArguments(argc,argv);engine();clean();r eturn 0;}


This response reminds of something I've been wating to say to all of you young
aspiring programmers out there. DON'T DO DRUGS! DRUGS WILL REALLY MESS YOU UP
BUT YOU WON"T REALISE IT!
Jul 22 '05 #7

P: n/a
On Thu, 11 Dec 2003 11:31:14 -0500, "jeffc" <no****@nowhere.com>
wrote:
"Chris Mantoulidis" <cm****@yahoo.com> wrote in message
news:a8**************************@posting.google. com...


[snip]
But I am thinking of a change, a change to a style more widely used in
programming languages. If I seem not to like it, I will return to my
current one... The new style is:

int main() {
for (int i = 0; i < 50; i++) {
cout << "Hello World!\n";
j := 2*i+1; ^ ^^
Who? <g>
Invalid operator.
}

return 0;
}


I would not exactly put this on par with moving to Alaska, losing 200
pounds, or having a sex change operation. It really doesn't make much
difference.


I find this style, where braces are at other than the beginning
of a line, to be very awkward when I have to analyse a program. It
means that I have to scan the entire source line to find the braces.
This is considerably slower. My native style is:

int main()

{
for (int i=0; i<50; ++i)
{
std:: cout << "Hello World!\n";
int j=2*i+1; // errors minimally corrected
}

return 0;
}

I can and do adapt to different styles, but braces not at the
start of the line: blech!

Sincerely,

Gene Wirchenko

Jul 22 '05 #8

P: n/a
Peter said he uses a style like the first one...

What about you Nils???

The thread is about the styles people use, I just brought my styles as
an example...

I wanna see what's the most widely used style. I'm curious even though
I know curiosity killed the cat.
Jul 22 '05 #9

P: n/a
"Gene Wirchenko" <gw**************@CAPITALSwencomine.com> wrote in message
news:53********************************@4ax.com...
On Thu, 11 Dec 2003 11:31:14 -0500, "jeffc" <no****@nowhere.com>
wrote:
"Chris Mantoulidis" <cm****@yahoo.com> wrote in message
news:a8**************************@posting.google. com...


[snip]
But I am thinking of a change, a change to a style more widely used in
programming languages. If I seem not to like it, I will return to my
current one... The new style is:

int main() {
for (int i = 0; i < 50; i++) {
cout << "Hello World!\n";
j := 2*i+1; ^ ^^
Who? <g>
Invalid operator.
}

return 0;
}


I would not exactly put this on par with moving to Alaska, losing 200
pounds, or having a sex change operation. It really doesn't make much
difference.


I find this style, where braces are at other than the beginning
of a line, to be very awkward when I have to analyse a program. It
means that I have to scan the entire source line to find the braces.
This is considerably slower. My native style is:

int main()

{
for (int i=0; i<50; ++i)
{
std:: cout << "Hello World!\n";
int j=2*i+1; // errors minimally corrected
}

return 0;
}

I can and do adapt to different styles, but braces not at the
start of the line: blech!

Sincerely,

Gene Wirchenko


Ahhh! That will eat 25% of my vertical editing space. On another note,
horizontal whitespace is your friend:

int main() {
int j = 0;

for ( int i = 0; i < 50; ++i ) {
cout << "Hello World!" << endl;
j = 2 * i + 1;
}

return 0;
}

Mike
Jul 22 '05 #10

P: n/a
jeffc wrote:
"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote in message
news:pa***************************@spam.for.me.inv alid...
Tabs for indentation is evil...

I agree, but why do you say that exactly?


Because they expand differently on different editors?

--
Mike Smith

Jul 22 '05 #11

P: n/a
On Thu, 11 Dec 2003 16:46:33 -0500, Mike Smith wrote:
jeffc wrote:
"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote in message
news:pa***************************@spam.for.me.inv alid...
Tabs for indentation is evil...

I agree, but why do you say that exactly?


Because they expand differently on different editors?


Yep. But the worst is when one programmer edits code using tabs for
indentation, and then another opens it and uses spaces in his changes
assuming tabs are eg 8 spaces and then another edits som other part
assuming tabs are 4 spaces etc...

The code can become horribly unreadable.

--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up

Jul 22 '05 #12

P: n/a
On Thu, 11 Dec 2003 10:36:46 -0800, Chris Mantoulidis wrote:
Peter said he uses a style like the first one...

What about you Nils???

The thread is about the styles people use, I just brought my styles as
an example...

I wanna see what's the most widely used style. I'm curious even though
I know curiosity killed the cat.


Here:
I usually use 4 spaces for indentation but I use 2 here because I don't
want the lines to get too long for a usenet post.

I try not to break functions and parameters into more than one line like:
function(int a,
int b);
because not breaking it makes for easier grep-ing. But if I have to
because of line length I indent the parameters on the next line.

int main(void)
{
/* starting brace on next line for functions */

if (check_condition()) {
/* braces for one statement ifs too */
do_stuff();
} else {
/* else together with clocing brace */
do_other_stuff();
}

while (other_condition) {
do_stuff();
}

for (int i = 0; i < 50; ++i) {
do_stuff();
}

do {
stuff();
} while (yet_another_condition());
return 0;
}

class MyClass : public ParentClass
{
public:
MyClass();
virtual ~MyClass();

public: /* functions */
void do_it();

private: /* functions */
void my_function();

private: /* variables */
Type *data;
};

MyClass::MyClass()
: ParentClass(true,false)
{
/* Initializer list on separate indented line */
data = new Data;
}

--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up

Jul 22 '05 #13

P: n/a
On Thu, 11 Dec 2003 16:40:44 +0000, jbruno4000 wrote:
This response reminds of something I've been wating to say to all of you
young aspiring programmers out there. DON'T DO DRUGS! DRUGS WILL REALLY
MESS YOU UP BUT YOU WON"T REALISE IT!


But, but, but ....
I need my coffee.

Caffeine is a stimulant isn't it? :)

As if young aspiring programmers (read: fresh out of school without a job)
could afford drugs anyway. Maybe they could have a few years ago when
there were more jobs and more money in the system.

Anyway seriously, don't do it, even if you think it's safe why take the
risk. Also even if you're not using the really dangerous stuff you're
giving money to the kind of people that sell the dangerous stuff. Do you
really want to keep drug dealers in business?

--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up

Jul 22 '05 #14

P: n/a

WHAT IS YOUR FAVORABLE SYTLE???????????????? give an example if
necessary


I think that many programmers follow GNU Coding Style:
http://www.gnu.org/prep/standards.html

But personally don't think It is very important. Pay attension on "best
practices" such us: not using magic numbers, commets etc.
You can easly and automatically reformat your source into your favourite
style using i.e. ident.

Piotr

Jul 22 '05 #15

P: n/a

"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote in message
news:pa***************************@spam.for.me.inv alid...

I try not to break functions and parameters into more than one line like:
function(int a,
int b);
because not breaking it makes for easier grep-ing.


grep - hmmmm. Well, anyway, breaking the parameters over lines does have
the benefit of allowing a convenient way to comment:
function(int, // the length, in feet. must not be 0.
int); // the cost in pennies per foot. 0 is valid.

That has 2 benefits:
a) in true OO style, the prototype should not really list the actual
parameter names (this is an implementation detail)
b) no name that you'd want to type can hold all the information I listed
above anyay.
Jul 22 '05 #16

P: n/a
Snap <pp****@onet.pl> wrote in message news:<br**********@news.onet.pl>...

WHAT IS YOUR FAVORABLE SYTLE???????????????? give an example if
necessary
I think that many programmers follow GNU Coding Style:
http://www.gnu.org/prep/standards.html


Ouch, my eyes hurt. Still this is funny: "If you do support Windows,
please do not abbreviate it as "win". In hacker terminology, calling
something a "win" is a form of praise."

I stick to roughly the first style, but then I don't run naive text
parsing algorithms on my source (compiling, grep / find-in-files yes)
so I haven't learned the importance of starting function names in the
first column.
But personally don't think It is very important. Pay attension on "best
practices" such us: not using magic numbers, commets etc.
You can easly and automatically reformat your source into your favourite
style using i.e. ident.

Piotr


Yes, naming things well (including magic numbers and types - not
everything is an int or a void* !) and commenting the uses/ranges of
member variables is more important than formatting and cannot be done
automatically.

Carl.
Jul 22 '05 #17

P: n/a
Chris Mantoulidis wrote:
I am thinking of a change
to a style more widely used in programming languages.


Check out Artistic Style:

http://astyle.sourceforge.net/

Artistic Style is a reindenter and reformatter
of C++, C and Java source code.

Jul 22 '05 #18

P: n/a
On Fri, 12 Dec 2003 10:37:19 -0500, jeffc wrote:

"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote in message
news:pa***************************@spam.for.me.inv alid...

I try not to break functions and parameters into more than one line like:
function(int a,
int b);
because not breaking it makes for easier grep-ing.
grep - hmmmm. Well, anyway, breaking the parameters over lines does have
the benefit of allowing a convenient way to comment:
function(int, // the length, in feet. must not be 0.
int); // the cost in pennies per foot. 0 is valid.


Unless you find that style of commenting ugly.

That has 2 benefits:
a) in true OO style, the prototype should not really list the actual
parameter names (this is an implementation detail)
But the name can contain a little information, and when reading a
(well documented) header file I find it easier to remember that, "'length'
is an int that represents the length in feet" than "the n-th integer
parameter represents the length in feet".

The upside of not using parameter names would be that constructing a
regexp (to find some particular function body and/or declaration in a
source tree) will be easier since you can grep on the types without
needing to ignore parameter names.
b) no name that you'd want to type can hold all the information I listed
above anyay.


In the case above I would probably have used something like
/* function does this and that
*
* length : length in feet, > 0
* cost : cost in pennies per foot, >= 0
*/
void function(unsigned int length, unsigned int cost);

--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up

Jul 22 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.