[QUESTION] Java: Help with Optimizing Code
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [QUESTION] Java: Help with Optimizing Code

  1. #1
    Join Date
    Jul 2010
    Posts
    2

    Exclamation [QUESTION] Java: Help with Optimizing Code

    Hi. I am a student, and I have an report to do for our Computer Programming Fundamentals class.

    We are required to demonstrate the use and function of constructors and the JOptionPane class.

    I have implemented a simple age calculator using Java as my demo, and I intend to go through the code to show them what each line does, however I'd like to ask the forumers here for advice on optimizing my code so that it is:

    a) smaller
    b) simpler
    c) well-documented (prefer this)
    d) faster (prefer this over size)

    I used three classes:

    a Main class (http://pastebin.com/BBT0pkWt);
    a UserInput class to gather user input (http://pastebin.com/94KRwgDi);
    a ProgramOutput class to display the output of the program (http://pastebin.com/DYGLsNqG); and
    a GlobalVariables class to contain public static variables to simulate "global" variables (http://pastebin.com/DCh71xu8).

    Please check my code, and suggest improvements.

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: [QUESTION] Java: Help with Optimizing Code

    Quote Originally Posted by Sachiru View Post
    I'd like to ask the forumers here for advice on optimizing my code so that it is:

    a) smaller
    b) simpler
    c) well-documented (prefer this)
    d) faster (prefer this over size)
    a. Smaller how? fewer lines of code? fewer statements?

    d. The first question to ask about spending time on performance optimization is "Is it necessary?". If you don't have clear evidence of a performance problem, why waste your time and risk introducing bugs? If you do have clear evidence of a performance problem, you must establish what is causing it, and tackle that specific area. For this you should use a performance profiler that will time how long various methods take and indicate where the bottlenecks are. In general, going in 'blind' to try and optimize code is unlikely to have any significant benefit (unless you are particularly well experienced in that area).

    Premature optimization is the root of all evil in programming...
    C.A.R. Hoare
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Jul 2010
    Posts
    2

    Re: [QUESTION] Java: Help with Optimizing Code

    Quote Originally Posted by dlorde View Post
    a. Smaller how? fewer lines of code? fewer statements?

    d. The first question to ask about spending time on performance optimization is "Is it necessary?". If you don't have clear evidence of a performance problem, why waste your time and risk introducing bugs? If you do have clear evidence of a performance problem, you must establish what is causing it, and tackle that specific area. For this you should use a performance profiler that will time how long various methods take and indicate where the bottlenecks are. In general, going in 'blind' to try and optimize code is unlikely to have any significant benefit (unless you are particularly well experienced in that area).

    Premature optimization is the root of all evil in programming...
    C.A.R. Hoare
    Smaller as in fewer statements.

    Regarding optimization, I'd simply like to ask our forumers here about the code, if it has redundancies or dead code. Thus, by removing that bloat, I believe we can arrive at an elegant and simple solution.

  4. #4
    Join Date
    Mar 2006
    Posts
    54

    Re: [QUESTION] Java: Help with Optimizing Code

    Don't use global variables. It's a design decision in Java, not a weakness. Global variables are for people still using procedural languages and is counter-OOP.

    One improvement I see is to combine all of your getters in UserInput into a single "Calendar/Date/etc getDate()" method and then let that class worry about building a date object for you. You could then pass the returned date and the current date to ProgramOutput, and it would calculate the difference between them and print the result on the screen. This would eliminate any need for global variables too.

  5. #5
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: [QUESTION] Java: Help with Optimizing Code

    Quote Originally Posted by Sachiru View Post
    Smaller as in fewer statements.
    Why? Would you prefer one complex statement to three simple statements?

    Programs must be written for people to read, and only incidentally for machines to execute...
    H. Abelson and G. Sussman
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center