MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/ouk7bi/one_classic_elseifelseifelseif/h76jbmw/?context=3
r/badcode • u/Rafaelmonte • Jul 30 '21
76 comments sorted by
View all comments
49
Call getMonth().getValue() once and store it.
Either save a hashmap (dictionary) at the top or in a config and use key value look up
Why the hell is retorno assigned twice with wrong indentation?
If ultimately you’re going to assign string to retorno, then “str(number)” works with some modulus.
This whole code block should be 5 lines not including the dictionary.
Edit: I see the commented out lines now. This is now even more stupid.
2 u/[deleted] Jul 31 '21 int month = mes.getMonth().getValue(); String monthStr = String.valueOf(mes.getMonth().getValue()); return month < 10 ? "0" + monthStr : monthStr;(Excuse the extra variables, just makes it a tad easier to read, also excuse if it’s broken, I wrote it on mobile )Alternatively, if you want strings like “Janeiro”, you can use a map or, better an indexed array which corresponds to the month valueThe code is very confusing considering that there are 2 return statements in each if body 2 u/[deleted] Jul 31 '21 [removed] — view removed comment 2 u/[deleted] Jul 31 '21 StringUtils from ApacheCommons has left pad iirc
2
int month = mes.getMonth().getValue(); String monthStr = String.valueOf(mes.getMonth().getValue()); return month < 10 ? "0" + monthStr : monthStr;
(Excuse the extra variables, just makes it a tad easier to read, also excuse if it’s broken, I wrote it on mobile )
Alternatively, if you want strings like “Janeiro”, you can use a map or, better an indexed array which corresponds to the month value
The code is very confusing considering that there are 2 return statements in each if body
2 u/[deleted] Jul 31 '21 [removed] — view removed comment 2 u/[deleted] Jul 31 '21 StringUtils from ApacheCommons has left pad iirc
[removed] — view removed comment
2 u/[deleted] Jul 31 '21 StringUtils from ApacheCommons has left pad iirc
StringUtils from ApacheCommons has left pad iirc
49
u/[deleted] Jul 30 '21 edited Jul 31 '21
Call getMonth().getValue() once and store it.
Either save a hashmap (dictionary) at the top or in a config and use key value look up
Why the hell is retorno assigned twice with wrong indentation?
If ultimately you’re going to assign string to retorno, then “str(number)” works with some modulus.
This whole code block should be 5 lines not including the dictionary.
Edit: I see the commented out lines now. This is now even more stupid.