Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: shrinking code

  1. #1
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,958
    Thanks
    120
    Thanked 76 Times in 76 Posts

    shrinking code

    this is a bit of java script code:
    functions like checkNick .. all return "" if it is all OK or otherwise some kind of message for user
    Code:
    result = checkNick(nick) || checkMail(email) || checkPwd(pwd1,pwd2);
    if (result)
    {
      alert(result);
      return;
    }
    
    // go forvard ..
    This is attempt to do the same in java, knowing that HashSet does not allow duplicates. It works as wanted, first occurance that does not satisfy , jumps out.


    Code:
        java.util.HashSet hs = new java.util.HashSet();
        boolean f;
        f = hs.add("");
    
        result = hs.add(checkNick(nick)) ||
                 hs.add(checkPwd(pwd1, pwd2)) || 
                 hs.add(checkMail(email)) || 
                 hs.add(checkCaptcha(clientCaptcha, serverCaptcha));
    
        if (!result)
        {
          //ALL OK
        else
        {
          String[] a = (String[])hs.toArray(new String[0]); 
          // output to user about error
          return a[a.length-1].toString();
        }
    Comments welcome.

  • #2
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Untested

    Code:
    java.util.List<String> list = java.util.Arrays.asList(
      checkNick(nick), checkPwd(pwd1, pwd2), checkMail(email), checkCaptcha(clientCaptcha, serverCaptcha)
    );
    String s;
    for(Iterator<String> iter = list.iterator(); iter.hasNext();) {
      s = iter.next();
      if(s.length() != 0)
        return s; // return erroneus string
    }
    // all okay
    or

    Code:
    String[] strs = new String[]{
      checkNick(nick), checkPwd(pwd1, pwd2), checkMail(email), checkCaptcha(clientCaptcha, serverCaptcha)
    };
    for(int i = 0; i < strs.length; ++i)
      if(strs[i].length() != 0)
        return strs[i]; // return "bad" string
    // all okai
    Last edited by Trinithis; 01-07-2008 at 03:19 AM.
    Trinithis

  • #3
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,958
    Thanks
    120
    Thanked 76 Times in 76 Posts
    looks less code than mine, which is what I want, but has fundamental error:
    it has to check all functions before, rolls thru all results and decide what to do.
    Mine jumps out as soon as first result is bad.

  • #4
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Code:
    java.util.Set<String> set = new java.util.TreeSet<String>(2);
    set.add("");
    if(set.add(checkNick(nick)) ||
        set.add(checkPwd(pwd1, pwd2)) ||
        set.add(checkMail(email)) ||
        set.add(checkCaptcha(clientCaptcha, serverCaptcha))) 
      return (String)set.toArray(new String[2])[1];
    // okay
    Last edited by Trinithis; 01-11-2008 at 07:53 PM. Reason: removed bug

  • #5
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    Why don't you just return boolean values from your checking methods. This would make much more sense. Also, wouldn't returning "" in the functions and then using a HashSet's failure to add more than 1 "" just mean that one thing can fail but 2 can't. Do you really want 1 thing being able to fail? For example the nickname and email may be correct but an invalid password returning "" is only returned false by adding it to a hash set if something else has previously failed and thus added "".

    Also, be wary of lazy execution. I expect that Java only executes statements in boolean expressions until the expression is certain. For example (false && foo()) will always return false even before foo is to be executed, so if you want all functions to be executed, do something like
    Code:
    boolean isFoo = foo();
    boolean isBar = bar();
    if(isFoo || isBar)
    {
      // exists
    }
    else
    {
      // doesn't exist.
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •