Skip to main content
Question Protected by gnat
replaced http://programmers.stackexchange.com/ with https://softwareengineering.stackexchange.com/
Source Link

This question is inspired by a question about final in javaa question about final in java.

In C/C++, should I use const whenever possible?

I know there is already a related question about using const in parametersabout using const in parameters. Unfortunately that question and it's answers don't fully answer my question, because it's only about function parameters, but I would also like to know about other cases (eg: local variables).

Also, almost all answers to that question say we should use const because it contains useful information about the accessibility of variables. But this seems to conflict with an answer about using final in Javaan answer about using final in Java which states final may be superfluous if it doesn't contain extra information and so it should be omitted to keep the code short and clean.

So, should I use const whenever possible? If so, why is the advice for const in C++ different from the advice for final in Java?

This question is inspired by a question about final in java.

In C/C++, should I use const whenever possible?

I know there is already a related question about using const in parameters. Unfortunately that question and it's answers don't fully answer my question, because it's only about function parameters, but I would also like to know about other cases (eg: local variables).

Also, almost all answers to that question say we should use const because it contains useful information about the accessibility of variables. But this seems to conflict with an answer about using final in Java which states final may be superfluous if it doesn't contain extra information and so it should be omitted to keep the code short and clean.

So, should I use const whenever possible? If so, why is the advice for const in C++ different from the advice for final in Java?

This question is inspired by a question about final in java.

In C/C++, should I use const whenever possible?

I know there is already a related question about using const in parameters. Unfortunately that question and it's answers don't fully answer my question, because it's only about function parameters, but I would also like to know about other cases (eg: local variables).

Also, almost all answers to that question say we should use const because it contains useful information about the accessibility of variables. But this seems to conflict with an answer about using final in Java which states final may be superfluous if it doesn't contain extra information and so it should be omitted to keep the code short and clean.

So, should I use const whenever possible? If so, why is the advice for const in C++ different from the advice for final in Java?

Tweeted twitter.com/StackSoftEng/status/798048744088293377
Translated to English, tried to keep the meaning identical.
Source Link

In C/C++, should I use 'const' in parameters and local variables when it can bepossible?

This question for me is inspired by this question:

http://programmers.stackexchange.com/questions/48413/in-a question about final in java-should-i-use-final-for-parameters-and-locals-even-when-i-dont-have-t.

But in cIn C/c++ version with const:C++, should I use const when it can beconst whenever possible?

I know there is already a similarrelated question about using const in parameters:

http://programmers.stackexchange.com/questions/331678/is-readability-a-valid-reason-to-not-use-const-in-parametersabout using const in parameters

But this. Unfortunately that question cannotand it's answers don't fully answer my question.

First, it isbecause it's only about function parameters only, but I would also like to know the casesabout other than parameterscases (eg: local variables).

SecondAlso, almost all answer above statedanswers to that question say we should use constconst because it contains useful information about the accessibility of variables and hence should be used when it can be, but I found an answer about using final in Java:

. But this seems to conflict with http://programmers.stackexchange.com/a/48414/248528an answer about using final in Java

It seems stated 'final' which states final may be superfluous at some cases whichif it doesn't contain extra information and so it should be omitted to keep methods and codesthe code short and clean, which contradicts with the answer that should use const when usable.

So, should I use const when it can beconst whenever possible? If so, why is it contradicts with the case that no need to always use final even when usableadvice for const in C++ different from the advice for final in Java?

In C/C++, should I use 'const' in parameters and local variables when it can be?

This question for me is inspired by this question:

http://programmers.stackexchange.com/questions/48413/in-java-should-i-use-final-for-parameters-and-locals-even-when-i-dont-have-t

But in c/c++ version with const: should I use const when it can be?

I know there is already a similar question about using const in parameters:

http://programmers.stackexchange.com/questions/331678/is-readability-a-valid-reason-to-not-use-const-in-parameters

But this question cannot fully answer my question.

First, it is about parameters only, but I would also like to know the cases other than parameters(eg: local variables)

Second, almost all answer above stated we should use const because it contains useful information about the accessibility of variables and hence should be used when it can be, but I found an answer about using final in Java:

http://programmers.stackexchange.com/a/48414/248528

It seems stated 'final' may be superfluous at some cases which doesn't contain extra information and should keep methods and codes short and clean, which contradicts with the answer that should use const when usable.

So, should I use const when it can be? If so, is it contradicts with the case that no need to always use final even when usable in Java?

In C/C++, should I use 'const' in parameters and local variables when possible?

This question is inspired by a question about final in java.

In C/C++, should I use const whenever possible?

I know there is already a related question about using const in parameters. Unfortunately that question and it's answers don't fully answer my question, because it's only about function parameters, but I would also like to know about other cases (eg: local variables).

Also, almost all answers to that question say we should use const because it contains useful information about the accessibility of variables. But this seems to conflict with an answer about using final in Java which states final may be superfluous if it doesn't contain extra information and so it should be omitted to keep the code short and clean.

So, should I use const whenever possible? If so, why is the advice for const in C++ different from the advice for final in Java?

added 21 characters in body
Source Link
ocomfd
  • 5.8k
  • 8
  • 33
  • 40

This question for me is inspired by this question:

http://programmers.stackexchange.com/questions/48413/in-java-should-i-use-final-for-parameters-and-locals-even-when-i-dont-have-t

But in c/c++ version with const: should I use const when it can be?

I know there is already a similar question about using const in parameters:

http://programmers.stackexchange.com/questions/331678/is-readability-a-valid-reason-to-not-use-const-in-parameters

But this question cannot fully answer my question.

First, it is about parameters only, but I would also like to know the cases other than parameters(eg: local variables)

Second, almost all answer above stated we should use const because it contains useful information about the accessibility of variables and hence should be used when it can be, but I found an answer about using final in Java:

http://programmers.stackexchange.com/a/48414/248528

It seems stated 'final' may be superfluous at some cases which doesn't contain extra information and should keep methods and codes short and clean, which contradicts with the answer that should use const when usable.

So, should I use const when it can be? If so, is it contradicts with the case that usingno need to always use final even when usable in Java?

This question for me is inspired by this question:

http://programmers.stackexchange.com/questions/48413/in-java-should-i-use-final-for-parameters-and-locals-even-when-i-dont-have-t

But in c/c++ version with const: should I use const when it can be?

I know there is already a similar question about using const in parameters:

http://programmers.stackexchange.com/questions/331678/is-readability-a-valid-reason-to-not-use-const-in-parameters

But this question cannot fully answer my question.

First, it is about parameters only, but I would also like to know the cases other than parameters(eg: local variables)

Second, almost all answer above stated we should use const because it contains useful information about the accessibility of variables and hence should be used when it can be, but I found an answer about using final in Java:

http://programmers.stackexchange.com/a/48414/248528

It seems stated 'final' may be superfluous at some cases which doesn't contain extra information and should keep methods and codes short and clean, which contradicts with the answer that should use const when usable.

So, should I use const when it can be? If so, is it contradicts with the case that using final when usable in Java?

This question for me is inspired by this question:

http://programmers.stackexchange.com/questions/48413/in-java-should-i-use-final-for-parameters-and-locals-even-when-i-dont-have-t

But in c/c++ version with const: should I use const when it can be?

I know there is already a similar question about using const in parameters:

http://programmers.stackexchange.com/questions/331678/is-readability-a-valid-reason-to-not-use-const-in-parameters

But this question cannot fully answer my question.

First, it is about parameters only, but I would also like to know the cases other than parameters(eg: local variables)

Second, almost all answer above stated we should use const because it contains useful information about the accessibility of variables and hence should be used when it can be, but I found an answer about using final in Java:

http://programmers.stackexchange.com/a/48414/248528

It seems stated 'final' may be superfluous at some cases which doesn't contain extra information and should keep methods and codes short and clean, which contradicts with the answer that should use const when usable.

So, should I use const when it can be? If so, is it contradicts with the case that no need to always use final even when usable in Java?

Source Link
ocomfd
  • 5.8k
  • 8
  • 33
  • 40
Loading