syvaidya / openstego

OpenStego is a steganography application that provides two functionalities: a) Data Hiding: It can hide any data within an image file. b) Watermarking: Watermarking image files with an invisible signature. It can be used to detect unauthorized file copying.
https://www.openstego.com
GNU General Public License v2.0
1.13k stars 206 forks source link

Increase the usage of compound assignment operators #47

Closed elfring closed 1 year ago

elfring commented 2 years ago

:eyes: Some source code analysis tools can help to find opportunities for improving software components. :thought_balloon: I propose to increase the usage of compound operators accordingly.

diff --git a/src/main/java/com/openstego/desktop/plugin/dctlsb/DctLSBInputStream.java b/src/main/java/com/openstego/desktop/plugin/dctlsb/DctLSBInputStream.java
index f061a95..58f9905 100644
--- a/src/main/java/com/openstego/desktop/plugin/dctlsb/DctLSBInputStream.java
+++ b/src/main/java/com/openstego/desktop/plugin/dctlsb/DctLSBInputStream.java
@@ -90,8 +90,8 @@ public class DctLSBInputStream extends InputStream {
         this.imgHeight = imgData.getHeight();

         // Calculate widht and height rounded to 8
-        this.imgWidth = this.imgWidth - (this.imgWidth % DCT.NJPEG);
-        this.imgHeight = this.imgHeight - (this.imgHeight % DCT.NJPEG);
+        this.imgWidth -= (this.imgWidth % DCT.NJPEG);
+        this.imgHeight -= (this.imgHeight % DCT.NJPEG);

         this.y = ImageUtil.getYuvFromImage(imgData).get(0);

diff --git a/src/main/java/com/openstego/desktop/plugin/dwtkim/DWTKimPlugin.java b/src/main/java/com/openstego/desktop/plugin/dwtkim/DWTKimPlugin.java
index 813ec5c..b68c9f5 100644
--- a/src/main/java/com/openstego/desktop/plugin/dwtkim/DWTKimPlugin.java
+++ b/src/main/java/com/openstego/desktop/plugin/dwtkim/DWTKimPlugin.java
@@ -362,7 +362,7 @@ public class DWTKimPlugin extends WMImagePluginTemplate {
             double r;

             this.watermark = new double[this.watermarkLength];
-            for (int cnt = 0; cnt < (this.watermarkLength >> 1); cnt = cnt + 2) {
+            for (int cnt = 0; cnt < (this.watermarkLength >> 1); cnt += 2) {
                 do {
                     x1 = 2.0 * ((rand.nextInt() & Integer.MAX_VALUE) / (Integer.MAX_VALUE + 1.0)) - 1.0;
                     x2 = 2.0 * ((rand.nextInt() & Integer.MAX_VALUE) / (Integer.MAX_VALUE + 1.0)) - 1.0;
diff --git a/src/main/java/com/openstego/desktop/ui/OpenStegoUI.java b/src/main/java/com/openstego/desktop/ui/OpenStegoUI.java
index f9b52e3..fa97863 100644
--- a/src/main/java/com/openstego/desktop/ui/OpenStegoUI.java
+++ b/src/main/java/com/openstego/desktop/ui/OpenStegoUI.java
@@ -249,7 +249,7 @@ public class OpenStegoUI extends OpenStegoFrame {
                     // If the output filename extension is not supported for writing, then change the same
                     if (!dhPlugin.getWritableFileExtensions()
                             .contains(outputFileName.substring(outputFileName.lastIndexOf('.') + 1).toLowerCase())) {
-                        outputFileName = outputFileName + "." + dhPlugin.getWritableFileExtensions().get(0);
+                        outputFileName += "." + dhPlugin.getWritableFileExtensions().get(0);
                     }

                     if ((new File(outputFileName)).exists()) {
@@ -512,7 +512,7 @@ public class OpenStegoUI extends OpenStegoFrame {

                     // If the output filename extension is not supported for writing, then change the same
                     if (!wmPlugin.getWritableFileExtensions().contains(outputFileName.substring(outputFileName.lastIndexOf('.') + 1).toLowerCase())) {
-                        outputFileName = outputFileName + "." + wmPlugin.getWritableFileExtensions().get(0);
+                        outputFileName += "." + wmPlugin.getWritableFileExtensions().get(0);
                     }

                     if ((new File(outputFileName)).exists()) {
@@ -792,13 +792,13 @@ public class OpenStegoUI extends OpenStegoFrame {
             if ((action.equals(OpenStegoFrame.ActionCommands.BROWSE_DH_EMB_STGFILE) && (coverFileListSize <= 1))
                     || (action.equals(OpenStegoFrame.ActionCommands.BROWSE_WM_EMB_OUTFILE) && (wmInputFileListSize <= 1))) {
                 if (!plugin.getWritableFileExtensions().contains(fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase())) {
-                    fileName = fileName + "." + plugin.getWritableFileExtensions().get(0);
+                    fileName += "." + plugin.getWritableFileExtensions().get(0);
                 }
             }
             // Check for valid extension for signature file
             if (action.equals(OpenStegoFrame.ActionCommands.BROWSE_WM_GSG_SIGFILE)) {
                 if (!fileName.toLowerCase().endsWith(SIG_FILE_EXTENSION)) {
-                    fileName = fileName + SIG_FILE_EXTENSION;
+                    fileName += SIG_FILE_EXTENSION;
                 }
             }
             textField.setText(fileName);
diff --git a/src/main/java/com/openstego/desktop/util/CommonUtil.java b/src/main/java/com/openstego/desktop/util/CommonUtil.java
index eff9ce4..236c90a 100644
--- a/src/main/java/com/openstego/desktop/util/CommonUtil.java
+++ b/src/main/java/com/openstego/desktop/util/CommonUtil.java
@@ -167,7 +167,7 @@ public class CommonUtil {
     public static int byteToInt(int b) {
         int i = b;
         if (i < 0) {
-            i = i + 256;
+            i += 256;
         }
         return i;
     }
diff --git a/src/main/java/com/openstego/desktop/util/dct/DCT.java b/src/main/java/com/openstego/desktop/util/dct/DCT.java
index 5672941..1c7701d 100644
--- a/src/main/java/com/openstego/desktop/util/dct/DCT.java
+++ b/src/main/java/com/openstego/desktop/util/dct/DCT.java
@@ -679,10 +679,10 @@ public class DCT {

             m = len >> 1;
             while (j > m) {
-                j = j - m;
+                j -= m;
                 m = (m + 1) >> 1;
             }
-            j = j + m;
+            j += m;
         }
     }

@@ -803,7 +803,7 @@ public class DCT {
                     ii2 = ii1 + wingspan;
                     T = Cfac * f[ii2];
                     f[ii2] = f[ii1] - T;
-                    f[ii1] = f[ii1] + T;
+                    f[ii1] += T;
                     baseptr += increment;
                 }
             }
@@ -835,7 +835,7 @@ public class DCT {
                     ii2 = ii1 + wingspan;
                     T = f[ii2];
                     f[ii2] = Cfac * (f[ii1] - T);
-                    f[ii1] = f[ii1] + T;
+                    f[ii1] += T;
                     baseptr += increment;
                 }
             }
diff --git a/src/main/java/com/openstego/desktop/util/dwt/DWTUtil.java b/src/main/java/com/openstego/desktop/util/dwt/DWTUtil.java
index cb4ecbf..e97f801 100644
--- a/src/main/java/com/openstego/desktop/util/dwt/DWTUtil.java
+++ b/src/main/java/com/openstego/desktop/util/dwt/DWTUtil.java
@@ -549,8 +549,8 @@ public class DWTUtil {
         int h = height;

         while ((w % 2 == 0) && (h % 2 == 0)) {
-            w = w / 2;
-            h = h / 2;
+            w /= 2;
+            h /= 2;
             level++;
         }
syvaidya commented 1 year ago

@elfring I use IntelliJ for all optimization suggestions, and have never seen this coming up. I searched web for any mention of real advantages of using compound assignment operators. Other than lesser typing required, I don't see any pros. Do you have any strong reason of why this should be used?

elfring commented 1 year ago

I searched web for any mention of real advantages of using compound assignment operators.

The amount of available information differs for this technical detail according to the selected programming language.

Other than lesser typing required, I don't see any pros.

I would appreciate to avoid the specification of duplicate expressions a bit more.

Do you have any strong reason of why this should be used?

:thought_balloon: I suggest to reconsider optimisation opportunities also according to the programming language “Java”.

syvaidya commented 1 year ago

I'll leave it for now as I don't see much value in doing this. I might take it up in future when I do cleanup or large refactoring.