Location of const function in function

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

I find it difficult to decide where consts should be located in the function. I know that many people put them at the top, but if you put them as close as possible to where they are used, you will reduce the range of the code. 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 tend to use const at the top in small functions and keep the range as close as possible to large functions, but I was wondering what these styles / thoughts of others are.

+3
source share
7 answers

In the lowest possible volume and immediately before their first use.

/, , .

+8

, ( "" ) , . , const , , , , .

+3

.

+1

.

, , "" "" .

0

, . , .

, , , (, ).

0

, , ( const), , , .

0

C C99 . , C-. , Code Complete , , C ++.

, C ++ , .

, , , , . , . , ++ . , . ​​ , . + , .

, , . const? const int n = 3; , const, const char * s = "FOO";? , ? const char * const s = "FOO";? , , , , , const , , .

0

Source: https://habr.com/ru/post/1724676/


All Articles