Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By JosAH

Thread: Code architecture

  1. #1
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    4

    Default Code architecture

    Have four different code examples with same output result.
    Question is, which example is better?

    Java Code:
        public static void main (String[] args) {
            String prefix = prefix("xxx00", "xxxxxxxxxx");
        }
        
        public static String prefix(String s1, String s2) {
            int min = s1.length() < s2.length() ? s1.length() : s2.length();
            String prefix = "";
            int i = 0;
            while (i < min && s1.charAt(i) == s2.charAt(i)) {           
                prefix += s1.charAt(i++);
            }
            return prefix;
        }
    or:

    Java Code:
            while (i < min) {           
                if (s1.charAt(i) == s2.charAt(i))
                    prefix += s1.charAt(i);
                i++;
            }
    or:

    Java Code:
            for (int i = 0; i < min; i++)
                if (s1.charAt(i) == s2.charAt(i))
                    prefix += s1.charAt(i);
    or:

    Java Code:
            for (int i = 0; i < min && s1.charAt(i) == s2.charAt(i); i++)
                prefix += s1.charAt(i);
    or?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,043
    Blog Entries
    7
    Rep Power
    23

    Default Re: Code architecture

    I'd get the prefix after the loop has finished; it doesn't matter much for your small examples but if the common prefix happens to be large, a lot of Strings have been created and thrown away during those loops.

    kind regards,

    Jos
    diamonddragon likes this.
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    4

    Default Re: Code architecture

    Do You mean each time I concatenate String object, it is actually new String object, because of String immutability?

    So, is something like this best solution?
    Java Code:
            int i;
            for (i = 0; i < min && s1.charAt(i) == s2.charAt(i); i++);
            prefix = s1.substring(0, i);
    Last edited by diamonddragon; 01-27-2012 at 09:27 PM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,043
    Blog Entries
    7
    Rep Power
    23

    Default Re: Code architecture

    Quote Originally Posted by diamonddragon View Post
    Do You mean each time I concatenate String object, it is actually new String object, because of String immutability?

    So, is something like this best solution?
    Java Code:
            int i;
            for (i = 0; i < min && s1.charAt(i) == s2.charAt(i); i++);
            prefix = s1.substring(0, i);
    Yup, that's much better (big-Oh wise that is).

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. What is MVC architecture, how do I use it
    By africanhacker in forum New To Java
    Replies: 6
    Last Post: 03-21-2011, 03:49 PM
  2. Architecture of pos
    By j-mi-jim in forum Advanced Java
    Replies: 6
    Last Post: 11-12-2010, 02:57 PM
  3. Question about architecture
    By sdmente in forum Advanced Java
    Replies: 7
    Last Post: 07-28-2010, 01:07 PM
  4. Architecture Rules 2.0.1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-17-2007, 03:03 PM
  5. Architecture of Any IDE...
    By vikki_pu in forum Other IDEs
    Replies: 5
    Last Post: 10-30-2007, 02:08 PM

Posting Permissions

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