Truth, revisited



  • Just found this buried deep in the bowels of a system I inherited. It's been in the works for 2 years, and is about to go live. I feel the developer's pain:

     // We need to interact w/several systems, and they all define booleans
    // differently. This attempts to encapsulate all of that nonsense *sigh*

    public class Truth {
       private boolean value = false;

       public Truth(final String s) {
          if (s == null) {
             value = false;
          } else if (s.length() == 0) {
             value = false;
          } else {
            switch (s.charAt(0)) {
              case 'Y': // 'Y'es
              case 'y': // 'y'es
              case 'T': // 'T'rue
              case 't': // 't'rue
              case '0': // string-zero
              case 'A':     // 'A'ctive
              case 'a': // 'a'ctive
              case 'P': // 'P'rocess
              case 'p':     // 'p'rocess
              case ' ': // system xxx uses a space to indicate action-to-be-taken
                        value = true;
             }
          }
       }

       public Truth(final boolean b) {
          value = b;
       }

       public Truth(final Boolean b) {
          value = b.booleanValue();
       }

       public Truth(final long n) {
          value = (n == 0);
       }

       public Truth(final YesNoEnum e) {
          value = (e != null && e != YesNoEnum.Unknown && e == YesNoEnum.Yes);
       }

       public Truth(final TrueFalseEnum e) {
          value = (e != null && e != TrueFalseEnum.unknown && e == TrueFalseEnum.TRUE);
       }

       public final boolean getValue() {
          return value;
       }
    }



  • YesNoEnum? TrueFalseEnum?

    The very name of the class - Truth - is amazing. But I think there's a mistake in one of the methods:

    public Truth(final TrueFalseEnum e) {
          value = (e != null && e != TrueFalseEnum.unknown && e == TrueFalseEnum.TRUE);
       }

    What happens when e == TrueFalseEnum.FILENOTFOUND? 



  • value = (n == 0)

    Wrong! I want non-zero values to be true and zero to be false!   =P



  • @Zecc said:

    value = (n == 0)

    Wrong! I want non-zero values to be true and zero to be false!   =P

    Actually, to be fair to the guy who wrote this class, the remote system defined numeric-booleans as 0=true, anything else=false (isn't that the way "C" does it?) - he just wrapped the condition.



  • @H|B said:

    YesNoEnum? TrueFalseEnum?

    The very name of the class - Truth - is amazing. But I think there's a mistake in one of the methods:

    public Truth(final TrueFalseEnum e) {
          value = (e != null && e != TrueFalseEnum.unknown && e == TrueFalseEnum.TRUE);
       }

    What happens when e == TrueFalseEnum.FILENOTFOUND? 

    Sadly, you're not far off. Those enum-wrapper-classes from the external systems define stuff beyond logically true/false/unknown values. Fortunately, our system is written to be consistent around the paradigm: you are not allowed to do something unless you are explicitly permissioned to do it, so we only had to test if some condition was true, and could ignore all the other nonsense.

     



  • I nominate this for best-of-sidebar. This is truly a WTF.



  • @snoofle said:

    Sadly, you're not far off. Those enum-wrapper-classes from the external systems define stuff beyond logically true/false/unknown values. Fortunately, our system is written to be consistent around the paradigm: you are not allowed to do something unless you are explicitly permissioned to do it, so we only had to test if some condition was true, and could ignore all the other nonsense.

    Yes, you're right -- the coder avoided the nonsensical values. But, shoud he/she really check for the "unknown" condition? The expression would result the same without it...



  • @ChZEROHag said:

    I nominate this for best-of-sidebar. This is truly a WTF.
    No way.

    It doesn't have:

    • Factories
    • FactorieFactories
    • Message Queues
    • XML
    • All the other eterprisey JAVA / .NET madness i'm to lazy to list
    • Wooden Tables
    • Desktop Search and random vieo editing capabilities

    So it definetly can't qualify.



  • @H|B said:

    @snoofle said:

    Sadly, you're not far off. Those enum-wrapper-classes from the external systems define stuff beyond logically true/false/unknown values. Fortunately, our system is written to be consistent around the paradigm: you are not allowed to do something unless you are explicitly permissioned to do it, so we only had to test if some condition was true, and could ignore all the other nonsense.

    Yes, you're right -- the coder avoided the nonsensical values. But, shoud he/she really check for the "unknown" condition? The expression would result the same without it...

    Agreed. Interesting aside: the code base was about 500K LOC when I joined the team. Since then we've added about 40% more functionality and through substantial refactoring I've managed to cut the total LOC to a little less than 400K. Gotta love that offshore outsourcing (I don't believe we use rent-a-coder, but some of the folks we work with aren't the brightest bulbs in the pack)



  • @bdew said:

    @ChZEROHag said:

    I nominate this for best-of-sidebar. This is truly a WTF.
    No way.

    It doesn't have:

    • Factories
    • FactorieFactories
    • Message Queues
    • XML
    • All the other eterprisey JAVA / .NET madness i'm to lazy to list
    • Wooden Tables
    • Desktop Search and random vieo editing capabilities

    So it definetly can't qualify.

    Don't be so sure. Factories? Got em. Factory-Factories? In a half-arsed kind of way! Message queues? Got em - using 2 different messaging systems! XML? Got it! .NET? No, but we are still using Java 1.4. We do work ON wooden tables...



  • @bdew said:

    Filed under: !WTF-complete

    I started wondering -- can a system be Turing-complete _and_ WTF-complete?



  • Wow.  Let's take a String from any system with no additional context and determine its boolean value.  Sounds like a plan!!!!

     



  • @wgh said:

    Wow.  Let's take a String from any system with no additional context and determine its boolean value.  Sounds like a plan!!!!

    Not any system; just the ones it's designed to work with, so the context is known!



  • @snoofle said:

    @Zecc said:

    value = (n == 0)

    Wrong! I want non-zero values to be true and zero to be false!   =P

    Actually, to be fair to the guy who wrote this class, the remote system defined numeric-booleans as 0=true, anything else=false (isn't that the way "C" does it?) - he just wrapped the condition.

     

    Uh, no.  In C 0 is false and anything else is true.



  • @snoofle said:

    @wgh said:

    Wow.  Let's take a String from any system with no additional context and determine its boolean value.  Sounds like a plan!!!!

    Not any system; just the ones it's designed to work with, so the context is known!

    That's only partially true.  While you do have the incoming context, there's no way of telling it inside this code.  The way it is now you can end up with truths that aren't really true.  The '0' is the best example.  In most programming a zero would definitely be false (I would think, anyway).


Log in to reply