It makes the lifetime of the loop index be just the loop.Modern C lets you just do
for (int i=0; i<k; i++) {...}.
(Maybe I'm an old fart for this habit, which I cling to from my Fortran days [early 1970's], but limiting the lifetimes of variables makes things easier for register allocators and for people reading your code.)Which is why I like Lisp, with explicit
let blocks.
(defun example ()
(let ((a 42))
; a is defined here
)
; a is not defined anymore
)
Of course most of the control-flow stuff has implicit let built in one way or another.
int example()
{
/* ... */
{
int a = 42;
/* a is defined here */
}
/* a is not defined anymore */
}I don't like side effects in conditionals (or embedded in most expressions, but let's just think about conditionals for now.) Code almost always reads better if you separate the side effects from the tests. For example, I writeDoes anyone want to weigh in on this? Personally, I find the first version more obvious because it's such a common idiom. Is avoiding side effcts in conditionals something I should be thinking about?
do
c=getchar();
while(strchr(" \t\n", c));
in preference to
while(strchr(" \t\n", c=getchar()));
« Older A long-brewing online poker scandal reaches the ma... | Two guys join forces together ... Newer »
This thread has been archived and is closed to new comments
posted by delmoi at 9:32 PM on November 30, 2008 [1 favorite]