Another lame isTrue method



  • Okay, besides this being another stupid method that allows the user to enter text instead of forcing them to supply the correct info via a drop-down or button, this one is confusing.

    Since when is: yes == y == true == t == CHECKED == ON ???? I mean, from a fundamental level I understand that checked could mean true I guess. On? It depends on the question. This method is called to validate a users response to asking if they want to continue/proceed...

    So, "Are you sure you want to process the form?" is the question, and the user respons "ON" err... okay...

     <font face="times new roman,times">public static boolean isTrue(String bol)
        {
            boolean result = false;
            if ( bol != null )
            {
                bol = bol.trim();
                if ( bol.equalsIgnoreCase("yes") || bol.equalsIgnoreCase("y") || bol.equalsIgnoreCase("true") || 
    bol.equalsIgnoreCase("t") || bol.equalsIgnoreCase("checked") || bol.equalsIgnoreCase("on") ) { result = true; } } return (result); }</font>


  • (I edited your post for you; I just put in some PRE tags in HTML view around the code and added a line break ....)



  • I have no idea what the context of this is, but perhaps an HTML form control's value might get passed to this method. The value of an HTML checkbox is "on", if the checkbox is checked and no other value is specified.



  • @Someone You Know said:

    I have no idea what the context of this is, but perhaps an HTML form control's value might get passed to this method. The value of an HTML checkbox is "on", if the checkbox is checked and no other value is specified.

    We have a function like this in our code base, and I've done my best to ensure it's used in the least WTFy way possible.

    Here's the issue: html checkboxes represent a boolean, but return that boolean value as a string "on". Postgres stores booleans, but returns them as the string "t" or "f". To keep our PHP sane, we need to effectively cast these two string representations of booleans to PHP booleans. So whenever we're accepting data from either an HTML checkbox, or a postgres boolean, we immediately run isTrue() on it to "cast" it to a boolean. This is done once, and only once.

    Then, in our PHP code, we can manipulate it as a true boolean from end to end, using PHP's native comparison operators. When we write back out to postgres, another function "casts" our PHP bool to the string "t" or "f" again.




    Btw, apparently the WYSIWYG editor doesn't work in Google Chrome. You just get a text box, and your linebreaks are ignored -- you have to manually insert html break tags of some sort. Awesome.



  • @merreborn said:

    Btw, apparently the WYSIWYG editor doesn't work in Google Chrome. You just get a text box, and your linebreaks are ignored -- you have to manually insert html break tags of some sort. Awesome.

    That's better than having the WYSIWYMG editor louse things up.



  • @amischiefr said:

    Since when is: yes == y == true == t == CHECKED == ON ????

    Dude, you're not even close!

    Yaarrrrr!

     



  • @amischiefr said:

    So, "Are you sure you want to process the form?" is the question, and the user respons "ON" err... okay...
     

    The beautiful thing about this approach is that the function returns true for a mistyped "no".

     



  • @Someone You Know said:

    I have no idea what the context of this is, but perhaps an HTML form control's value might get passed to this method. The value of an HTML checkbox is "on", if the checkbox is checked and no other value is specified.

    In fact (in Firefox at least) the check box doesn't even need to be checked for the value to be on.

     [IMG]http://i11.photobucket.com/albums/a173/NeoMojo/checkboxvalue.png[/IMG]



  •  @NeoMojo said:

    @Someone You Know said:

    I have no idea what the context of this is, but perhaps an HTML form control's value might get passed to this method. The value of an HTML checkbox is "on", if the checkbox is checked and no other value is specified.

    In fact (in Firefox at least) the check box doesn't even need to be checked for the value to be on.

    True, in regard to what the values are in the browser. You can get away with that because when the form is submitted, only the values of checked checkboxes are sent with the form; you'll only see checked checkboxes as "on" in whatever server-side code the form is submitted to.



  • @merreborn said:

    @Someone You Know said:

    I have no idea what the context of this is, but perhaps an HTML form control's value might get passed to this method. The value of an HTML checkbox is "on", if the checkbox is checked and no other value is specified.

    We have a function like this in our code base, and I've done my best to ensure it's used in the least WTFy way possible.

    Here's the issue: html checkboxes represent a boolean, but return that boolean value as a string "on". Postgres stores booleans, but returns them as the string "t" or "f". To keep our PHP sane, we need to effectively cast these two string representations of booleans to PHP booleans. So whenever we're accepting data from either an HTML checkbox, or a postgres boolean, we immediately run isTrue() on it to "cast" it to a boolean. This is done once, and only once.

    Then, in our PHP code, we can manipulate it as a true boolean from end to end, using PHP's native comparison operators. When we write back out to postgres, another function "casts" our PHP bool to the string "t" or "f" again.




    Btw, apparently the WYSIWYG editor doesn't work in Google Chrome. You just get a text box, and your linebreaks are ignored -- you have to manually insert html break tags of some sort. Awesome.

     

     

    There just has to be a better way.  I absolutely hate methods that try to blanket cover possible responses instead of forcing one (or several) controlled responses.  I guess with HTML and postgres you don't have an option somtimes. 

     

    This method may have originally been intended to catch such things (not using PHP or postgres, this is a JSP Struts app running Tomcat btw) but it turned out to used catching user typed responses.  Needless to say this function will turned into a yes no option with a button (or something similar) lol.

     

    Yeah what is up with the Chrome, I like using it or forumns because of its speed but I noticed yesterday when trying to post this that I had to put the p tag in order  to get a seperation of text.  You could be a hero and submit a bug report to them!!!

     



  • @amischiefr said:

    There just _has_ to be a better way.
     

    The proposed method is definitely hackish.  Form-handling code should be independent from database code.  The form-handler should translate checkboxes into booleans to make it easier to work with in the native language.  The postgres problem should be handled transparently by the database driver, which is what my database layer does.  However, I got so sick of postgres' stupid-ass booleans I just started using smallints with 0 or 1 for boolean values, which ends up being much easier, even if it does use a bit more space. 



  • I have something very similiar to this in my PHP input handling class, only I call it "getFuzzyBoolean", which is great to get the values of checkboxes and whatever else.



  • @rfsmit said:

    @amischiefr said:

    So, "Are you sure you want to process the form?" is the question, and the user respons "ON" err... okay...
     

    The beautiful thing about this approach is that the function returns true for a mistyped "no".

     

    There's no such thing as a mistyped "no"; it's just a Freudian slip for "on".


  • @amischiefr said:

    There just has to be a better way.  I absolutely hate methods that try to blanket cover possible responses instead of forcing one (or several) controlled responses.

      A long time ago, I used to think the principle "Be generous in what you accept, conservative in what you generate" was a good idea.

     

    And you know what?  I still think it's a good idea - if you have line noise.

    But on any modern system, I think now it's a mistake.  Be conservative in what you generate, and bloody reject out of hand anything at all that looks the least bit dodgy.  Computers are deterministic systems, and if they aren't doing what you expect - THERE'S SOMETHING WRONG!  So don't try and pretend nothing happened and carry on with a smiley face and a stiff upper lip.  You're a computer, not a person, you can't possibly decide what matters and what's just noise, so don't even try.  Flag up a requester, or fall down hard if you have to; get someone who knows what they're doing to have to come and look at it.



Log in to reply