Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Where to split methods (Say 4b)

 
Dean Funk
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lessee if I can get my point across without a spoiler...
I'm trying to decide where to split up a process into methods.
I have a Long.
I want to split it into three-digit chunks, then further process the chunks.
Does it make more sense to split up the Long in main(), then pass the chunks to a processor method,
or to pass the Long to some method, split it up, process it in that method or pass the chunks to some tertiary method?
I can argue it to myself each way, please somebody weigh in.
tanks
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would separate processes into methods. Personally, I put processing in main only when it's some simple output or very basic logic needed (ie testing to make sure an input is within the scope of the program). Otherwise, I group related processes in a method and return values or call other methods as needed. Figure out what processes you are going to need, divide those into logical groupings, and design your methods around those.
Just to give you an idea, I had a total of 4 methods in my 4b code.
Hope this helps,
Jason
[This message has been edited by jason adam (edited August 08, 2001).]
 
Amber Woods
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I sent the long to a method to be processed. Like Jason, I also ended up with 4 methods besides main to do all of the processing. Take Jason's advice on this he seems to be a wiz when it comes to the Assignments
Best of Luck to you Dean
[This message has been edited by Amber Woods (edited August 08, 2001).]
 
Dean Funk
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What made y'all decide on passing a Long?
I was starting to gravitate toward handling the thing as a String, because it doesn't stay Long very long.
 
Greg Harris
Ranch Hand
Posts: 1012
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i ended up with 5 methods. i converted the string to a long in the first method and dealt with the math from then on using other containers for each segment... my long did not stay long after the first method.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dealt with a number instead of a String through most of the program just because that is the way I think. If we're talking about numbers, it was easier for me to write code that dealt with the number pattern versus how to break apart the String, but it can be done relatively similar either way. I guess it all depends on how you view things.
I actually had 3 extra methods, and then main(). main() just checked the range of the input, and then sent the number on for processing. I'd probably be giving away too much if I even hinted at what the others did
Jason
 
Dean Funk
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awww c'mon...
Thanks, everybody.
DF
 
Terence Doyle
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
I do a little processing in the main() which I supppose could be an excuse for a separate method - though personally I think its OK.
The rest then is done by just one method...
Course I haven't been nitpicked yet...
I'm sure I'll be posting back with a different structure once I get on to sending it in.

Bye,
Terry
 
Adam Vinueza
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dean,
For what it's worth, here's my own 2 cents.
The rough guide I've been using is that if I'm repeating code more than I need to, I use a separate method; otherwise, I use main(). Since I'd break the argument into three-digit chunks only once, and doing it is not essential to some other procedure demanding a separate method, I'd favor doing it in main().
As for String vs. long: I don't see any special advantage to using String, because it seems to me that, in the case of Say, to break a String up into three-digit chunks is more trouble than it's worth. Say has to be able to either add chars to the front of the String to make it divisible by 3 w/o remainder, or take a less-than-three-digit chunk. Also, the chunks have to be converted to ints separately. None of this is necessary if long is used instead of String.
--Adam
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic