Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Very good site for Best Practices to be followed  RSS feed

 
VP Jain
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I found this site
web page

Where it discusses about best practices for Collections,Strings,Exceptions,
Serialization,Loops,Objects and lot more.

Hope this will be useful for those who are preparing for the exam
 
saiprasad raut
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds good.

thanks
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for sharing this great site!!!

Joyce
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ummmm.... kids? Friends don't let friends take bad advice. I just checked several pages on that site, and although it looks very professionally done, the content is for the most part, seriously, garbage -- absolute garbage. The "performance tips" are simply all incorrect, and the "best practices" are mostly just bad personal opinions. Do yourself a favor and don't take anything you read there too seriously -- at least try to confirm the information agrees with some other source.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For performance links I like

http://www.javaperformancetuning.com - Run by Jack Shirazi author of O'Reilly's Java perfomance tuning book

I briefly looked at the following and it looked good:
http://www.javamug.org/mainpages/J2EE.html
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some open source profiling tools

http://www.manageability.org/blog/stuff/open-source-profilers-for-java
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ummmm.... kids? Friends don't let friends take bad advice.

I believe it is out of goodwill when someone wants to share a site which he/she thinks is good. Thru' sharing, one can learn his/her mistakes from other people.

The "performance tips" are simply all incorrect, and the "best practices" are mostly just bad personal opinions. Do yourself a favor and don't take anything you read there too seriously -- at least try to confirm the information agrees with some other source.


Ernest, thanks for the cautionary advice. At that site, I only read the article on "String" and found that the performance tips given were reasonable.

Could you please share with us which "performance tips" and "best practices" are incorrect so that we can avoid it? Thanks!

Joyce
[ May 16, 2004: Message edited by: Joyce Lee ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a funny comparision of exceptions inside or outside of loops.
precise java :)
The benchmark shows: 50 millisec. inside loop, 0 millisec. outside the loop.
The loop shall iterate over 25000 elements, and it does in the first block.
But in the second block the block is left immeadiately, because the first iteration fires the exception, and the block is left.
It's not too astonishing, that this behaviour is faster.

But even, if there was a big performance-benefit, when only the last iteration fired an exception, - you may not do much errorhandling outside the loop.
The whole idea is .

Another adivce I found was this: (loops

In the second line the test on 'a.length' is only performed once, while in the first line 'a.length'-times, which seems to be too much work, if the length of 'a' doesn't change in the loop.
It makes no measurable difference on my 1.4.2_02 -linux-jdk, (which is, to be fair, announced). But it is allways about 20% faster than the suggested solution

And they use

while a[i] was =0 before, which looks like a secret knowledge of another performance issue. But
is again about 20% faster on my machine.

It's really a pity, because they made well looking pages!
I would engage them in Documentation.

[ May 16, 2004: Message edited by: Stefan Wagner ]
[ May 16, 2004: Message edited by: Stefan Wagner ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Wagner:
There is a funny comparision of exceptions inside or outside of loops.


That was the first page I looked at, and virtually all of it is just junk. The loop thing is very funny because, as you point out, the two versions of the code actually do different things -- it's not really fair to do a performance comparison if the semantics are different. If the try/catch is outside the loop, then the whole operation fails if any of the strings aren't parseable; if it's inside the loop, then all the parseable strings are processed, and the others ignored. Both approaches are perfectly valid, but it depends on what you want the code to do, and not on performance considerations.

When I get a minute I'll go collect some more examples.
 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not clear errors, but some other questionable advice:

"Whenever you are done with an object make that reference null so that it is eligible for garbage collection."

Are you really going to choke without that one object's few bytes of memory before the variable goes out of scope at the end of the block or the end of the function? What about the memory used up by, depending on the compiler, all those extra assignment statements making the references null? And what was the reason for garbage collection again?

"Whenever possible avoid using class variables, use local� variables since accessing local variables is faster than accessing class variables."

In this case, the advice may be good - use local variables unless you actually need the persistence of a member variable - but the reasoning is questionable. Local variables are not necessarily faster, and efficiency shouldn't be the primary reason for scoping decisions, anyway.

These examples gave me the impression that the page perhaps listed a bunch of naive microoptimizations, while missing the big picture on where one can make really big performance gains. But to be fair, I checked out the section on collections, since that's an area where picking the right class for one's needs actually can result in major performance benefits. I found a comparison of ArrayList, Vector, and LinkedList:

"Use ArrayList with proper initialization if you don't want thread safe for the collection whenever you� add/remove/access objects at end and middle of collection."

Now this was somewhat intriguing, since it's linked lists that are generally considered theoretically more efficient when one does a lot of additions and removals in the middle of the list. So I looked at the test code ... it turns out that it tests "additions in the middle" by adding items at a fixed index position, rather than in a particular position relative to other objects in the list. Essentially, it's testing the collection on how good an array it is, not how good a list it is. Not surprising that the ArrayList comes out ahead on this array oriented test - but the page completely misses the point of what linked lists are actually good for!

I come away with the general impression that the site is full of simplistic "optimizations" that won't do much good, and may do some harm by distracting people from coding clearly.

Sometimes I think this forum needs as a subtitle, the quote from Knuth that "premature optimization is the root of all evil."
[ May 16, 2004: Message edited by: Warren Dew ]
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stefan, Ernest and Warren for the inputs.

Stefan wrote:
The benchmark shows: 50 millisec. inside loop, 0 millisec. outside the loop.
The loop shall iterate over 25000 elements, and it does in the first block.
But in the second block the block is left immeadiately, because the first iteration fires the exception, and the block is left.
It's not too astonishing, that this behaviour is faster.


I agree with you. It's not fair to compare this way. So I modified the code such that no exception was thrown at all. The execution times for both approaches were about the same which is normal with JIT turned on. I had to run the code a few rounds before making conclusion. That's because sometimes loop1 was faster and sometimes loop2 was faster. The code was tested on win98, p3.

In the book "Practical Java" by Peter Haggar, pg 81, praxis 23, it says "Place try/catch blocks outside of loops". With the JIT compiler turned on, no differences between the execution times for both approaches. But with the JIT compiler turned off, "outside loop" approach is faster.

So I turned off the JIT compiler, still, the execution times for both approaches were about the same.

In the second line the test on 'a.length' is only performed once, while in the first line 'a.length'-times, which seems to be too much work, if the length of 'a' doesn't change in the loop.
It makes no measurable difference on my 1.4.2_02 -linux-jdk, (which is, to be fair, announced). But it is allways about 20% faster than the suggested solution.


Hmmm...there's a performance difference on my PC. Also, "a.length" didn't seem to incur extra overhead as compared to a constant number.



Warren wrote:

"Whenever you are done with an object make that reference null so that it is eligible for garbage collection."

Are you really going to choke without that one object's few bytes of memory before the variable goes out of scope at the end of the block or the end of the function? What about the memory used up by, depending on the compiler, all those extra assignment statements making the references null? And what was the reason for garbage collection again?


Once again, you can see this praxis 7 in Java Practical, pg 18. It says " Set object references to null when they are no longer needed." This is helpful if your program is running in a memory-constrained environment. The objects which are being referenced are still within the scope but no longer in need.


"Whenever possible avoid using class variables, use local variables since accessing local variables is faster than accessing class variables."

In this case, the advice may be good - use local variables unless you actually need the persistence of a member variable - but the reasoning is questionable. Local variables are not necessarily faster, and efficiency shouldn't be the primary reason for scoping decisions, anyway.


Once again, you can see this praxis 35 in Practical Java, pg 122. It says "Use stack variables whenever possible".

I did a small test with the following code. The stack was the fastest and the static class was the slowest. I set the val to 200000000.



"Use ArrayList with proper initialization if you don't want thread safe for the collection whenever you add/remove/access objects at end and middle of collection."


I agree with you. LinkedList collection is more appropriate for insertion/deletion.

Ok, I'm not trying to promote the book "Practical Java" by Peter Haggar. But some of the tips you raised in PreciseJava.com are in the book "Practical Java". You can see the review at JavaRanch's Bunkhouse.

I also use another book "Effective Java Programming Language Guide" by Joshua Bloch for references.

If you've any good sites on Java Best Practices, could you please share with us? Thanks.

Joyce

PS: After modifying the message, certain part of the message disappeared. I must rest now, I've high fever.
[ May 17, 2004: Message edited by: Joyce Lee ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joyce: Yes, I encountered the vanished text phenomen too, and was told, that it sometimes happens, when editing posts with mixtures of code and quote, and that the only suggestion is: avoid mixing code and quote.

The whole idea of inner- or outer try-catch-blocks is somewhat braindamaged.
An outer try-catch block will allways exit your loop - so what do you want to catch outside?
My performance-advice: Put System.exit (0); or 'return;' as first statement in your main-Method, and you gain unexpected performance profits!

To cite 'Practical Java' is no help.
It's not my bible, but thanks for warning - I will keep away from it.

Object-nulling is a style I saw a few times, but never saw an example, where it made sense. I believe it's an imported style from another language, where it might make sense. I believe in scope too - greetings to Warren.

I got a book: "Performant Java programmieren, Hendrik Schreiber, Addison Wesley, (c) 2002" - I don't know whether it is available in english too. I didn't find such mistakes in it.
I enjoyed Joshuas Book too.

Choosing a good design, appropriate data-structures and the best algorithm is much more important, than learning suspicious rules, which are out of date with the next jvm.
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joyce: Yes, I encountered the vanished text phenomen too, and was told, that it sometimes happens, when editing posts with mixtures of code and quote, and that the only suggestion is: avoid mixing code and quote.

Thanks.

The whole idea of inner- or outer try-catch-blocks is somewhat braindamaged.
An outer try-catch block will allways exit your loop - so what do you want to catch outside?

Yes, just like Ernest said, "it depends on what you want the code to do, and not on performance considerations."


To cite 'Practical Java' is no help.
It's not my bible, but thanks for warning - I will keep away from it.

Actually, I didn't cite "Practical Java" to support those praxis. I just want people to aware that these tips could be found in that book too. And this book happened to receive good reviews in JavaRanch Bunkhouse and amazon.com.

Choosing a good design, appropriate data-structures and the best algorithm is much more important, than learning suspicious rules, which are out of date with the next jvm.


I agree. In fact, "Practical Java" emphasizes on design, data structures and algorithm. It has a section for performance tuning. Whereas "Effective Java" does not focus much on performance.

Joyce
 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joyce Lee:

Once again, you can see this praxis 7 in Java Practical, pg 18. It says " Set object references to null when they are no longer needed." This is helpful if your program is running in a memory-constrained environment. The objects which are being referenced are still within the scope but no longer in need.

I guess I'll stay away from Practical Java, then. In my opinion, if the objects are no longer needed, they should no longer be in scope, at least not for any extended period of time. If this becomes a problem in a real program, the solution in my opinion is to decompose the functions that are excessively lengthy, rather than to further exacerbate what is likely spaghetti code by keeping unnecessary null references in scope.

If you've any good sites on Java Best Practices, could you please share with us? Thanks.

I haven't personally noticed any good sites or books primarily geared towards these issues. I think that's because the reasons behind best practices tend not to be simple, and as a result, really good 'best practices' don't tend to be in the form of simple rules that are easily memorized.

That said, I would recommend reading Chapter 2 of Stroustrup's The C++ Programming Language. While that chapter claims to be C++ specific, it's actually a really good historical summary of various programming paradigms that led to languages like Java as well as C++, and helps one understand the issues involved. The rest of the book also contains many notes on practices that are as applicable to Java as C++, but as they are scattered through 800+ pages of C++ specific stuff, I don't necessarily recommend anything beyond Chapter 2 to Java programmers.

PS: After modifying the message, certain part of the message disappeared.

Yes, I've had a similar problem when trying to quote some messages when responding. I've quit using the quote codes as a result.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree in recommending Stroustrup even for Java-Developers, but would recommend anything beyond Chapter 2 to Java programmers (referring to the second edition, german translation, which might differ when translating back):
9. Exceptionhandling
11. Program development
12. Design and C++
13. Library Design
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!