Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Small Device C Compiler 3.6.0 released Date: Fri, 17 Jun 2016 12:32:51 -0700 Organization: None to speak of Lines: 70 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="660"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+LiPh8fvTwl+3QN3ZEE+v7" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:wHfgKy+IPNGZ6Nkh8ljL2l5UlD0= sha1:8yPN/z7a6rj4kNJ2fjst7XIRkEg= Xref: csiph.com comp.lang.c:89444 BartC writes: [...] > Functions are an obvious and very well known and understood method of > structuring code into smaller pieces. It is useful to create a local > environment with its own declarations to deal with a specific task. So are blocks, to anyone who knows C reasonably well. [...] > And even with functions with their undoubted usefulness, you're only > allowed one level of function scoping; there are no nested functions. > Yet nested block scopes to any level are allowed even though they are > less useful. Because it's simpler to permit unlimited nesting than to impose an arbitrary restriction. [...] > So what *are* blocks for? > > For me, you use a block - ie. statements with {,} braces - when you have > a branch of an if-statement or a loop body that consists of more than > one statement. (Apart from function bodies of course.) Personally, I use braces even if there's only one statement; with that difference, that's the primary use of blocks. > That is their primary use, not for inventing yet another local scope > within a function that should already be providing a small, local, > self-contained environment so that it doesn't need a dozen more to > bamboozle the reader of the code. If I need to add a new variable in the middle of a medium-length function, I can either just declare and initialize it at the point where I need it (which means its scope extends to the end of the existing block), or I can restrict its scope by creating a new block to contain it. None of this is for the purpose of bamboozling anybody. For example, if I need to swap two variables, I might write something like this: int x, y; /* code that sets x and y */ { int old_x = x; x = y; y = old_x; } Or perhaps: int x, y; /* code that sets x and y */ { const int old_x = x; x = y; y = old_x; } Now if I saved the address of old_x in a variable whose scope is bigger than the scope of old_x, then I could have problems. *Which is why I don't do that.* If you'd rather define old_x at the top of the function, and wait to assign a value to it until the value of x has been computed, you can do that. But I find my approach cleaner and easier to maintain. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"