3

I have a requirement to implement following logic with multiple/nested if-else. Basically i have three tables and i need to insert record in one and delete from others based on different conditions (explained in code snippet below).

I have implemented it as below, just wondering is there a better/maintainable way to do it using OOPS and some design pattern in java

if (conditionA || conditionB || conditionC) {
    deleteRecordFromTableA();

    if (conditionB) {
        insertRecordInTableB();
    }

    if (conditionC) {
        insertRecordInTableB();
    }
} else {
    deleteRecordFromTableB();
    deleteRecordFromTableC();

    if (conditionD) {
        validateRecordInUpdateMode();
        updateRecordInTableA();
    } else {
        validateRecordInInsertMode();
        insertRecordInTableA();
    }
}    
1
  • 1
    There is nothing OOP-ish about this question. Commented Feb 17, 2016 at 1:05

1 Answer 1

2

Based on your code :-

if (conditionA || conditionB || conditionC) 
{
  deleteRecordFromTableA();

  if (conditionB || condition C)  // can be done together as they both do the same thing
  {
      insertRecordInTableB();
  }
}
else 
{
  deleteRecordFromTableB();
  deleteRecordFromTableC();

  if (conditionD)
  {
    validateRecordInUpdateMode();
    updateRecordInTableA();
  }
  else 
  {
    validateRecordInInsertMode();
    insertRecordInTableA();
  }
}  

An other way (Probably easier to understand) is :-

if(conditionA)
{
  deleteRecordFromTableA();
}
else if (conditionB || condition C)
{
  deleteRecordFromTableA();
  insertRecordInTableB();
}
else if(conditionD)
{
    deleteRecordFromTableB();
    deleteRecordFromTableC();
    validateRecordInUpdateMode();
    updateRecordInTableA();
}
else
{
    deleteRecordFromTableB();
    deleteRecordFromTableC();
    validateRecordInInsertMode();
    insertRecordInTableA();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.