Modularize more efficiently in an if/else setup I wrote a program for one of my Object Oriented Programming in Java classes(Program 2) and I got docked off because my method was too long, which I'm not complaining about.

I got the note: "Modularize more, method too long."

I was wondering what any of you more experienced programmers would do to simplify/shorten my code for future sake so that I learn how to shorten this sort of code.

public void run() throws java.io.IOException
{
   stdin = new Scanner(System.in);
   String input;
   String fraction;
   int numPerLine;
   boolean inList = false;
   input = stdin.next(); // Get the read

  while(!input.equalsIgnoreCase("Q"))
  {
     if(input.equalsIgnoreCase("A"))
     {
        fraction = stdin.next(); // Get the fraction read
        thisFract = new Fraction(fraction);
        list.add(thisFract);
        System.out.println(thisFract.toString() + " was added "
              + "to the list.");
     }
     else if (input.equalsIgnoreCase("D"))
     {
        fraction = stdin.next();
        thisFract = new Fraction(fraction);
        inList = list.delete(thisFract);

        if(inList == true)
           System.out.println(thisFract.toString() + " was removed "
                 + "from the list.");
        else
           System.out.println(thisFract.toString() + " is not in the"
                 + " list.");
     }
     else if(input.equalsIgnoreCase("S"))
     {
        System.out.println("The sum of the list is: " + list.sum());
     }
There are some code smells in your code.

Duplicated Code:

fraction = stdin.next(); // Get the fraction read
thisFract = new Fraction(fraction);
list.add(thisFract);

fraction = stdin.next();
thisFract = new Fraction(fraction);
inList = list.delete(thisFract);


These lines do (almost) the same thing. It is commonly practice in software development to hold solutions at one place. In this case a method which reads the next fraction and another method which add a fraction to a list. This is meant by modularization. Divide your solution in little subsolutions which can be changed at one place and used at different places.

This code optimization is called Refactoring.
There a lots of patterns for code refactoring.

Replace Conditional with Polymorphism:

You could think about using a switch-statement instead of the if-else blocks but it is highly recommended to use Polymorphism instead of conditional checks in object oriented programming languages.

Clean Code:

I would recommend also to read the book Clean Code written by Robert "Uncle Bob" C. Martin which explains lots of patterns to write well organized code.

For Example you can modularize your code like this:


move the if-else in one method
find code clones in the condition blocks and separate into methods
refactor methods top-down

public void run() throws java.io.IOException {
    stdin = new Scanner(System.in);
    String input;
    input = nextFraction();

    while (!input.equalsIgnoreCase("Q")) {
        handleInput(input);
    }
}

private void handleInput(String input) {
    String fraction;
    int numPerLine;
    boolean inList = false;

    if (input.equalsIgnoreCase("A")) {
        handleA();      
    } else if (input.equalsIgnoreCase("D")) {
        handleD();      
    } else if (input.equalsIgnoreCase("S")) {
        handleS();          
    }
}

private void handleA() {
    assignNextFraction();
    addFractionToList();
    outputFractionAdded();      
}

private void handleD() {
    assignNextFraction();
    inList = deleteFractionFromList();
    if (inList) {
        outputFractionRemoved();
    } else {
        outputFractionNotInList();
    }   
}

private void handleS() {
    outputListSum();        
}

private boolean deleteFractionFromList() {
    boolean inList;
    inList = list.delete(thisFract);
    return inList;
}

private void addFractionToList() {
    list.add(thisFract);
}

private void assignNextFraction() {
    String fraction;
    fraction = nextFraction();
    thisFract = createFraction(fraction);
}

private void outputFractionAdded() {
    output(thisFract.toString() + " was added " + "to the list.");
}

private void outputFractionRemoved() {
    output(thisFract.toString() + " was removed " + "from the list.");
}

private Fraction createFraction(String fraction) {
    return new Fraction(fraction);
}

private String nextFraction() {
    String fraction;
    fraction = stdin.next(); // Get the fraction read
    return fraction;
}

private void outputFractionNotInList() {
    output(thisFract.toString() + " is not in the" + " list.");
}

private void outputListSum() {
    output("The sum of the list is: " + list.sum());
}

protected void output(String s) {
    System.out.println(s);
}