Skip to main content
added 263 characters in body
Source Link
Christophe
  • 82.3k
  • 11
  • 136
  • 202

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 practicegood practice to mark such functions as const, independently of any coding practice, 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 constconst 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 (if the language allows it).

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, independently of any coding practice, 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.
  • 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 (if the language allows it)

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.
added 263 characters in body
Source Link
Christophe
  • 82.3k
  • 11
  • 136
  • 202

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) as 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, independently of any coding practice, 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.
  • If you want to be sure that an objectthe method doesn’t change any other objects, avoid globals and make best use of encapsulation on, in addition to marking parameters as const (ofif the language allows it)

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) as 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, independently of any coding practice.

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.
  • If you want to be sure that an object doesn’t change any other objects, avoid globals and make best use of encapsulation on addition to marking parameters as const (of the language allows it)

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, independently of any coding practice, 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.
  • 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 (if the language allows it)
Source Link
Christophe
  • 82.3k
  • 11
  • 136
  • 202

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) as 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, independently of any coding practice.

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.
  • If you want to be sure that an object doesn’t change any other objects, avoid globals and make best use of encapsulation on addition to marking parameters as const (of the language allows it)