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.
Regards
Petr
On (28/08/15 09:03), 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;...)|
^ There is a synteax error; variable inti is not definded :-) s/inti/int i/
otherwise +1
and there are also precedents in sssd code. src/lib/sifp/sss_sifp_parser.c:434: for (unsigned int i = 0; src/providers/ipa/ipa_init.c:103: for (int i = 0; list[i]; i++) { src/tests/ipa_ldap_opt-tests.c:267: for (int i=0; i < SDAP_OPTS_BASIC; i++) { src/tools/tools_mc_util.c:173: for (size_t i = 0; i < steps_count; ++i) { src/util/domain_info_utils.c:74: for (int i=0; parent->sd_enumerate[i]; i++) {
LS
On 08/28/2015 09:18 AM, Lukas Slebodnik wrote:
On (28/08/15 09:03), 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;...)|
^ There is a synteax error; variable inti is not definded :-) s/inti/int i/
Sorry for typo. My mail client plays game with me.
otherwise +1
and there are also precedents in sssd code. src/lib/sifp/sss_sifp_parser.c:434: for (unsigned int i = 0; src/providers/ipa/ipa_init.c:103: for (int i = 0; list[i]; i++) { src/tests/ipa_ldap_opt-tests.c:267: for (int i=0; i < SDAP_OPTS_BASIC; i++) { src/tools/tools_mc_util.c:173: for (size_t i = 0; i < steps_count; ++i) { src/util/domain_info_utils.c:74: for (int i=0; parent->sd_enumerate[i]; i++) {
LS _______________________________________________ sssd-devel mailing list sssd-devel@lists.fedorahosted.org https://lists.fedorahosted.org/mailman/listinfo/sssd-devel
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.
According to C standard, declaration expression in for() should only be used to declare identifiers for objects having storage class auto or register and "the scope of any identifiers it declares is the remainder of the declaration and the entire loop, including the other two expressions" (6.8.5.3 of C11 standard, C99 had similar sentence).
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.
LS
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.
Reducing scope is fine if you understand the context but claiming 'misuse' is a bit too much here.
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.
On Mon, Aug 31, 2015 at 02:09:19PM +0300, 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.
Reducing scope is fine if you understand the context but claiming 'misuse' is a bit too much here.
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.
Then I would suggest not to use the generic "i" name, but a more descriptive one for the variable.
On a related note -- Michal, any progress on updating the code guidelines?
On 08/31/2015 01:20 PM, Jakub Hrozek wrote:
On Mon, Aug 31, 2015 at 02:09:19PM +0300, 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.
Reducing scope is fine if you understand the context but claiming 'misuse' is a bit too much here.
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.
Then I would suggest not to use the generic "i" name, but a more descriptive one for the variable.
On a related note -- Michal, any progress on updating the code guidelines?
Yes, but I would prefer to update the coding guidelines after Petr finish the wiki refactoring, so that I will not be adding stuff under his nose.
Michal
On Mon, Aug 31, 2015 at 01:24:17PM +0200, Michal Židek wrote:
On 08/31/2015 01:20 PM, Jakub Hrozek wrote:
On Mon, Aug 31, 2015 at 02:09:19PM +0300, 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.
Reducing scope is fine if you understand the context but claiming 'misuse' is a bit too much here.
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.
Then I would suggest not to use the generic "i" name, but a more descriptive one for the variable.
On a related note -- Michal, any progress on updating the code guidelines?
Yes, but I would prefer to update the coding guidelines after Petr finish the wiki refactoring, so that I will not be adding stuff under his nose.
Michal
If you have time, can you help with reviewing Petr's wiki changes, then?
On 09/01/2015 10:58 AM, Jakub Hrozek wrote:
On Mon, Aug 31, 2015 at 01:24:17PM +0200, Michal Židek wrote:
On 08/31/2015 01:20 PM, Jakub Hrozek wrote:
On Mon, Aug 31, 2015 at 02:09:19PM +0300, 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.
Reducing scope is fine if you understand the context but claiming 'misuse' is a bit too much here.
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.
Then I would suggest not to use the generic "i" name, but a more descriptive one for the variable.
On a related note -- Michal, any progress on updating the code guidelines?
Yes, but I would prefer to update the coding guidelines after Petr finish the wiki refactoring, so that I will not be adding stuff under his nose.
Michal
If you have time, can you help with reviewing Petr's wiki changes, then?
Ok, will do.
Michal
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
On Fri, 2015-08-28 at 09:03 +0200, 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.
I think it is ok to accept this style when there is *only one* variable involved and the iterator variable is useful exclusively within the for loop. I used this style elsewhere and it was ok.
Much care need to be taken when *converting* code to this style, because in many case the index is used after the for loop to check for failure/success when the loop breaks in the middle.
Simo.
P.S: please do not use HTML emails, see how butchered your email comes out in the txt version.
On (30/08/15 15:31), Simo Sorce wrote:
On Fri, 2015-08-28 at 09:03 +0200, 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.
I think it is ok to accept this style when there is *only one* variable involved and the iterator variable is useful exclusively within the for loop. I used this style elsewhere and it was ok.
Much care need to be taken when *converting* code to this style, because in many case the index is used after the for loop to check for failure/success when the loop breaks in the middle.
We should avoid mass converting to this style. The new approch should be only used as part of rewriting function or in new functions.
BTW Should it be recommended or just allowed to use?
LS
sssd-devel@lists.fedorahosted.org