[SSSD] Code style -- for loop iterative variables initial declaration

Petr Cech pcech at redhat.com
Mon Aug 31 11:22:15 UTC 2015



On 08/31/2015 01:09 PM, Alexander Bokovoy wrote:
> On Mon, 31 Aug 2015, Lukas Slebodnik wrote:
>> On (29/08/15 14:33), Alexander Bokovoy wrote:
>>> On Fri, 28 Aug 2015, Petr Cech wrote:
>>>> Hi everyone,
>>>>
>>>> I would like to ask you what you think about the initialization of
>>>> iterative variables in forloops. I know that present code style does
>>>> not
>>>> allow it. But how I recognized, we use C99, and this feature is here
>>>> now.
>>>>
>>>> (example)
>>>> Instead of:|
>>>> |||# inti;
>>>> # for(i =0;...)|||
>>>> we could write:
>>>> ||# for(inti =0;...)|
>>>>
>>>> I see an advantage in limiting the validity of such variables. That
>>>> means
>>>> higher code readability. Disadvantages I searched but did not find.
>>> What this misses is a use case of indexed searches where resulting index
>>> value is used beyond the loop itself. By changing context of variable
>>> declaration, you make variable inaccessible outside of the loop.
>>>
>> I would say it's exactly the purpose of this proposal.
>> To decrease scope of visibility so the index variable with short name
>> cannot be misused for different purpose.
> Huh? There are valid cases where you search for an element and then use
> it further in the code. The index is what you get as the result of the
> search, not a reference to the element. Sometimes you need an element's
> reference but in many cases you need an index.
>
Yes, I agree. There are different situations.

> Reducing scope is fine if you understand the context but claiming
> 'misuse' is a bit too much here.
>
+1
> I'd suggest adding this syntax recommendation to SSSD C coding style
> guidelines but add as well a bit of explanation on these two types of
> loop usage patterns.
+1

Petr


More information about the sssd-devel mailing list