• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code Optimizatoin

 
Arun vel
Greenhorn
Posts: 15
Java MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to optimize this code..
Can anyone please help..


Please suggest me..
 
Tim Moores
Saloon Keeper
Posts: 3266
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Optimize for what?

(I doubt anyone will go through 360 lines of code and make suggestions, by the way, but you never know.)
 
Arun vel
Greenhorn
Posts: 15
Java MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By this code I am doing the same function for two types of records..For that I have repeated the same logic twice in two classes..Other than the numner of parameters passed,there is no other difference between those two classes..
I want to know how use the same funcetionality but with different parameters..
Thus the code will be optimized..
If you see the code correctly,you will find that the same logic is repeated twice in two classes..
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun kumar.ck wrote:By this code I am doing the same function for two types of records..For that I have repeated the same logic twice in two classes..Other than the numner of parameters passed,there is no other difference between those two classes..
I want to know how use the same funcetionality but with different parameters..
Thus the code will be optimized..
If you see the code correctly,you will find that the same logic is repeated twice in two classes..


So what specific problem are you having doing that?

Again, that's a lot of code to ask anyone to read. I'm certainly not going to read it. Perhaps if you could condense it down to just enough code to show the relevant bits that are giving you trouble...
 
Arun vel
Greenhorn
Posts: 15
Java MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay..Now I wil explain what I need to know..
I have created two classes(Thread class) to read two types of records(Textfiles-Both have different number of values)..
Both classes does same process but with two type of files(Textfiles-Both have different number of values)..Both class looks same as one..Both class uses same insertion method to insert the textfile data into the database..
Now I want to know,Instead of doing this can we create only one class(Thread class) to do both the functions..
I cant condense it because there is nothing rather than the same class is repeated twice..
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun kumar.ck wrote:I have created two classes(Thread class) to read two types of records(Textfiles-Both have different number of values)..
Both classes does same process but with two type of files(Textfiles-Both have different number of values)..Both class looks same as one..Both class uses same insertion method to insert the textfile data into the database..


Okay, so both classes are the same. So get rid of one class and just use the remaining one for both tasks.

Also, you shouldn't extend Thread. You should implement Runnable instead.

I cant condense it because there is nothing rather than the same class is repeated twice..


I guarantee you it's possible to condense it.
 
Winston Gutkowski
Bartender
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun kumar.ck wrote:Now I want to know,Instead of doing this can we create only one class(Thread class) to do both the functions.

Yes. The question is: do you want to?

Personally, I think I'd be creating classes (Student? Exercise?) for the objects that you want to read in, and put the code for actually reading them in there.

I cant condense it because there is nothing rather than the same class is repeated twice..

As Jeff said...

Winston
 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Also, you shouldn't extend Thread. You should implement Runnable instead.

Why is that better? The only advantage I see with the "implements" approach is that you can implement multiple interface, but extend from only 1 class. Are there any other benefits over the "implements" approach?
 
Tim Moores
Saloon Keeper
Posts: 3266
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See http://www.coderanch.com/how-to/java/ExtendingThreadVsImplementingRunnable for some discussion. Using Callable or Future may be even better, depending on what you're trying to achieve.
 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:See http://www.coderanch.com/how-to/java/ExtendingThreadVsImplementingRunnable for some discussion. Using Callable or Future may be even better, depending on what you're trying to achieve.

Thanks; that makes sense. To be honest I've never used Executor and Callable before but it's definitely something to look into next opportunity.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Koen Aerts wrote:
Jeff Verdegan wrote:Also, you shouldn't extend Thread. You should implement Runnable instead.

Why is that better?


Because it more properly reflects your design. We're not creating a specialized type of Thread; we're creating a task to be run in its own thread.

Granted, it is convenient for anonymous inner classes to be able to skip the Runnable layer, and I'm guilty of doing it, but I still consider it bad design that Thread even implements Runnable in the first place.
 
Arun vel
Greenhorn
Posts: 15
Java MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all for the suggestions..I have done that..
The problem with me was I know the way to do,but dont know how o do..
Now my application is working fine..
Also I am not that much explainatory..So only I am not able to explain myself..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!