Good Taste In Code

So what makes for good taste in code? Here is an example of three ways to accomplish the same thing with Java:

Context:

There are two java classes:

  1. The Main.java class that represents the package
  2. SummingClass.java that has three primitive values in it. Two inputs, and their sum.

My goal is to make the SumClass into a container such that if I update even one of the two inputs, the update reflects whenever I call back for the sum.

Method 1:

Main.java:

package com.example.java;

public class Main {

   public static void main(String[] args) {
      
    SummingClass sum1 = new SummingClass(3 , 5);
    sum1.sum();
    System.out.println("Before changing: " + sum1.sum);
    
    sum1.a1 = 10;
    sum1.sum();
    System.out.println("After changing: " + sum1.sum);

    }
}

SummingClass.java:

package com.example.java;

public class SummingClass {
    public int a1, a2, sum;

    public SummingClass(int a1, int a2) {
        this.a1 = a1;
        this.a2 = a2;
    }

    public sum(){
        this.sum = this.a1 + this. a2;
    }


    public int getA1() {
        return a1;
    }

    public void setA1(int a1) {
        this.a1 = a1;
    }

    public int getA2() {
        return a2;
    }

    public void setA2(int a2) {
        this.a2 = a2;
    }

    public int getSum() {
        return sum;
    }

}

Analysis:

There is a separate method in SummingClass.java that needs to be explicitly called from Main.java for addition to happen. This gives the most modular control to the person writing the Main class. At the same time, if the person writing the Main class has absolute modular control if “sum1.sum()” is not called, the sum will be “0”. 

Is this a preferable situation?

Method 2:

Main.java:

package com.example.java;

public class Main {

   public static void main(String[] args) {

        SummingClass sum1 = new SummingClass(34 , 5);
        System.out.println("Before changing: " + sum1.sum);

        sum1.setA1(10);
        System.out.println("After changing: " + sum1.sum);

    }

}

SummingClass.java:

package com.example.java;

public class SummingClass {
    public int a1, a2, sum;

    public SummingClass(int a1, int a2) {
        this.a1 = a1;
        this.a2 = a2;
        sum();
    }

    public void sum() {
        this.sum = this.a1 + this.a2;
    }

    public int getA1() {
        return a1;
    }

    public void setA1(int a1) {
        this.a1 = a1;
        sum();
    }

    public int getA2() {
        return a2;
    }

    public void setA2(int a2) {
        this.a2 = a2;
        sum();
    }

    public int getSum() {
        return sum;
    }

}

Analysis:

Here is a better way to do the same. instead of calling the sum1.sum() every time, one would want the addition to happen immediately after the setter sets a new value. So this was accomplished with two steps:

  • patching setter methods for every possible input
  • retaining the sum() method within the class
  • disciplining the coder of the main class to use only the setter methods and not the primitive assignments. That means:
//this is forbidden
sum1.a1 = 10;

//this is required
sum1.setA1(10);

after these steps, the value of “sum1.sum” will always return the desired output.

Method 3:

Main.java:

package com.example.java;

public class Main {

   public static void main(String[] args) {

        SummingClass sum1 = new SummingClass(34 , 5);
        System.out.println("Before changing: " + sum1.getSum());

        sum1.a1 = 10;
        System.out.println("After changing: " + sum1.getSum());

    }

}

SummingClass.java

package com.example.java;

public class SummingClass {
    int a1, a2, sum;

    SummingClass(int a1, int a2) {
        this.a1 = a1;
        this.a2 = a2;
    }

    public int getA1() {
        return a1;
    }

    public void setA1(int a1) {
        this.a1 = a1;
    }

    public int getA2() {
        return a2;
    }

    public void setA2(int a2) {
        this.a2 = a2;
    }

    int getSum() {
        sum = a1 + a2;
        return sum;
    }

}

Analysis:

This is one more way to solve the same problem. We were able to get rid of the “sum()” method, saving some space in the memory, and:

  • patched only the getter method for the sum in the SummingClass class.
  • disciplined the coder of the main class to use just the getSum() method instead of referencing to the sum primitive. i.e.
//this is allowed now
sum1.a1 = 10;


//even this is allowed
sum1.setA1(10);


//this is required
System.out.println("Sum: " + sum1.getSum());


//this is forbidden
System.out.println("Sum: " + sum1.sum);

/*this will not work because the addition takes place in the getter method which is not called when referencing primitives.*/

this is the best out of the three for:

  • minimum patchwork
  • minimum number of objects
  • minimum concern when writing main

Verdict

We have often heard about recruiters asking you to write just 10 lines of code. One may wonder why people without result output got hired over those who got the output.

Point is, bugs in code can be removed with short-term effort but it takes long-term dedication developing a good taste in code.

A house that has been patched many times is bound to fall but the buildings that have been designed to stand will stay up longer. Same goes with code.

The output isn’t everything. Taste matters!

Leave a Comment

(1 Comment)

  • Divya

    Thank you for sharing this😊

  • Your e-mail address will not be published. Required fields are marked *