r/badcode • u/the96jesterrace • Jun 19 '22
vb Is that some kind of genius I cannot understand rn or can you scale that down to 1 line?
441
Upvotes
r/badcode • u/the96jesterrace • Jun 19 '22
3
u/pekkalacd Jun 20 '22
What language is this?
135
u/TheDawgLives Jun 19 '22
Barring any crazy side-effects from the
Valmethod then you can condense the whole thing down to just the top level else.28
u/[deleted] Jun 20 '22
You know your code is bad when you struggle to understand 11 lines
78
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jun 19 '22
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!
43
u/JacksHQ Jun 20 '22 edited Jun 22 '22
Also worth mentioning is that this code is using non-short-circuiting operators in the if statement, which is unnecessary. It's worth mentioning as a point of educating people who don't know about it.
In VB, And is the same as & in other languages, which is NOT a short-circuiting logical operator (short circuiting operators make it so that it doesn't have to evaluate all the expressions in the if statement if it is clear that the whole combination of expressions will evaluate to false). The VB equivalent of the short-circuiting && operator from other languages is the AndAlso operator (the || equivalent is OrElse).
So, for example, the code
we have two expressions which will both be checked every time, but isn't always needed, such as when name has a value of something else like "Foo". If name = "Value" is false then it makes more sense for it to stop at that point (short-circuit) without having to move on to check to see if table = "Config" as well. This is where we would use AndAlso instead of And to indicate that we want it to short-circuit the expression evaluation if the first expression is false.
TLDR; In VB it's better to use AndAlso and OrElse in general (unless you specifically need to check all expressions every single time) as these are the equivalent of && and || from other languages that will be more efficient and short-circuit if the first expression is false (or true in the case of OrElse).
47
u/IDontByte Jun 19 '22
If that condition is true, then
Val(AttrVal)should also equalAttrValwhich makes both branches equivalent. I think you can substitute that conditional block with:If you can perform that substitution, then both branches of the outer condition become equivalent and the entire thing can be simplified into
If
AttrValis a temporary variable, then yes this can be simplified down to one line.It seems like there is some other intent behind this code though, so it wouldn't surprise me that this is buggy code rather than code bloat.
6
u/[deleted] Jun 20 '22
This uses the VB Val function. This encodes one specific case (Value/Config) to convert AttrVal to numeric objects if possible, otherwise leave them as (presumably) strings.
Bad code maybe but possibly more likely an idiomatic use of the language. The Val function is a downright bizarre invention.
4
u/nekokattt Jun 20 '22
I Always Find That Visual Basic's Weird Capitalization Is Really Distracting When Trying To Read Code
1
u/[deleted] Sep 25 '22
Two
2
u/[deleted] Jul 17 '22
What the hell is Val() supposedt to do?
-6
u/josephblade Jun 20 '22
I don't see much value in making this a single line of code, given that there is already 1 special case (value/config) which treats fields different from everything else.
so it is conceivable that there will be other special cases in the future. Better use a few lines than make a developer split open a condensed line to insert another special case.
having said that there are definitely some improvements possible. that way you can more clearly show what is the fallback and what are the exceptional situations.