If the user puts zero in the amount, and you want 0 returned when they do that, then you should put in code at the business rules level to catch that value and return 0.not have some special case where division by 0 = 0. So Edwardo asks in the comments "what if the user puts in a 0?", and he advocates that it should be okay to get a 0 in return. (Use of null in this case is reasonable, as it is not a value that will be used in later mathematical calculations). If you're dividing by zero, you're trying to do something that doesn't make sense mathematically, so no numeric answer you can get will be valid. It's an indication that something is fundementally wrong. I think there's an underlying issue here, which is that division by 0 is not legal. Some of my answer was addressed to concerns like that of Edwardo, in the comments, who seemed to be advocating returning a 0. I'm getting a lot of downvotes on this recently.so I thought I'd just add a note that this answer was written before the question underwent it's most recent edit, where returning null was highlighted as an option.which seems very acceptable.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |