zig.guide icon indicating copy to clipboard operation
zig.guide copied to clipboard

Add reasoning for claim in chapter 1 (const preferred over var)

Open Falconerd opened this issue 3 years ago • 2 comments

I agree that const should be used rather than var where possible for the simple fact that one can not mistakenly write over data that should stay constant. However, this is just asserted with no reasoning. Perhaps there's another technical reason? I'm not sure.

Falconerd avatar Feb 05 '22 08:02 Falconerd

There is no good reasoning yet, because the final decision on what to make const and var is still outstanding/discussed without clear guideline/semantic decision being made.

matu3ba avatar Apr 04 '22 08:04 matu3ba

Const is preferred as a best practice these days in favor of immutability. If a declaration is immutable the compiler can typically make optimizations, and additionally you don’t have to be as concerned with data races in multi-threaded scenarios. Immutable data is a good thing so declaring as much data as possible as immutable leads to less bugs overall.

deckarep avatar Apr 12 '22 04:04 deckarep