Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Do variables need to be initialized? |
| Date | 2015-06-07 09:07 +1200 |
| Message-ID | <cth5p5Fn0m1U1@mid.individual.net> (permalink) |
| References | (4 earlier) <mkshk3$9jt$1@dont-email.me> <mkslqf$srg$1@dont-email.me> <1bpp59h7go.fsf@pfeifferfamily.net> <ctf8tcF23b8U1@mid.individual.net> <mkugbv$v1h$1@dont-email.me> |
Bartc wrote: > On 06/06/2015 04:49, Ian Collins wrote: >> Joe Pfeiffer wrote: >>>> On 05/06/2015 17:10, Stephen Sprunk wrote: >>>> >>>>> mixing declarations and >>>>> statements is an obvious improvement. >>> >>> When mixing became possible I tried it on a couple of projects and >>> concluded I hate it. I want to be able to look in one place for my >>> variables' declarations, not have to go searching through the function. >> >> If you have to search, the function is too long. > > But if the function is short, what is the advantage of having > declarations in arbitrary places, or even of having nested declarations > within blocks? For one you don have to worry about whether or not to initialise variables! > And even short functions can have complicated looking lines (I suppose > your solution is to factor even more, but then there is even less need > to declare things in random locations). One of my pet hates is complicated expressions in conditionals. Assigning the value of the expression to a local const (or building the value up in steps with several) makes the code easier to read and debug. Declaring these anywhere other that the point of first use makes the code harder to read. > But, sometimes you have int a,b; two thirds through the function, and > a=b; shortly after. Then you decide you need a=b; earlier on as well. > Now, you have to keep schlepping the declarations around the function as > the first appearance of each name in the code changes! Not something I can recall doing. > And if you forget to do so, the declaration occurs after the use, the > code will still compile if there more file-scope names that match. But > there's now a non-obvious bug. Just a breakdown in process. > I really can't see any justification for this awful practice at all > (excepting as 'for (int i=0....)', but that's the only code example). > Especially when other people have to read the code. Consider the situation I mentioned above. > This is some real code (not mine). /Fortunately/, most C open source > projects seem to have adopted the style of declaring all the local > variables at the start of a function. Given that rule, consider how the code would look if the function were to start with some precondition checks, would you declare the local variables before or after them? -- Ian Collins
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Do variables need to be initialized? fl <rxjwg98@gmail.com> - 2015-06-01 16:48 -0700
Re: Do variables need to be initialized? Richard Heathfield <rjh@cpax.org.uk> - 2015-06-02 00:55 +0100
Re: Do variables need to be initialized? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-06-01 18:22 -0600
Re: Do variables need to be initialized? Keith Thompson <kst-u@mib.org> - 2015-06-02 08:05 -0700
Re: Do variables need to be initialized? Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-11 08:07 -0700
Re: Do variables need to be initialized? Keith Thompson <kst-u@mib.org> - 2015-07-11 16:47 -0700
Re: Do variables need to be initialized? Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-15 07:42 -0700
Re: Do variables need to be initialized? Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-11 08:03 -0700
Re: Do variables need to be initialized? David Brown <david.brown@hesbynett.no> - 2015-06-02 09:15 +0200
Re: Do variables need to be initialized? Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-11 08:09 -0700
Re: Do variables need to be initialized? David Kleinecke <dkleinecke@gmail.com> - 2015-06-03 09:30 -0700
Re: Do variables need to be initialized? gwowen <gwowen@gmail.com> - 2015-06-03 09:41 -0700
Re: Do variables need to be initialized? David Kleinecke <dkleinecke@gmail.com> - 2015-06-04 10:17 -0700
Re: Do variables need to be initialized? Stephen Sprunk <stephen@sprunk.org> - 2015-06-05 11:10 -0500
Re: Do variables need to be initialized? Bartc <bc@freeuk.com> - 2015-06-05 18:22 +0100
Re: Do variables need to be initialized? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-06-05 20:54 -0600
Re: Do variables need to be initialized? Ian Collins <ian-news@hotmail.com> - 2015-06-06 15:49 +1200
Re: Do variables need to be initialized? Dr Nick <nospam-4@temporary-address.org.uk> - 2015-06-06 08:42 +0100
Re: Do variables need to be initialized? Keith Thompson <kst-u@mib.org> - 2015-06-06 13:11 -0700
Re: Do variables need to be initialized? Dr Nick <nospam-4@temporary-address.org.uk> - 2015-06-07 16:58 +0100
Re: Do variables need to be initialized? Bartc <bc@freeuk.com> - 2015-06-06 11:01 +0100
Re: Do variables need to be initialized? Ian Collins <ian-news@hotmail.com> - 2015-06-07 09:07 +1200
Re: Do variables need to be initialized? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-06-06 15:06 -0600
Re: Do variables need to be initialized? gwowen <gwowen@gmail.com> - 2015-06-08 05:39 -0700
Re: Do variables need to be initialized? Bartc <bc@freeuk.com> - 2015-06-08 14:10 +0100
Re: Do variables need to be initialized? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-06-08 14:21 +0100
Re: Do variables need to be initialized? Bartc <bc@freeuk.com> - 2015-06-08 14:34 +0100
Re: Do variables need to be initialized? David Brown <david.brown@hesbynett.no> - 2015-06-08 16:51 +0200
Re: Do variables need to be initialized? Richard Heathfield <rjh@cpax.org.uk> - 2015-06-08 16:15 +0100
Re: Do variables need to be initialized? Bartc <bc@freeuk.com> - 2015-06-08 17:31 +0100
Re: Do variables need to be initialized? Ian Collins <ian-news@hotmail.com> - 2015-06-09 08:06 +1200
Re: Do variables need to be initialized? Bartc <bc@freeuk.com> - 2015-06-08 22:58 +0100
Re: Do variables need to be initialized? Keith Thompson <kst-u@mib.org> - 2015-06-08 08:27 -0700
Re: Do variables need to be initialized? gwowen <gwowen@gmail.com> - 2015-06-08 08:01 -0700
Re: Do variables need to be initialized? Stephen Sprunk <stephen@sprunk.org> - 2015-06-06 11:11 -0500
Re: Do variables need to be initialized? Bartc <bc@freeuk.com> - 2015-06-06 19:09 +0100
Re: Do variables need to be initialized? Robert Wessel <robertwessel2@yahoo.com> - 2015-06-06 13:57 -0500
Re: Do variables need to be initialized? Keith Thompson <kst-u@mib.org> - 2015-06-06 13:52 -0700
Re: Do variables need to be initialized? James Kuyper <jameskuyper@verizon.net> - 2015-06-06 20:57 -0400
Re: Do variables need to be initialized? Stephen Sprunk <stephen@sprunk.org> - 2015-06-10 14:33 -0500
Re: Do variables need to be initialized? Keith Thompson <kst-u@mib.org> - 2015-06-05 10:56 -0700
csiph-web