Questions tagged [const]

37 questions
104
votes
3 answers

When and for what purposes should the const keyword be used in C for variables?

While getting my code reviewed here the issue of using the const keyword came up. I understand that it is used for implementing read-only behaviour on variables. I am confused about what are the various situations when it can be useful. Should it…
Aseem Bansal
  • 2,954
  • 6
  • 20
  • 33
81
votes
9 answers

How can I make a call with a boolean clearer? Boolean Trap

As noted by in the comments by @benjamin-gruenbaum this is called the Boolean trap: Say I have a function like this UpdateRow(var item, bool externalCall); and in my controller, that value for externalCall will always be TRUE. What is the best way…
Mario Garcia
  • 813
  • 1
  • 6
  • 9
38
votes
8 answers

Difference between immutable and const

I've often seen the terms immutable and const used interchangeably. However, from my (little) experience, the two differ a lot in the 'contract' they make in code: Immutable makes the contract that this object will not change, whatsoever (e.g.…
K.Steff
  • 4,475
  • 2
  • 31
  • 28
28
votes
7 answers

May a value of a constant be changed over time?

During development phase, there are certain variables which need to be the fixed in the same run, but may need be modified over time. For example a boolean to signal debug mode, so we do things in the program we normally wouldn't. Is it bad style to…
GregT
  • 407
  • 1
  • 4
  • 7
23
votes
6 answers

Is readability a valid reason to not use const in (reference) parameters?

When writing some functions, I found a const keyword in parameters like this: void MyClass::myFunction(const MyObject& obj,const string& s1,const string& s2,const string& s3){ } often causes splitting a line into 2 lines in IDE or vim, so I want to…
ggrr
  • 5,725
  • 11
  • 35
  • 37
22
votes
5 answers

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…
ocomfd
  • 5,652
  • 8
  • 29
  • 37
20
votes
4 answers

What are the problems of bringing C++-like const into a language?

I am interested in the idea of C++-like const not that particular execution (like casting away const). Take for example C# -- it lacks C++-like const, and the reason for it is the the usual -- people and time. Here additionally it seems the C# team…
greenoldman
  • 1,506
  • 1
  • 14
  • 27
17
votes
7 answers

Zero as a constant?

I have come across this programming idiom recently: const float Zero = 0.0; which is then used in comparisons: if (x > Zero) {..} Can anyone explain if this is really any more efficient or readable or maintainable than: if (x > 0.0) {..} NOTE:…
NWS
  • 1,319
  • 8
  • 17
15
votes
2 answers

Const C++ DRY Strategies

For avoiding non-trivial C++ const related duplication, are there cases where const_cast would work but a private const function returning non-const wouldn't? In Scott Meyers' Effective C++ item 3, he suggests that a const_cast combined with a…
JDiMatteo
  • 375
  • 2
  • 14
13
votes
3 answers

Do people use const a lot when programming in Objective C?

Related: “sending 'const NSString *' to parameter of type 'NSString *' discards qualifiers” warning Sometimes, I think it's useful though. I may need to pass an a table and want to make sure that the content of the table does not change. However, in…
user4951
  • 699
  • 6
  • 14
11
votes
4 answers

In C# Is using private constant strings better than using strings directly in the code?

Consider the following two cases: case 1: class A { private const string MyConst="Hello"; internal void CallMe() { System.Console.WriteLine(MyConst); } } Case2: class A { internal void CallMe() { …
GawdePrasad
  • 473
  • 2
  • 4
  • 11
10
votes
5 answers

Should I be using const more in C++?

There are some things I am pretty strict about, but const has not been one of them. For example, I might make a local variable that I use three times in a function, that does not get changed, and yet I do not bother making it const. Is this…
Anon
  • 3,565
  • 3
  • 27
  • 45
8
votes
4 answers

SQL Query and Java Constant Abuse?

I am currently charged with taking over a lot of code that is written with fields names placed into Java constant at the top of the file, and then the SQL queries constructed using string concatenation with the field names. At the top of the file…
AgilePro
  • 312
  • 2
  • 7
8
votes
3 answers

Passing parameters that need to be copied by value or const reference

I have a basic (mathematical) vector class, which in my opinion benefits from C++'s operator overloading. Vector-scalar operations are defined as self-modifying functions in the class itself, class Vec3i { Vec3i& operator+=(int const n) { for…
Kolja
  • 233
  • 2
  • 4
6
votes
4 answers

Does internal state "leak" when it influences externally-visible behavior?

I have a method (in C++) which generates a value based on a parameter and the parameters from previous calls. Calling it more than once with the same parameter may generate different values each time. For example: public: int getValue(const int…
sourcenouveau
  • 6,460
  • 4
  • 29
  • 44
1
2 3