Results 1 to 7 of 7
  1. #1
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default Good habit questions: Sequence in which to write methods?

    Hello again,

    so I have another non code specific question. I have been going through the Oracle Java Tutorials as well as third party codes and I'm noticing that the sequence in which people write their codes is a bit unusual...to me.

    So let's take the following as an example:
    Java Code:
    public class Anagram {
    
        public static boolean areAnagrams(String string1,
                                          String string2) {
    
            String workingCopy1 = removeJunk(string1);
            String workingCopy2 = removeJunk(string2);
    
    	    workingCopy1 = workingCopy1.toLowerCase();
    	    workingCopy2 = workingCopy2.toLowerCase();
    
    	    workingCopy1 = sort(workingCopy1);
    	    workingCopy2 = sort(workingCopy2);
    
            return workingCopy1.equals(workingCopy2);
        }
    
        protected static String removeJunk(String string) {
            int i, len = string.length();
            StringBuilder dest = new StringBuilder(len);
      		char c;
    
    	    for (i = (len - 1); i >= 0; i--) {
    	        c = string.charAt(i);
    	        if (Character.isLetter(c)) {
    		        dest.append(c);
    	        }
    	    }
    
            return dest.toString();
        }
    
        protected static String sort(String string) {
    	    char[] charArray = string.toCharArray();
    
    	    java.util.Arrays.sort(charArray);
    
            return new String(charArray);
        }
    
        public static void main(String[] args) {
            String string1 = "Cosmo and Laine:";
            String string2 = "Maid, clean soon!";
    
            System.out.println();
            System.out.println("Testing whether the following "
                             + "strings are anagrams:");
            System.out.println("    String 1: " + string1);
            System.out.println("    String 2: " + string2);
            System.out.println();
    
            if (areAnagrams(string1, string2)) {
                System.out.println("They ARE anagrams!");
            } else {
                System.out.println("They are NOT anagrams!");
            }
            System.out.println();
        }
    }
    It's a simple little code which checks if two strings are anagrams. I am happy to report that I can read all of it and understand the logic...which after 5 days of going through the Tutorial is a bit encouraging for a n00b.

    Anyway, here is my inquiry. While going through the code aforementioned, I noticed that the sequence in which the methods are listed is counter-intuitive...to me. So if you look at it, the removeJunk() method and the sort() method are both invoked in the first method, but they're actually declared after. It's backwards to me as I would think of scripting by incrementally building upon my previous methods. So I would declare the small parts first, then I would add the big part at the end. So that someone reading it would know that removeJunk() reads the string and only appends the StringBuilder object if it is a letter, then sort() is modified to pass a string to an array then sorts the elements of the array. Finally, I would package it in my areAnagrams() method.

    Clearly this isn't what I see, so is it just a few exceptions, or is it generally better to put the most important thing first, then slowly going through the rest to find the smaller parts?

    Ironically, after typing this, I am now beginning to see the wisdom in this approach, but let's see what you have to say.

    Also while you are at it, could you "kindly" remind me why anyone would add the modifiers protected and static to methods declared within the same class where the main method is for such a small program that is unlikely to be used by another party who might be tempted to modify it from another class? Thanks!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    The order that methods are listed in a class does not matter. I'm slightly anal and like to list my methods alphabetically (making them easier to find). As long as the method exists the compiler and JVM can find them.

  3. #3
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    The order that methods are listed in a class does not matter. I'm slightly anal and like to list my methods alphabetically (making them easier to find). As long as the method exists the compiler and JVM can find them.
    I understand that but is there a "preferred" convention? A consensus among programmers? Something that everyone agrees to do?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    I doubt it.
    I tend to write it similar to the above, with the public method first, followed by any methods it calls. That just falls out of how I write code rather than being a conscious decision.

    Alphabetical used to be a "standard" in the days before IDEs, in order to help in finding methods, but these days the IDE will happily list the methods for you.

  5. #5
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Currently I am just simply adding methods as I add things to my programs. I looks a little messy, but if I'm looking for a specific method I just double click the method name in Eclipse.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    If you're using Eclipse you can make it sort the elements of a class/interface in (almost) any way you want; it does hinder code repositories a bit though if you don't apply that sorting feature consistently.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Thanks to you all. I have been using JGrasp but I am increasingly tempted to use Eclipse from your comments and feedback from another thread about what IDE coders are using.

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Replies: 10
    Last Post: 05-21-2011, 01:47 PM
  3. order of sequence
    By dannyy in forum New To Java
    Replies: 23
    Last Post: 04-14-2011, 07:29 PM
  4. Linked list sequence and array sequence
    By Predz in forum New To Java
    Replies: 1
    Last Post: 12-31-2009, 01:30 AM
  5. Hello Good Morning, Good afternoon, and Good Evening
    By MrFreeweed in forum Introductions
    Replies: 3
    Last Post: 12-11-2009, 03:32 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
  •