What simplicity means in OO design principles ? How to write a simple OO program ?

Updated: Aug 29, 2021

Simplicity is one of the Object Oriented Design Principles that helps developers keep the complexity low in an object oriented program.


Do not code for future

It says, a class and its method should be as simple as possible, the code should do exactly what it should do, nothing more. We should refrain from anticipating some functionality for future and implement

it for completeness. It's better to code what is known as of now and code in a way that it can be easily extended when new functionality is needed.


Do not provide many arguments

Sometimes programmers add more arguments to methods to make it more generic so that it can handle a lot of cases. But in reality all parts of the code may not be executed because all the possible scenarios the programmer thought of, is not happening now. just write what is known to be happening and don't make your method a generic one.


Detect reusable logic and refactor

If you anticipate you are writing a logic part of which can be used later in some other cases, break that method into parts, white the reusable logic in separate method(s). Call the reusable method from another method.

An easy way to detect this is, when during development you need to refactor a certain part once that means it might have to be modified later too. So better to put an extra effort to refactor it so that later a refactoring is not needed, otherwise just leave as it is. You can use design patterns to refactor the code and write a more general solution but design patterns do come with more code and complexity. So if the situation demands it then only apply it.


Here is an example of a class that has unnecessary code which is not being used at present

public class SearchEmployee {
  
  public Employee findEmployee(String name, String empId) {
    if(name !=null && empId != null) {
      throw new Exception(“You need to provide either name or id”);
    } else if(name == null && empId != null) {
      return Employees.getById(empId);
    } else if(name != null && empId == null) {
      return Employees.getByName(name); 
    } else {
      List<Employee> employees = Employees.getbyName(name);
      return Employees.filterById(employees, empId); 
    }
  }
  public Address findEmployeeAddress(String empId) {... }   
}
public static void main(String args[]) {
  SearchEmployee searchEmployee = new SearchEmployee();
  Employee employee = searchEmployee.findEmployee(null,1234);
  System.out.println(employee.name());
}


In the above code only the findEmployee() method of the SearchEmployee is used. Though there is a close relation between finding an employee by name or Id and finding an employee’s address, these two tasks are different. Also, the findEmployeeAddress() method is not being used. So it's better not to implement the findEmployeeAddress() method here.


The method findEmployee takes two arguments, the name and Id of an employee. This performs search based on either or both. When this method needs to be used and only the name or the Id of the employee is known, a null value has to be provided for the unknown field. This is a bad practice. Rather than providing too many arguments it's better to have two overloaded methods, findEmployeeById(String empId) and findEmployeeByName(String name). We also can avoid writing the else part of the code, which otherwise needs to be written for completion but this part is unnecessary and will not be executed.


Here is an example of an Employee class that has code which can be refactored


public class Employee {
  private String name;
  private String id;
  private String address;
  private String department;

  public Employee(String name, String id){
   if(name == null || name.equals(“”)) {
     throw new IllegalArgumentException(“name must not be null or empty”);
   }
   if(id == null || id.equals(“”)) {
     throw new IllegalArgumentException(“id must not be null or empty”);
   }
   this.name = name;
   this.id = id; 
  }
  public void setAddress(String address) {
   if(address == null || address.equals(“”)) {
     throw new IllegalArgumentException(“address must not be null or empty”);
   }
   this.address = address;
  }
  public void setDepartment(String department) {
   if(department == null || department.equals(“”)) {
     throw new IllegalArgumentException(“department must not be null or empty”);
   }
   this.department = department;
  }
}

Here you can see every method check whether the input string is a non null and non empty string. So it's better to refactor and reuse this code.

public class Employee {
  private String name;
  private String id;
  private String address;
  private String department;

  public Employee(String name, String id){
   checkField(name);
   checkField(id);
   this.name = name;
   this.id = id; 
  }
  public void setAddress(String address) {
   checkField(address);
   this.address = address;
  }
  public void setDepartment(String department) {
   checkField(department);
   this.department = department;
  }
  private void checkField(String fieldName) {
   if(fieldName == null || department.equals(“”)) {
     throw new IllegalArgumentException(fieldName + “ must not be null or 
                                        empty”);
   }
  }  
}

This is how you need to think before writing code and keep your object oriented program simple .


4 views0 comments

Recent Posts

See All