Most software is built to last a long time, and one of the key challenges is to keep technical debt at bay. Applying fundamental principles of clean code is crucial to the long-term success of any software project. In this course, Java: Writing Readable and Maintainable Code, you will gain the ability to write high quality code that others will enjoy reading and working with. First, you will learn the importance of naming and how to choose great names for your classes, variables, and methods. Next, you will discover the pitfalls of Java constructors, methods, tests, comments, and exception handling before examining how you can either prevent or overcome them. Finally, you will learn some hands-on practical tips on how you can maintain the top quality of your code. By the end of this course, you will have the necessary skills to write clean, readable and maintainable code that human beings can read and understand clearly.
Course Overview Hi everyone, my name is Andrejs Doronins, and welcome to my course, Java: Writing Readable and Maintainable Code. Have you ever worked on a project that had terrible code? You need to develop it and maintain it, and on a daily basis, you wish that people before you did a better job. But are you absolutely sure that you write clean and maintainable code? If not, you should watch this course. Some of the major topics that we will cover include the importance of naming and how to choose great names for your classes, variables, and methods; pitfalls of Java constructors, methods, tests, comments, and exception handling, and how you can either prevent or overcome them; as well as hands-on practical tips how you can maintain the top quality of your code. By the end of this course, you will gain the ability to do just that, write code that you and others will enjoy reading and working with. Before beginning the course, you should have some professional experience with Java in any IDE, such as IntelliJ or Eclipse, and be able to write object-oriented code. I hope you'll join me on this journey to better code quality with the course Java: Writing Readable and Maintainable Code, here at Pluralsight.
Naming Matters Hi and welcome to the next module of this course in which we are going to discuss naming. If someone asked me to talk about one topic on clean code, I think I would choose to talk about naming. I truly believe it's one of the most important aspects of coding, yet somehow, it is also one thing that people tend to underestimate. Ironically, the concept of naming is the easiest to understand, but sometimes difficult to get right. As I already mentioned in the previous module, we spend most of our time reading code. Sometimes up to 90% of our time is reading, so it shouldn't be a struggle. You shouldn't look at code and spend valuable time thinking, what's the purpose of this class, or what kind of data this field contains, or what this method really does. The ideal situation is when you can glance through the code and get the gist of it right away. So in this module, we are going to talk about naming best practices. We'll discuss how to name classes and what naming patterns we should avoid. We'll then move on to variables and methods, each has specific rules, best practices, and pitfalls. We will then sum up with common naming tips that apply to all of the above.
Better Constructors Hello, and welcome to the next module. In this module, we are going to talk about constructors. When you have simple objects without many parameters, there isn't much to talk about. But often, objects that we need to create can be quite complex, and that complexity can be further multiplied by business logic and rules. So things can go wrong when defining and using them. So in this module, we are going to first look at the concept of static factory methods and how they benefit client code that needs to initialize an object. This is a scenario where you typically have a single constructor. We'll then look at a different scenario where we'll have several constructors and look at how chaining helps us write less boilerplate code and keep things dry. But that doesn't always help. You still might end up with what is called the constructor telescoping antipattern, and we'll see how the builder pattern helps us overcome this.
Implementing Methods Hi, and welcome to the next module. In this module, we are going to discuss everything, or almost everything, related to methods. In other languages, they are called functions, in Java, they are methods, and in the context of this course, it doesn't make a difference. One thing we are not going to talk about is naming methods because we covered that in the module about naming. So a method is where most of your programming happens. Classes are containers, and constructors are for creating objects, but the vast majority of application functionality is placed inside methods or functions, and is a consequence, most of dirty code revolves around them. So we'll approach methods in a structured way. We'll first start at the surface and look at the method signature, what you should and should not return. Then we'll move on to parameters and what kind of best practices applies to them. After that, we'll take a look inside the methods. We'll talk about the fail-fast and return early principles and how they contribute to clean code, what kind of duplication we can encounter, and some simple techniques to avoid it. We'll then talk about conditionals and best ways to handle them.
Handling Exceptions Hi, and welcome to the next module. As we all know, things can go wrong inside a program, and it definitely happens more often than we'd like to. Because it happens quite often, we also write exception handling code quite a lot, and this is why this course has a separate module on exceptions. The topic is big enough to deserve its own module. So exception handling, when used properly can improve a program's reliability and maintainability, but when used improperly, they can have the opposite effect. It could cause bugs that are much more difficult to investigate and it can clutter up the code so much that you don't know anymore what the code is actually supposed to do. So in this module, we are going to talk about best practices surrounding exception handling, why you shouldn't try and catch all possible exceptions, why you shouldn't ignore exceptions, and how you can balance between the two. To be honest, this is a very short list. The topic of exception handling is much wider, but I want to stay focused and give you just the most essential guidelines, so I've picked the most frequent and important exception handling issues that can be encountered.
Class Organization Hi, and welcome to the next module. This time we're going to take a step back and talk about classes. So far, we have been talking about one thing at a time, just variables and their names, or just constructors, or just methods and what happens inside of them. So if we take care of each individual piece, then the sum, your entire software should be clean as well, right? Well, yes and no. If you follow practices that we have discussed until now, your code should be in good shape, but that doesn't mean there is no more room for improvement. And sometimes you need to take a step back and inspect how all of the individual pieces are put together, and whether they form a coherent logical picture. So in this module, let's zoom out a little bit, and look at the class as a whole. We'll first briefly revisit the single responsibility principle and follow up with the concept of cohesion and coupling. We'll discuss what they mean to the size and content of a class. We'll then talk about Java style conventions that you should adhere to and why, as well as how formatting and even the order of code impacts it equality. We'll wrap up by looking at several things that you can study after this course to take your clean code skills to the next level.
Improving Tests It is widely accepted that writing tests is necessary for any nontrivial project. You might write only unit tests, or a range of functional integration, or UI tests. It doesn't matter which kind of test, but what's important is to treat the test code just like any other code, it should be clean. Although a lot of guidelines are the same for test and application code, some are different, or they have different priorities. Entire books and courses exist on how to write tests, and the aim of this module is not to duplicate that effort, but to highlight the most important principles that you should follow. So in this module, we're going to talk about the DAMP principle, the benefit of keeping your tests focused, and following some kind of test format template, such as AAA or BDD style.