Re: a few style examples don't comply
On Mon, 05 Oct 2015 23:36:11 +0200, Benny Lofgren wrote: > This reminds me of something I've always wondered, but never gotten > around to ask about... > > Why is the return value in return statements almost always enclosed in > parantheses in the codebase? > > It's not explained in style(9) as far as I know, so I guess it just > crept in over time... The old (CSRG) style(9) for return values was: return (val); /* space before the paren */ The new (OpenBSD) style(9) is: return val; /* no parens */ But there is still lots of code using the old style. - todd
Re: a few style examples don't comply
On Mon, Oct 05, 2015 at 05:38:34PM +0059, Jason McIntyre wrote: > On Mon, Oct 05, 2015 at 11:50:49AM -0400, Rob Pierce wrote: > > There are some offending braces. I just added leading tabs in the right > > places to correct indentation. > > > > Rob > > > > why are you indenting? the point of "-offset indent" in the list/display > is to do just that. > > jmc Hey Jason, I am back at my desk now, so here is a better explination of my diff. In my example corrections I indented the commands, but not the function's closing brace. Is it KNF compliant to have an exit() or return() at the same indentation as the closing function brace? For example: exit(1); } Or: return(1); } My interpretation was that the style guide (and source code that I have looked at) suggests that this is not the preferred style. My understanding was that the following is preferred: exit(1); } And: return(1); } The common usage() function that I have seen in code is as follows: /* From ntp.c. */ __dead void usage(void) { extern char *__progname; if (strcmp(__progname, "ntpctl") == 0) fprintf(stderr, "usage: ntpctl -s all | peers | Sensors | status\n"); else fprintf(stderr, "usage: %s [-dnSsv] [-f file]\n", __progname); exit(1); } Rob
Re: a few style examples don't comply
On 2015-10-05 22:21, Rob Pierce wrote: > On Mon, Oct 05, 2015 at 05:38:34PM +0059, Jason McIntyre wrote: >> On Mon, Oct 05, 2015 at 11:50:49AM -0400, Rob Pierce wrote: > Is it KNF compliant to have an exit() or return() at the same indentation as > the closing function brace? For example: > > exit(1); > } > > Or: > > return(1); > } This reminds me of something I've always wondered, but never gotten around to ask about... Why is the return value in return statements almost always enclosed in parantheses in the codebase? It's not explained in style(9) as far as I know, so I guess it just crept in over time... Semantically parantheses are of course completely unnecessary, and in my not so humble opinion they only adds ugliness and distraction. Also it makes the code inconsistent, because we don't use goto (label); or break(); or continue(); for example. Nor do we "return();" from void functions either, for that matter... There! I just had to get that off of my chest. I feel much better now, thank you. :-) Regards, /Benny
Re: a few style examples don't comply
Rob Pierce wrote: > On Mon, Oct 05, 2015 at 05:38:34PM +0059, Jason McIntyre wrote: > > On Mon, Oct 05, 2015 at 11:50:49AM -0400, Rob Pierce wrote: > > > There are some offending braces. I just added leading tabs in the right > > > places to correct indentation. > > > > > > Rob > > > > > > > why are you indenting? the point of "-offset indent" in the list/display > > is to do just that. > > > > jmc > > Hey Jason, > > I am back at my desk now, so here is a better explination of my diff. > > In my example corrections I indented the commands, but not the function's > closing brace. i don't think we need the closing braces in many cases. i'll take a look, thanks.
Re: a few style examples don't comply
Benny Lofgren wrote: > On 2015-10-05 22:21, Rob Pierce wrote: > > On Mon, Oct 05, 2015 at 05:38:34PM +0059, Jason McIntyre wrote: > >> On Mon, Oct 05, 2015 at 11:50:49AM -0400, Rob Pierce wrote: > > Is it KNF compliant to have an exit() or return() at the same indentation as > > the closing function brace? For example: > > > > exit(1); > > } > > > > Or: > > > > return(1); > > } > > This reminds me of something I've always wondered, but never gotten > around to ask about... > > Why is the return value in return statements almost always enclosed in > parantheses in the codebase? > > It's not explained in style(9) as far as I know, so I guess it just > crept in over time... rumor has it that you can turn return() into a debug statement with a macro if it uses parenthesis, but since i've never done that, i don't find it a good argument. i think code should try to be consistent in a file, but new code i write doesn't follow this rule.
Re: a few style examples don't comply
On Mon, Oct 05, 2015 at 04:21:34PM -0400, Rob Pierce wrote: > On Mon, Oct 05, 2015 at 05:38:34PM +0059, Jason McIntyre wrote: > > On Mon, Oct 05, 2015 at 11:50:49AM -0400, Rob Pierce wrote: > > > There are some offending braces. I just added leading tabs in the right > > > places to correct indentation. > > > > > > Rob > > > > > > > why are you indenting? the point of "-offset indent" in the list/display > > is to do just that. > > > > jmc > > Hey Jason, > > I am back at my desk now, so here is a better explination of my diff. > > In my example corrections I indented the commands, but not the function's > closing brace. > > Is it KNF compliant to have an exit() or return() at the same indentation as > the closing function brace? For example: > > exit(1); > } > > Or: > > return(1); > } > > My interpretation was that the style guide (and source code that I have > looked at) suggests that this is not the preferred style. My understanding > was that the following is preferred: > > exit(1); > } > > And: > > return(1); > } > > The common usage() function that I have seen in code is as follows: > > /* From ntp.c. */ > > __dead void > usage(void) > { > extern char *__progname; > > if (strcmp(__progname, "ntpctl") == 0) > fprintf(stderr, > "usage: ntpctl -s all | peers | Sensors | status\n"); > else > fprintf(stderr, "usage: %s [-dnSsv] [-f file]\n", > __progname); > exit(1); > } > > Rob > that's fine, i see what you're trying to do now. someone else will have to be the judge of whether this is wanted though. jmc
Re: a few style examples don't comply
On Mon, Oct 05, 2015 at 11:50:49AM -0400, Rob Pierce wrote: > There are some offending braces. I just added leading tabs in the right > places to correct indentation. > > Rob > why are you indenting? the point of "-offset indent" in the list/display is to do just that. jmc > Index: style.9 > === > RCS file: /cvs/src/share/man/man9/style.9,v > retrieving revision 1.62 > diff -u -p -r1.62 style.9 > --- style.9 5 Oct 2015 01:22:34 - 1.62 > +++ style.9 5 Oct 2015 15:46:43 - > @@ -377,10 +377,10 @@ k = !(l & FLAGS); > .Pp > Exits should be 0 on success, or non-zero for errors. > .Bd -literal -offset indent > -exit(0); /* > - * Avoid obvious comments such as > - * "Exit 0 on success." > - */ > + exit(0);/* > + * Avoid obvious comments such as > + * "Exit 0 on success." > + */ > } > .Ed > .Pp > @@ -526,11 +526,11 @@ and > family of functions. > Don't roll your own! > .Bd -literal -offset indent > -if ((four = malloc(sizeof(struct foo))) == NULL) > - err(1, NULL); > -if ((six = (int *)overflow()) == NULL) > - errx(1, "Number overflowed."); > -return (eight); > + if ((four = malloc(sizeof(struct foo))) == NULL) > + err(1, NULL); > + if ((six = (int *)overflow()) == NULL) > + errx(1, "Number overflowed."); > + return (eight); > } > .Ed > .Pp > @@ -600,8 +600,8 @@ The > .Li __progname > string may be used instead of hard-coding the program name. > .Bd -literal -offset indent > -(void)fprintf(stderr, "usage: %s [-ab]\en", __progname); > -exit(1); > + (void)fprintf(stderr, "usage: %s [-ab]\en", __progname); > + exit(1); > } > .Ed > .Pp >
a few style examples don't comply
There are some offending braces. I just added leading tabs in the right places to correct indentation. Rob Index: style.9 === RCS file: /cvs/src/share/man/man9/style.9,v retrieving revision 1.62 diff -u -p -r1.62 style.9 --- style.9 5 Oct 2015 01:22:34 - 1.62 +++ style.9 5 Oct 2015 15:46:43 - @@ -377,10 +377,10 @@ k = !(l & FLAGS); .Pp Exits should be 0 on success, or non-zero for errors. .Bd -literal -offset indent -exit(0); /* -* Avoid obvious comments such as -* "Exit 0 on success." -*/ + exit(0);/* +* Avoid obvious comments such as +* "Exit 0 on success." +*/ } .Ed .Pp @@ -526,11 +526,11 @@ and family of functions. Don't roll your own! .Bd -literal -offset indent -if ((four = malloc(sizeof(struct foo))) == NULL) - err(1, NULL); -if ((six = (int *)overflow()) == NULL) - errx(1, "Number overflowed."); -return (eight); + if ((four = malloc(sizeof(struct foo))) == NULL) + err(1, NULL); + if ((six = (int *)overflow()) == NULL) + errx(1, "Number overflowed."); + return (eight); } .Ed .Pp @@ -600,8 +600,8 @@ The .Li __progname string may be used instead of hard-coding the program name. .Bd -literal -offset indent -(void)fprintf(stderr, "usage: %s [-ab]\en", __progname); -exit(1); + (void)fprintf(stderr, "usage: %s [-ab]\en", __progname); + exit(1); } .Ed .Pp