Except arrays are harder to maintain and harder to read. You are suggesting exactly the same thing as me except using list vs dictionary and the only advantage list has is a tiny tiny memory difference vs bad maintainability.
Edit: sorry I realize this is not python so not dictionary but either way hashmap > arrays. And config is better if this is a system wide lookup and needs changes on the fly per user
Hashing adds extra calculations, if you run your function a lot every second, it'll be significant. Or if you have a service with a lot of users.
This is a 12-index array based on months, there's no need to maintain it since there's no need to increase or decrease it, 12 months is a constant value, so the most you'll have to do is to change the strings that the array will contain.
You can load strings into an array directly from some form of config.
There are more performance advantages than just a "tiny tiny memory difference" - hashmaps have terrible data locality, which can be a really big deal on modern CPU architectures; if you want to know why, there's an excellent Scott Meyers talk somewhere on YouTube. In any case, these performance concerns only matter for the hottest paths in your application.
Also I don't think there's any maintaibility penalty to using an array here. Are we going to add a 13th month anytime soon, and will that new month need an index of 14 or higher for some reason? In general I agree that you should prefer the hashmap, but for this concrete example, what's the benefit?
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/HaroldTheSenpai, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
To be fair, the indentation is wrong because they've commented out the word return values. They just put the // right on the left so it's hard to see. I only noticed because I was wondering why it was highlighted differently.
BUT I would like to point out that this is a self-contained method, so anyone with better understanding of programming can improve “on point” without having to search where it is used and how a change could influence the code
What do you mean it's a self-contained method? In order to improve this code, you would need to search the other code to see if it's supposed to be return the month number or written out.
Some high-iq redditor decided to downvote this so allow me to clarify leftPad() will add to the passed string as much of the "expansion" string as is needed until you achieve the desired length
I wouldn't do one-line I would definitely break it up into the following
//Alternatively based on how it's called passing the month directly //would be better
function getMonthNumberAsString(DateTime date){
int monthAsNumber = date.getMonth().getValue();
//format to two digit number i.e "01", "02", "03", ... "10", "11", ...
return StringUtils.leftPad(monthAsNumber.toString(), 2, "0");
}
I hate one-lines that straight-up nest shit in them.
Took me a while to notice that all of the first assignments are commented, so this function basically converts the month number to a formatted string.
Not only that, but the function name translates to "getMonths",while it only returns a single number, also the parameter is a complete DateTime object with the name of "month".
This code feels like someone updated an old function which had a different purpose
Assembly doesn't technically have an else-if, but I suppose the best alternative would be to abuse the ever-loving hell out of conditional jumps & labels, which I'm really starting to wish existed in high-level languages.
I just did what I could from what I got. There's a function argument named "mes" that is passed by value (I think). If I create a variable inside of a func, it should be local (scoping... yay). And the "temp" var name is fine as long as the func only has one var that is returned after an one if statement.
Also, it's pretty much pseudo code meant to be learned from. I'm here just to give the minimal solution that people can improve upon themselves. that's why I don't give a lot of thought about it.
One is commented out. He’s calling getvalue in every if statement instead of assigning it to a variable and the whole thing could be avoided by just converting the int to a string.
It is bad because it could be just as simple as converting Integer into String, then checking length of string whether it is only single digit. If single digit, pad with ‘0’
38
u/[deleted] Jul 30 '21
portuguese?
121
u/sweetkatydid Jul 30 '21
Yanderecoding
49
u/[deleted] Jul 30 '21 edited Jul 31 '21
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.
39
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jul 30 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
21
u/bistr-o-math Jul 30 '21
Bad code? Yes.
BUT I would like to point out that this is a self-contained method, so anyone with better understanding of programming can improve “on point” without having to search where it is used and how a change could influence the code
8
u/kakassss Jul 30 '21
so how can i do this with better way? any idea or something
4
u/FermentedBarley Jul 30 '21
Why does the site get slow at the end of the year?
4
u/PedroBenza Jul 30 '21
I worked at a place where this was the in-house style of coding. Why write one line of code when you can write 50? I didn't stay long in that job.
5
u/[deleted] Jul 30 '21
r/suddenlycaralho
5
u/r0ut66 Jul 30 '21
ATÉ AQUI MALUCO? r/suddenlycaralho
2
u/TheMind14 Jul 30 '21
NumberFormat, this unknown thing…
Edit: strings too
2
u/Ekonios Jul 31 '21
Took me a while to notice that all of the first assignments are commented, so this function basically converts the month number to a formatted string.
Not only that, but the function name translates to "getMonths",while it only returns a single number, also the parameter is a complete DateTime object with the name of "month".
This code feels like someone updated an old function which had a different purpose
1
u/ElDodi-0 Jul 31 '21
Switch-cases are understimated
1
u/[deleted] Jul 30 '21
Previous interviews...
What language?
Language
Does it have else if?
No
I don't want the job
1
u/FloweyTheFlower420 sadistic Jul 30 '21
yanderedev simulator
1
u/[deleted] Jul 30 '21
r/YandereTechnique
1
u/Jackiboi307 Jul 30 '21
the comments
1
u/GoldenPalazzo Jul 30 '21
Yandere dev is that you?
1
u/ta69ta69 Jul 30 '21
Aaa, si, retorno! Get meses! Una tortilla por favor signor!
1
u/[deleted] Jul 31 '21
This hurts so much
1
u/s_zlikovski Jul 31 '21
ElseIfology
1
u/Super-administrator Jul 31 '21
returno
1
u/[deleted] Jul 31 '21
Hehe I’m gonna say the funny get ready...
YANDERE DEV!
1
u/[deleted] Jan 12 '23
I literally do this kind of thing sometimes
0
u/nati9931 Jul 31 '21 edited Aug 01 '21
return "0" + to_string(mes.getMonth().getValue());
fixed!: var temp = mes.getMonth().getValue(); if(temp > 9) return to_string(temp); else return "0" + to_string(temp);
(still pseudo code)
0
u/[deleted] Jul 30 '21
[deleted] — view deleted comment