• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Which would be more efficient and time saving?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Joe Davis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic