|
|
Subscribe / Log in / New account

An interesting historical artifact

An interesting historical artifact

Posted Oct 25, 2007 15:01 UTC (Thu) by brinkmd (guest, #45122)
In reply to: An interesting historical artifact by tialaramex
Parent article: A potential competitor for GCC: pcc

Many modern programs are written with portability to old environments in mind.


to post comments

An interesting historical artifact

Posted Oct 25, 2007 17:55 UTC (Thu) by RobSeace (subscriber, #4435) [Link] (12 responses)

Or, sometimes, are just written by old programmers who grew up in a K&R environment, so they
just always keep variable declarations at the start of a block, because it just feels ugly and
WRONG to have them strewn around intermixed with the code...  It almost makes me feel like I'm
writing C++ code! *shudder* ;-)

An interesting historical artifact

Posted Oct 26, 2007 3:13 UTC (Fri) by giraffedata (guest, #1954) [Link] (11 responses)

written by old programmers who grew up in a K&R environment, so they just always keep variable declarations at the start of a block,

Start of a block? The vast majority of C code I see keeps all the declarations at the start of the function.

An interesting historical artifact

Posted Oct 26, 2007 10:18 UTC (Fri) by RobSeace (subscriber, #4435) [Link] (10 responses)

> Start of a block? The vast majority of C code I see keeps all the
> declarations at the start of the function.

Which also happens to be the start of a block... ;-)  Yeah, true, most go at the start of the
function, but I at least can tolerate some inside other blocks, if they're only needed
there...  I can't tolerate them just spread willy-nilly throughout the code, though...  And, I
absolutely loathe declaring "int i" inside the parenthesis of a for() loop...  Whoever came up
with that one should be beaten... ;-)

Placement of variable declarations in C

Posted Oct 26, 2007 15:46 UTC (Fri) by giraffedata (guest, #1954) [Link] (9 responses)

I don't tolerate variables declared outside the scope in which they are used (and you should see how easy it is to move code around and split functions when you observe this rule). And I don't use the index variable of a for structure outside of the structure, so I saw the invention of the internally declared index as a great advancement. My blocks and functions are too short for anything to be spread willy-nilly throughout them.

Placement of variable declarations in C

Posted Oct 26, 2007 16:14 UTC (Fri) by RobSeace (subscriber, #4435) [Link] (8 responses)

> I don't tolerate variables declared outside the scope in which they are used

Sometimes, one uses the same variable in multiple blocks within a single function...

> And I don't use the index variable of a for structure outside of the structure,

You don't?  I often do...  Sometimes, when you break out of the loop, you need to know the
value of the index at that point for some reason or other (null terminate a string, obtain an
array count, etc.)...

> so I saw the invention of the internally declared index as a great advancement.

Bah, it's useless fluff...  Just declare one "int i;" at the top of the function, and use it
in ALL for() loops! ;-)  (Unless you need to nest multiple with differing indices, of course,
in which case you add an "int j;" to the top of the function as well... ;-))

Or, I could just be a grumpy old C coder, too set in his ways, too... ;-)

Placement of variable declarations in C

Posted Oct 26, 2007 16:47 UTC (Fri) by giraffedata (guest, #1954) [Link] (5 responses)

Sometimes, one uses the same variable in multiple blocks within a single function...

Then the scope in which it is used is the block that contains those blocks. Unless you're talking about reusing a C variable for multiple values, which is something else I don't tolerate.

And I don't use the index variable of a for structure outside of the structure,
You don't? I often do... Sometimes, when you break out of the loop, you need to know the value of the index at that point for some reason or other (null terminate a string, obtain an array count, etc.)...

I'm a structured programmer, so I never break out of a loop. In fact, I don't even recognize a for structure as a "loop." Structured programs don't branch.

Or, I could just be a grumpy old C coder, too set in his ways, too... ;-)

You sound like a very typical C coder; I'm unusual. C coders tend to be low-level coders; they write code which is a set of instructions to a computer. A variable is a storage location. Declarations get in your way. But I use C only grudgingly, in a careful paradigm to make it look like a high level language. High level code is a description of the solution to a computational problem. Its audience is a person, not a computer, and a compiler's job is create a low-level program that implements it. A variable is a symbol for a piece of information and the declarations are the heart of the code (indeed where most of my comments are).

Placement of variable declarations in C

Posted Oct 26, 2007 17:38 UTC (Fri) by RobSeace (subscriber, #4435) [Link] (4 responses)

> I'm a structured programmer, so I never break out of a loop.

So, all your loops are infinite, is that what you're saying?? ;-)

I was using "break out" not to ONLY mean using an explicit "break" statement, but also
naturally falling out due to failure of the test condition...  There are MANY cases where one
wants to use the loop index outside the loop, at least in my experience...

> In fact, I don't even recognize a for structure as a "loop." Structured
> programs don't branch.

*blink*  Ok, you're obviously using some strange definition of the term "structured" that I
wasn't previously aware of... ;-)  And, if you don't think a for() loop is a loop, WTF do you
think it IS??  It's certainly not a "structure", as you call it; a structure is a data record
created with the keyword "struct"... ;-)  In a for() loop, you LOOP over the same set of
statements multiple times until a condition is met, at which point you break out of that
loop...  If that's not a loop, I can't imagine what IS a loop, in your mind??

And, if your programs "don't branch", you must have a really hard time getting anything done,
forgoing if() statements, function calls, and all other such things, as you apparently have to
do... ;-)

The way I always understood the term, "structured programming" merely meant doing away with
the twisted spaghetti-code brought on by excessive goto use...  Eg: old-school BASIC code is
all unstructured, because you've got no other choose but goto for branching and control...
But, any time you're using something sane, like a loop, a conditional statement, or a function
call, that's structured...  Using goto or longjmp() is unstructured...

Placement of variable declarations in C

Posted Oct 28, 2007 2:16 UTC (Sun) by giraffedata (guest, #1954) [Link] (3 responses)

Structured programming isn't algorithmic programming that avoids excessive gotos. It's a different form of programming, and it doesn't have a goto concept at all.

Ideally, one would use a structured programming language for structured programming, but when forced to use C, one can use a paradigm that makes a structured meta-language out of it. For example, never use a goto statement.

You can look at a C program written in the structured paradigm as algorithmic and see looping and branching (obviously a compiler does), but since it wasn't written to be read that way, it won't be as easy to understand.

Structured programming has a for structure. It was named before C existed, and C's 'for' statement is meant to allude to it, though it is not the same as a structured for statement, e.g. in Pascal. The fact that the value of the index variable is predictable outside of the for statement is one way to tell the difference.

Besides algorithmic (branching and looping) and structured programming, there are various other forms of programming that don't have branches. LISP, Forth, and SAS are examples.

Placement of variable declarations in C

Posted Oct 28, 2007 11:28 UTC (Sun) by nix (subscriber, #2304) [Link]

I don't know what your definition of `branching' is, but it's not anything 
I'm familiar with. In order to describe any version of Lisp as `not having 
branches' one would have to define the two branches of `if' as `not 
branches', and similarly for `cond'.

If an exception throw/catch isn't a `branch', I don't know what is.

Placement of variable declarations in C

Posted Oct 28, 2007 14:26 UTC (Sun) by RobSeace (subscriber, #4435) [Link] (1 responses)

Yeah, what nix said...  I think you're just playing semantic games, here...  Just because you
don't want to call something a "branch" or a "loop", doesn't mean it doesn't fit the
definition of what I (and most other programmers I've encountered) use those words to mean...
Perhaps they mean something radically different in your world, I don't know...  Got a link to
any supporting documentation on your alternate meanings??

And, BTW, I happen to hold a degree in CS, so I'm fairly familiar with the various forms of
programming (though, it has been many years now), as well as languages such as LISP (which is
functional programming, BTW, and DOES have what *I* would call both loops and branches)...  I
just never encountered anyone who had your strange notions about structured programming, and
the supposed complete absense of "loops" and "branches", before... *shrug*

Placement of variable declarations in C

Posted Oct 28, 2007 19:04 UTC (Sun) by nix (subscriber, #2304) [Link]

Well, it's more accurate to say that Lisp *can* be functional. You have to 
avoid most forms of `set'/`setf', though, and it's sort of tricky compared 
to the mixed functional/declarative approach generally adopted.

Placement of variable declarations in C

Posted Oct 27, 2007 15:58 UTC (Sat) by tjc (guest, #137) [Link] (1 responses)

Just declare one "int i;" at the top of the function, and use it in ALL for() loops! ;-)
If you're not too set in your ways you might want to try "int ii;" instead. I've been doing this for a few years, and while it looks odd at first, I got used to it. It's a lot easier to find "ii" in code; searching for "i" is futile.

(off-topic) Searching for variables

Posted Nov 18, 2008 18:40 UTC (Tue) by pjm (guest, #2080) [Link]

You would benefit a lot from learning regular expressions. If you use vim, then it suffices to type ‘*’ when the cursor is over an ‘i’ variable to find all occurrences of ‘i’ as a “word” (so excluding ‘if’ etc.).

Many IDEs go one step farther and allow searching for just the current variable as distinct from other variables & functions of the same name.

Incidentally, I hope that the original poster is joking (as the ‘;-)’ suggests) in the suggestion to use a shared variable for all temporaries. Such sharing makes it harder to modify one use of the variable because one needs to check whether subsequent code is using the resulting value.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds