DEV Community

Mark Rubin
Mark Rubin

Posted on • Updated on

Good Habits For New Java Programmers

tl;dr Here's the habits List

What's this all about?

I'm an instructor for an introductory Java course. My students are learning Java mostly on their own using course texts, and I'm available for general help, and I review their code assignments.

There are some common themes developing for the advice I've been giving my students on Java style, so I'm collecting that advice here. I use "Java style" loosely, covering more than just what a code formatter might handle, to include some basic, good habits that will help the students write cleaner, easier to read, and easier to maintain Java programs: topics such as variable, class, and method naming; what makes a good comment; some language usage tips (e.g. == true is redundant); and more.

Although all the discussions are presented in the context of Java programming, they are generally good programming tips and extend naturally to other languages. For example, picking meaningful names for identifiers is important for any language you program in.

Habits?

I'm framing these tips as helpful habits to develop because that framing helps motivate why I'm advocating for them in my code reviews of students' programs. The small size and short-lived nature of those programs obscure the value of the advice a bit. So some of the advice may seem like it isn't so important for this or that specific program, but it is a good habit nonetheless, and developing it will pay off in the long run with larger programs.

For example, I push for giving variables meaningful names; but a variable named just p is not hard to track in a ten line program, and that program, just a one-off homework assignment, isn't going to be maintained over time or read by lots of people. So what's the harm in a one letter name in this case? Well, at the very least, the harm is in not developing the right habits. Following my advice will help students start off straight away doing the right things. Bad habits are hard to break, and if you're a student reading this, if you continue with your programming, you'll be glad you developed the right habits early on. I wish I had gotten this advice and developed those habits sooner than I did.

Developing these habits can add a bit of extra mental work as you start out, but not that much, and it's very worth it. If the other reasons I offer for each item don't hit home, you will just have to trust me on the good habits bit. Sometimes you have to learn the whats before the whys.

Checklist

If you're a student, it might be useful to maintain a checklist based off of these habits I'm asking you to develop. Then you have a structured way to review your code before you submit it. Reviewing and applying the checklist will help you develop the right habits and will reduce how much of your assignment I'll ask you to revise and improve your grade.

List of habits:

Meaningful Names
Naming Conventions
Default To private Access Modifiers
Writing Good Comments
Always Use Braces For if/else
== true and == false Are Redundant
Use Your IDE's Code Formatter
What's A Good Error Message
How To Get Out Of Main And Reduce Static
Method Decomposition And Class Decomposition
Where Should Main Live
How To Order Your Fields And Methods

Top comments (0)