By the way, the triple-backtick code block doesn't work on all clients. On Old Reddit, it all displays on one line (without a scrollbar, making it impossible to read), while I believe on most mobile clients it just doesn't form any kind of code block. The only universal code block formatting on Reddit uses four spaces at the start of each line.
Also, a year or two ago, there was an issue where all accounts' feeds would show up merged together no matter which account was logged into... was real fun to have that happen in public. They fixed that issue, but it permanently eroded my trust in them.
In here, it probably won't make that much of a difference but I believe that const should be the default and let should be used only if you can't use const.
I mean, yeah, it's bad/wasteful that it's making three separate API calls to the same path, and that all of this isn't just handled in one function.
But on the bright side, since the code is using async/await, it shouldn't be that much slower than the single-function approach. It's able to queue up the three API calls really quickly, and then just let the DOM updates happen through the microtask queue.
in order to do API requests you need to await them, and await is only available inside of async functions.
When the function is called an unresolved promise is returned to the caller once the async function hits the await command and since the function doesn't return anything since that value is set to the DOM it's completely fine.
If you wanted to use the value that the async value returned then you should await it so that the promise is resolved to a value and not a promise that isn't resolved.
Are you referring to the API returning Pascal case? Who knows, I'd guess it's probably written in C# or something where that's the convention for properties.
In c# youWouldStillUseCamelCase the only difference is that its PreferedToHaveTheFirstCharCapital for functions or public variables aahavingthemalllowercase makes it hard to read. like_i_would_even_take_snake_case here
camelCase with first letter capitalised is called PascalCase.
In C#, the convention is that public fields are in camelCase, private fields are _camelCase, but methods are PascalCase. Properties, which are what you'd normally use for exposing fields publically are implemented as methods, and so the method convention of PascalCase applies to them.
Edit: also, realised that you where probably talking about HTML element IDs, yeah, I agree, alllowercase is terrible
just started learning js for my job, what exactly is wrong with this? is it the redundant asignments to { body }? Should that just be declared once as const?
The function names are all lower case, when it should really be in camerCase (gettotalrecovered vs getTotalRecovered has a big impact on readability)
The let could be a const, as it's not being changed again. However, when you take point 3 into consideration, this is not even the biggest offence.
Every API call is a seperate function. This could have legitimate reasons, but as all the functions get called immediately I dare assume there is no legitimate reason.
This means that currently what is happening is:
--> Run gettotalrecovered, ask API for info abcdefg, use abc
--> Run gettotalcases, ask API for info abcdefg, use def
--> Run gettotaldeaths, ask API for info abcdefg, use g
This could be simplified into
--> Run API call, ask API for info abcdefg, use info abc, def, and g (one function)
This seems espec "bad" because it both kills readibility, and the API is possibly going to fetch the information multiple times. Luckily, the browser cache will probably fix the second point, but I doubt that was considered while designing this.
Also, as a last "fuck you", the order of the function calls and the function declarations are out of order. Nitpick, but annoying nonetheless.
Even thought there was a good attempt to split the code into functions for reusability and readness the functions call the indencial api 3 times unnecessarily (although it will probably get the cached call from browser it's still really bad practise)
No naming convention. Using camelCase or snake_case would make the functions and variables much more readable
Async calls need to be awaited or used as a promise .then.catch
No error handling
Variables that are not intended to be modified should be const(readonly) this will reduce chances of bugs. Plus it's really good practise to use cost on objects you know will never need to be modified
Functions called get should return. Just an additional tip booleans should have a 01 suffix for instance has,is,should, e.g. isOpen,
At first I didn't read the URLs so I was thinking what could be the wrong. Splitting things up into functions and them calling them when everything is loaded doesn't seem like a bad idea, I do it all the time.
Then I realised.
I'm not a professional JS developer, so this might sound a bit daft, but wouldn't it be easier to have a single function to load everything, check against a global and then just grab the required data from there?
Seems like the data being requested won't change more than once a day or so.
I really don't think there's anything wrong with this one, he made them as a seperate function, which is a good practice, and they are async. What's the issue? it just looks weird, what else do you think would be better?
EDIT: didn't see they are same calls, okay, this can be improved, but if they were seperate calls, this looks fine.
They're calling the same api to get total confirmed, deaths, etc... just a bug. If they refactored the base url out maybe they would catch this easier.
Since this is in a script tag in the HTML markdown I don’t think it applies as much here, but could this be useful if you wanted to only display the cases/deaths/recovered? I suppose you could have a function to fetch the data, and 3 separate functions to adjust the html accordingly
I agree, downvoting at all is a bad response to a sincere comment.
That said, the first sentence is kind of confusing and seems to be a nonsequitur? I'm not sure what you're trying to say there.
As you say at the end, fetching the data should definitely only happen once here. And seperating just the extraction of the different counts into three functions would just be odd. If you want to reuse those later, you should instead think of storing the counts themselves somewhere, it makes no sense to store the entire page and look them up each time.
I made something like this once and it worked great. If a promise already existed for an endpoint it would return the existing promise. For a component driven framework it meant each component could request whatever it needed and if multiple low level components needed the same thing you didn’t have to worry about passing it around. Just make your calls and let the cache take care of you.
Omg I did not even notice the get. Also its bad practise to call the same get couple of times even if its cached externally or on browser its unnecessary. With good practices its easily avoidable.
63
u/sertdfyguhi Sep 11 '21
Code:
47
u/status_200_ok evil Sep 11 '21
2 extra API calls and functions.
43
u/stahkh Sep 11 '21
This is brilliant. After a week of "hard" work you comment out 2 of them and:
I improved performance by 67%, boss!
80
u/the_monkey_of_lies Sep 11 '21
I want to yel at this person for not using const
53
u/Parkreiner Sep 11 '21
I mean, yeah, it's bad/wasteful that it's making three separate API calls to the same path, and that all of this isn't just handled in one function.
But on the bright side, since the code is using async/await, it shouldn't be that much slower than the single-function approach. It's able to queue up the three API calls really quickly, and then just let the DOM updates happen through the microtask queue.
26
u/brakkum Sep 11 '21
Not great, no terrible. Just more code I could see myself writing when I need to get something done and don’t have the hours.
1
u/Character_Medical Sep 11 '21
Real production code or a joke?
3
u/m2thek Sep 12 '21
This is at least a simple refactor away from being OK. Most of the stuff that gets posted on here is a total nightmare.
2
u/victoroliveirab Sep 11 '21
No one complaining about the lack of camelCase? Damn…
2
u/bigc503 Sep 11 '21
took me a while to realize it's the same endpoint lol
2
u/ranzadk Sep 11 '21
i tried to fix it https://gist.github.com/kirkegaard/4139a544e8b183cd86e2c4bd6a232d28
1
u/kevv_m Sep 11 '21
Yeah... I also hate when people put the open curly braces in a new line. I mean what's wrong with them.
1
u/AzurasTsar Sep 11 '21
just started learning js for my job, what exactly is wrong with this? is it the redundant asignments to { body }? Should that just be declared once as const?
1
u/[deleted] Sep 12 '21
At first I didn't read the URLs so I was thinking what could be the wrong. Splitting things up into functions and them calling them when everything is loaded doesn't seem like a bad idea, I do it all the time.
Then I realised.
I'm not a professional JS developer, so this might sound a bit daft, but wouldn't it be easier to have a single function to load everything, check against a global and then just grab the required data from there?
Seems like the data being requested won't change more than once a day or so.
1
u/ofekt92 Sep 12 '21
I love this code. At first, you're like "well it's not brilliant but it's not really that bad"
But then you notice the horrendous naming, no camelCase!
And then, you see that the 3 functions call to the exact same URL.
The only saving grace this code has is that it's relatively fast. But that's not because of the programmer.
0
u/i_will_yeet biggus dickus Nov 10 '21
I really don't think there's anything wrong with this one, he made them as a seperate function, which is a good practice, and they are async. What's the issue? it just looks weird, what else do you think would be better?
EDIT: didn't see they are same calls, okay, this can be improved, but if they were seperate calls, this looks fine.
-12
u/jzia93 Sep 11 '21
I mean, it's not DRY but other than that it's perfectly fine.
-5
u/marsmanify Sep 11 '21
Since this is in a script tag in the HTML markdown I don’t think it applies as much here, but could this be useful if you wanted to only display the cases/deaths/recovered? I suppose you could have a function to fetch the data, and 3 separate functions to adjust the html accordingly
0
u/[deleted] Sep 11 '21
[deleted] — view deleted comment
0
u/[deleted] Sep 11 '21
[deleted] — view deleted comment
0
u/[deleted] Sep 12 '21
[deleted] — view deleted comment