views:

442

answers:

10

Can anyone point out some java code which is considered "good"?

I have started programming recently, about two years ago. I mostly program using java. I write bad code. I think the reason behind this, is that I have never actually seen "good" code. I have read a couple of books on programming, but all of them just have some toy examples which merely explain the concept. But this is not helpful in complex situations. I have also read books/ articles / SO questions on what is "good" code, but none of them has a complex enough example.

So, can anyone point me to some java code which is considered "good"? (I know that my coding skills will improve as I practice, but perhaps looking at some examples will help me.)

A: 

You try to go through some open source code..it might help you..

A: 

You can always check the jdk source code, there are some good algorythms around it: http://java.sun.com/j2se/1.5.0/source_license.html

Also, sometimes I use to research some good open source apps code...

edited: Also is a good idea to participate in communities like this....

Check this out: http://www.javadb.com/

Garis Suero
Thanks for the javadb link.
athena
A: 

I think you can find what you need in this post:

Good Java Style: Part 1

Sir Gallahad
+5  A: 

Have you read Effective Java? http://java.sun.com/docs/books/effective/

There is a good amount of good code in it.

Macarse
+5  A: 

The best option for you to study good code is to look at some popular open source projects. I think 2 years is good enough time to understand code in these projects. Some of the projects you could look at:

Enough for you study and understand a variety of concepts. I frequently study code in JDK catalina(tomcat) and spring, jboss, etc.

naikus
Yes, Spring is a beauty.
Jean-Philippe Caruana
Thanks a lot for the pointers naikus. Sadly, only one answer can be accepted.
athena
+1  A: 

My experience with Java's Swing API shows how good code is written. You should be able to use your favorite IDE to naviagate to Java's source code.

Typically there are two concepts in object-oriented (OO) programming that you should be aware of when developing code that is easy to maintain. These concepts or principles are cohesion and coupling. Good OO programs typically have loose coupling and high cohesion.

See this article for more info: http://javaforyou.wordpress.com/2008/06/26/cohesion-and-coupling-two-oo-design-principles/

In addition, learning about Java Design Patterns will help you write better code. Here's something that I just found via Google. http://www.javacamp.org/designPattern/

BTW, the Swing API is full of examples of design patterns. For instance model-view-controller MVC is most of Swing. Java JTable and TableCellRenderer is a good example of the Flyweight pattern.

I've also discovered that writing unit tests for your classes also helps you identify when you class is trying to do much and/or is strongly coupled to too many classes.

Leo
Thanks for the javacamp link.
athena
A: 

It's good to go through Code Conventions for the Java Programming Language. It explains clearly and teaches how to code. Follow this :)

Paul
I think I have crossed this level. But thanks for the link.
athena
A: 

I suggest you try fixing your code, you are likely to learn more that way.

Try the code analyser in IntelliJ (which has 650+ checks and can auto fix the code) or the basic fixes in Netbeans or Eclipse. You can also run PMD and FixBugs on your code to see what they suggest.

Peter Lawrey
A: 

Use Checkstyle, Findbugs and PMD to check your source code. And if the tools find a issue, then think about why this is an issue.

Ralph
+1  A: 

To me, one of the best books about the suject is Clean Code by Robert C. Martin.

Jean-Philippe Caruana
Will grab a copy. Thanks.
athena