Results 1 to 7 of 7
- 06-23-2011, 02:36 AM #1
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
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:
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.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(); } }
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!
- 06-23-2011, 02:41 AM #2
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.
- 06-23-2011, 03:22 AM #3
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
- 06-23-2011, 10:10 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 06-23-2011, 10:19 AM #5
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.
- 06-23-2011, 10:54 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-23-2011, 04:54 PM #7
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
A music collection printer, questions about 2 String toString methods returned maybe
By silverglade in forum New To JavaReplies: 10Last Post: 05-21-2011, 01:47 PM -
order of sequence
By dannyy in forum New To JavaReplies: 23Last Post: 04-14-2011, 07:29 PM -
Linked list sequence and array sequence
By Predz in forum New To JavaReplies: 1Last Post: 12-31-2009, 01:30 AM -
Hello Good Morning, Good afternoon, and Good Evening
By MrFreeweed in forum IntroductionsReplies: 3Last Post: 12-11-2009, 03:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks