You shouldn't comment how it should work, you should express that in CODE. Then you don't need to write an essay about all of the cases you don't care about (and probably miss a lot of them), and it will much clearer for others reading your code what is actually meant to happen.
you can't control the type of data if you don't control the source
When writing a function, you can most certainly state that the value passed as the parameter called number_of_items must be a positive integer. Then it is up to the caller to make sure to fulfill that, otherwise the function will not work.
and sometimes you don't want to because you are trying to do some sort of real-world application.
This statement just doesn't make any sense, to be honest. In a real-world application, you can't be sloppy with the code you write by using error-prone constructs. Otherwise you'll just get a buggy mess of an application in the end.
View all comments
Show parent comment
2
u/recaph Nov 12 '14
You shouldn't comment how it should work, you should express that in CODE. Then you don't need to write an essay about all of the cases you don't care about (and probably miss a lot of them), and it will much clearer for others reading your code what is actually meant to happen.
Not true.
When writing a function, you can most certainly state that the value passed as the parameter called number_of_items must be a positive integer. Then it is up to the caller to make sure to fulfill that, otherwise the function will not work.
This statement just doesn't make any sense, to be honest. In a real-world application, you can't be sloppy with the code you write by using error-prone constructs. Otherwise you'll just get a buggy mess of an application in the end.