Dos and Don’ts

Here’s some advice to all who have just begun learning Java. This also serves as an overview to my blog, where I write about all kinds of misconceptions and common problems with programming in Java.

What you should do

Read good Books

There are good and bad books. I do not want to compile a list of books I recommend. The list would soon be outdated and I didn’t read that many. Just know that a good book probably won’t be for free.

Read the Manuals, Documentations, API, and Specifications

It’s shocking how many students apparently don’t read. It’s not that hard to look up the documentation of any project and read it. If you can’t do that you won’t be a programmer. It’s that simple. Learn to use any web search service, such as Google Search, to find the information you need. Just find the official website of any project and look at the documentation there. Java actually has a complete tutorial. You also find the documentation of all APIs. And the language specification is there too. All accessible in a web browser.

Write clean Code

Always try to write clean code and try to improve your code constantly. A rose by any other name would smell as sweet. But code with bad names for types, methods or variables stinks. There are books about how to write clean code. And “code smell” is the term actually used in professional literature about programming.

Learn to share Code

You should show your code to others. If you have a question you should be able to provide some code (preferably a SSCCE). To share your code you have two options:

1: Paste a single code unit online

See this post on this wiki: How to paste Source Code

2: Share your complete project

If you are at a good university they will teach you to manage your code. Maybe with SVN or git. That is important so you can work in teams. Note that a book about programming doesn’t cover this topic. You need to buy another book. But that book could contain more than just SVN or git. It could also explain the complete process of Continuous Integration (CI). So you can also learn about version control, automatic builds, etc.

Be abstract!

Java is all about abstraction. The JVM is an abstraction of a computer. For each platform there is a concrete implementation. Try to be as abstract as possible.
When you learn Java you will learn about arrays before ArrayList<T> and then about List<T> interface. So you are more familiar with arrays. I often see code by beginners that use arrays when they could use an ArrayList. But an ArrayList still limits you to one type. If you return it from a method, you should use List. Then you could still switch to LinkedList<T>, Arrays.asList(T…), or return Collection.emptyList() if there are no results.
But do not confuse abstraction with abstract!

Learn from the Best

Example codes are usually simplified. They are not examples of good code. They only show how to achieve some task. If you want to look at good code check out the source code of the JRE runtime library. The source code is included in the JDK. You find it in a file “src.jar”. String would be in: \jdk1.#.0_##\src.zip!\java\lang\String.java

String might be too hard to understand (heavily optimized!). Classes by Josh Bloch are good, such as EnumSet and LinkedList. Note that a good class has a lot of documentation. Sometimes more  than code. Source code is for programmers, not computers (computers read machine code). So know your audience and write good code that others like to read.

Learn from your Mistakes

You still need to make mistakes. That’s how you learn. But what’s important is that you actually know your mistakes. So test your code and let others review your code. If you are not aware of your mistakes you will only reinforce those mistakes and continue doing them.

What you should not do

I only list some rookie mistakes. This is not a complete list. It’s just to show that example code is full of such mistakes and therefore unsuitable to learn how to write good code.

Online Tutorials and Blogs

Do you think physicist and doctors learn from YouTube-videos? Why should this be different for Computer Science?

Read my article about this topic: Blogs about Programming suck!

Don’t start with an IDE

Start with text editor (no, Word is not a text editor!). Notepad++, vim, gedit, sublime, etc. There are many and they usually have syntax highlighting and other nice features. You do not need Eclipse or NetBeans.

Don’t just send your code around

I have explained how to share code above. Don’t:

  • Post your code as a message or comment! Send a link instead.
    That goes for facebook, e-mail, etc.
  • Don’t take a screenshot. That’s just stupid!
  • Don’t use dropbox or any other system that isn’t made for code.
    GitHub is for free, so use that.
  • Don’t merge code with anything else than a diff/merge tool.

Just because you can doesn’t mean you should!

It’s not enough to know basic syntax. To be a good Java programmer you need to know much more. You need to know some basic libraries and frameworks. You need to know how to solve common problems. And you need to know what common problems there are in your language.
Another thing is to know what not to do. Effective Java by Josh Bloch is full of examples. But his book is for programmers who already know basic Java. I also write about such things on this blog and I list some examples that are relevant for beginners:

Don’t ever implement Object.finalize().

There is finalize in Java, but there is no good reason to ever use it. You could add some debug code to see when some objects become eligible for GC. Everything else is a misconception, often taught by people who never write actual code (teachers).  For more information read this post.

Don’t extend classes from the JRE unless you have to.

Do not extend JLabel or ArrayList. Those types already implement some interface. So instead write your own implementation. However, you can create an instance of a given class inside your own implementation and delegate some calls to that inner instance. For more information read this post. Just because a class isn’t final doesn’t mean you should extend it.

Do not ignore Compiler Warnings

Code that compiles but generates a lot of warnings is not good code. All those warnings have a good reason to be there. It’s up to you to do something against them. However, even code with no warnings could still be buggy. Once you manage to write code without compiler warnings you can use tools such as CheckStyle and FindBugs to further improve your code.

Do not ignore Exceptions

Exceptions and Errors should never be ignored. They must be handled. When you start you may not know how. But later you need to learn how to handle them. And you may also not know how to properly log such information. So to start you can simply print the message of the exception to System.err. Later you will learn to use a logging system (e.g. Log4J) instead.

Be aware that Generics and Regular Expressions are not easy

Online tutorials and even some books try to convince you that you can easily learn generics and regular expressions (regexp). That’s simply not true and I explain that in details in these two posts:

So don’t just use them unless you actually know the theories behind them.

Conclusion

Make sure you have good learning material before you waste your time with bad material. Your first code will not be very good but you must focus on solving some problem. Just don’t ignore other things such as code quality and how to manage your code.

Leave a Reply

Your email address will not be published. Required fields are marked *