Ironically, you've omitted their value for testing :P. Default arguments also make it super easy to test typical usage, as well as all the weird edge cases.
Thanks for your message! I'm not sure I understand what you mean by "omitted their value for testing", so feel free to elaborate!
Yes exactly! Default arguments make it easy for the function callers to use your function, but they also allow for the use & unit testing of a wide range of values & edge cases.
I know that this is an example, but that size value should be checked in function body. This is usual pattern in introducing security bugs in C/C++, when we allow user of our function to set size of something.
Hi Karlo, you're absolutely right! Validating user input is a very important part of any code, but we've omitted it in our examples above to keep the code slim & focused on our discussed topic.
Good question, Igor! We're actually not advocating against constants in classes, those have their use cases for sure.
For example, the const & function in the example in red are quite OK if they're defined in your own class that is only used by your project where the logic is specific & never/rarely changes. That code becomes an issue though, for the reasons discussed in the post, if it's part of a public code library accessible to many other engineers & projects where it should cater for a wide variety of use cases & values.
Ironically, you've omitted their value for testing :P. Default arguments also make it super easy to test typical usage, as well as all the weird edge cases.
ReplyDeleteThanks for your message! I'm not sure I understand what you mean by "omitted their value for testing", so feel free to elaborate!
DeleteYes exactly! Default arguments make it easy for the function callers to use your function, but they also allow for the use & unit testing of a wide range of values & edge cases.
He meant that this refactor enables for easy testing, compared to having hardcoded value inside the class. (this is a test blog after all:)
DeleteHi Adel, thank you for this post.
ReplyDeleteI know that this is an example, but that size value should be checked in function body. This is usual pattern in introducing security bugs in C/C++, when we allow user of our function to set size of something.
Regards, Karlo.
Hi Karlo, you're absolutely right! Validating user input is a very important part of any code, but we've omitted it in our examples above to keep the code slim & focused on our discussed topic.
DeleteWhen is it a good idea to use constant fields in a class, then?
ReplyDeleteGood question, Igor! We're actually not advocating against constants in classes, those have their use cases for sure.
DeleteFor example, the const & function in the example in red are quite OK if they're defined in your own class that is only used by your project where the logic is specific & never/rarely changes. That code becomes an issue though, for the reasons discussed in the post, if it's part of a public code library accessible to many other engineers & projects where it should cater for a wide variety of use cases & values.
I hope that clarifies things!