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:
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!