3 ms·
I for one have read a lot of code with C89 declare-first style that was subjectively a lot less clear. This applies in particular to assign-once variables that
by jstimpfle 5y ago
I for one have read a lot of code with C89 declare-first style that was subjectively a lot less clear. This applies in particular to assign-once variables that depend on other calculations.
stuff like this: [0]. I mean, what's the point of declaring variables like cp or err at the beginning when they're assigned first only halfway down? What is the point of having path around for the whole function, when it is a super temporary assign-once variable with a scope of 3 lines? I'm having a really hard time coming up with a justification for this, other than "this is the way we've always done it and we like it" (you can find actual rants in defense of this style from about 10 years ago).
For-loop declarations are another instance of this that make the code more fluent IMO. And they help reducing the scope of variables to a strictly smaller block. I would find it hard to argue that this does not remove some bugs or improve readability.
The only thing that declare-first has going for it is that the variables that are in use can be seen at once, a little bit like in a struct declaration. Looking at how optimizers butcher those variables depending on liveness makes this feature seem less valuable, though.
[0] https://github.com/torvalds/linux/blob/2729cfdcfa1cc49bef5a90d046fa4a187fdfcc69/fs/ext4/file.c#L788 https://github.com/torvalds/linux/blob/2729cfdcfa1cc49bef5a9...
- pjmlp 5y agoThat is something I fully agree with.