views:

211

answers:

11

I have heard a lot of people saying that to be a good programmer one needs to read a lot of good code. Can you help me find some good code in Java? Thanks.

+2  A: 

The source code for the standard Java library is often good to read, because it consists of small functions which do well-defined things.

I think you can download the source for Java 1.7 from Sun Oracle, and there are all kinds of open-source remakes of the standard Java library.

Sjoerd
+2  A: 

I would recommend you to take a look at various open-source projects. As their code is freely available, it tend to be of honorable quality.

To access those sources, you can go on http://koders.com or its alternatives (see as an example this question on C++)

Riduidel
Actually, open source code quality tends to be just as bad as proprietary code.
Michael Borgwardt
That could raise a subjective debate, so I'll reply with care :-) Anyway, I tend to see better Open-source code (as bad open-source is left over by the darwinian process) when closed source (the one I see in my company) is protected from natural selection by its niche status.
Riduidel
I've seen a lot of code in my time. It's mostly bad.
JeremyP
To be slightly less facetious. I've seen good and bad proprietary code and good and bad open source code. But, in my experience, bad open source code tends to be bad in a different way to bad proprietary code. Bad proprietary code tends to be inept, full of basic mistakes, because the programmers only do programming as a job and don't practise enough. Bad open source code tends to be over-engineered, over complex monstrosities that are very clever, but nobody has a hope of understanding.
JeremyP
+1  A: 

Pick your favorite open source library and start reading that. This will have the ancillary benefit of helping you gain a deeper understanding of how the library works. Here is a pretty good list of open source java libraries.

However, I find it more useful to tackle a programming problem myself, and then afterwords see how others approached the same problem. Project Euler is a great place to do this. Just my personal preference.

dbyrne
+2  A: 

I would recommend looking at the Apache Java projects and the java.net projects

bjg
A: 

Take a look to the Swing library - it uses some design patters and good object-oriented design.

Also, I used to read source code of Atlassian JIRA issue tracker, but you need special license to do that.

Vitaliy
+1  A: 

As an alternative or a starter: Those two books have a good reputation amongst Java programmers and help to make up your mind how good code should look like:

  1. Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin)
  2. Effective Java (2nd Edition) (Joshua Bloch)

Here is another interesting web link to a book that covers code reading: Code Reading: The Open Source Perspective

An interesting way to train your skills towards good code is exercising coding katas.

Andreas_D
Thanks!!! That will help.
SidCool
I may annoy some people, but I would recommend against *Clean Code*. It encourages fragmenting one's code into many ridiculously tiny methods, the sheer number of which makes code difficult to read. Just my opinion, of course.
Carl Smotricz
@Carl - yes, I have trouble with those advices too. I tried for some time to create a lot of small methods and found it extremely difficult to understand, maintain and refactor this code. So I decided to keep my old style but there's much more in his book. And the rest is valuable!
Andreas_D
+2  A: 

I would also read Martin Fowler's book on code refactoring ...
http://www.refactoring.com/catalog/index.html

Know the Design patterns and when and how to apply them.

Use static code analysis like PMD or another to point out some issues.
Do not ignore warnings in your own code.

Romain Hippeau
+1  A: 

I like to look through the code in Java's standard libraries too. Start off small with the methods that you actually use; then move on to classes like String, Integer, and other basic types, and move on slowly to entire packages, the Collections package, for e.g.

Looking at a method or two, is probably not going to be that enriching an experience compared to looking at different classes and figuring out the design.

Open source libraries are good too. Pick those that you actually use in your daily work and were developed by programmers/organizations you respect.

Vijay Kotari
+1  A: 

I have the Java sources installed and integrated in Eclipse, so when I'm programming and I'm using some class from Java's API I can dig into the class' source code and see how some things are implemented. Particularly interesting are the Collections and Concurrency related classes.

Iker Jimenez
A: 

There are much more opensource projects source are found in SourceForge.net. Utilize them properly. If u really wanna get more follow your instinct, for what you need & want..

As far as I know if you've the real thrust you can accomplish much more, Believe that you can do and Do

All the Best

venJava
+1  A: 

Although the code may not be legitimate, look at some MMO Private Servers' java source. They're great because the source is open, bountiful, and contains methods to concepts and actions that you can visibly see when playing the game.

I do NOT promote hosting one, though, because that is illegal in most areas, but feel free to glance at the work.

EDIT:

Important note: I don't develop games, myself, but they are great sources to understand programmer input and visual output. Look at David Brackeen's book and sources on his site. Interesting stuff, but not godly.

Justian Meyer
Links to the most interesting stuff?
Thorbjørn Ravn Andersen
http://www.brackeen.com/javagamebook/Search: OdinMs in ragezone.comAlso take a look at some open-source projects:- http://apache.org/- http://java-source.net/- http://vue.tufts.edu/
Justian Meyer