Tushar Goel wrote:than why you used default case?
Tushar Goel wrote:like this:
Tushar Goel wrote:You can throw an exception as well in case wrong score..
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
No, don't try that. There is something very wrong about that bit of code. Actually, two things very wrong with it. Apart from the poor indentation.Partheban Udayakumar wrote:. . .
Try this
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
Regardless the truth value of this boolean expression it evaluates to true always. It is called tautology (I have been taught about it by Campbell Ritchie).Partheban Udayakumar wrote:if (score > 0 || score <= 10)
Now look at this part on its own. You do have cases 10, 9, 8, 7, 6. Why you'd need to check if it is less or equal 10 in a first place?Partheban Udayakumar wrote:score <= 10
I wasn't accurate enough.Earlier I wrote:Regardless the truth value of this boolean expression it evaluates to true always.
Campbell Ritchie wrote:There is another problem; please tell us what the likely values of score are at that point.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
Serkan Kilic wrote:But then it finds an error with
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Stephan van Hulst wrote:I agree with Paweł. You should write a separate method that translates a numeric score to an alphabetic one. Validate the input at the start of the method. Normally you would throw an exception for illegal arguments, but if you're not allowed, you can return null. Use the switch only for the translation step, not the validation.
Stephan van Hulst wrote:I have no idea what this is supposed to teach.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Probably not. That did not answer my question and you have probably got those values wrong.Partheban Udayakumar wrote:. . .
Campbell Ritchie wrote:There is another problem; please tell us what the likely values of score are at that point.
Negative infinity to 5 and 11 to positive infinity will take you to default case. Am I right?
. . .
Campbell Ritchie wrote:Probably not. That did not answer my question and you have probably got those values wrong.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
Campbell Ritchie wrote:I asked you what the value of score is when you reach the default part of the switch. What will the value of score on your line 13 be, if you enter the following values:-
0 100 35 65 99999999 -9999999999?
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
CorrectPartheban Udayakumar wrote:. . .
For 0, its 0
IncorrectFor 100, its 10
IncorrectFor 35, its 3
IncorrectFor 65, its 6
IncorrectFor 99999999, its 9999999
IncorrectFor -9999999999, its -999999999
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
Yes, you have got it nowPartheban Udayakumar wrote:. . .
Sorry these will be the values of score/10
. . .
Campbell Ritchie wrote:Maybe actually three things wrong with it.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
Clear code is the best code.
Shawn Lau wrote:Switch statements are very fast and become a simple jump statement on an assembly level.
They are faster than an if or series of if else statement - probably 3 times as fast...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
"That might be true for C, but Java doesn't use assembler. "
Clear code is the best code.
Shawn Lau wrote:Well it is true for Java as well and that is the reason you MUST use a constant as a case and not a variable. The compiler/assembler turns the constant into an offset and that would be impossible to do with a variable. Again mysticism and programming strike me as somewhat weird.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
What's wrong? Where are you going? Stop! Read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|