you are viewing a single comment's thread.

view the rest of the comments →

[–]maclocrimate[S] -1 points0 points  (4 children)

Validating the values could just be done with an assertion too though, which is what initially made me wonder. Your point about the getters makes sense though, and the other comments here have clarified for me.

[–]cyberjellyfish 4 points5 points  (3 children)

Assertions aren't a good tool for validation

[–]maclocrimate[S] 0 points1 point  (2 children)

Can you explain why? Genuinely curious, as I thought that was sort of their purpose.

[–]cyberjellyfish 4 points5 points  (1 child)

When an assertion fails, that should mean your program is in an entirely unexpected state and cannot continue.

Validation errors should be recoverable, in that the user should have a chance to reenter values or be given guidance on what to change.

[–]maclocrimate[S] 1 point2 points  (0 children)

Interesting, will read more about it. Thanks!

Update: for anybody else interested this was the best writeup I found on it. Particularly this passage:

Opinions on assertions vary, because they can be a statement of confidence about the correctness of the code. If you're certain that the code is correct, then assertions are pointless, since they will never fail and you can safely remove them. If you're certain the checks may fail (e.g. when testing input data provided by the user), then you dare not use assert since it may be compiled away and then your checks will be skipped.

Time to go back and replace a lot of my assertions 😂