|
Lazy<Something> and what's lazy? The Person. Read it out loud and it makes perfect sense
Besides, I was more worried with signature design than OO design
It's an OO world.
public class Naerling : Lazy<Person>{
public void DoWork(){ throw new NotImplementedException(); }
}
|
|
|
|
|
I've seen code like
x = 0 - y;
which, apparently was a workaround for a compiler bug which sometimes generated the wrong code for
x = -y;
I've also seen lots of legacy code of the type you've described but not in VB. The thing is that the concatenation operator is & so think about why they've used + before you change it. Sometimes there is a reason for using the +. Just make sure the item on the right is a string: it may not always be a string. When it isn't, what is happening and what are they doing?
|
|
|
|
|
Member 4608898 wrote: what is happening and what are they doing? No one really knows... There's lots of obscure bugs in code like that Luckily, one of the other 'magical solutions for all your problems' is the wonderful On Error Resume Next command. Really, if it was allowed people would've used On Error Resume Next + ""
It's an OO world.
public class Naerling : Lazy<Person>{
public void DoWork(){ throw new NotImplementedException(); }
}
|
|
|
|
|
That is a programmer's trick to handle null values - it converts a null value to an empty string, which then evaluates to zero. The use of the ampersand (&) makes this work - if the plus sign (+) is used to concatenate, an invalid use of null error will be thrown, as any variable concatenated using + will be nulled if any of the concatenated values are null
It is equivalent to using functions such as IsNull, NZ etc
====================================
Transvestites - Roberts in Disguise!
====================================
|
|
|
|
|
I realize that, but that still doesn't excuse conceding from a long to a string, then back to a long again. That's just sloppy coding.
Regardless, it's an appalling way to do the conversion.
|
|
|
|
|
It's not pretty, admittedly, but VB6 did not include the NZ function that later versions of VBA had.
The alternative would be to create an NZ function and use that e.g.
Function Nz(ByVal V As Variant, Optional ByVal ValueIfNull As Variant) As Variant
If Not IsNull(V) Then
Nz = V
Else
If IsMissing(ValueIfNull) Then
If VarType(V) = vbString Then
Nz = ""
Else
Nz = 0
End If
Else
Nz = ValueIfNull
End If
End If
End Function
====================================
Transvestites - Roberts in Disguise!
====================================
|
|
|
|
|
You've missed the point again. I know exactly what the trick is doing. I'll spell it out slowly...
1. The first line declares PolNumb As Long.
2. Consequently, we know PolNumb is always a number.
3. The line If Val("" & PolNumb) > 0 Then effectively converts PolNumb to a string and appends it to an empty string, simply to convert it back to a number using Val , and finally checks if the result is greater than 0.
That line could be replaced with If PolNumb > 0 Then and be more efficient and more correct (as conceivably PolNumb could be zero).
The issue is not the use of the Val("" & variant) trick, but the misuse of it applied to something we already know to be a number.
|
|
|
|
|
You can see from most of the replies, cargo cult programming is common in VB6.
I had to deal with this sort of thing in code written for VB.NET but with Option Strict Off
Regards,
Mark Hurd, B.Sc.(Ma.) (Hons.)
|
|
|
|
|
Yes, I'm particularly surprised that in spite of writing explicitly why it is inappropriate in this case (to convert long->string->long), that people still post to indicate "this is a common idiom in VB6". I'd rephrase that as "this is a common idiocy in VB6". Cargo-cult programming sums it well too.
I see why VB6 has such as bad reputation - and its not the language for the most part, and why programming languages aimed at mainstream use should take steps to protect programmers from themselves (strong typing, correct scoping, eliminate global variables, and make bad-idioms a struggle to use).
I wonder if there's a cargo-cult approach on message boards too, where if you keep repeating the same wrong reply it will magically begin to work.
"Insanity: Doing the same thing over and over again and expecting different results". Albert Einstein.
modified 4-Jan-13 16:05pm.
|
|
|
|
|
Slightly in defence of the VB.NET Option Strict Off equivalent, I did see it provides a more user friendly error message. (Programmatically it loses information, because the exception type is always the same, but the error message actually displays the invalid "value".)
This is NOT a defence of the current Long->String->Long scenario though.
Regards,
Mark Hurd, B.Sc.(Ma.) (Hons.)
|
|
|
|
|
From some legacy VB6 code I'm in the process of making redundant...
Thanks
seobook
|
|
|
|
|
I've always told it: It's better to be in the safe side...
|
|
|
|
|
This is a standard VB6 idiom for dealing with data that might contain a Null value.
|
|
|
|
|
...and in my replies above I've already indicated why it is unnecessary to convert long->string->long.
modified 4-Jan-13 16:14pm.
|
|
|
|
|
I'm still boggled a bit by the number of people who responded "this is a common idiom" without actually analyzing what was written. It's not that I'm not familiar with why it happens, it's just that so many who reply this way are being paid good money to manage code bases.
"Seize the day" - Horace
"It's not what he doesn't know that scares me; it's what he knows for sure that just ain't so!" - Will Rogers, said by him about Herbert Hoover
|
|
|
|
|
cpkilekofp wrote: I'm still boggled a bit by the number of people who responded "this is a common idiom" without actually analyzing what was written.
Good, it's not just me then.
|
|
|
|
|
Hi, Greetings from {London|Austin|NY|...}.
Something tells me that something has gone wrong with the blog comment auto-generator at this blog here[^].
Kinda backfires when the auto-generated comments aren't processed, and, well, the result is a thing to behold!
|
|
|
|
|
Bob Dole The internet is a great way to get on the net.
2.0.82.7292 SP6a
|
|
|
|
|
Unholy mother of spam
|
|
|
|
|
private void toggleSwitch(bool? visible = false)
{
if (visible.Value) toggleState(visible);
}
|
|
|
|
|
Well, it is completely redundant, and why is it Nullable?
I guess the developer had a case of hypocaffenia and encountered a '404: Brain Not Found' error.
Bob Dole The internet is a great way to get on the net.
2.0.82.7292 SP6a
|
|
|
|
|
Zac Greve wrote: hypocaffenia
Love it!
|
|
|
|
|
It's entirely possible that they would pass null into toggleSwitch. Of course, it should be HasValue rather than Value.
|
|
|
|
|
It's not redundant at all. But it probably doesn't function the way the author intended. What the author probably had in mind was:
private void toggleSwitch(bool? visible = false)
{
toggleState(visible);
}
...which wraps a non-nullable toggle function with a nullable one.
But as it is written, the functionality that this method offers is actually:
1. If user passes toggleSwitch(True), then toggleState is toggled. BUT,
2. If user passes toggleSwitch(False) or toggleSwitch(), then NOTHING happens (because of the test).
|
|
|
|
|
Congrats. You have successfully installed bugs!
|
|
|
|