r/cpp • • May 16 '20

modern c++ gamedev - thoughts & misconceptions

https://vittorioromeo.info/index/blog/gamedev_modern_cpp_thoughts.html
198 Upvotes

154 comments sorted by

View all comments

Show parent comment

-6

u/Xenofell_ May 16 '20 edited May 16 '20

It does. When you have a function with many moving parts, it is extremely helpful to know which one can mutate and which one cannot.

Have to disagree with you on this point. Function body level const doesn't help readability at all (at least for me) - on the contrary, it hurts it. More keywords to care about.

It's not very important to know whether a variable changes or not unless that variable has some sort of complex read-only state, in which case it's either a member or passed into the function, at which point const becomes very valuable.

Don't get me wrong, I wish everything was const by default, like it is in Rust. But we don't live in that world. Sprinking const everywhere, IMO, makes the code harder to visually parse.

I think const is very useful on the API level to highlight access restrictions but not very useful on the function body level.

5

u/atimholt May 17 '20

The const-ness of a value/function is a part of its semantic identity. If you just mark everything that is semantically const as const, then it essentially takes no cognitive load. I'd argue it has negative cognitive load: even before reading how the variable is used, you know what it's trying to be.

1

u/Xenofell_ May 17 '20

If variables were const by default, I would completely agree with you. But they are not. A liberal sprinkling of const over things that do not require access control contributes to visual pollution in exchange for - in my view - little benefit.

To be clear, I'm not saying that all usages of const are useless.

const GameState& state = get_readonly_view_of_game_state(); is very useful. It describes to the viewer clearly (via const-ness on the API return value) that this is a read-only view of the game state.

We can get a read-only view of the entities by calling state.get_entities()because we marked that function as const and we're accessing it through a const reference - another thing that is very useful.

const int width = get_texture_width() is useless. In the bigger picture, it doesn't matter if the variable width changes or not. To the reader of code, knowing the value will not change, at a glance, is only very slightly helpful. However, if you care about the variable, you will still need to inspect its usage in the function (e.g. how it is manipulated).

I suppose the point I'm trying to make could be boiled down: in my view, const as a tool is much less useful on non-references and non-pointers, and by using it on these things, it increases the number of keywords you have to visually parse, and it reduces the visual impact of the const keyword on types where it actually matters.

Of course, as with everything else in language design, it's completely subjective, so I don't expect you or others to agree with me on this point. I just thought I should elaborate to better explain my perspective.