File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Programming Techniques 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 » Beginning Java
Bookmark "Programming Techniques" Watch "Programming Techniques" New topic

Programming Techniques

Rajesh Kumar

Joined: Nov 16, 2001
Posts: 28
Hi Friends
I am new to Java
My java class file size is 15 k now.
Is there anyway to reduce the size of class file.
I heard that the efficient way of doing programming is doing program which generates less size of class name.
is there anybody knows how to do the programming which will creates the less size class name ?
please give me some programming techniques.
Thanks in advance
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Unless you really have a need, don't spend time worrying about the sizes of your classes. Worry about writing code that makes sense.

Please ignore post, I have no idea what I am talking about.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Well tinkering with the size of a classfile is really just optimization, much the same as trying to improve performance. So I thought that I would share with you some thoughts on the topic.
From "Effective Java" by Joshua Bloch (quotes that he took from elsewhere) Item 37 (page 162)

More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity.
-William A. Wulf

We should forget about small efficiencies, say about 97% of the time; premature optimization is the root of all evil.
-Donald E. Knuth

We follow two rules in the matter of optimization:
Rule 1. Don't do it.
Rule 2 (for experts only) Don't do it yet-that is, not until you have a perfectly clear and unoptimized solution.

Strive to write good programs rather than fast ones.
Strive to avoid design decisions that limit performance.
-Joshua Bloch

So the advice is to forget about the footprint of your class and the speed of the code until the code is bug-free. Then get a refactoring book and start worrying about such things.

[This message has been edited by Cindy Glass (edited December 19, 2001).]

"JavaRanch, where the deer and the Certified play" - David O'Meara
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286

I have to disagree with Cindy on this. Sorry Cindy. Of course, you are a better source for knowledge on JAVA than I am. But I look at this situation from a different point of view.
Forget about the language. Optimization is something that programmers have ignored since memory and CPU speeds began to grow. "Oh, we are only utilizing 16 of the 64 bits of a CPU running at 1.3 GigHz. Who cares if it is optimized. It will still run fast." Bah I say!!
Can you imagine going back to a full blown app and try to optimize it then?? How much code would you realize that you had to re-write?
I think that optimization and speed should be at least in the top 5 programming stages. (Yes, even in the JAVA world)
Just think how much better Windows could be if Microsoft worried about such things.
Just my opinion.

GenRocket - A Test Data Generation Platform
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521

And a valid opinion it is too.
Note that I was just quoting Joshua.
While he has a very valid point, I must admit that we have code reviews that look at performance and efficiencies - even on an original write.
However admitting that makes for much fewer fireworks .
That said, I am not sure that just decreasing the size of a classfile is going to make an application more efficient. A typical method of doing this is to split that large class into smaller classes. While that is probably a good idea from an OO point of view, it could be LESS efficient, it sort of depends on the code.
Junilu Lacar

Joined: Feb 26, 2001
Posts: 4419

The thing with performance tuning is that focus is often misplaced. Use a profiler to identify the bottlenecks in your application. That takes a lot of the guesswork and "gut feel" out of the process. Then decide which bottleneck, if any, is worth optimizing.
Is it really worth having 4 reviewers spend 15 minutes (for a total of 1 hour of costly development time) arguing over whether a for loop is more efficient that a while loop? This example may be extreme but the point is, optimize only when it is worth the effort.
Good programs are those that are: clear, simple, well-structured. Get the form right and speed will follow. For the majority of the work we do, this is as optimal as we should strive for.
My $0.02

Junilu - [How to Ask Questions] [How to Answer Questions]
Reuben Cleetus
Ranch Hand

Joined: Jul 13, 2001
Posts: 50
I suppose what can be best said for Optimization, is that it is something that ought not to take precedence over the primary goals in Software Engineering -- create an app that satisfies user requirements, AND which is easy to maintain.
Readability and maintainability are therefore a very high priority. I think programmers in general should err on the side of readability, unless speed is absolutely mission-critical. I cannot think of any such application that I have been involved in, and frankly, I don't think there are many cases when speed absolutely is THE most important issue.
Surely device drivers and other low-level system programming can justifiably promote speed very high up on the list of priorities, but elsewhere, it should be balanced with maintainability.
Reuben Cleetus.
Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
Just an addition to what said above. In my opinion, readability is the most important aspect of coding, besides meeting the specs. To anyone who is new reading this--there is almost nothing easier to maintain than well commented code. Self-commenting code along with well placed block and inline comments can save much and many headaches.

Jason R. Kretzer
Software Engineer

Jason R. Kretzer<br />Software Engineer<br />System Administrator<br /><a href="" target="_blank" rel="nofollow"></a>
I agree. Here's the link:
subject: Programming Techniques
Similar Threads
java socket programming
Question on how to run an application....
Parsingof XML taking time
Deploymen Descriptor (web.xml) file
Why Java at all?