(Programming) Various Syntax styles

C

Chris Mantoulidis

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
 
P

Peter van Merkerk

Chris Mantoulidis said:
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.
 
N

Nils Petter Vaskinn

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

Dario

Chris said:
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: (e-mail address removed)
************************************************************//*
Version History:
1.0 Dec 6 1994
Initial version
1.1 Dec 7 1994
1. Correct from:
sscanf(argv+1, "%ld", &maxOutLen)
to:
sscanf(argv+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++){len=strlen(
operatorString);if(strlen(p)>=len&&!MYstrncmp(p,operatorString
,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*)*line,(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(inLine)){c=fgetc(inF);
if(c==EOF)break;addCharToLine((char)c,&inLine);}len=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(lastToken)+1));if(!p)
outOfMemory();(void)sprintf(p,"%s%s",prevToken,lastToken);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=needSpace();(void)
fprintf(outF,"%s%s",need?" ":"",lastToken);outCounter+=need+len;if(
inComment||(isComment&&len>=2&&!MYstrncmp(lastToken,"//",2)))
forceNewLine();}static void preProcessorLine FUNC_1(char*,p){
forceNewLine();(void)fprintf(outF,"%s",p);outCounter+=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(!lastToken)
outOfMemory();for(i=0;i<len;i++)lastToken=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_SIGN,3))))
preProcessorLine(inLine);else cLine(inLine);}static void engine(VOID
){while(!feof(inF)){readLine();analizeLine();}forceNewLine();}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,"-X"))removeComment=1;else if(inF==stdin&&!
strcmp(argv,"-C"))removeComment=0;else if(inF==stdin&&argv[0]
=='-'){if(argv[1]==0)maxOutLen=70;else if(sscanf(argv+1,"%d",&
maxOutLen)!=1||maxOutLen<0)usage(argv[0]);maxOutLen--;if(maxOutLen<0
)maxOutLen=1;}else{if(inF!=stdin&&outF!=stdout)usage(argv[0]);f=
fopen(argv,(inF==stdin)?"r":"w");if(!f){(void)fprintf(stderr,
"Unable to open file %s\n",argv);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();return 0;}
 
J

jeffc

Chris Mantoulidis said:
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.
 
J

jbruno4000

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: (e-mail address removed)
************************************************************//*
Version History:
1.0 Dec 6 1994
Initial version
1.1 Dec 7 1994
1. Correct from:
sscanf(argv+1, "%ld", &maxOutLen)
to:
sscanf(argv+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++){len=strlen(
operatorString);if(strlen(p)>=len&&!MYstrncmp(p,operatorString
,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*)*line,(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(inLine)){c=fgetc(inF);
if(c==EOF)break;addCharToLine((char)c,&inLine);}len=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(lastToken)+1));if(!p)
outOfMemory();(void)sprintf(p,"%s%s",prevToken,lastToken);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=needSpace();(void)
fprintf(outF,"%s%s",need?" ":"",lastToken);outCounter+=need+len;if(
inComment||(isComment&&len>=2&&!MYstrncmp(lastToken,"//",2)))
forceNewLine();}static void preProcessorLine FUNC_1(char*,p){
forceNewLine();(void)fprintf(outF,"%s",p);outCounter+=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(!lastToken)
outOfMemory();for(i=0;i<len;i++)lastToken=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_SIGN,3))))
preProcessorLine(inLine);else cLine(inLine);}static void engine(VOID
){while(!feof(inF)){readLine();analizeLine();}forceNewLine();}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,"-X"))removeComment=1;else if(inF==stdin&&!
strcmp(argv,"-C"))removeComment=0;else if(inF==stdin&&argv[0]
=='-'){if(argv[1]==0)maxOutLen=70;else if(sscanf(argv+1,"%d",&
maxOutLen)!=1||maxOutLen<0)usage(argv[0]);maxOutLen--;if(maxOutLen<0
)maxOutLen=1;}else{if(inF!=stdin&&outF!=stdout)usage(argv[0]);f=
fopen(argv,(inF==stdin)?"r":"w");if(!f){(void)fprintf(stderr,
"Unable to open file %s\n",argv);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();return 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!
 
G

Gene Wirchenko

[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;
^ ^^
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
 
C

Chris Mantoulidis

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

Vis Mike

Gene Wirchenko said:
[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;
^ ^^
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
 
N

Nils Petter Vaskinn

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

Nils Petter Vaskinn

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;
}
 
S

Snap

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
 
N

Nils Petter Vaskinn

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?
 
J

jeffc

Nils Petter Vaskinn said:
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.
 
C

Carl Muller

Snap said:
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.
 
N

Nils Petter Vaskinn

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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top