• (cs)

    Correct me if I'm wrong, but if you take away all of the containsKey and !=null checks, the pseudo-code is basically this:

    if parentValue = replaceOn  // case insensitive
         or ( replaceOn = "*" and fieldValue is not empty )
    

    It's possible they may have started with something like this and as they ran into NullPointerExceptions or missing keys (probably in production), they added the rest of the code, leading to the monstrosity we see today. It's what some coders like to call "iterative development".

    Still, I'll be sure to keep a copy of today's code around for the next time a developer brags to me about their code being "dense".

  • (cs) in reply to Rosuav
    Rosuav:
    "*".equalsIgnoreCase(sub.getReplaceOn())

    This right here is an argument in favour of short function names like strcmp/stricmp. It wouldn't look so ridiculous if the superfluous 'do this check case insensitively' added only a single character to the line, although it's still superfluous in run-time cost (probably not an issue here as it seems to be in response to UI action). But seriously dude, didn't your mother ever tell you about temporary variables?

    A difference of a single character is easier to overlook. Perhaps a typo resulted in strcmp; you'd need a sharp eye to catch the error (and I'm guessing the dev team that churned out this crap isn't that sharp). I'm fine with the difference being 10 characters; the 1 second to type them is trivial compared to being able to see at-a-glance if it's ignoring case. </imho>

    Although I'm glad they used equalsIgnoreCase there. You never know which case of "*" they might use.

  • JuanCarlosII (unregistered) in reply to The Enterpriser
    The Enterpriser:
    3) (The replace field must be an asterix...)
    The replace field must be a cartoon Gaul? Those are some strict requirements.
  • Whatever Man (unregistered)

    One thing left to say:

    Sir, You're an idiot!

  • Christopher Martin (unregistered)

    TRWTF is using 'text-align: justify' with a monospace font.

  • Rince (unregistered) in reply to Anon

    Cast to string?

  • Merrow (unregistered) in reply to Anon
    Forgive my ignorance, but does the !"".equals() actually do anything useful?

    Maybe it's a really long way of writing (!str.isEmpty())?

  • TRWTF (unregistered) in reply to Christopher Martin
    Christopher Martin:
    TRWTF is using 'text-align: justify' with a monospace font.
    Why do people insist on taking utterly trivial non-functional asides and declaring them "TRWTF"? Did you not read today's article? Yet you think "TRWTF" is using text-align with a monospace font? A superfluous CSS element is TRWTF? If that's honestly what you believe then please don't write any code ever for any reason. At all.
  • HerroRygar (unregistered) in reply to Anon
    Anon:
    For readability:
    if (
    	params.containsKey(sub.getParentField().toLowerCase()) 
    	&& (
    		params.get(sub.getParentField().toLowerCase()).getValue().equalsIgnoreCase(sub.getReplaceOn()) 
    		|| (
    			"*".equalsIgnoreCase(sub.getReplaceOn()) 
    			&& params.get(sub.getFieldName().toLowerCase()) != null 
    			&& params.get(sub.getFieldName().toLowerCase()).getValue() != null 
    			&& !"".equals(params.get(sub.getFieldName().toLowerCase()).getValue().trim())
    		)
    	)
    	&& params.containsKey(sub.getFieldName().toLowerCase())
    ) {

    Forgive my ignorance, but does the !"".equals() actually do anything useful?

    I think that tests whether the string passed as the argument to .equals() is empty or not. It's much more intuitive to write it as stringToTest.equals(""), or even better stringToTest.length > 0, but that's a pretty pointless suggestion given the level of WTF already present. That'd be like chiding a pilot for having a crease in his uniform during a freefall.

  • anonymous (unregistered) in reply to wtf

    You're probably not going to see this.. but..

    how do you figure that?

  • Juarez (unregistered) in reply to Anon

    The better question would be does any of this do anything useful...

  • M.I.Stupid (unregistered)
    HerroRygar:
    It's much more intuitive to write it as stringToTest.equals(""), or even better stringToTest.length > 0, but that's a pretty pointless suggestion given the level of WTF already present. That'd be like chiding a pilot for having a crease in his uniform during a freefall.

    Congratulations, sir! I admire your bravery in facing potential NPEs!

    Captcha: dolor (sit amet)

  • (cs) in reply to jonsjava
    jonsjava:
    DOA:
    I think the only solution is to wire pepper spray to certain people's keyboards and have it activate when Ctrl + C is pressed.

    +1 Best idea ever.

    This.

  • (cs)

    The asshole who wrote this needs to learn what a local variable does.

  • TJ (unregistered) in reply to Anon

    Unless i'm mistaken, i think it validates it's not blank --- that's not to say that it's a really weird way of doing so

  • dew|frost (unregistered) in reply to the beholder

    Yes, it pretty much sums it all up. To zero.

  • dew|frost (unregistered) in reply to hoodaticus

    The asshole who wrote this needs to learn an awful lot of things.

  • wtf (unregistered) in reply to dew|frost
    dew|frost:
    The asshole who wrote this needs to learn an awful lot of things.

    The asshole who wrote this does exactly what assholes are meant to do: he produces shit.

  • Olius (unregistered) in reply to Christopher Martin
    Christopher Martin:
    TRWTF is using 'text-align: justify' with a monospace font.

    wipes tear of laughter from eye

    It's funny because it's so true!!!!!

  • (cs) in reply to DOA
    DOA:
    I think the only solution is to wire pepper spray to certain people's keyboards and have it activate when meta + w is pressed.

    FTFY.

  • JaeRae (unregistered) in reply to Anon

    yeah it is checking if ther parameter returned in the () is not equal to an empty string. it's actually doing it correctly to avoid a NPE if the parameter returned is a null gotta lover the .toLowerCase().equalsIgnoreCase(...)

  • digitig (unregistered) in reply to Rosuav

    Nah. It should just be "equals", but should take a comparison function as a parameter.

    I'll get my coat...

  • JoshKraker (unregistered) in reply to Anon

    !"".equals(OtherString)

    In other words, checking for "" != OtherString

  • Oldtimer (unregistered) in reply to Billy The Squid
    Billy The Squid:
    DOA:
    I think the only solution is to wire pepper spray to certain people's keyboards and have it activate when Ctrl + C is pressed.

    What of Shift+Delete / Control+Insert / Shift+Insert? Flying puppies that shit green M&M's fly from the wall to deliver gifts of canned walrus meat?

    Do NOT reveal secret, undocumented productivity tips. The whippersnappers will have your job now!

    P.S. You must have worked with Windows 2.x.

  • Ryan (unregistered) in reply to Anon

    The !"".equals is an empty string check.

  • (cs) in reply to wtf
    wtf:
    I don't get it. Why would you want to mace someone for cancelling a job?
    +1

    http://en.wikipedia.org/wiki/SIGINT_(POSIX)

  • (cs)

    Now this is a TDWTF. For me, the part that really did it is this:

        ("*".equalsIgnoreCase(sub.getReplaceOn())

    Is the author unaware of the "equals" method? Is the author too lazy to leave out "IgnoreCase"? Or maybe the author just plain doesn't get "case"?

    Any way I look at it, it leads to another laugh.

  • (cs)
    Code snippet:
    !"".equals((...).trim())
    This is why .NET has System.String.IsNullOrWhiteSpace.
    !string.IsNullOrWhiteSpace((...))
  • (cs) in reply to wtf
    wtf:
    Doodle:
    Markus:
    apaq11:
    I don't even see &, | or () anymore. All I see is brunette, redhead...

    +1 :D

    ++1

    Damn you, now 1==2 and math's all screwed up. Someone call Russell and Whitehead in, we need to reboot.

    There is no such thing as 2.

  • Hamburger (unregistered) in reply to Anon
    Anon:
    Forgive my ignorance, but does the !"".equals() actually do anything useful?

    Apparently the coder(s) have never heard of org.apache.commons.lang.StringUtils.isBlank

  • Christopher Martin (unregistered) in reply to TRWTF
    TRWTF:
    Christopher Martin:
    TRWTF is using 'text-align: justify' with a monospace font.
    Why do people insist on taking utterly trivial non-functional asides and declaring them "TRWTF"? Did you not read today's article? Yet you think "TRWTF" is using text-align with a monospace font? A superfluous CSS element is TRWTF? If that's honestly what you believe then please don't write any code ever for any reason. At all.

    I think you're reading a bit too much into things if you believe I was actually asserting that this article's alignment is a worse offense than the article's content.

    Also, it's not really superfluous. Before I realized how the alignment was set, I thought the main focus of this WTF was how the code was spaced.

  • Prestaul (unregistered) in reply to Anon

    !"".equals(val)

    is the same as

    val != ""

  • Aaron (unregistered) in reply to Anon
    Forgive my ignorance, but does the !"".equals() actually do anything useful?
    Yes. It checks whether or not the string in the () is equal to an empty string (that is, whether or not it is blank). !"".equals(foo) will return true only if foo is not an empty string.
  • wtf (unregistered) in reply to Prestaul
    Prestaul:
    !"".equals(val)

    is the same as

    val != ""

    Not exactly. == compares reference, .equals() compares contents.

    a == b is only true if and b point the same location in memory, a.equals(b) is true if the contents of a and b do not differ. Now due to the way Java handles Strings, you will generally get the results you want from that, but you're asking for a different thing.

  • sino (unregistered) in reply to DOA
    DOA:
    I think the only solution is to wire pepper spray to certain people's keyboards and have it activate when Ctrl + C is pressed.
    That's a little vicious, don't you think? With these big clumsy fingers, I often hit Ctrl+C by accident on my way to Ctrl+X. Luckily the Ctrl+C is idempotent in this scenario.

    Fucks me up something proper if I hit it on my way to Ctrl+V, though...

  • North Shore Beach Bum (unregistered) in reply to Anon
    Anon:
    For readability:

    Forgive my ignorance, but does the !"".equals() actually do anything useful?

    "".equals checks to see if the other string is not null and has a length of 0. By negating this logic, it accept a string that is either null or has a length greater that 0 (strings can't have negative length). A much more elegant solution would make use of the length() method in the String class.

  • sockmonk (unregistered) in reply to Anon

    Yes, it does I think. It checks whether the expression inside the .equals() evaluates to an empty string.

  • Johann (unregistered) in reply to Anon

    "" would be an empty string (String.Empty).

    .equals does a string comparison test.

    Therefore !"".equals() tests if the string passed in is empty.

  • Vagabond (unregistered) in reply to Anon

    There are better ways to do it, but basically !"".equals(otherString) means that the other string is not empty (or not null, but in the above case it can not be null)

  • FredBob (unregistered) in reply to wtf

    I see the SPAM detector has been switched on again.....

  • BBCode Okay (unregistered) in reply to Anon
    Anon:
    For readability:
    if (
    	params.containsKey(sub.getParentField().toLowerCase()) 
    	&& (
    		params.get(sub.getParentField().toLowerCase()).getValue().equalsIgnoreCase(sub.getReplaceOn()) 
    		|| (
    			"*".equalsIgnoreCase(sub.getReplaceOn()) 
    			&& params.get(sub.getFieldName().toLowerCase()) != null 
    			&& params.get(sub.getFieldName().toLowerCase()).getValue() != null 
    			&& !"".equals(params.get(sub.getFieldName().toLowerCase()).getValue().trim())
    		)
    	)
    	&& params.containsKey(sub.getFieldName().toLowerCase())
    ) {

    Forgive my ignorance, but does the !"".equals() actually do anything useful?

    !"".equals(blah) is same as blah != ""

  • jblake (unregistered) in reply to Anon

    Sure. It tests to see that the field (after blanks have been trimmed) is not empty. I'm not sure why we had to convert it to lower case first...

  • Jan Jungnickel (unregistered) in reply to Anon
    Anon:
    Forgive my ignorance, but does the !"".equals() actually do anything useful?

    "Not empty"?

  • (cs)

    Haven't any of you jerks heard of Conjuctive Normal Form? Sheesh...

  • (cs) in reply to digitig
    digitig:
    Nah. It should just be "equals", but should take a comparison function as a parameter.

    I'll get my coat...

    Nice!

  • Core Xii (unregistered)
    !"".equals(x)
    seems like
    x != ""
    to me. There's just lots of repetition and no caching of intermediate results in the condition... I'd write it like this:
    if
    	(
    	params.containsKey(parentField = sub.getParentField().toLowerCase())
    	&&
    		(
    		(parentFieldParams = params.get(parentField)).getValue().equalsIgnoreCase(subReplace = sub.getReplaceOn())
    		||
    			(
    			subReplace == "*"
    			&& parentFieldParams != null
    			&& parentFieldParams.getValue() != null
    			&& parentFieldParams.getValue().trim() != ""
    			)
    		)
    	&& params.containsKey(sub.getFieldName().toLowerCase())
    	)
    	{
    	}
  • Robert (unregistered) in reply to Anon
    Anon:
    Forgive my ignorance, but does the !"".equals() actually do anything useful?

    would be 'and not an empty string equals the param value trimmed' or in english 'and the param value trimmed doesn't equal an empty string'

  • Matt (unregistered) in reply to ih8u
    ih8u:
    Spell check doesn't catch errors if the errors are legitimate words.

    "Spell checkers make you week."

  • Daniel (unregistered) in reply to Anon

    !"".equals(var) is the same as (var != ""), just a really, really strange way of making the comparison.

  • wtf (unregistered) in reply to Daniel
    Daniel:
    !"".equals(var) is the same as (var != ""), just a really, really strange way of making the comparison.

    You don't do much Java, do you?

Leave a comment on “The Other If”

Log In or post as a guest

Replying to comment #:

« Return to Article