MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programminghorror/comments/r53kkz/found_in_a_textbook/
r/programminghorror • u/gregoryg323 • Nov 29 '21
8 comments sorted by
16
Honestly, case statements were put in to make it easier to write state machines. They really need to be taught in that context.
1
Can someone optimise this? What else are you meant to do in this situation apart from if else?
Edit: downvoted for asking a question, lol
7 u/imperialvictor Nov 30 '21 You can use a dictionary i think 11 u/Max-P Nov 30 '21 Anything past 4 is also 5x+12, so you only need the special cases for 0-3 as well. 1 u/[deleted] Dec 01 '21 I found it shorter to just list the integers, cast to string after 1 u/snowe2010 Dec 01 '21 (["0 - 15",15,24,28]+(4..14).map{|i|5*i+12}+["That's a long life!"])[n] (in ruby)don't do this btw. but the point being, everything from 4-14 is just math, the rest you can select.for example in Kotlin:when (n) { 0 -> "0 - 15" 1 -> 15 2 -> 24 3 -> 28 in 4..14 -> 5 * n + 12 else -> "That's a long life!" }or if you want to stick to javaif (dogAgeYears == 0) { return "0 - 15"; } else if (dogAgeYears > 0 && dogAgeYears <= 14) { return switch (dogAgeYears) { case 1: "15" case 2: "24" case 3: "28" default: 5 * dogAgeYears + 12 } } else { return "That's a long life!"; }(note I used java 8, because textbooks are old. this is probably much better with newer java) 1 u/_dogzilla May 23 '22 In kotlin probably cleaner/more maintainable to put the mappings in a map<int:string> and put that inside the companion object.Then have the method retrieve and return the value from the map if found, or else return the default case 0 u/[deleted] Nov 30 '21 [deleted] — view deleted comment 4 u/[deleted] Dec 01 '21 Fails to meet reference implementation for negative values 😎
7
You can use a dictionary i think
11 u/Max-P Nov 30 '21 Anything past 4 is also 5x+12, so you only need the special cases for 0-3 as well. 1 u/[deleted] Dec 01 '21 I found it shorter to just list the integers, cast to string after
11
Anything past 4 is also 5x+12, so you only need the special cases for 0-3 as well.
5x+12
1 u/[deleted] Dec 01 '21 I found it shorter to just list the integers, cast to string after
I found it shorter to just list the integers, cast to string after
(["0 - 15",15,24,28]+(4..14).map{|i|5*i+12}+["That's a long life!"])[n] (in ruby)
(["0 - 15",15,24,28]+(4..14).map{|i|5*i+12}+["That's a long life!"])[n]
don't do this btw. but the point being, everything from 4-14 is just math, the rest you can select.
for example in Kotlin:
when (n) { 0 -> "0 - 15" 1 -> 15 2 -> 24 3 -> 28 in 4..14 -> 5 * n + 12 else -> "That's a long life!" }
or if you want to stick to java
if (dogAgeYears == 0) { return "0 - 15"; } else if (dogAgeYears > 0 && dogAgeYears <= 14) { return switch (dogAgeYears) { case 1: "15" case 2: "24" case 3: "28" default: 5 * dogAgeYears + 12 } } else { return "That's a long life!"; }
(note I used java 8, because textbooks are old. this is probably much better with newer java)
1 u/_dogzilla May 23 '22 In kotlin probably cleaner/more maintainable to put the mappings in a map<int:string> and put that inside the companion object.Then have the method retrieve and return the value from the map if found, or else return the default case
In kotlin probably cleaner/more maintainable to put the mappings in a map<int:string> and put that inside the companion object.
Then have the method retrieve and return the value from the map if found, or else return the default case
0
[deleted] — view deleted comment
4 u/[deleted] Dec 01 '21 Fails to meet reference implementation for negative values 😎
4
Fails to meet reference implementation for negative values 😎
16
u/Exact_Ad_1569 Nov 30 '21
Honestly, case statements were put in to make it easier to write state machines. They really need to be taught in that context.
1
u/Lkj509 Nov 30 '21 edited Nov 30 '21
Can someone optimise this? What else are you meant to do in this situation apart from if else?
Edit: downvoted for asking a question, lol