1

Is it a good idea to mark a function const from clean code perspective if it change other objects' states? I'd like to know what is the experience with this or is it considered a bad practice for any reasons?

3 Answers 3

3

I’m not sure to which Clean Code advice you’re thinking of, but indeed some languages (e.g. C++) allow to make a method (member function) const if it doesn’t change the state of the object itself.

This is useful to determine what method can be called if the object itself is const or passed as a const parameter. So, yes, it is a good practice to mark such functions as const, even if they change state of other objects. (Not doing so would prevent you the safeguard against accidental by using const objects or parameters whenever possible).

By the way:

  • If you want to make sure that other objects passed as parameter are not modified either, C++ provides you the possibility to mark as const the reference parameters as well (doing so whenever possible is a core guideline)
  • If you want to be sure that the method doesn’t change any other objects, avoid globals and make best use of encapsulation, in addition to marking parameters as const.
3

All that a const method is forbidden to do is to assign to a member of *this directly. At the extreme, it can cast this to a non-const pointer and assign through that.

Trying to modify a const object is undefined behaviour. Modifying through a const pointer isn’t, if the object is actually non-const.

Now doing this is an awfully bad style, except when your code accesses a cache which in turn loads some state; the cache variable would be modified according to the language laws, but wouldn’t be semantically modified.

3
  • 2
    I don’t think OP tries to modify a const object, but just wonders if it could be misleading to mark a method const if it modifies other non-const objects (so not *this) Commented Sep 27, 2022 at 6:38
  • Christoph, to see whether it is misleading you first need to know what "const" for a method actually means. Commented Sep 27, 2022 at 11:44
  • In fact, fromnthe popular programming languages I know, only C++ offers the const member functions. There is no equivalent in Java, C#, objective C and swift (except that swift methods on struct are by default const unless declared as mutable). and in c++ its purpose is well defined. Commented Sep 27, 2022 at 18:49
2

(I'm not working with languages that allow this, so take this with a grain of salt from an outsider):

Sounds like a bad idea. Functions with side effects are already a smell (there may be some, such as caching or updating access statistics, which are acceptable). The "const" keyword signals constantness much more explicitly, and a developer would rightfully assume that they can skip over the internals of such a function if they try to understand where some values change.

Since you don't mention your actual use case and motivation, it's hard to find a reason why it would be acceptable in your case.

3
  • The reason was just a code review where i've got the comment I should make my function const (it was a lambda function in c++) and it made me thinking if it's really a good idea. As I couldn't come to a decision and didn't find an answer to this online, I came here to ask this Commented Sep 26, 2022 at 10:51
  • 3
    @66Gramms: In C++, a function that takes arguments as references or pointers to non-const is expected_to modify them. You signal that you won't do that with const on the parameters. (And lambdas are const by default. Marking it mutable when it doesn't modify its own state would be weird.) Commented Sep 26, 2022 at 12:43
  • So the lambda is modifying some objects' that the class the lambda is residing in has a reference to as a class member. Commented Sep 27, 2022 at 12:22

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.