• Nagesh (unregistered)

    Can someone plz post Java™ code of equivalency for this?

  • Frist (unregistered)

    Poor Jeff!

  • frits (cs)

    At least they didn't use Val().

  • Julchen (cs)

    My brain is dribbling out of my ears ....

  • Qvazar (unregistered) in reply to Nagesh
    Nagesh:
    Can someone plz post Java™ code of equivalency for this?

    public double doubleize(String t) { return Double.parseDouble(t); }

  • Niten (unregistered)

    Double your pleasure Double your fun

    Also double your migraines.

  • Geoff (unregistered)

    I would imagine conversion of strings to some time integer value, hopefully not floats for currency dealings, is about the most common input gathering activity in programing business apps. Luckily there exist clean, reasonably safe, easy ways to do this. - Or you can do this way.

    You'd think in some course somewhere along the line programing instruction could spend a day or so the correct patterns for this activity.

  • Seth (unregistered) in reply to Geoff
    Geoff:
    I would imagine conversion of strings to some time integer value, hopefully not floats for currency dealings, is about the most common input gathering activity in programing business apps. Luckily there exist clean, reasonably safe, easy ways to do this. - Or you can do this way.

    You'd think in some course somewhere along the line programing instruction could spend a day or so the correct patterns for this activity.

    I would imagine you said something. But - what

  • WC (unregistered)

    If you're going to Doubleize something, you have to do it twice. Duh.

  • powerlord (cs) in reply to frits
    frits:
    At least they didn't use Val().

    Yes, don't use Val. It's much better to use Double.Parse.

  • Meep (unregistered) in reply to Qvazar
    Qvazar:
    Nagesh:
    Can someone plz post Java™ code of equivalency for this?

    public double doubleize(String t) { return Double.parseDouble(t); }

    Strictly, since he's also rounding to 4 places, and depending on VB.net's implementation of round:

    public static double doubleize(String t) { return Math.floor(Double.parseDouble(t) * 10000.0d + 0.5d) / 10000.0d; }

    (Yeah, it's probably off for negatives.)

  • Aether (unregistered) in reply to WC
    WC:
    If you're going to Doubleize something, you have to do it twice. Duh.

    Wouldn't that Quadrupleize it?

  • Severity One (cs)

    Was that VB syntax invented by humans? Esepcially the 'OrElse'; it sounds rather threatening.

    Although I believe that VB can be summarised in a single statement: 'Dim'.

  • Meep (unregistered) in reply to Geoff
    Geoff:
    You'd think in some course somewhere along the line programing instruction could spend a day or so the correct patterns for this activity.

    You can lecture people on the right way, but they won't internalize it until they've screwed up and fixed it.

  • SeiginoRaikou (unregistered)

    While the implementation is pretty WTF (especially if not centralized), the intention of the method itself is okay. .NET's Double.Parse throws an exception if the string is invalid, and Double.TryParse just catches that exception. Exception handling is way slower in .NET than just detecting failure like this is trying to do. Our company has something similar (and much better) in our internal code library as an extension method. Basically (in C#):

    if(myString.IsNumeric())
        double d = Double.Parse(myString);
    

    is much better than Double.TryParse and also doesn't rely on a goofy out parameter.

  • frits (cs) in reply to powerlord
    powerlord:
    frits:
    At least they didn't use Val().

    Yes, don't use Val. It's much better to use Double.Parse.

    Where's the funny? Val() is a commonly misused VB-specific function. Double.Parse() is probably an acceptable solution.

  • Oik (unregistered)

    Once upon a time when I was but a nipper I had to write what should have been a trivial educational program to graphically illustrate scaling of decimals. Sadly Pascal didn't have accurate enough floats, or big enough integers so I toiled away and wrote a string based simple math library which was 100% accurate.

    Now I realise that I only needed to address the one digit we were working with and simply use the ones to the left and right of it as a identifiers. That's what 30 years of experience does for you.

  • dohpaz42 (cs) in reply to Meep
    Meep:
    Strictly, since he's also rounding to 4 places, and depending on VB.net's implementation of round:
    public static double doubleize(String t) throws FileNotFoundException { return Math.floor(Double.parseDouble(t) * 10000.0d + 0.5d) / 10000.0d; }

    (Yeah, it's probably off for negatives.)

    FTFY

  • Anon (unregistered) in reply to Meep
    Meep:
    Qvazar:
    Nagesh:
    Can someone plz post Java™ code of equivalency for this?

    public double doubleize(String t) { return Double.parseDouble(t); }

    Strictly, since he's also rounding to 4 places, and depending on VB.net's implementation of round:

    public static double doubleize(String t) { return Math.floor(Double.parseDouble(t) * 10000.0d + 0.5d) / 10000.0d; }

    (Yeah, it's probably off for negatives.)

    I don't believe these manage to replicate the bugs in the VB Doubleize. Perhaps they replicate its intent...

  • Anon (unregistered) in reply to frits
    Comment held for moderation.
  • frits (cs) in reply to Anon
    Anon:
    frits:
    powerlord:
    frits:
    At least they didn't use Val().

    Yes, don't use Val. It's much better to use Double.Parse.

    Where's the funny? Val() is a commonly misused VB-specific function. Double.Parse() is probably an acceptable solution.

    I can't get any work done with all this WHOOSH!

    Most likely on your part, unless offering the obvious solution in a sarcastic way passes for funny now.

  • Anon (unregistered) in reply to SeiginoRaikou
    SeiginoRaikou:
    While the implementation is pretty WTF (especially if not centralized), the intention of the method itself is okay. .NET's Double.Parse throws an exception if the string is invalid, and Double.TryParse just catches that exception. Exception handling is way slower in .NET than just detecting failure like this is trying to do. Our company has something similar (and much better) in our internal code library as an extension method. Basically (in C#):
    if(myString.IsNumeric())
        double d = Double.Parse(myString);
    
    is much better than Double.TryParse and also doesn't rely on a goofy out parameter.

    What do you do if it's not numeric?

  • HP PhaserJet (unregistered) in reply to frits
    Comment held for moderation.
  • QJo (unregistered)

    If Commenting or AboutToComment AndAlso MightThinkAboutCommenting Then

    WriteComment()

    Else If HasCommented AndFurthermore HasReceivedComment NotwithstandingAbovementioned MightComment Then

    DontWriteComment()

    However If WontComment AndNevertheless NeverCommenting Then

    OughtToComment()

    OrElse

    Buster()

  • SeiginoRaikou (unregistered) in reply to Anon
    Anon:
    What do you do if it's not numeric?

    Whatever you normally do when given invalid input- show an error message or something.

  • airdrik (unregistered) in reply to QJo
    QJo:
    If Commenting or AboutToComment AndAlso MightThinkAboutCommenting Then

    WriteComment()

    Else If HasCommented AndFurthermore HasReceivedComment NotwithstandingAbovementioned MightComment Then

    DontWriteComment()

    However If WontComment AndNevertheless NeverCommenting Then

    OughtToComment()

    OrElse

    Buster()

    My sentiments exactly.

    TRWTF is a language which needs two versions of and/or: one with short-cut logic built-in and one without. Did they really assume that people expect And and Or to execute all paths to ensure that all side effects get triggered - another WTF is people adding side effects to boolean logic.

  • HP PhaserJet (unregistered) in reply to SeiginoRaikou
    SeiginoRaikou:
    Anon:
    What do you do if it's not numeric?

    Whatever you normally do when given invalid input- show an error message or something.

    Maybe I was confused, the function doesn't look like

    if(myString.IsNumeric())
        double d = Double.Parse(myString);
    
    • the use case does.

    But then aren't you parsing the double twice? One pass to validate it for parsing and then another to actually parse it.

  • SeiginoRaikou (unregistered) in reply to HP PhaserJet
    HP PhaserJet:
    Maybe I was confused, the function doesn't look like
    if(myString.IsNumeric())
        double d = Double.Parse(myString);
    
    - the use case does.

    But then aren't you parsing the double twice? One pass to validate it for parsing and then another to actually parse it.

    Was showing how we do it- the use case is a little different from the WTF, but taking 2 passes on the string is still faster than catching a FormatException from Double.Parse

  • HP PhaserJet (unregistered) in reply to SeiginoRaikou
    SeiginoRaikou:
    HP PhaserJet:
    Maybe I was confused, the function doesn't look like
    if(myString.IsNumeric())
        double d = Double.Parse(myString);
    
    - the use case does.

    But then aren't you parsing the double twice? One pass to validate it for parsing and then another to actually parse it.

    Was showing how we do it- the use case is a little different from the WTF, but taking 2 passes on the string is still faster than catching a FormatException from Double.Parse

    I did not know that, dude.

  • Someone who can't be bothered to login from work (unregistered) in reply to SeiginoRaikou
    SeiginoRaikou:
    While the implementation is pretty WTF (especially if not centralized), the intention of the method itself is okay. .NET's Double.Parse throws an exception if the string is invalid, and Double.TryParse just catches that exception.

    No it doesn't, at least it didn't last time I checked. It aborts processing the string as soon as it finds invalid input and returns false; as opposed to Parse() which aborts parsing the string as soon as it finds invalid input and then throws an exception. TryParse() incurs far less overhead in the case of invalid input.

  • trtrwtf (unregistered) in reply to frits
    Comment held for moderation.
  • PedanticCurmudgeon (cs) in reply to airdrik
    airdrik:
    TRWTF is a language which needs two versions of and/or: one with short-cut logic built-in and one without. Did they really assume that people expect And and Or to execute all paths to ensure that all side effects get triggered - another WTF is people adding side effects to boolean logic.
    Actually, TRWTF is that in VB, And and Or did execute all paths. If, for example, you wanted to do a division, but check for zero first, you needed nested ifs. The real RWTF, however, is that they most likely kept the non-short-circuited version for "backward compatibility."
  • ratis (unregistered) in reply to SeiginoRaikou
    SeiginoRaikou:
    .NET's Double.Parse throws an exception if the string is invalid, and Double.TryParse just catches that exception. Exception handling is way slower in .NET than just detecting failure like this is trying to do. Our company has something similar (and much better) in our internal code library as an extension method. Basically (in C#):
    if(myString.IsNumeric())
        double d = Double.Parse(myString);
    
    is much better than Double.TryParse and also doesn't rely on a goofy out parameter.

    TryParse() does not throw and catch exceptions internally. It works completely without exceptions. Your IsNumeric() just wastes time.

    Dim d As Double = 0
    Dim tim1 As Long = DateTime.Now.Ticks
    Double.TryParse("aaa", d)
    Dim tim2 As Long = DateTime.Now.Ticks
    Try
        d = Double.Parse("aaa")
    Catch
    End Try
    Dim tim3 As Long = DateTime.Now.Ticks
    System.Diagnostics.Debug.WriteLine("" & (tim2 - tim1))
    System.Diagnostics.Debug.WriteLine("" & (tim3 - tim2))
    
  • C-Octothorpe (cs)

    The WTF trifecta is in play here:

    1. VB ('nuff said)
    2. Rewriting 'parse', badly
    3. Calling this shite twice on the same line (or at all for that matter)

    This SOD shows a complete lack of understanding on the developers side, or they were a junior, or both... Reminds me of what I saw throughout the codebase I inherited:

    string myString = "this is a string"; return (string)myString.ToString() as string;

    WTF?! It's a string already!!

  • Anon (unregistered) in reply to Severity One

    I think that OrElse, AndAlso are VB's way of introducing the short circuit behaviour of C's (and others) || and && operators.

    In C,

    if (1 || f(x)) { ... }

    and

    if (0 && g(y)) { ... }

    will result in neither f(x) nor g(y) being evaluated.

  • Meep (unregistered) in reply to Anon
    Anon:
    Meep:
    Qvazar:
    Nagesh:
    Can someone plz post Java™ code of equivalency for this?

    public double doubleize(String t) { return Double.parseDouble(t); }

    Strictly, since he's also rounding to 4 places, and depending on VB.net's implementation of round:

    public static double doubleize(String t) { return Math.floor(Double.parseDouble(t) * 10000.0d + 0.5d) / 10000.0d; }

    (Yeah, it's probably off for negatives.)

    I don't believe these manage to replicate the bugs in the VB Doubleize. Perhaps they replicate its intent...

    Right, sorry about that, the method should start with:

      Matcher m = Pattern.compile("^[+-]?[0-9\.]*").matcher(t);
      if(!m.find()) { return 0.0d; }
      t = m.group();
    

    So, obviously the strings '+', '-' and '.' will be passed to CDbl and possibly throw an error.

    And, of course, it doesn't handle exponents. The way it's used in the program is almost vindictive: if CStr decides to format a number in exponential form, it will be then be doubleized as its exponent. So 123456789 will be doubleized properly, CStr'd to 1.23456789E8 and doubleized to 1.2346.

  • Severity One (cs) in reply to airdrik
    airdrik:
    TRWTF is a language which needs two versions of and/or: one with short-cut logic built-in and one without.
    You mean, like Java? This
        if( x!=0 & 4/x>1 )
    is perfectly valid Java. It will also throw an exception if x=0.

    Possibly, there are instances where this comes in useful, but I can't think of any.

  • doctor_of_common_sense (unregistered)

    The code clearly had no WTF. It was clearly a management effort to doubleize the productivity of a (clearly) half by half working app. And much like many management efforts, it zeroed the result in most cases.

  • Inno (unregistered)

    I desperately need the source code for Floatate() as well. Please help me, I have an assignment due tomorrow! Please please please...

    ;-)

  • Mythran (cs) in reply to Aether
    Aether:
    WC:
    If you're going to Doubleize something, you have to do it twice. Duh.

    Wouldn't that Quadrupleize it?

    Public Function Quadrupleize(ByVal StrVal As String) As String Return CStr(Doubleize(CStr(Doubleize(StrVal)))) End Function

    There ya go. People pay people for this!

  • ~~~ (unregistered)
    ReasonStr = ReasonStr & _
       "Changed Unit Weight (" & _
       CStr(Doubleize(CStr(Doubleize(UnitWeightTextBox.Text) - InvItem_UnitWeight))) & _
       ") //Bugfix 199: UnitWeightTextBox not being converted correctly -- Added second conversion
  • K (unregistered)

    Bonus:

    Most people don't know that, but the numeric separator ("." in the US) is a "," instead in Germany. Oh how much fun this is!

  • Jay (unregistered) in reply to trtrwtf
    trtrwtf:
    You know, I've never known a joke that was improved by arguing over whether it was funny or not.

    If you tell a joke and the other person doesn't laugh, and then you explain to them why it's funny, logically, now they should laugh. Retroactively.

  • Somebody (unregistered) in reply to Aether
    Aether:
    WC:
    If you're going to Doubleize something, you have to do it twice. Duh.

    Wouldn't that Quadrupleize it?

    I think my head just exploded.

  • Jay (unregistered) in reply to Severity One
    Severity One:
    airdrik:
    TRWTF is a language which needs two versions of and/or: one with short-cut logic built-in and one without.
    You mean, like Java? This
        if( x!=0 & 4/x>1 )
    is perfectly valid Java. It will also throw an exception if x=0.

    Possibly, there are instances where this comes in useful, but I can't think of any.

    Well, how about

    if (checkCustomerWithSideEffects(customerid) & checkStockWithSideEffects(itemid)) ...
    

    If you used && instead of &, the side effects on the second call wouldn't happen if the first call returned false.

    This may or may not be desirable, depending on what you're up to.

  • Marvin the Martian (unregistered)

    "Classic ASP"?

    Pff... I'm getting old. What's next? "Canonical Ruby-on-rails"? "Ancient Python"?

  • Jay (unregistered)

    Well obviously you should do the conversion twice. What if the computer made a mistake the first time?

  • boog (cs) in reply to Severity One
    Severity One:
    airdrik:
    TRWTF is a language which needs two versions of and/or: one with short-cut logic built-in and one without.
    You mean, like Java? This
        if( x!=0 & 4/x>1 )
    is perfectly valid Java. It will also throw an exception if x=0.

    Possibly, there are instances where this comes in useful, but I can't think of any.

    Isn't & a bitwise operator in Java? I don't think it was ever intended to be used as a non-short-cut boolean "AND".

    Or were you joking?

  • Meep (unregistered) in reply to Severity One
    Severity One:
    airdrik:
    TRWTF is a language which needs two versions of and/or: one with short-cut logic built-in and one without.
    You mean, like Java? This
        if( x!=0 & 4/x>1 )
    is perfectly valid Java. It will also throw an exception if x=0.

    Possibly, there are instances where this comes in useful, but I can't think of any.

    Logic where you evaluate all tests regardless can be useful to prevent side-channel attacks. Essentially, you're trying to make the machine do the same amount of work no matter which path the code goes through.

    For instance, if someone tries to log in with a username and password, you might find right away that the username isn't valid, but then go through equivalent steps as if it had been a real password. That way an attacker can't determine valid usernames by observing patterns of response time.

  • Meep (unregistered) in reply to Jay
    Jay:
    Severity One:
    airdrik:
    TRWTF is a language which needs two versions of and/or: one with short-cut logic built-in and one without.
    You mean, like Java? This
        if( x!=0 & 4/x>1 )
    is perfectly valid Java. It will also throw an exception if x=0.

    Possibly, there are instances where this comes in useful, but I can't think of any.

    Well, how about

    if (checkCustomerWithSideEffects(customerid) & checkStockWithSideEffects(itemid)) ...
    

    If you used && instead of &, the side effects on the second call wouldn't happen if the first call returned false.

    This may or may not be desirable, depending on what you're up to.

    Executive summary: "I don't know of any either."

Leave a comment on “Doubleizing”

Log In or post as a guest

Replying to comment #:

« Return to Article