This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Which would be more efficient and time saving? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Which would be more efficient and time saving?" Watch "Which would be more efficient and time saving?" New topic
Author

Which would be more efficient and time saving?

Joe Davis
Greenhorn

Joined: Apr 02, 2012
Posts: 10
Hello, I've recently wrote a bankAccount class that is pretty basic but we all start somewhere and I originally threw everything together in the main method but now I've distributed everything into their own seperate methods. Is this better?

First code - Main method driven.



Or my second attempt which is using methods.



Thanks, any help is appreciated!
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4446
    
    5

It's a step in the right direction.

If I have to write a class that had a public static void main method as the program's entry point, I normally name it something like FooApplication or FooMain. In your case, I'd probably call it AtmApplication or AtmMain to indicate that it's the class that needs to be run first. Then I'd move most of the code that has to do with the account out to a separate class, oh say, Account.

So AtmMain becomes a sort of traffic cop (the "Controller") and all it does (its responsibilities) is present a menu of actions to choose from, get the user's choice, asks (delegates to) Account to do the heavy lifting before presenting the results to the user as a meaningful message. Note: the phrases in parentheses in the previous sentence are the more formal terms used in object-oriented parlance.


Junilu - [How to Ask Questions] [How to Answer Questions]
Joe Davis
Greenhorn

Joined: Apr 02, 2012
Posts: 10
Junilu Lacar wrote:It's a step in the right direction.

If I have to write a class that had a public static void main method as the program's entry point, I normally name it something like FooApplication or FooMain. In your case, I'd probably call it AtmApplication or AtmMain to indicate that it's the class that needs to be run first. Then I'd move most of the code that has to do with the account out to a separate class, oh say, Account.

So AtmMain becomes a sort of traffic cop (the "Controller") and all it does (its responsibilities) is present a menu of actions to choose from, get the user's choice, asks (delegates to) Account to do the heavy lifting before presenting the results to the user as a meaningful message. Note: the phrases in parentheses in the previous sentence are the more formal terms used in object-oriented parlance.


Thank you for replying.

Ah I understand; I was going to try split all the code up and move everything pretty much into relevant separate classes and like you said; use the main method or AtmMain as the controller which will only show the ATM menu.

In the first example of code I posted; if everything is jam packed into the main method on one class, is this bad programming style or is it just less efficient and harder to read/understand?

Much appreciated.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4446
    
    5

Oh yeah, and to answer your question regarding efficiency and time savings: what kind of efficiency and time saving are you looking for? There's a quote I like to cite:
Donald Knuth, preeminent computer scientist, wrote:"Premature optimization is the root of all evil."

If you mean efficiency and time savings in terms of computer cycles, then in a program as small as this, any differences coming from the way it's coded will be practically insignificant.

However, the more important and more costly consideration is ease of understanding, maintaining, and enhancing the code. That makes up the biggest chunk of cost in developing software. The whole point of using an object-oriented language, or any higher level language in fact, is to clarify concepts and relationships between information and actions taken on it. If you have one big glob of code in one class, it will be much more difficult to understand, maintain, and enhance in the long run than if you had responsibilities separated as I described in my previous reply.
Joe Davis
Greenhorn

Joined: Apr 02, 2012
Posts: 10
Junilu Lacar wrote:Oh yeah, and to answer your question regarding efficiency and time savings: what kind of efficiency and time saving are you looking for? There's a quote I like to cite:
Donald Knuth, preeminent computer scientist, wrote:"Premature optimization is the root of all evil."

If you mean efficiency and time savings in terms of computer cycles, then in a program as small as this, any differences coming from the way it's coded will be practically insignificant.

However, the more important and more costly consideration is ease of understanding, maintaining, and enhancing the code. That makes up the biggest chunk of cost in developing software. The whole point of using an object-oriented language, or any higher level language in fact, is to clarify concepts and relationships between information and actions taken on it. If you have one big glob of code in one class, it will be much more difficult to understand, maintain, and enhance in the long run than if you had responsibilities separated as I described in my previous reply.


Thanks for clearing that up for me. I was just curious as for when I develop more complicated programs, I don't fall into any bad habits that would use more resources then necessary.
I always try to layout my code best so it's easy to read/understand and like you said enhance upon the code wherever I need to without any effort searching one massive class for one little method.

Thanks again for the information, it's helped a huge amount.
 
 
subject: Which would be more efficient and time saving?
 
Similar Threads
Having troubles with a complex program, need help with an illegal start of type error.
Need help with username and password validation
Junit test cases
Suppressing proceeding statements after if statement fails
k & b , Ryan and Monica problem......