ANTI-IF

Here’s a little story...


about a developer working in a software company who was building up a nice platform. One day the boss calls the developer on the phone and says: "There’s a new task I need you to do for a really important client, and it must be done by the end of the day. All that’s needed," the boss continues "is to add a small piece of functionality to that class method you’ve been working on... it shouldn’t be too complex..."

The days go by...



and that small class, edit by edit, grows into a small code monster: the more you feed it with IFs, the bigger it grows!

Do you think it’s just a fairy tale?

Well, it’s not!
What follows below is a single class method taken from real code that is live on a server somewhere on the Internet right now. And this is just a "baby monster". The more you feed it, the bigger it grows!

How to join the Campaign

public ArrayList eseguiAnalisi() {
  ArrayList listaSegnalazioni = new ArrayList();

  List domande = null;
  List risposte = null;
  Domanda domanda = null;
 
  DAOReques req = new DAOReques();
  DAOArea ar = new DAOArea();
  ar.setIdQuestionario(getIdQuestionario());
 
  boolean quizDomandaNonForzata = false; // warning 7
 
  try {
    //set Aree e
    if (getArea() == 1) { 
      List aree = ar.getAree();
      Area area = null;
   
      for (int i = 0; i < aree.size(); i++) {
        area = (Area) aree.get(i);
        domande = req.getDomande(getIdQuestionario(),area.getIdArea());
        if (domande != null) {
          for (int j = 0; j < domande.size(); j++) {
            domanda = (Domanda) domande.get(j);
            risposte = req.getRisposteDomanda(getIdQuestionario(),domanda.getIdDomanda());
            if (risposte != null)
            domanda.setRisposte(risposte);
          }
        }
      }
   
      area.setDomandeArea(domande);
   
      if (aree != null)
      setAreeDomande(aree);
      } //else {
      // set ListaDomande
      domande = req.getDomande(getIdQuestionario());
   
   
      for (int i = 0; i < domande.size(); i++) {
        domanda = (Domanda) domande.get(i);
        risposte = req.getRisposteDomanda(getIdQuestionario(),domanda.getIdDomanda());
        if (risposte != null)
        domanda.setRisposte(risposte);
      }
      if (domande!=null)
      setDomande(domande);
  //}
  } catch (DAOException de) {
    de.printStackTrace();
  }

Comments

Popular posts from this blog

PostgreSQL bytea and oid

Microservices Architecture with Spring Boot in 15mins

Adding MySQL datasource to JBOSS AS 7