Edit: After discussion and reinspection I came to the conclusion that data serves no purpose and no side effects are generated, so this could be a 3-liner. This is indeed r/badcode.
Original: Why is this bad code?
If I read it correctly, this performs type correction on formFields (on every entry that is valid) using data and in the end returns if any fields were invalid.
The only "bad" part is in the else-block, but this may have its reasons. (And validation / type modification should probably be split into their own coroutines, but whatever)
It also shows that the author doesn't know TypeScript too well due to the Java / C style loop over an array, but other than that, what am I missing?
I mean i get it what it das and how, but it wast that long ago this code was written, and it could be written cleaner and more readable, especially this for loop. This is for validating inputs using refs in react
So is data just useless here? As stated in a different comment, I expected this to be necessary as they want side-effects (string -> bool in formFields using data). Is this wrong and data actually serves no purpose?
It serves absolutely no purpose. Values are assigned to it but it isn't used anywhere in this function and isn't returned for use outside of this function, and half the (frankly nonsensical) code here is dedicated to it.
And what's worse, this developer would have realized as much if they just bothered to clean up their code to the point where at least they could read it themselves.
I specifically mentioned the for loop in my comment.
Also: No they should not return false right there, since then the other values in the form would not be processed. This should be split into its own routine, but as is, this is required.
valid is set to true initially, then it can be set to false in the loop before it is returned.
I assumed the function simply checks that all fields are valid. Unless it has side effects, it could just return as soon as it finds the first invalid.
Did this previously return a mapping of whether each field was valid and later get refactored to just return if the form overall was valid? The useless generic and data local would seem to suggest that.
16
u/Darknety Mar 09 '23 edited Mar 09 '23
Edit: After discussion and reinspection I came to the conclusion that
dataserves no purpose and no side effects are generated, so this could be a 3-liner. This is indeed r/badcode.Original: Why is this bad code?
If I read it correctly, this performs type correction on
formFields(on every entry that is valid) usingdataand in the end returns if any fields were invalid.The only "bad" part is in the else-block, but this may have its reasons. (And validation / type modification should probably be split into their own coroutines, but whatever)
It also shows that the author doesn't know TypeScript too well due to the Java / C style loop over an array, but other than that, what am I missing?
24
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 09 '23
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
42
u/shootfasteatass69420 Mar 09 '23
You see, what worries me the most is that I look at this code and think "that looks like my code, whats wrong with it?"
1
u/zickige_zicke Mar 09 '23
This is legit what I see in everyday projects
1
u/TheUnlocked Mar 09 '23
Did this previously return a mapping of whether each field was valid and later get refactored to just return if the form overall was valid? The useless generic and
datalocal would seem to suggest that.1
u/[deleted] Mar 09 '23
Its valid.
1
u/FalseWait7 Mar 10 '23
Wait is that mine?
1
u/thekwoka Mar 13 '23
to be fair, most people don't know how to make
"false"intofalseprogrammatically without a comparison.They're stupid, but they are most people.
1
u/[deleted] Mar 09 '23
They just have to be extra verbose
1
u/[deleted] Mar 09 '23
[removed] — view removed comment