2

After long pause I'm forced to use C EDIT( Version C89) again. What really disturbs, the declaration of the variables at the beginning, which violates this C++ well-coding rule. The functions of customer are huge and the variable lists at the beginning of each function are nearly not manageable. Could one use scopes for this problem? Is it appropriate or a "hack"?

1st case. instead:

{//Beginning of the function
    int i;
    int Important=0;

using:

 {//Beginning of the function
    int Important=0;
    //... code
    { int i; for (i=0,... } //use i in close scope

2nd case instead:

{//Beginning of the function
    int i;
    // many other var definitions
    if ( !initialized ) return;

using:

{//Beginning of the function
    {if ( !initialized ) return; ... other checks}
    int i;
    // many other var definitions
Valentin H
  • 7,240
  • 12
  • 61
  • 111
  • 1
    Are you asking because "it doesn't work"? – Jongware Jan 26 '18 at 09:22
  • 3
    Mixed declarations/statements and declarations inside for-loops were introduced with C99, if I remember correctly. – And it look *loong* until the MS Visual Studio C compiler implemented that. – Martin R Jan 26 '18 at 09:29
  • Exactly @MartinR, probably I should include that in my answer... – gsamaras Jan 26 '18 at 09:30
  • 2
    While this particular rule is also valid in C, C++ coding guidelines aren't the best reference for C language. – Gerhardh Jan 26 '18 at 09:31
  • I mean yeah, it is an ugly hack, precisely because you don't need to do this in C - it supports that C++ rule 100%. Definitely please don't add extra scope blocks just for your `for` control vars when `for` itself already allows tighter scoping. – Alex Celeste Jan 26 '18 at 09:31
  • Btw I wouldn't just blindly buy everything PRQA is saying - including the linked standard. It is a private company and their primary interest is to sell tools. MISRA-C++ or CERT C++ are much more authoritative coding standards. Though as it happens, PRQA are represented in the MISRA committees. – Lundin Jan 26 '18 at 09:38
  • @Lundin Don't know if Valentin followed "blindly" - me, however, consider this a good rule, followed it already almost 20 years ago when learning Java (not obeying the professor, who encouraged differently, BTW...). – Aconcagua Jan 26 '18 at 09:46
  • [Coding Standards for pure C (not C++)](https://stackoverflow.com/q/1262459/995714), [programming style guide for C](https://stackoverflow.com/q/3495255/995714) – phuclv Jan 26 '18 at 09:50
  • @LưuVĩnhPhúc The coding standards recommended in that thread are trash. The Linux kernel coding standard is an amateur-level document written by hobbyists. They aren't even close to playing in the same league as MISRA-C. – Lundin Jan 26 '18 at 09:52
  • @usr2564301 it works. I want to know it it a common practice. One motivation to change it - the code checker report some of too early defined vars. But suggests to move them only to existing scopes. However, there are many index-variables, which would be best candidates to be ihmo moved from the beginning in such anonymous scopes. – Valentin H Jan 26 '18 at 11:16
  • @Lundin This rule comes from Herb Sutter or Scott Meyer, I just found the first link looking for the text "postpone variable declaration" – Valentin H Jan 26 '18 at 11:19

3 Answers3

10

Your pause from C must have been some 20 years, because C has allowed variable declarations anywhere for ages. Simply use a modern, standard-compliant C compiler, then make sure your C knowledge is up to date, and then all these problems will go away. For example the gcc compiler, version 5.x or later.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 1
    "_20+ years_" would be more meaningful. – machine_1 Jan 26 '18 at 11:14
  • I agree, I have not known, that in C99 it is allowed. Looks like none of my customers notices it as well. The code in C I ever got to deal with, is 20+ years old. As I tried `for( inti...` I got: use option -std=c99 or -std=gnu99 to compile your code :-( It is industrial code initially written for an 8bit controller and gradually ported to Atom CPU. I won't persuade the old developers here to use C99. No chance. But, good to know, C supports it and indeed a usable language, provided people use a proper version. – Valentin H Jan 26 '18 at 14:21
4

Probably, you took a ruther long pause, or have been taught C by someone who didn't keep himself updated.

It's perfectly fine to declare a variable anywhere you like, since .

Minimal example:

int main(void) {
    int a = 1;
    for(int i = 0; i < 2; ++i)
        a = i;
    int b = a;
    return 0;
}
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 2
    Just a side note: even with pre-99 C one did not have to declare all variables at beginning of a *function*, just at the beginning of a new *block* was sufficient (e. g. within some `if` block). Sure, still far more limited than compared to C99... – Aconcagua Jan 26 '18 at 09:40
  • @Aconcagua I know, that's why I try to introduce anonymous scopes to reduce the var definition list at the beginning. But the 60+ years old developers here don't know it. – Valentin H Jan 26 '18 at 14:23
  • If I could decide, I'd rewritten all code in C++x11. But the people here seem to love C89. – Valentin H Jan 26 '18 at 14:25
  • OK, if forced to sticking to C89 consider your own third example - `int i` is not at the beginning of a block (there is an entire block before), thus invalid! – Aconcagua Jan 26 '18 at 14:30
1

OK, as you cannot switch to a modern compiler, as you denoted in the now edited question and some comments, here my thoughts about:

  1. Do not introduce artificial scopes. That won't make your (and other's) live(s) easier. I rather recommend just to stay with what pre-99 C imposes on you, as the additional braces and additional indentation level (if formatted correctly) over-weigh as disadvantage the advantages of more local scopes.

  2. Where ever you already have a block scope, move as many variables into it as possible.

  3. If you do not have to modify existing code for other reasons, do not touch it at all.

  4. If you have large functions and you fear losing overview, consider breaking them into smaller pieces. If those pieces are to be called from the original function only, make them inline. (This last point is a general recommendation even with C99 or C11...).

Aconcagua
  • 24,880
  • 4
  • 34
  • 59