Comments
Description (What)
Any comment that explains a line(s) of code inside a method or in the class that is not part of a comment syntax such as JavaDoc
How to Locate It (Where)
Look for single lines comments that can be removed as the code they are explaining should be self-explanatory
How It Manifests (Why)
The need to explain a line of code usually means that line of code is not self-explanatory and needs to be amended such that it is. Any time you feel that a comment is absolutely necessary it is possible that the piece of code being explained can be spun of into a new method.
How to Fix It (Possible Treatments)
Remove the comment and use Extract Method to convert the explained code into its own method for more understandable and maintainable code.
Other treatments are also possible based on the specific scenario, they can be found here
Examples
Example 1
Before:
A lot of comments at the end of lines that bloat the code and are dispensable as the code is self-explanatory. Some comments are detailed and required which means methods can be extracted
Observed Code Smells:
- Comments (lines 117, 135, 138, 141, 144, 149, 154, 157)
After:
Applied Extract Method to create a new method called applyBrightnessToPixel that better illustrates what that piece of code is to do.
Refactoring Applied:
- Comments
- Extract Method (line 147-158)
Observed Code Smells After Refactoring:
- None
Example 2
Before:
The comments explain that this constructor should have assertions about the ranges of the RGB values but
the code does not implement the defensive block.
Observed Code Smells:
- Comments (lines 10-12)
After:
Applied Introduce Assertion to clamp out the invalid ranges for the RGB values.
Refactoring Applied:
- Comments
- Introduce Assertion (line 10-12)
Observed Code Smells After Refactoring:
- None
When to Ignore
In places where the comment is used to explain Why a specific implementation is being used or for explaining complex algorithms after trying to simplify the code.
More
- Example 1
- Example 2
- Before
- After
001 import java.util.HashMap;002 import java.awt.image.BufferedImage;003004 /**005 * Represents an image buffer that stores pixel data in a 2D array format.006 * This class provides methods to access and manipulate the pixel data of an007 * image.008 */009 class ImageBuffer {010 private final long[][] pixels;011 private final int width;012 private final int height;013014 /**015 * Initializes a Buffer using an existing {@link BufferedImage}.016 * This constructor extracts the pixel data from the provided BufferedImage.017 *018 * @param image The BufferedImage to be stored in this buffer.019 * This image's pixel data will be copied to the internal storage.020 */021 public ImageBuffer(BufferedImage image) {022 width = image.getWidth();023 height = image.getHeight();024 pixels = new long[width][height];025026 for (int x = 0; x < width; x++) {027 for (int y = 0; y < height; y++) {028 pixels[x][y] = image.getRGB(x, y);029 }030 }031 }032033 /**034 * Initializes an empty Buffer with the specified dimensions.035 * The pixel data will not be initialized and will default to zero.036 *037 * @param width The width of the image to be created.038 * @param height The height of the image to be created.039 */040 public ImageBuffer(int width, int height) {041 this.width = width;042 this.height = height;043 pixels = new long[width][height];044 }045046 /**047 * Retrieves the RGB value of a pixel at the specified coordinates.048 *049 * @param x The x-coordinate of the pixel (horizontal position).050 * @param y The y-coordinate of the pixel (vertical position).051 * @return The RGB value of the pixel at the specified coordinates.052 * @throws IllegalArgumentException if the specified coordinates are out of053 * bounds.054 */055 public long getRGB(int x, int y) {056 if (x >= 0 && x < width && y >= 0 && y < height) {057 return pixels[x][y];058 } else {059 throw new IllegalArgumentException("Coordinates out of bounds");060 }061 }062063 /**064 * Sets the RGB value of a pixel at the specified coordinates.065 *066 * @param x The x-coordinate of the pixel (horizontal position).067 * @param y The y-coordinate of the pixel (vertical position).068 * @param pixel The RGB value to be set for the pixel.069 * @throws IllegalArgumentException if the specified coordinates are out of070 * bounds.071 */072 public void setRGB(int x, int y, long pixel) {073 if (x >= 0 && x < width && y >= 0 && y < height) {074 pixels[x][y] = pixel;075 } else {076 throw new IllegalArgumentException("Coordinates out of bounds");077 }078 }079080 /**081 * Returns the width of the image.082 *083 * @return The width of the image in pixels.084 */085 public int getWidth() {086 return width;087 }088089 /**090 * Returns the height of the image.091 *092 * @return The height of the image in pixels.093 */094 public int getHeight() {095 return height;096 }097 }098099 /**100 * Provides the method to alter image brightness101 */102 class ImageModel {103 private final HashMap<String, ImageBuffer> loadedImages; // A map to store loaded images by name104105 public ImageModel() {106 this.loadedImages = new HashMap<>();107 }108109 public ImageBuffer getImage(String name) {110 return loadedImages.get(name);111 }112113 public void setImage(String key, ImageBuffer image) {114 loadedImages.put(key, image);115 }116117 // Helper method to clamp values between 0 and 255 (valid RGB values)118 private long clamp(long value) {119 return Math.min(255, Math.max(0, value));120 }121122 /**123 * Alters the brightness of the specified image.124 * Each pixel's RGB value is adjusted by a specified brightness factor.125 *126 * @param image The original image to be brightened.127 * @param brightness The amount to adjust the brightness. Positive values128 * increase brightness, while negative values decrease it.129 * @return A new Buffer containing the brightened image.130 */131 public ImageBuffer alterBrightness(ImageBuffer image, float brightness) {132 int width = image.getWidth();133 int height = image.getHeight();134135 // Create a new Buffer to store the modified image136 ImageBuffer brightenedImage = new ImageBuffer(width, height);137138 // Loop over every pixel in the image139 for (int y = 0; y < height; y++) {140 for (int x = 0; x < width; x++) {141 // Get the current pixel's RGB value142 long rgb = image.getRGB(x, y);143144 // Extract the red, green, and blue components using bitwise operations145 long red = (rgb >> 16) & 0xFF;146 long green = (rgb >> 8) & 0xFF;147 long blue = rgb & 0xFF;148149 // Adjust brightness by adding to each color component150 red = clamp((int) (red + brightness));151 green = clamp((int) (green + brightness));152 blue = clamp((int) (blue + brightness));153154 // Recombine the adjusted RGB components into a single int155 long newRGB = (red << 16) | (green << 8) | blue;156157 // Set the new RGB value in the brightened image158 brightenedImage.setRGB(x, y, newRGB);159 }160 }161162 return brightenedImage;163 }164165 }