• 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

Java Code Enhancement Needed

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Kindly need your expertise here to enhanced my code as below,
because my amateur Java level now is stopping me to improve this code.

Question:
1) Seems like i'm using a lot of dummy method. Any advanced/fancy way to recode it?
2) Please correct any mistake that causing performance issue in this code.

prerequisite: **Global Variable with the @Getter @Setter cannot be changed**


Thanks in advanced, your help is much appreciated.

 
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And where do you have a performance problem?
Where do you have any global variables? I didn't notice any, but you should not have global variables in the first place.
 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Opps, maybe I should use the word Instance variable. prerequisite:all the instance variable with @getter @setter method cannot be changed.

is it possible to modify the code to be more advanced? or in other word, more intelligent/fancy/complicated design pattetn. I have a strong feeling that my written code is newbie style. I'm in learning progress. please help.
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Making code "fancy" or "complicated" is not a desirable goal.

You question should be "Can this code be made more elegant and with improved clarity".

Code should not be fancy or complicated, it should be clear.
 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi bear bibeault
noted and thanks.
so my corrected question is..
Can this code be made more elegant and with improved clarity?
kindly need your advise. its out of my capability to improve this code. need your help.
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code is long and complicated. I'm not sure why the constraint that you need almost 100 instance variables and can't change that. it would be better to have some of those in objects and those objects be an instance variable in the main class. Or have some of those be maps of keys like "R02" to the percentage.

As far as clarity, let's take a look at this method. It looks like it returns a value based on compareTo(). I see a few things that could be better in this method:
1) How does it differ form just writing return before.compareTo(after)?
2) It's impossible to get to the line with return 2;
3) Why are you catching an exception? What exception are ypu expecting. And why return 3. What does that mean? And where do you check for that return code? Hint: you don't. It's better for the exception to just propogate if you aren't handling it so you don't have a bug later in the program.



The last method is also really complicated. The nesting logic is too deep. If nothing else, you could extract some of the logic to other methods. As a start, can you explain how it works in English?
 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeanne Boyarsky,

Thanks for your time. appreciate it sincerely.

1)

it would be better to have some of those in objects and those objects be an instance variable in the main class.

Oh, i never thought of it that way, noted & thanks. and i will amend it as per suggested. This really helps.

2)

Or have some of those be maps of keys like "R02" to the percentage.

I don't really understand what do you mean by this suggestion?

3)

How does it differ form just writing return before.compareTo(after)?

Oh ya, you are right. i will amend it as per suggested. This really helps.

4)

It's impossible to get to the line with return 2;

Oh ya, you are right again. Thanks a lot.

5)

It's better for the exception to just propogate if you aren't handling it

I don't really understand this, are you mean throw the exception to the calling class instead?


Thanks guys.
Will amend it and repost it again.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeansonne Pierre wrote: . . . I don't really understand this, are you mean throw the exception to the calling class instead?
. . .

Yes. but check carefully whether there are any exceptions at all. I do not think there is any possibility of comparing dates giving Exceptions, unless either of the dates is null.

Stop using Date and use the newer classes if you are on Java8.
 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell Ritchie, Jeanne Boyarsky, Bear Bibeault,

I have modified the code as per suggested. Looks much better than before. Thanks a lot.
Anymore improvement that i can make? because i still lack of some confident on my code especially the design pattern. Thanks.

 
Jeanne Boyarsky
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeansonne Pierre wrote:
2)

Or have some of those be maps of keys like "R02" to the percentage.

I don't really understand what do you mean by this suggestion?



It's a big refactoring, but have something like this instead of all those variables.



That would require changing your AchPercentageView class as well since it has the same setters.
 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot guys ! appreciate it so much
reply
    Bookmark Topic Watch Topic
  • New Topic