4 ms·
It doesn't need to be about OOP and hiding details, this seems like basic type safety. Both of these are member functions and are hiding details already, so OOP
by sqeaky 4y ago
It doesn't need to be about OOP and hiding details, this seems like basic type safety. Both of these are member functions and are hiding details already, so OOP is an aside, you would need invest effort reading on both or neither if there are robust unit test that solid OOP would enable.
With ints as months is 0 or 1 January? What about -1 and 13, do they have sensible meaning? You need to re-apply that effort constantly to any int that might be a month. This isn't a hidden implementation this is an easy mistake waiting to happen. Using a month enum solves this, and you are not fixing bugs in the implementation of an enum unless you are a compiler developer.
Same with const, it prevents accidental changes to the class from this member function. It allows more sensible code and compiler optimizations. The "do" line will almost always be faster than the "don't" line. There is no way adding const changes the amount of effort for a reader in a negative way.
- Jtsummers 4y ago> this seems like basic type safety. Please stop sharing my secrets. I've done pretty well fixing up things with basic type safety because my predecessors couldn't be bothered to use their brains. I like being employed.
- account42 4y ago> It allows more sensible code and compiler optimizations. Unfortunately no, const only tells the compiler optimizer something when applied to variables but not for reference or pointer types (or member functions, where it just means apply const to the this pointer). It would be nice if there were a stronger co_const that would tell the compiler could assume is never cast away (or at least that the variable is not modified through that pointer/reference even if the const is cast away) but C++ const is not that.
- kllrnohj 4y agoThe const in this example is on the member function so nothing external can cast it away, but the value of it there is really more about contract/behavioral enforcement than optimizations yes.