Good Habits For New Java Programmers
Published: 2022-08-31 18:49:48


**tl;dr** Here's the [habits List](#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](https://dev.to/mbrubin56/-true-and-false-are-redundant-5aoc)); 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](https://dev.to/mbrubin56/meaningful-names-bh1) 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:<a name="habits-list"></a>
[Meaningful Names
](https://dev.to/mbrubin56/meaningful-names-bh1)[Naming Conventions](https://dev.to/mbrubin56/naming-conventions-55ni)
[Default To private Access Modifiers](https://dev.to/mbrubin56/default-to-private-access-modifiers-ooj)
[Writing Good Comments](https://dev.to/mbrubin56/writing-good-comments-1j3e)
[Always Use Braces For if/else](https://dev.to/mbrubin56/always-use-braces-for-ifelse-2fkg)
[== true and == false Are Redundant](https://dev.to/mbrubin56/-true-and-false-are-redundant-5aoc)
[Use Your IDE's Code Formatter](https://dev.to/mbrubin56/use-your-ides-code-formatter-1al9)
[What's A Good Error Message](https://dev.to/mbrubin56/whats-a-good-error-message-5hmf)
[How To Get Out Of Main And Reduce Static](https://dev.to/mbrubin56/how-to-get-out-of-main-and-reduce-static-1ddp)
[Method Decomposition And Class Decomposition](https://dev.to/mbrubin56/method-decomposition-and-class-decomposition-32ik)
[Where Should Main Live](https://dev.to/mbrubin56/where-should-main-live-244h)
[How To Order Your Fields And Methods](https://dev.to/mbrubin56/how-to-order-your-fields-and-methods-2ihh)