This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cutting down 1394 lines of code.

 
joseph mcgratton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
JTextareaA.taskA;
if (RandomNumber == 1)
JTextAreaA.taskB;
if (RandomNumber == 1)
JTextAreaA.taskC;

if (RandomNumber == 2)
JTextareaB.taskA;
if (RandomNumber == 2)
JTextAreaB.taskB;
if (RandomNumber == 2)
JTextAreaB.taskC;

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

Joseph.
 
Theodore Casser
Ranch Hand
Posts: 1902
Hibernate Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
joseph mcgratton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

ie

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)

taskA(JTextAreaA)

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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic