Factorizing

Factorizing #

Duplicate code #

Duplicate code within a project has undesirable consequences. In particular:

  1. Duplicate code means duplicate bugs.
  2. The application may be harder to extend: adding a new feature may require modifying several nearly identical copies of the same method and/or class.

Factorizing #

By code factorization, we mean eliminating duplicate statements or expressions, by (loose) analogy to the factorization of an algebraic expression (e.g. ab + ac can be rewritten a(b+c)).

This usually comes down to simple techniques. We illustrate here a few of them.

Conditional statement #

Factorize the following program:

if(<condition 1>){
    <code block 1>
    <code block 3>
} else if(<condition 1> && <condition 2>){
    <code block 1>
    <code block 2>
    <code block 3>
} else {
    <code block 3>
}
if(<condition 1>){
    <code block 1>
}
<code block 3>

Auxiliary method #

Factorize the following Java class:

public class LayoutValidator {

    boolean noCompiledFileInSource() {
        try {
            if (Files.walk(Path.of("src/main/java"))
                    .anyMatch(f -> f.toString().endsWith(".class"))) {
                return false;
            }
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        return true;
    }

    boolean atLeastOnePropertyFile() {
        try {
            if (Files.walk(Path.of("src/main/resources"))
                    .anyMatch(f -> f.toString().endsWith(".properties"))) {
                return true;
            }
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        return false;
    }
}
public class LayoutValidator {

    boolean noCompiledFileInSource() {
        return !containsFileWithExtension("src/main/java", ".class");
    }

    boolean atLeastOnePropertyFile() {
        return containsFileWithExtension("src/main/resources", ".properties");
    }

    private boolean containsFileWithExtension(String directory, String extension) {
        try {
            if (Files.walk(Path.of(directory))
                    .anyMatch(f -> f.toString().endsWith(extension))) {
                return true;
            }
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        return false;
    }
}

Abstract class #

Factorize the following Java classes:

public class Unicorn extends Unit {

    public void wait(){
        <code block 1>
        <code block 2>
    }
}
public class Butterfly extends Unit {

    public void wait(){
        <code block 1>
        <code block 3>
    }
}
public class Wall extends Unit {

    public void wait(){
        <code block 4>
    }
}
public abstract class MobileUnit extends Unit {

    public abstract void wait(){
        <code block 1>
    }
}
public class Unicorn extends MobileUnit {

    @Override
    public void wait(){
        super.wait();
        <code block 2>
    }
}
public class Butterfly extends MobileUnit {

    @Override
    public void wait(){
        super.wait();
        <code block 3>
    }
}
public class Wall extends Unit {

    public void wait(){
        <code block 4>
    }
}