• Ilyak (unregistered)

    They have to do that all the time because Java doesn't have proper verbs.

  • Vroomfundel (unregistered)

    OK, real first post from me then.

    The nice thing here is the self-proclaimed WTF. Self-criticism is good thing, I'm starting to look at my old code immediately to try to qualify.

  • mister (cs)

    I don't see the WTF. The purpose seems to be to add "rules" to an object, maybe for some kind of form validation check. Uses "addrule" to... well, add a rule to any item that needs it, then "checkrules" and it checks them all, warning about any errors.

  • Kluge Doctor (unregistered)

    Oh, the lengths some of us go through to go down in the anals of WFT History! It makes me smile.

    Note: do not reply and tell me I misspelled "annals".

  • Kluge Doctor (unregistered) in reply to Kluge Doctor
    Kluge Doctor:
    Oh, the lengths some of us go through to go down in the anals of WFT History! It makes me smile.

    Note: do not reply and tell me I misspelled "annals".

    I did misspell WTF, of course! :-)

  • SQB (cs)

    TRWTF(tm) is that it's "their", not "there". And that's coming from non-native speaker.

  • NeoMojo (unregistered) in reply to SQB

    lol, whut?

  • Thomas (unregistered) in reply to mister
    mister:
    I don't see the WTF. The purpose seems to be to add "rules" to an object, maybe for some kind of form validation check. Uses "addrule" to... well, add a rule to any item that needs it, then "checkrules" and it checks them all, warning about any errors.

    The real WTF is that Ben did not include more of the code context in the article. Of course this looks like a WTF alone.

  • alunharford (cs)

    Enjoy your mug

  • Cpt (unregistered) in reply to SQB
    SQB:
    TRWTF(tm) is that it's "their", not "there". And that's coming from non-native speaker.

    You mean that native speakers will never make this mistake? Hmmm... somehow I doubt that...

  • Jamie (unregistered) in reply to SQB
    SQB:
    TRWTF(tm) is that it's "their", not "there". And that's coming from non-native speaker.

    Yup, as I said yesterday, the editors on this site are a real WTF. Unfortunately, my post was deleted by those same editors and they just corrected their awful grammar.

  • claudiu (unregistered) in reply to mister
    mister:
    I don't see the WTF. The purpose seems to be to add "rules" to an object, maybe for some kind of form validation check. Uses "addrule" to... well, add a rule to any item that needs it, then "checkrules" and it checks them all, warning about any errors.
    This will be OK for a validation framework if you passed the object to be tested in the the call to check() like check(Object o) not in the constructor of the rule. Because it is in the constructor you need to recreate the rule every time you validate a new object.
  • IceFreak2000 (cs)

    If the only point of this code fragment was to check whether a single instance of a variable was null or not then, yes, it'd be a WTF.

    Unfortunately, that's not what's shown here. It's perfectly obvious that this is an abstract rule that would be combined with other rules (of varying types) that would be validated at some future point.

    For those with ASP.NET experience, think about the various different Validation controls that get invoked when required - it's exactly the same pattern.

    The real WTF is that this (non-)WTFy article was posted as the CodeSOD. Pathetic.

  • yanni (unregistered) in reply to SQB
    SQB:
    TRWTF(tm) is that it's "their", not "there". And that's coming from non-native speaker.

    Of course it's coming from a non-native speaker. You actually learned the rules of the language. Native speakers speaking their own language well, especially english? That's the real WTF!!

    (I feel dirty)

  • Nick J (unregistered)

    This is actually a pretty neat system. Presumably the Rule interface defines the check() method, and the rule checker would then loop through all the Rules added, and execute the check method on each. Nice and simple. The code given is merely an example of how to set up a rule to check for an object not being null. But this system allows for any check, not just this trivial example.

    So, not a WTF at all, but actually very clever.

  • Sam (unregistered) in reply to IceFreak2000
    IceFreak2000:
    It's perfectly obvious that this is an abstract rule that would be combined with other rules (of varying types) that would be validated at some future point.

    TWTF is that "o" (and, therefore, whether or not o==null – we're not checking a mutable field here, and last time I checked Java doesn't let you overload "==") is set when the Rule is constructed. So, unless there's some reason you might want to later decide not to ever check() the rules, why bother with all the enterprisiness?

    Still, respect to Ben for turning himself in.

  • Sam (unregistered) in reply to yanni
    yanni:
    SQB:
    TRWTF(tm) is that it's "their", not "there". And that's coming from non-native speaker.

    Of course it's coming from a non-native speaker. You actually learned the rules of the language. Native speakers speaking their own language well, especially english? That's the real WTF!!

    (I feel dirty)

    Someone whose website I read used to think that Spanish spelling was easy – until he saw a sign written by a native speaker with about one misspelling for every three words.

    TRWTF is that my spell-checker didn't know the word "website" until I right-clicked it and clicked "Learn Spelling".

  • TGV (cs) in reply to SQB
    SQB:
    TRWTF(tm) is that it's "their", not "there". And that's coming from non-native speaker.
    Oh, come on. He could have written "they're". But it wouldn't have been caught by a spell-checker. "couln't" on the other hand...
  • T604 (unregistered) in reply to mister
    mister:
    I don't see the WTF. The purpose seems to be to add "rules" to an object, maybe for some kind of form validation check. Uses "addrule" to... well, add a rule to any item that needs it, then "checkrules" and it checks them all, warning about any errors.

    At that point shouldn't you be using a rules engine? instead of trying to roll your own?

  • notJoeKing (unregistered) in reply to SQB
    SQB:
    TRWTF(tm) is that it's "their", not "there". And that's coming from non-native speaker.

    Are you talking about the line: "That code is still there"?

    Because it should be 'there' and not 'their'. Otherwise, to WTH are you refering?

  • al3x (unregistered)

    "...will go to 'enterprise up' there Java apps..."

    "there" above should be "their". can't believe you didn't see that :)

  • Claxon (cs)

    It's so much worse when you notice that your mind is going...

  • blizzaga (unregistered)

    The real WTF is that you did not provide a IRuleFactory that can initialize rules from a simple configuration file such as

    <ValidationRules type="Initech.Practices.Enterprisey.Validation.ValidationRules, Initech.Practices.Enterprisey.Validation.dll, Version=1.1.2.3, PublicKeyToken=1e9a2c36f"> <addRule type="Initech.Practices.Enterprisey.Validation.NotNullRule,, Initech.Practices.Enterprisey.Validation.dll, Version=1.3.2.4, PublicKeyToken=1e9a2c36f" objName="myObject" objectRetriever="Initech.Practices.Enterprisey.ObjectRepository.NullableObjectRetriever, , Initech.Practices.Enterprisey.ObjectRepository.dll, Version=1.6.2.3, PublicKeyToken=1e9a2c36f" />
  • notme (unregistered) in reply to SQB
    SQB:
    TRWTF(tm) is that it's "their", not "there". And that's coming from non-native speaker.

    Your first language doesn't happen to be German, does it? Also, you forgot the word "a".

  • Mizchief (unregistered) in reply to blizzaga

    Huh, didn't realize they had dll's in java

  • German B. (unregistered) in reply to yanni
    yanni:
    Of course it's coming from a non-native speaker. You actually learned the rules of the language. Native speakers speaking their own language well, especially english? That's the real WTF!!

    TRWTF(tm) is that it's "English", not "english". And that's coming from a non-native speaker.

  • Mizchief (unregistered) in reply to Mizchief
    Mizchief:
    Huh, didn't realize they had dll's in java

    Ok this has screwed me many times, How come there is a "Reply" and a "Quote" Next to every comment?

    Why not do this: Remove the "Reply" button from each comment. Include the Input Boxes for a new comment at the bottom of the list of comments. If you are worried about some timeout for the captcha have a button that refreshes the captcha.

    This way you can refer back to the comments while posting and can easily quote several comments without having to go back and forth

  • Chesterton Wendelbury (unregistered) in reply to blizzaga
    blizzaga:
    The real WTF is that you did not provide a IRuleFactory that can initialize rules from a simple configuration file such as <ValidationRules type="Initech.Practices.Enterprisey.Validation.ValidationRules, Initech.Practices.Enterprisey.Validation.dll, Version=1.1.2.3, PublicKeyToken=1e9a2c36f"> <addRule type="Initech.Practices.Enterprisey.Validation.NotNullRule,, Initech.Practices.Enterprisey.Validation.dll, Version=1.3.2.4, PublicKeyToken=1e9a2c36f" objName="myObject" objectRetriever="Initech.Practices.Enterprisey.ObjectRepository.NullableObjectRetriever, , Initech.Practices.Enterprisey.ObjectRepository.dll, Version=1.6.2.3, PublicKeyToken=1e9a2c36f" />

    Yeah, get your .Net rules executed in Java!

  • Chesterton Wendelbury (unregistered)

    So wait, TDWTF is a confessional now???

  • savar (cs) in reply to Ilyak
    Ilyak:
    They have to do that all the time because Java doesn't have proper verbs.

    Winner.

    You can't test for null now because the architect wants to add the rules now but have the ability to defer execution of the rules to a later point in time.

    If java allowed you to pass a function as a parameter, then case closed.

  • Mr Ascii (cs) in reply to SQB
    SQB:
    TRWTF(tm) is that it's "their", not "there". And that's coming from non-native speaker.
    TRWTF is non-native speakers are often much clearer and make fewer errors than some of the people who learned English as their first language.
  • Karl von L. (unregistered)

    The Real WTF(TM) is that objName and o are being initialized to null when declared. In Java, instance object vars are always initialized to null automatically, so this is redundant.

  • snoofle (cs) in reply to claudiu
    claudiu:
    mister:
    I don't see the WTF. The purpose seems to be to add "rules" to an object, maybe for some kind of form validation check. Uses "addrule" to... well, add a rule to any item that needs it, then "checkrules" and it checks them all, warning about any errors.
    This will be OK for a validation framework if you passed the object to be tested in the the call to check() like check(Object o) not in the constructor of the rule. Because it is in the constructor you need to recreate the rule every time you validate a new object.
    This may be necessary to work in threaded environments. Without more context, there's no way to know if this is appropriate.
  • Outlaw Programmer (cs) in reply to savar

    I can see this making sense in some cases, where different parts of the system have different constraints so you'll want to compare the object against a different set of rules depending on the context. So their whole rules engine thing might not be tooooooo crazy.

    The biggest WTF here is that there is a rule to check for null. If 'xml' is null, then my guess is that it's in a bad enough state that no one can use it and the program should just throw an exception/move on. I can just imagine a new Rule object instantiated after this one that tries to dereference this pointer and bombs the whole system.

    In fact, I suspect the author submitted this because he found himself making "Rule" objects for the simplest things, such as the GreaterThan rule, or the IsNotEqualsOrNull rule. I bet these Rule things work great when there is some complex validation being done but simple checks like this would definitely be filed under "over-engineering."

  • snoofle (cs) in reply to Karl von L.
    Karl von L.:
    The Real WTF(TM) is that objName and o are being initialized to null when declared. In Java, instance object vars are always initialized to null automatically, so this is redundant.
    True, but compiler-warnings of xxx may not have been initialized are annoying...
    Object xxx; // implicitly init'd to null for someParam==0
    if      (someParam > 0) { xxx = someObject;      }
    else if (someParam < 0) { xxx = someOtherObject; }
    return xxx;  // warning: xxx may not have been set!
    
  • vt_mruhlin (cs)

    At least it's addRule(NotNull(obj)), being called presumably from some other object.

    try{
       obj.addRule(NotNull(obj))
    }
    catch(NullPointerException)
    

    would be truly enterprisey

  • GF (unregistered) in reply to Mizchief
    Mizchief:
    Mizchief:
    Huh, didn't realize they had dll's in java

    Ok this has screwed me many times, How come there is a "Reply" and a "Quote" Next to every comment?

    Why not do this: Remove the "Reply" button from each comment. Include the Input Boxes for a new comment at the bottom of the list of comments. If you are worried about some timeout for the captcha have a button that refreshes the captcha.

    This way you can refer back to the comments while posting and can easily quote several comments without having to go back and forth

    Firefox, middle mouse click. Your friend and mine.

    captcha: haero usage: haero r fire, don't matter to me.

  • James (unregistered)

    Because I try to learn something from all the WTFs, let me be crystal clear on this: the problem is that Java does not have an analog to C# "delegates" or C++ function pointers/callbacks. So if I wanted to implement a rules engine like this in C#/C++, I would maintain a list of delegates/callbacks, one per rule, then fire them all off at test-time. And since Java doesn't implement a callback-type functionality, it has to be done with interfaces.

    Did I get that right? Did this wind up as a WTF because there's a better way to do it in Java?

  • brazzy (cs) in reply to James
    James:
    Because I try to learn something from all the WTFs, let me be crystal clear on this: the problem is that Java does not have an analog to C# "delegates" or C++ function pointers/callbacks. So if I wanted to implement a rules engine like this in C#/C++, I would maintain a list of delegates/callbacks, one per rule, then fire them all off at test-time. And since Java doesn't implement a callback-type functionality, it has to be done with interfaces.

    Did I get that right? Did this wind up as a WTF because there's a better way to do it in Java?

    Interfaces are the way Java implements callbacks. The only difference is that you can't have a standalone method, so it always needs to be enclosed in a class. More boilerplate, sure. But with local or anonymous classes (classes that are defined within a method and have access to all variables in that method's scope), it's not that much more verbose than a "real" function pointer or even a closure.

  • snoofle (cs) in reply to James
    James:
    Because I try to learn something from all the WTFs, let me be crystal clear on this: the problem is that Java does not have an analog to C# "delegates" or C++ function pointers/callbacks. So if I wanted to implement a rules engine like this in C#/C++, I would maintain a list of delegates/callbacks, one per rule, then fire them all off at test-time. And since Java doesn't implement a callback-type functionality, it has to be done with interfaces.

    Did I get that right? Did this wind up as a WTF because there's a better way to do it in Java?

    Not quite. You can do callbacks in Java with either base/sub classes or interfaces:

    interface MyIntfc { public void cbMethod(Object arg); }
    class MyClass1 implements MyIntfc { 
      // ...
      public void cbMethod(Object arg) { ... }
    }
    class MyClass2 implements MyIntfc { 
      // ...
      public void cbMethod(Object arg) { ... }
    }
    
    ...
    private MyIntfc callback;
    public void setCallback(final MyIntfc intfc) { callback = intfc; }
    ...
    public void doStuff() {
      ...
      if (callback != null) {
         try {
             callback.cbMethod(someObject);
         } catch (Exception e) {
           // error in callback
         }
      }
    }
    

    To do the above with base/sub-classes:

    public class MyClass {
      abstract class Base {
         public abstract void callback(Object object);
      }
      class Sub1 extends Base {
         public void callback(Object object) {
         }
      }
      class Sub2 extends Base {
         public void callback(Object object) {
         }
      }
      public MyClass() {
        Base cb1 = new Sub1();
        Base cb2 = new Sub2();
        // Pass cb1 or cb2 to the thing that will make the callback
        // Which could then simply invoke the abstract method
        cb1.callback(new Object());
        cb2.callback(new Object());  
      }
    }
    
  • notJoeKing (unregistered) in reply to al3x
    al3x:
    "...will go to 'enterprise up' there Java apps..."

    "there" above should be "their". can't believe you didn't see that :)

    Thanks. I think I mentally skipped over that line like I skip the commentary between the pictures in the "Error'd" posts. :)

  • brazzy (cs) in reply to T604
    T604:
    mister:
    I don't see the WTF. The purpose seems to be to add "rules" to an object, maybe for some kind of form validation check. Uses "addrule" to... well, add a rule to any item that needs it, then "checkrules" and it checks them all, warning about any errors.

    At that point shouldn't you be using a rules engine? instead of trying to roll your own?

    Two future architects, I see.

    Instead of setting up frameworks, shouldn't you simply, you know, check rules? Maintainability is NOT the result of hiding each single piece of code separately in a gazillion layers of abstraction.

  • Thuktun (unregistered) in reply to Sam
    Sam:
    TWTF is that "o" (and, therefore, whether or not o==null – we're not checking a mutable field here, and last time I checked Java doesn't let you overload "==") is set when the Rule is constructed. So, unless there's some reason you might want to later decide not to ever check() the rules, why bother with all the enterprisiness?
    This makes the most sense. I can see using Rule objects if you're allowing runtime configuration of validations to be assembled on-demand for something, and I can see the message constant rather than a hard-coded string.

    Constructing an object whose sole purpose is to check a whether one of the constructor parameters is non-null is silly. The result surely won't change, as you've copied the reference you're comparing against. If it was referring to an external field and checking that to see if something finally got initialized properly, then it might make some sense.

  • Devi (cs) in reply to Sam
    Sam:
    IceFreak2000:
    It's perfectly obvious that this is an abstract rule that would be combined with other rules (of varying types) that would be validated at some future point.

    TWTF is that "o" (and, therefore, whether or not o==null – we're not checking a mutable field here, and last time I checked Java doesn't let you overload "==") is set when the Rule is constructed. So, unless there's some reason you might want to later decide not to ever check() the rules, why bother with all the enterprisiness?

    Still, respect to Ben for turning himself in.

    I thought the same as IceFreak when I first read the code, but you're perfectly correct :)

    This could be used in C++ though and might be quite a neat system:

    class NotNull : public Rule
    {
    private:
       void **mToCheck; 
    public:
       NotNull(void*& pToCheck) 
       {
         mToCheck=&pToCheck;
       }
    
       // etc...
    };
    

    You could set up rules to validate class members or something if you want to check the integrity of an instance. It would be horribly wasteful though, since every instance would have to maintain its own rule list, might as well just have a function like:

    HRESULT MyClass::Validate()
    {
       HRESULT hr=InheritedClass::Validate();
    
       if (mXml==NULL)
          return E_NULL_POINTER;
    
       return hr;
    }
    
  • Dave (unregistered) in reply to snoofle
    snoofle:
    Karl von L.:
    The Real WTF(TM) is that objName and o are being initialized to null when declared. In Java, instance object vars are always initialized to null automatically, so this is redundant.
    True, but compiler-warnings of xxx may not have been initialized are annoying...
    Object xxx; // implicitly init'd to null for someParam==0
    if      (someParam > 0) { xxx = someObject;      }
    else if (someParam < 0) { xxx = someOtherObject; }
    return xxx;  // warning: xxx may not have been set!
    

    <Preaching>That compiler warning would be a Good Thing (TM), because in your code snippet, xxx would not be set at all if someParam == 0. I'll go out on a limb here and say that if your compiler goes through the trouble of following all the branches of your code to determine if a variable may not have been set, then you should use that feature to your advantage wherever possible, rather than disabling it by setting an object to null when it is declared. In your example above, I would add an extra else clause that throws an exception or sets xxx to yetAnotherValue.</Preaching>

  • Christophe (unregistered) in reply to SQB
    SQB:
    TRWTF(tm) is that it's "their", not "there". And that's coming from non-native speaker.

    Often on this site, when people submit posts, they meant to put their "their" there, but put "there" instead. There is where they're mistaken...

  • NoOneKnows (unregistered) in reply to blizzaga
    blizzaga:
    The real WTF is that you did not provide a IRuleFactory that can initialize rules from a simple configuration file such as <ValidationRules type="Initech.Practices.Enterprisey.Validation.ValidationRules, Initech.Practices.Enterprisey.Validation.dll, Version=1.1.2.3, PublicKeyToken=1e9a2c36f"> <addRule type="Initech.Practices.Enterprisey.Validation.NotNullRule,, Initech.Practices.Enterprisey.Validation.dll, Version=1.3.2.4, PublicKeyToken=1e9a2c36f" objName="myObject" objectRetriever="Initech.Practices.Enterprisey.ObjectRepository.NullableObjectRetriever, , Initech.Practices.Enterprisey.ObjectRepository.dll, Version=1.6.2.3, PublicKeyToken=1e9a2c36f" />

    It's more enterprise to use the short assembly name and no PublicKeyToken, that way you can load your rule validator in a new appdomain for every call and slip-stream in new rule assemblies without shutting down the app! Five 9s here we come!

  • AC (unregistered) in reply to snoofle
    snoofle:
    True, but compiler-warnings of xxx may not have been initialized are annoying...
    Object xxx; // implicitly init'd to null for someParam==0
    if      (someParam > 0) { xxx = someObject;      }
    else if (someParam < 0) { xxx = someOtherObject; }
    return xxx;  // warning: xxx may not have been set!
    

    Last time I worked with Java, that was an error, not a warning.

    Instance variables are initialised to null but variables within methods (which this is an example of) are not. Hence, the requirment for explicit initialisation. The compiler checks this, and the bytecode verifier might too.

  • Andrew (unregistered) in reply to savar
    savar:
    Ilyak:
    They have to do that all the time because Java doesn't have proper verbs.

    Winner.

    You can't test for null now because the architect wants to add the rules now but have the ability to defer execution of the rules to a later point in time.

    If java allowed you to pass a function as a parameter, then case closed.

    Yes, you can do this using a worker class & interface with a method signature for the worker to call.

    Suppose you have a RulesProcessor "worker" class & a RuleChecker interface. Then, everything that implements RuleChecker can use the RulesProcessor like so:

    // User class is "A". class A implements RuleChecker { ... // Apply checks on oneself using RulesProcessor. void checkRules(RulesProcessor p) { p.checkAll(this); } }

    class RulesProcessor { ... boolean checkAll(RuleChecker c) {...} }

    The function pointer is instead an object reference of "type" RuleChecker with a checkRules() function. Type checking is strict as hell in Java!

  • real_aardvark (cs) in reply to mister
    mister:
    I don't see the WTF. The purpose seems to be to add "rules" to an object, maybe for some kind of form validation check. Uses "addrule" to... well, add a rule to any item that needs it, then "checkrules" and it checks them all, warning about any errors.
    Not being a Java expert, I'm confused. I was under the impression that the language itself checks to see whether an object is null, and then throws one of those all-too-frequent "null reference" exceptions (which are either checked or not checked -- I can't be bothered to look up the difference).

    Why in the world would anybody, under any circumstances, want to put themselves through this unholy side-show, and then throw an entirely different, and unexpected exception? Under any circumstances?

    In any case, just about any other "Rule" I can think of would throw a null reference exception as a side-effect. Thus, either

    (a) There are other rules, and this one is unnecessary and misleading, or (b) There are no other rules, and this is just a supremely heavyweight and misguided way of asking "is this object null, or what?" or (c) There is noooooo... Rule C.

    Huge brownie points to the OP, by the way. We all do loopy things like this that appear to make sense, and then regret it. The rest of this thread is frankly rather worrying.

Leave a comment on “The Enterprisey Null Test”

Log In or post as a guest

Replying to comment #:

« Return to Article