开发者

Location of const in a function

A similar question was previously asked, but none of the answers really provided what I was looking for.

I am having trouble deciding where consts should be located in a function. I know a lot of people put them at the top, but if you put them as close as possible to where they are used, you'll reduce code span. I.e.

void f() {
  const FOO = 3;
  ...// some code
  if ( bar > FOO ) {
    ...// do stuff
  }
}

or

void f() {
  ...// some code
  const FOO = 3;
  if ( bar > FOO ) {
    ...// do stuff
  }
}

I'm leaning towards using the const at the top in small functions, and keeping the span as close as possibl开发者_高级运维e in large functions, but I was wondering what others' styles/thoughts are regarding this.


At the lowest scope possible, and directly before their first use.

As a matter of style, exceptions can be made for clarity/asthetics, e.g., grouping conceptually similar constants.


Many times, const values are placed at the top of the file so that they are easily recognizable (and "findable") to individuals doing development. However, if you only need a const value for a very small piece of code, it would be better for it to be scoped to only where it is needed as you suggested.


I recommend putting them in the header file under a namespace or class.


Your approach sounds about right.

I even put these magic numbers at the top of a file sometime, to make sure any "settings" or tweakables are highly visible to others.


It depends on what you really want to do. I usually put them very close where they are actually used.

I put them on top when they are grouped and in order to make sense of one you have to look at the others (for instance when a constant depends on another constant).


Especially if you are going to code some longer algorithm having all start values (including const values) and variables declared at the top of the function makes for a lot more clarity when reading the algorithm itself.


In pre-C99 versions of C, you could only define variables at the beginning of blocks. Consequently, the second alternative was not valid C code. I believe that Code Complete favors putting the declaration as close as possible to the first use, but some would have argued against that rule on the grounds making it makes things inconsistent between C and C++.

Now that both standard C and C++ allow you to move the declaration close to the first usage, that objection no longer holds.

There are times when there are compelling reasons why putting the declaration as late as possible is better for non-const variables than at the top. For example, a declaration without an initialization opens up the possibility of accidentally reading an uninitialized variable. Furthermore, in C++, declaring a class variable at the top of the function with no initialization invokes the default constructor. When it's later assigned, it invokes the assignment operator. If the variable were instead declared at the point of initialization, that invokes the copy constructor. The cost of a default constructor + assignment can often be larger than the cost of the copy constructor.

This last argument can only apply to non-const variables, obviously, since there is no assignment on a const variable. But, why would you want to have to look in a different place for your const declarations? And if const int n=3; is obviously const, what about const char *s = "FOO";? Is that const enough to belong at top or not? Or does it have to be const char * const s = "FOO";? Also, what if you don't yet know what value you want your const variable initialized to at the top, then you must postpone declaring your const variable until you know what it needs to be initialized to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜