- I mentioned in previous lessons that CodeGuru Reviewer makes recommendations on a variety of aspects of your code, and we covered the different category that those recommendations fall into. Two of the categories that I mentioned were common coding best practices and code quality. For this video, we will be focusing on these two categories. First, we will dive into what these categories are all about. Then, I'm going to show you a code snippet that has some errors, or room for refactoring. I'll go over this code snippet briefly, check the code into an existing branch in my repository, called dev. Then I will open a pull request which will launch CodeGuru Reviewer to perform a code review. From there, we will review the recommendations it left in the code. After this video, you'll have an exercise where you can fix the defects found during this code review, check the code back in, and merge the pull request. Let's first go over the categories of recommendations we will be targeting for this video, starting with common coding best practices. Some examples of issues that fall into this category are forgetting to check whether an object is null before setting it, which can cause exceptions to be thrown at runtime, or reassigning a synchronized object to be a new instance of that object, which can lead to incorrect results or confusion. It also can make recommendations when a developer forgets to initialize a variable along an exception path. Now, for code quality. CodeGuru Reviewer is looking for potential code patterns that can be improved upon in order to make code more readable or maintainable. Let's run through some issues that can impact code quality, and I won't be going deep into detail for each of these, but you can find more information about them in the reading following this video. First up is method source lines of code, or source LOC. CodeGuru Reviewer analyzes the number of lines of code in a single method. Methods that have high numbers of lines of code can be difficult to read, and could include logic that is hard to understand or test. Next, we have method cyclomatic complexity. Cyclomatic complexity refers to the number of decisions that are made in a method. A method with high cyclomatic complexity, or lots of decision points, like if-else statements, or maybe even a lot of nested decisions, can make logic difficult to understand and test, similar to the number of lines of code. Next, there's method fan out. Method fan out indicates how many methods are called by a given method. Methods with high fan out are highly dependent or highly coupled with other methods. The next potential issue is class fan out. Class fan out is similar to method fan out, but it refers to how many other classes are referenced by a given class. Then, the last issue we will cover here before going over the code snippet and showing you how to launch a code review, is class cohesion. CodeGuru Reviewer will notice if a class contains methods that do not have any access data in common, meaning that the methods are unrelated, but are housed in the same class. Classes with low cohesion contain unrelated operations. This can be difficult to understand, and are often less likely to be used, because it signifies a disorganized code base. All right, so those are some of the things we are looking at for these first two categories. Now, I'll go ahead and pull up a code snippet that we are going to use to create a pull request, to see what types of recommendations CodeGuru Reviewer makes. Keep in mind, this code snippet is written to purposefully include defects or leave room for improvement. And the code snippets we go over in this course are intended to show you how to use CodeGuru Reviewer and give you an idea of what it can do. All right. Let's take a look. You can see I have a code snippet pulled up here, and this code is a guessing game, where the computer generates a number, and then the user has three guesses to guess the generated number. Each time a user guesses, we have a code block that determines if their answer was correct or not. There's a code block for all three guesses. Now, I'm not going to go over this code too much in depth here, but you can find a copy of the code in the exercise after this video, if you want to take a closer look. Okay, so we have one code snippet that we will open a pull request for. There's an existing dev branch we will be working in for all of these examples. Now to check this code into the branch, we will first add the file, through using the Git command line in the terminal. So, let's go ahead and hop over to the terminal. You can see I have the terminal up here, and the first thing we are going to do is add this file by typing git add, and then we'll type the path of the file, which is java/CodeGuru/src/main/GuessingGame.java. Then, we will type a commit message by typing git commit -m and then adding a message. So, in this case, I'm just going to say, adding guessing game. and then, we will do git push origin dev. And this will push this code into the dev branch. Okay, so we can see that that was successful. Now, to open a pull request, I'm going to go into GitHub here. So, I'm going to go into GitHub, and I'm going to click on the Pull requests tab. Then, I'm going to select open a new pull request. We are comparing the dev branch to the main branch, and then I will click Create pull request, and we can go ahead and leave a comment here. Leave a description, adding guessing game code, and then you click Create pull request. Now, heading over to the CodeGuru Reviewer console. If I go into Code reviews here, and these are all previous code reviews that I've done, but we can see that we have one code review that has been opened, and it's currently pending. So we're going to go ahead and wait for this to be done. We'll fast-forward through the processing and come back when it's complete. Okay, now it's done, and we can see that CodeGuru Reviewer has left one recommendation. I can view them in the CodeGuru Reviewer console or in GitHub. You can see here in the CodeGuru Reviewer console you can provide feedback with a thumbs up or thumbs down. I'm going to go ahead and go back over to GitHub, where I can click on this, and then I'm going to go back to our pull request, and then I'm going to select our open pull request, and then I'm going to select View changes. And this gives us a view where you can see both the code and the comments inline together, so it gives a full context. We only have one recommendation for this snippet, and it reads, Similar code fragments were detected in the same file at the following lines, and then it lists the lines. Refactoring can help improve code maintainability. Consider reducing duplicate code by extracting it into a separate method. You can then replace duplicated code with calls to this new method. This is a common coding best practice, that it is trying to recommend to me. When you have multiple parts of the program that are doing the same thing, it's a best practice to refactor that code and break it out into a method. This may seem like a simple thing, but it can actually save you a lot of time and headaches in the future. When I was in software development, we had an entire project dedicated to resolving technical debt like this, and hunting down duplicate code snippets and refactoring them into utility classes. A tool like CodeGuru Reviewer would have saved us a lot of time looking for where these types of occurrences were. Deduplicating code in this way is important for code maintainability. There are many other examples of defects for common coding best practices and code quality that CodeGuru can find, and this example is a small snippet to demonstrate how the tool works. Please read the course notes to learn more about recommendations in this category, and make sure that you've checked out the exercise following this video for a chance to fix the code that we submitted for review, take a look at this specific defect, and then merge that back into the code base.