Knowledge Reigns Supreme
Knowledge Reigns Supreme
OCPJP 6, OCEWCD Java EE 6
Campbell Ritchie wrote:Why is your array list static? Why are all your fields static?
Don't declare a List as ArrayList<T> but as List<T>.
Use spaces for indenting, not tabs.
Why are you not creating an object and calling methods on that?
Why has your class got no constructor?
Why do you have an elements field when you could use the size() method of the List instead?
Why are you passing a BufferedReader to the Scanner constructor rather than a File object?
Why are you altering the delimiter on the Scanner rather than using its nextInt() method?
Why are you using system.out rather than System.err for an Exception message?
Why have you got inconsistent indentation, with some lines containing {...}?
You have inconsistent use of whitespace, sometimes putting a space after keywords and before (, and you are doing the same with method names and invocations.
What I am least happy about is trying to do everything in a static context rather than creating an object which does its own loading and printing (at least).
I am also not happy about the return in the middle of the recursive method, but many people will disagree with me on that.
Knowledge Reigns Supreme
Alexander Sales wrote:i don't like the tabs on the static fields.
I dont know, I like to have my field-names start at the same point of the line (it shifted when pasting it here).
i like the high level abstractions down to the low level abstractions.
i like the method arrangements.
i like the naming conventions used.
cool usage of scanner! It's the first time i've seen it.
why NEWLINE instead of LINE_SEPARATOR? my interpretation of NEWLINE is "\n".
why use line.separator? instead of making it simple or what you really have used as delimiter "\".
looks like you have read Clean code by Robert Martin
Knowledge Reigns Supreme
There are valid reasons for a sort method to be static, but you need to know why you are using static.Jon Avadis wrote:
Campbell Ritchie wrote:Why is your array list static? Why are all your fields static?
I gotta admit im still confused when to use static and when not to.
Yes, there is an option for that. I think you should write code by hand to check for your code style.
Don't declare a List as ArrayList<T> but as List<T>.
Ill have to look into that.
Use spaces for indenting, not tabs.
Eclipse does that for me, i'll have to check if it can be changed in options/preferences
You should either create an object of your class which encapsulates the List to sort, or decide why having all your methods static is a valid decision (as I said earlier).
Why are you not creating an object and calling methods on that?
Why has your class got no constructor?
I guess thats the reason for all my static stuff. Im not thinking in objects enough yet.
I shall try and rewrite it this way
I often find when helping beginners that they include redundant code; I can often delete whole lines because they don't actually do anything..
Indeed, i shall change that. I even had an additional "counter" variable before and was happy to get rid of that, but your right,Why do you have an elements field when you could use the size() method of the List instead?
i dont need "elements".
Don't know. You can go to your Java™ installation folder and unzip the src.zip file and see the code for Scanner.
I thought buffered Reading was the preferred way to go. Wont the Scanner(File) constructer use an unbuffered Filereader toWhy are you passing a BufferedReader to the Scanner constructor rather than a File object?
read from the file?
Why are you altering the delimiter on the Scanner rather than using its nextInt() method?
That's what System.err is there for, but many people forget about it altogether.Why are you using system.out rather than System.err for an Exception message?
A bad habit of using system.out for everything i guess, will change that too.
But you asked about style and part of style means following the conventions. If you ask somebody to listen to a complicated explanation and introduce a grammatical error, many people will notice the grammatical error and miss the thread of the argument. If you ask somebody to check your code for errors and introcude an indenting error, they will notice the indenting error and may miss a more serious error in the code. Also consistent indentation makes it easier to find errors cause by mis-matched {} pairs.
Why have you got inconsistent indentation, with some lines containing {...}?
If i have for example a short "if" or "catch" blocks i can fit easily on one line, i tend to do it, because it look cleaner to me.
I understand its against conventions though.
What I am least happy about is trying to do everything in a static context rather than creating an object which does its own loading and printing (at least).
I am also not happy about the return in the middle of the recursive method, but many people will disagree with me on that.
I did, and found this, which looks promising.I earlier wrote: . . . look up "program to the interface, not the implementation" . . .
Campbell Ritchie wrote:What you should do for sorting Lists is to dump the entire contents of the List into a Comparable[] array (Integer implements Comparable<Integer>, so that's all right, otherwise you would supply a Comparator), sort the array and re-create the List from the array. There is an explanation for that peculiar procedure here.
Knowledge Reigns Supreme
knowledge is the difference between drudgery and strategic action -- tiny ad
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|