[Github-comments] [geany/geany] Update HACKING for best practices (C99) (#1282)
Colomban Wendling
notifications at xxxxx
Sat Oct 29 12:23:06 UTC 2016
b4n commented on this pull request.
> @@ -200,8 +200,18 @@ Coding
moment, we want to keep the minimum requirement for GTK at 2.24 (of
course, you can use the GTK_CHECK_VERSION macro to protect code using
later versions).
-* Variables should be declared before statements. You can use
- gcc's -Wdeclaration-after-statement to warn about this.
+* Variables should be declared (and initialized) as close as practical
+ to their first use. This reduces the chances of intervening code being
+ inserted between declaration and use, where the variable may be
+ uninitialized.
+* Variables should be defined within the smallest scope that is practical,
+ for example inside a conditional branch which uses them or in the
+ initialization part of a for loop.
+* Local variables that will not be modified should be marked as ``const``
+ to indicate intention. This allows the compiler to give a warning if
+ part of the code accidentally tries to change the value. This does not
+ apply to non-pointer parameters where it needlessly exposes the
+ implementation and it's obvious a copy is made anyway.
It doesn't have to expose it, adding the `const` qualifier to an argument so that the argument itself is not modifiable still is compatible with a prototype not specifying it. ISO/IEC 9899:201x n1570 6.7.6.3§15 states that
> […] the parameter type lists, if both are present, shall agree in the number of parameters and in use of the ellipsis terminator; **corresponding parameters shall have compatible types**. […\] (In the determination of type compatibility and of a composite type, each parameter declared with function or array type is taken as having the adjusted type and **each parameter declared with qualified type is taken as having the unqualified version of its declared type**.)
So it's perfectly valid to have
```C
int foo(int);
/* ... */
int foo(const int a) {
return a;
}
```
And qualifying the argument "variable" `const` is just as useful as for a local variable, or maybe even more as the value is likely not to be recoverable another way. We probably have very few of those in Geany's code, but there's a fair share in CTags and IMO it's a nice use of `const` just like on variables.
--
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/geany/geany/pull/1282#pullrequestreview-6346663
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.geany.org/pipermail/github-comments/attachments/20161029/350d135c/attachment.html>
More information about the Github-comments
mailing list