Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How often do you declare your functions to be const?

Do you find it helpful?

like image 609
amitlicht Avatar asked Mar 10 '10 07:03

amitlicht


People also ask

When should functions be const?

A function becomes const when the const keyword is used in the function's declaration. The idea of const functions is not to allow them to modify the object on which they are called. It is recommended the practice to make as many functions const as possible so that accidental changes to objects are avoided.

How do you declare a constant function?

A constant member function can't modify any non-static data members or call any member functions that aren't constant. To declare a constant member function, place the const keyword after the closing parenthesis of the argument list. The const keyword is required in both the declaration and the definition.

What does it mean for a function to be const?

Main Concept. A constant function is a function whose range consists of a single element. That is, the output value of the function at any input value in its domain is the same, independent of the input.

Should functions be let or const?

As a general rule, you should always declare variables with const, if you realize that the value of the variable needs to change, go back and change it to let. Use let when you know that the value of a variable will change. Use const for every other variable.


1 Answers

Every time You know that method won't change state of the object you should declare it to be constant.

It helps reading your code. And it helps when you try to change state of the object - compiler will stop you.

like image 168
Mykola Golubyev Avatar answered Oct 22 '22 09:10

Mykola Golubyev