4 ms·
> Do not use for loop initial declarations > Variadic macros are acceptable, but remember Maybe my brain is too smooth, but I don't understand how for(int i =
by unclad5968 2y ago
> Do not use for loop initial declarations
> Variadic macros are acceptable, but remember
Maybe my brain is too smooth, but I don't understand how for(int i = 0...) is too clever but variadic macros are not. That makes no sense to me.
- zzo38computer 2y agoAt least in my opinion, "for loop initial declaration" is especially useful in macros (although many programs will never need such macros, they are especially useful when you do have them). An example of such a macro is the following macro (the loop and the variable declaration will both be optimized out by the compiler; I have tested this): #define lpt_document() for(int lpt_document_=lpt_begin();lpt_document_;lpt_document_=(lpt_end(),0)) Another macro (which is a part of a immediate mode UI implementation) is: #define win_form(xxx) for(win_memo win_mem=win_begin_();;win_step_(&win_mem,xxx))
- astrobe_ 2y agoThat's a bad coding style document. There's no rationale given, except for a bunch of references at the top, which are clearly argument from authority. I think the no "loop initial declarations" is for consistency with "all declarations at the top". Other coding style guides favor "declarations as close as possible to first use", including guidelines for mission critical systems (if you resort to argument from authority I have some too...) [1]. As much as I like Suckless, this section is just pet peeves that can safely be ignored; unless you submit a patch to a project that aligns with it. [1] https://pvs-studio.com/en/docs/warnings/v2551/ https://pvs-studio.com/en/docs/warnings/v2551/
- flykespice 2y agotheir rationale is very inconsistent: tells you to use c99 but you must place declarations at the top, and you can't use C++ style // comments (introduced in c99 aswell). Why not force c90 altogether then?
- guenthert 2y ago> There's no rationale given True and it would indeed be desirable that it were. Here I go out on a limb and assume it's because someone got bitten by attempting to use the loop index outside the loop (common for search operations) while declaring the index within and outside the loop. A bug (gcc and clang can warn about using -Wshadow, but which sadly isn't part of -Wall) which might easily occur when multiple people edit the code over a longer time-span.
- celrod 2y agoI use Wshadow personally. I highly recommend it. I think code that violates it (even if correct) is harder to understand.