[FFmpeg-devel] [PATCH] doc/developer: update style guidelines to include for loops with declarations

Rostislav Pehlivanov atomnuker at gmail.com
Wed Nov 8 23:57:48 EET 2017


On 8 November 2017 at 21:45, Carl Eugen Hoyos <ceffmpeg at gmail.com> wrote:

> 2017-11-08 22:26 GMT+01:00 Rostislav Pehlivanov <atomnuker at gmail.com>:
>
> > + at item
> > +for loops with variable definition (@samp{for (int i = 0; i < 8; i++)});
>
> Don't you think that this makes the code slightly uglier?
>
> Carl Eugen
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>



Definitely not, and here are some reasons why, in order of importance:

1.) Gives a very very clear indication of whether a variable is used after
a for loop. Any loop without its variable defined means that variable is
used for something else so everyone would pay more attention.

2.) It saves a line in case the loop variable isn't used anywhere
2a.) This adds up and if all loops in our codebase were converted many 10s
of thousands of lines would be saved (I'm serious, at least this many
lines).

3.) Simplifies experimenting since you don't have to scroll up to add a
variable and then scroll down to use it in a loop.

4.) Most code editing (and general purpose text editing) software
highlights types, so on a glance there would be no doubt about which
bracketed segment is a loop and which is a condition.

5.) Compilers can better know whether to unroll loops in case the variable
isn't actually used in a small number of cases.

6.) Simple compilers would make less allocation/stack mistakes if a
variable is declared in the first loop in a two loop segment.


More information about the ffmpeg-devel mailing list