This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Cutting down 1394 lines of code. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Cutting down 1394 lines of code." Watch "Cutting down 1394 lines of code." New topic

Cutting down 1394 lines of code.

joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
hello everybody.

Im quite a beginner at java, and in my program i have been doing alot of cutting and pasting (i.e. repeating lines of code). The current number of lines of code for an alpha alpha version is 1394 and its probably down to me not understanding java very well.
Ok, i have an array of 100 textboxes. i have a JButton that when pressed gets a random number between one and ten. This random number then effectively runs alot of code, and dictates the textbox starting location. The purpose of the code is to change colors.
As such, i have had to retype out all the code from (random number = 1 ... random number = 10). So rather that retype all this code i want to type it out once and then the program will reference back to it when needed. I believe that this is dont through Classes. To my understanding, if object A and object B both have identical "parts" (eg values, or calculations), you use a class, and refer back to that class rather than retype everything. I am not very proficient at doing this however and would like help.

Do i (A) create a class within the current code so its only typed once, and refer back to it
(B) create a new class file that sits in the same folder as the application and refer back to it

As there is so much code, it isnt really applicable to post it all but ill try to post current structure for you.

if (RandomNumber == 1)
Do taskA;
if (RandomNumber == 1)
Do taskB;
if (RandomNumber == 1)
Do taskC;

if (RandomNumber == 2)
Do taskA;
if (RandomNumber == 2)
Do taskB;
if (RandomNumber == 2)
Do taskC;

Using the above example do i create Classes "taskA" "taskB" "taskC" and initate them when a RandomNumber is identified? if this is the correct way, would it be recoded as follows?

if (RandomNumber == 1)
if (RandomNumber == 1)
if (RandomNumber == 1)

if (RandomNumber == 2)
if (RandomNumber == 2)
if (RandomNumber == 2)

If im not on the right track please guide me.
Thankyou in advance,

Theodore Casser
Ranch Hand

Joined: Mar 14, 2001
Posts: 1902

I think to answer your question, if what you're looking for is altering similar objects in similar ways, you might be better suited into making a method in your class (rather than an entirely new class) for each kind of change, and having it take the object that needs to be modified/called as a parameter. So, if you're trying to change a JTextArea to a certain color (for example), you might call it by:

taskA(JTextArea, Color);

Which would make it quick and easy and extensible (allows for addition of more text areas and colors, for instance).

Also, assuming that the structure is how you're showing it (three conditionals in a row for each state that the random number could be in), I'd start by suggesting using code-blocks instead of multiple conditionals, as it'll simplify your code:

(where, obviously, the ... is the rest of the states the RandomNumber can be in.)

That should immediately reduce the lines of code. Another idea might be to use a switch statement:

Either one should help you, if I'm understanding what you're asking. (Who knows, I could be totally off-base.)

(Modified to clarify, after a coworker pointed out I wasn't entirely clear on what I meant.)
[ February 23, 2005: Message edited by: Theodore Casser ]

Theodore Jonathan Casser
SCJP/SCSNI/SCBCD/SCWCD/SCDJWS/SCMAD/SCEA/MCTS/MCPD... and so many more letters than you can shake a stick at!
joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
hello Theodore,

yes you have been a help - showing how i could restructure my code to cut out alot of lines. However, as my program stands, "taskA" "taskB" etc are 57 lines each, which are repeated for each random number.


if (RandomNumber == 1)
Do taskA for JTextAreaA; //the word "taskA" represents 57 lines being typed out

if (RandomNumber == 2)
Do taskA for JTextAreaB; //as you can see i have to retype out the SAME 57 lines

To cut out the duplication of lines, do i create a class called "taskA" which contains those 57 lines, then use (as you suggested)


Do i create a new file for this class or do i include it in the main code?

thanks in advance, joseph
[ February 23, 2005: Message edited by: joseph mcgratton ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If you can pull those 57 lines out and use them twice you can just make a new method in the same class.

It may not be quite that simple. If there are more differences between the two blocks or if other local variables are modified you may have to add parameters or rearrange the code a bit more.

BTW: Do you use Eclipse by any chance?
[ February 23, 2005: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
The key principle that you're practicing here is making your code generic. Where you have 57 lines that operate on textArea1 and then the same 57 lines operating exactly the same way on textArea2, you can put them all into a method that takes a JTextArea as the others have already said.

Once you have that done, I'd say the next good spot to genericize would be the 10 random number cases. Let's say that the above simplifies your code to this:You can replace the 10 explicit if blocks using an array of JTextAreas.Anywhere you find that you're performing the same tasks where only the objects on which you operate change you have a great opportunity to simplify your code.

By the way, what do you have against FireWire?
Don't get me started about those stupid light bulbs.
subject: Cutting down 1394 lines of code.
Similar Threads
How to run tasks with priority?
cant get counters to work correctly
How can I serialize thread handling with Java 5's java.util.concurrent package?
guessing game
randomly picking numbers