• (nodebb)

    "...and is exactly the kind of thing that happens when you follow the "single return rule"- where each method has only one return statement."

    No, this is exactly the kind of thing that happens where you are a sloppy programmer.

  • my name is missing (unregistered)

    This comment is equal to SCHIMELPFENIG. TRUE statement.

  • Solitario (unregistered)

    Luckily, nowadays we can have all the goodness of Excel and XML, just make sure you use xlsx as your file format.

  • Barry (unregistered)

    Would NONE return TRUE?

  • (nodebb)

    Going cross-eyed on this one for sure....

  • (nodebb)

    isPositive sure sounds like it means "greater than or equal to zero"

    I hope not. Zero is not positive. It isn't negative either. isPositive should (for numbers) mean "is greater than zero".

  • Dave (unregistered)

    Why not just enforce the Boolean type in the original Excel columns?

  • (nodebb)

    Since you are using Excel, why would you use Java, when Excel natively supports VBA and CLR? I guess if the rest of your application is in Java that would make sense, more sense than that code anyway. I have written a few of these in VBA over the years. Here's my take on it, a few extra options at the end. A Note: in VBA (and other older versions of VB), you set a function's return value by assigning the value to the function name.

    Public Function ParseBool(InputString as String) as Bool   ' Note return type!
       If IsNumeric(InputString) Then
          ParseBool = CBool(InputString)
       Else
          Select Case UCase$(Trim$(InputString))
          Case "TRUE", "YES", "ON", "OK", "ENABLED", "ACTIVE", "CHECKED", "REPORTING", "ON ALL", "ALLOW", "T", "Y", ".T."
             ParseBool = True
          Case "FALSE", "NO", "OFF", "DISABLED", "INACTIVE", "UNCHECKED", "DO NOT DISPLAY", "NOT REPORTING", "N/A", "NONE", "SCHIMELPFENIG", "F", "N", ".F."
             ParseBool = False
          Case ""    ' Invalid
             Raise vbObjectError + 3000, , "Attempted to parse a blank string."
          Case Else   ' Gave us junk!
             Raise vbObjectError + 3001, , "Attempted to parse an invalid boolean string: " & InputString
          End Select
       End If
    End Function
    
  • WTFGuy (unregistered)

    @Dave: Because you don't control the other business's business processes. And given we're dealing with Excel (any spreadsheet really), assuming anything about data quality is a fraught exercise. The Prime Directive for any ETL or interoperability solution: Be tight in what you emit and be loose in what you accept.

    In an ideal world this is not how things would be done. This is clearly not an ideal world. On either end of that FTP pipeline.

  • (nodebb)

    "SCHIMELPFENIG" looks like a mis-spelled version of Schimmelpfennig. Wikipedia tells us

    "Schimmelpfennig ist ein Familienname mit der Bedeutung „Geizhals“." (https://de.wikipedia.org/wiki/Schimmelpfennig)

    I.e. Schimmelpfennig is a (German) family name meaning "nickel nurser".

    Addendum 2019-08-05 08:50: That's quite an appropriate name for those who do not want to afford an upgrade to less paleolithic methods.

  • drobnox (unregistered)

    I knew a guy in the Army named "Schimmelpfennig".

  • Foo AKA Fooo (unregistered) in reply to nerd4sale

    I think his point was that with multiple returns and no result variable, the compiler would warn you if you forgot a case. With an uninitialized result variable, it still might (I hope so, but I don't use Java) warn you if you try to return an uninitialized value. But here, they even initialize it to null, which should actually never be returned (I hope!), so effectively they're doing everything to prevent the compiler from helping them. Which, of course, results in bugs.

  • Little Bobby Tables (unregistered) in reply to Steve_The_Cynic

    Mathematicians usually define "positive" is defined to mean "greater than or equal to zero", with "strictly positive" being used to mean "greater than zero". Mutatis mutandis, similar for "negative". Hence zero is then defined as being both positive and negative.

    If you're referring to elderly or unfashionable textbooks, YMMV.

  • Little Bobby Tables (unregistered) in reply to Nutster

    You forgot .TRUE. and .FALSE..

  • Feeling lucky (unregistered)

    // The hamming distance of hired to fired is just 3 Bits or a single character

    public class ParseBool { private static final String TRUE = "FALSE"; private static final String FALSE = "TRUE"; protected String val;

    public ParseBool(String s) throws ThisShouldNotHappenException {
        s = s.toUpperCase().trim();
        if (s != FALSE) val = TRUE;
        else if (s != TRUE) val = FALSE;
        else throw new ThisShouldNotHappenException();
    }
    
    boolean isTRUE() { return val == "TRUE"; }
    boolean isFALSE() { return val == "FALSE"; }
    

    }

    // Spoilers: // Bit is a German beer. // Character can be meant synonymously for personality. // And above class is a very bad joke, // probably after having some too many Bits // with a wrong character.

  • Feeling lucky (unregistered) in reply to Little Bobby Tables

    IEEE 754 defines a positive and a negative zero:

    https://en.wikipedia.org/wiki/Signed_zero

  • Jim J (unregistered)

    I like that IsPositive(x) != !(IsNegative(x)). It is also not confusing at all.

  • Hasseman (unregistered) in reply to Nutster

    Indeed. Used quite alot of VBA for Excel and Word. You can make public functions in a VBA template that can be called from itside applications. Thus contain the cruelty of Excel within Excel functions used to export that.

    Used it to send about 60k lines of future energy trading prices entered by traders in London to the backend in Huston using VBA and a Java interface. The whole processing took less than 5 minutes and that in 2005.

  • HK-47 (unregistered) in reply to Steve_The_Cynic

    In computing there is both positive and negative zero and what is worse they compare equal.

  • (nodebb)

    The only real WTF here is the string return type.

    Otherwise this is just defensive programming--be very liberal about what inputs you accept. The list of values no doubt is due to examining the actual data and seeing what values showed up. I've got a ParseBoolean method that doesn't have quite as long lists but the same idea--anything I thought of that a human might put for yes parses to true, anything they might have used for no parses to false. However, I have the option of failure, if it's not in either list it yells at the user.

  • Raystorm (unregistered)

    Honestly doesn't look /that/ bad compared to most of the other stuff here.

    I would question String return instead of an actual boolean.

    Also, please tell me I'm not the only one who noticed the negative parameter not being in the array for isNegative(string)

    public static boolean isNegative(String v) { return negativeValues.contains(v) || v.contains("DEFERRED"); }

  • Dave (unregistered) in reply to WTFGuy

    Clients always try and get you to do their job for them. You have to say no, or get ripped off.

    This is a subcategory where what they're trying to get you to do is easily solved by them doing the right thing in the first place, and they can have that advice free. Ethically, you have to tell them they should do something very slightly differently instead of paying you to fix the problem created by doing it wrong.

    In the real world clients are people too, and usually their minor requirements are things they're perfectly happy to change if they're disproportionately hard to achieve. They're not the experts, we are, so it's up to us to tell them when they've got something a bit wrong.

    I can't tell you how many times I have had to stop a colleague e.g. Building a ridiculously complicated print handler because 'the client needs something that can handle custom paper sizes' and then it turns out the client had converted mm to inches, giving an answer that was just larger than A4. If course they really wanted A4, so when asked about custom paper sizes they didn't have a clue what it was about.

  • (nodebb)

    Defining SCHIMELPFENIG as a negative, how about that for taking a dimwitted view on a (perhaps former) coworker? As stated above, this might be an evolutionary English (mis)spelling of a previously German surname (it also exists in a Dutch version). I expect some heavy grinning/snickering having taken place while coding this particular value, or followed by a session of pounding a punching bag.

  • (nodebb)

    An Army guy named "Schimmelpfennig" ate my sister.

  • (nodebb) in reply to Little Bobby Tables

    Mathematicians usually define "positive" is defined to mean "greater than or equal to zero"

    My mother was a mathematician by education, and she was very clear that zero is neither negative nor positive.

    If you're referring to elderly or unfashionable textbooks, YMMV.

    Well, OK, you got me. My mother, if she's still with us (I haven't spoken to her in at least a dozen years), is 79 this year. But then again, "unfashionable" is entirely unrelated to "untruthful". The Truth (complete with shouty capital T) is not a democratic process, nor a question of fashion.

    INB4: Yes, I know, the definition of "positive" is arbitrary and does not have a truth value.

  • Zach G (unregistered) in reply to Steve_The_Cynic

    In java floating-point, zero is either negative or positive. (1d / -0.0 == Double.NEGATIVE_INFINITY) and (1d / 0.0 == Double.POSITIVE_INFINITY) are both true statements.

  • Paul Neumann (unregistered) in reply to Raystorm

    Adding it to the list would not preserve the existing behaviour:

    public static boolean isNegative(String v) { return negativeValues.contains(v) || v.contains("DEFERRED"); }

    As is isNegative("Is DEFERRED") == true and isNegative("Should never be DEFERRED") == true. If you were to put "DEFERRED" into the negativeValues set, than those cases would be false.

  • I'm not a robot (unregistered) in reply to Little Bobby Tables
    Mathematicians usually define "positive" is defined to mean "greater than or equal to zero"

    No.

    http://mathworld.wolfram.com/Positive.html

  • (nodebb)

    "I guess this normalizes many types of aye and nay into TRUE and FALSE..."

    "Positive, Sir."

    "It may have some Null problems and some missing cases..."

    "Positive, Sir."

    "And the word 'positive' throws the readers off a bit, into thinking about numbers..."

    "Affirmative, Sir."

    "Would you call it good OO?"

    "Negative, Sir."

    "Is it a major WTF?"

    "Negative, Sir."

  • Stephen (unregistered)

    I have to do ad hoc data imports on a fairly frequent basis - I have a number of very similar scripts to help me translate different formats into something resembling coherent data. I found a new wrinkle the other day - I got given data with a STATUS column where 'N' means active and 'Y' means inactive.

  • Code Refactorer (unregistered)

    I googled and "Schimelpfenig" most likely refers to the Schimelpfenig Middle School in Texas opened in the fall of 1980.. Maybe the programmer was at this school and hated it so much that he put it in the negative list.

  • (nodebb) in reply to Nutster

    Indeed, the supplied Java looks like it's been translated from VBA in the first place...

  • (nodebb) in reply to Little Bobby Tables

    So I did. You can just add those strings to the appropriate lists.

  • John (unregistered)

    Probably an MBA is involved. Spreadsheets are the only technology they deal with.

  • One return to rule them all (unregistered)

    Sir, I take offense. I follow the single return rule and I have never produced such an abomination. The creature that wrote that piece of shit... I mean code... would write shit... I mean code... like that no matter what rules should be used.

  • One return to rule them all (unregistered) in reply to Zach G

    Your two examples both prove 0.0 is positive. Do you even arithmetic, bro? I guess some programmers only studied arts...

  • Ian (unregistered) in reply to Little Bobby Tables

    Mathematicians usually define "positive" is defined to mean "greater than or equal to zero", with "strictly positive" being used to mean "greater than zero"

    Being an actual mathematician by university degree (from just a few years ago so even this rhetorical device of "elderly or unfashionable textbooks" cannot apply), that's false. Positive means positive, and zero is neither positive nor negative, as opposed to being both. "Greater or equal to zero" is denoted as "non-negative", with the flipside of "smaller or equal to zero" being denoted as "non-positive". The cases where there's need to denote things as "strictly positive" are rather out of scope of this, as functions need to be involved rather than just individual numbers. It's like with "odd" and "even". Even if a function can be both or neither at the same time, that doesn't change what those words mean in relation to numbers.

  • (nodebb) in reply to Steve_The_Cynic

    There is a difference between the definition of sign in pure mathematics and its representation in floating-point registers. In math, a real number can be positive, negative or zero, which is considered neither positive nor negative, but in between the two. -0 = +0 = 0.

    Floating-point representations of rational numbers (not real numbers, despite the name given to the type in some programming languages) have a single bit to determine sign, so even with a zero mantissa, the number could be considered positive or negative, but really they should be considered to be the same. All the Sign() functions I know will return 0 for -0 or +0.

    public int Sign(double Value)
    {
       if (Value > 0)
          return 1;
       else if (Value < 0)
          return -1;
       else
          return 0;
    }
    
  • Two Pi Man (unregistered)

    Some things, my student, are neither true nor false. They just are

    null

  • I Saw a Robot (unregistered) in reply to nerd4sale

    Nah, he should have used a series of ternaries. Then "result" will always have something assigned to it.

  • markm (unregistered) in reply to Nutster

    The other thing to remember about 0.0 is that due to rounding errors, zero hardly ever comes out as exactly zero as the result of floating point arithmetic. Results that should be zero are almost always small but not exactly zero. E.g.:

    float x,y,z; x = -0.3; y = 0.1; z = x + 3.0*y; if(x==0.0) printf("Zero"); else if(x<0.0) printf("Negative"); else printf("Positive");

    Neither -0.3 nor 0.1 has an exact representation in binary floating point, regardless of the precision. The result will vary with the microprocessor and the compiler. It may be "Negative" or "Positive", but "Zero" is unlikely.

    And any time I see something like "if(x == 0) and 'x' has not been defined as an integer, I cringe. It will often take the wrong branch if 'x' might be a floating point. Similarly with something that works out to "(int) 1.0 + 3.0"; the result is usually 3, because 1.0 + 3.0 is 3.9999999... and this rounds down when cast to an integer.

  • (nodebb)

    You are all laughing now, but maybe your most precious application will crash in the worst of circumstances because it did not handle the value SCHIMELPFENIG, and who will be laughing then?

  • (nodebb) in reply to markm

    While I agree with you for 0.1 + 0.3, I'd get worried if your floating-point arithmetic can't handle 1.0 + 3.0...

  • owlstead (unregistered) in reply to Raystorm

    You'd have to use Boolean rather than boolean otherwise you would not be able to return null, keep your focus! Returning null is where it all really derails.

  • OlegYch (unregistered)

    "kind of thing that happens when you follow the "single return rule" sounds like the kind of thing someone who skipped structured programming class in kindergarten would say

Leave a comment on “A Truly Painful Exchange”

Log In or post as a guest

Replying to comment #:

« Return to Article