I'm having trouble accepting that this would have been a transpile. Switch has been part of Javascript since at least ECMAScript 1, so it seems unlikely that any language which supports a switch statement would generate if else blocks. On the other hand, it seems more like some sort of nieve hand coded generator might have been written to create this block rather than actual transpilation.
Seems like the if/else ifs are completely useless. The variable is assigned with a string and all conditions check for a specific integer, so they're never true.
I think it must be partly autogenerated. In the part that OP cropped out that comes after this, it does something like else document.write("來源" + laiyuan) which makes little sense if laiyuan must always be an integer, but makes more sense if it can sometimes be a string.
Only if the laiyuan is a number and doesn't have any gap in the middle an array would be a suitable solution. Still think a map is cleaner to read in this case (especially since there seems to be gaps between values)
Note that using a plain is object is 27% slower than using a sparse array for numbers and 13% slower for text (at least in iOS Safari) https://jsbench.me/h1ksffbtl8/1
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/Earhacker, 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.
It does have a gap. View the full image, the last if branch is for a much larger number. JavaScript doesn’t care, though. Values at indexes in between would be undefined or null, I forget which.
Ok, I stand corrected - almost all indices are consecutive integers. We can still use a list for checking the consecutive indices, and use fallback if-else-if for the rest of the indices (there are very few of these). I haven't checked, but IMO this should still be faster (on average) than dict in this case.
No matter what you do, the lookup time is completely dominated by document.write, so it is better to prefer legibility here than try to squeeze every last cycle out of it. Especially since all useful software is in a state of evolution and a map of strings like this is sure to change.
Well, OCR is terrible for these kinds of posts (this is its attempt), but as I said at the top of the transcription, it was copied from the source OP provided. I placed it in a comment because screen-readers don't automatically follow links, and many people don't like to click away to external websites.
No?? I just wouldn't be able to make a functioning code sample with specific characters I don't have easy access to, and would frankly struggle to differentiate between.
So use ASCII characters to make an example. It's not the Chinese characters that are the matter at hand, it's the if/then barrage and OP talking about writing out an entire switchcase.
Make sure you use else if in case the variable is two numbers at the same time. No one would want our code to break if laiyuan is accidentally 686 and 1240!
The laiyuan 671 in the if/else condition, 农民工工作司, is exactly the string that initialized the variable. I think they decided to stop checking the numbers and just initialized the variable with the only "laiyuan" possible and didn`t remove the useless code.
It's kind of fun to see all the responses. This if struct works just fine. Although, I'd personally like to see a map/hash, or even a switch, (if they are using TypeScript then an enum).
This code is more a question of style more than perhaps best practices. The readability isn't bad in my view, maintenance would be straightforward (assuming the key data type remains a number), the only quibble I really see is a possible issue of performance.
70
u/TsingJyujing Aug 16 '21
FYI, it's from a Chinese official GOV website:
http://www.mohrss.gov.cn/SYrlzyhshbzb/jiuye/gzdt/202108/t20210816_420736.html
170
u/[deleted] Aug 16 '21
Seems like the if/else ifs are completely useless. The variable is assigned with a string and all conditions check for a specific integer, so they're never true.
113
u/claimstoknowpeople Aug 16 '21
I like map lookup better than switch for something like this
68
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 16 '21
Image Transcription: Code
[Thanks to OP for providing a link to the source in this comment. I have also placed it here for ease of access.]
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!
35
u/patryky Aug 16 '21
The Yandere method
10
u/WpGgs Aug 16 '21
I would rather they use hash table than if.*
5
u/jogrohh Aug 16 '21
not even switch case, just have a giant lookup table, it'd be much cleaner, and easier to write/maintain.
If it weren't for the Chinese characters, I could demonstrate lol
4
u/greenpepperpasta Aug 17 '21
Just use nested ternary operators, you can do it in a single line of code /s
1
u/yard2010 Aug 17 '21
I bet the government executed this developer by now.
3
u/CoastRedwood Aug 16 '21
Python leaves the chat*
5
u/No-Mercy_ Aug 16 '21
Yandere dev : *types furiously... *
4
u/bugqualia Aug 16 '21
I would make a list and access by index
4
u/FinalGamer14 Aug 16 '21
I see yandare dev is implementing chinese translations
2
u/[deleted] Aug 16 '21
Or use mapping???
2
u/mrheosuper Aug 17 '21
Chinese yandere dev
3
u/CopperyMarrow15 Aug 16 '21
Make sure you use else if in case the variable is two numbers at the same time. No one would want our code to break if laiyuan is accidentally 686 and 1240!
1
u/deftware Aug 16 '21
Or just a lookup table?
1
u/rjlin_thk usinged Aug 17 '21
Use enum
(use typescript i mean)
or make an object to map those values
1
u/UAIMasters Aug 17 '21
The laiyuan 671 in the if/else condition, 农民工工作司, is exactly the string that initialized the variable. I think they decided to stop checking the numbers and just initialized the variable with the only "laiyuan" possible and didn`t remove the useless code.
1
u/RyanNerd shameless Aug 17 '21
It's kind of fun to see all the responses. This if struct works just fine. Although, I'd personally like to see a map/hash, or even a switch, (if they are using TypeScript then an enum).
This code is more a question of style more than perhaps best practices. The readability isn't bad in my view, maintenance would be straightforward (assuming the key data type remains a number), the only quibble I really see is a possible issue of performance.
0
u/SaltyMini Aug 16 '21
To do it correctly would it involve having a json file with all the text in then just going to that place in the file and returning the text?