aspose file tools*
The moose likes Java in General and the fly likes Getting rid of redundancy? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

Getting rid of redundancy?

Ashley Kin
Ranch Hand

Joined: Oct 18, 2011
Posts: 36
I'm trying to get rid of redundancy in my program, I know I can take out the System.out.println("What is the y coordinate?) but not sure what else to combine.
Any help is appreciated, thanks!
I posted this in the Beginners forum, but no one was answering, so I thought I'd try it out here!


Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
To get rid of redundancy, look for the things that are redundant and fold them into single structures.

Your code repeats several things: for instance, you prompt for a coordinate, then get a coordinate from the console in three places. You can have a method that takes a parameter so that it can name the coordinate (x or y) that is to be gotten, prompts for it, gets the input, and returns the input value.

One of your repeats -- getting y from the console in one place if x is negative and another if it is not -- is entirely there because you prompt for the input separately based on the value of x. But you don't need to do that; you are going to prompt for y regardless of x's value, so you can prompt for and get x, prompt for and get y (in one place), and then print out and return the appropriate values after you have gotten the input.

You always return "x + y + negatives" -- so your testing code can test x and y to figure out what the value of "negatives" is to be, and then just have the return and the calculation done once.



There are one or two other things you can do to the code, but this removes redundancies.

rc
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Please do not post the same question more than once.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
Don't get me started about those stupid light bulbs.
 
subject: Getting rid of redundancy?
 
Similar Threads
Exceptons
How should I split up this big method? Still need help please!
Trying to get rid of redundancy?
How to return value back to main?
cleaner code