vim's "C" for loop coding style

20 views
Skip to first unread message

Ernie Rael

unread,
Mar 19, 2023, 11:19:56 AM3/19/23
to vim...@googlegroups.com
Is "for (i = 0; i < len; i++, idx++)" OK style in vim?

-ernie

I'm looking at a change to a vim "C" file. It currently looks like

    for (i = 0; i < len; i++) {
        // stuff

        // more stuff

        ++idx;
    }

I need to make a change, the most clear (I think) is

    for (i = 0; i < len; i++, idx++) {
        // stuff

        if (cond)
            continue;

        // more stuff
    }

Bram Moolenaar

unread,
Mar 19, 2023, 1:49:15 PM3/19/23
to vim...@googlegroups.com, Ernie Rael

Ernie Rael wrote:

> Is "for (i = 0; i < len; i++, idx++)" OK style in vim?
>
> -ernie
>
> I'm looking at a change to a vim "C" file. It currently looks like
>
>     for (i = 0; i < len; i++) {
>         // stuff
>
>         // more stuff
>
>         ++idx;
>     }

Except that the "{" should be on the following line.

>
> I need to make a change, the most clear (I think) is
>
>     for (i = 0; i < len; i++, idx++) {
>         // stuff
>
>         if (cond)
>             continue;
>
>         // more stuff
>     }

The "for" statement in C is easily abused to all kinds of things it
wasn't intended for. The three parts inside the parens are:

- Initialization, usually setting a variable to a start value.
- Condition for continuing the loop, should use the variable that was
initialized.
- Advancing, usually incrementing the initialized variable.

Adding an increment in the third part that is not for the loop variable
makes it harder to understand. You have to look carefully to see what
happens. I rather avoid this.

I know some people want to do this anyway, so that "continue" executes
the statement. That avoids something complicated inside the loop, thus
it can be "less worse". In your example it's simple enough to do:

    for (i = 0; i < len; i++)
{
        // stuff

        if (!cond)
{
        // more stuff
}
++idx;
    }

With more conditions it can get complicated and moving "++idx" to the
for loop statement can be simpler.

--
When danger reared its ugly head,
He bravely turned his tail and fled
Yes, Brave Sir Robin turned about
And gallantly he chickened out
Bravely taking to his feet
He beat a very brave retreat
Bravest of the brave Sir Robin
Petrified of being dead
Soiled his pants then brave Sir Robin
Turned away and fled.
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Enan Ajmain

unread,
Mar 20, 2023, 7:34:38 AM3/20/23
to Bram Moolenaar, vim...@googlegroups.com, Ernie Rael
On Sun, 19 Mar 2023 17:49:10 +0000
Bram Moolenaar <Br...@moolenaar.net> wrote:
Did you change Ernie's

if (cond)
continue;
// more stuff

to your

>         if (!cond)
> {
>         // more stuff
> }

for any specific reason? For me, "continue" is the better approach,
because it reduces a block/scope. I prefer

for (i = 0; i < len; i++) {
// stuff
idx++;

if (cond)
continue;

// more stuff
}

I agree with you, Bram, that 'idx' shouldn't be in for block, simply for
readability. Honestly, as soon as I see "for (i", most of the time I
don't even read the rest of the line; easy to miss the 'idx++'.

--
Enan
3nan....@gmail.com
https://www.github.com/3N4N
Reply all
Reply to author
Forward
0 new messages