I want to reduce the number of if statement in my code. I need some advice on how to do this. I have a simple form, when I click submit, it trigger a workflow for approval process.

public void initiateWorkflow(){
   //some code here
   //workflow is started

Next, when the approver login the application, he either approves or rejects the application. So if it approves it triggers the following method:

public void approveRequest(String workflowId){
     //some code here
     //update status 
     update (param1,param2,param3,......);

After the approver approves the request, I need to update status of the request. Let's say it was a Parking form. The user applies for parking request. After approval, I need to update status in the parking table to approved.

So I've implemented a method to update the status using JPA:

public <T> Boolean update(Class<T> entityClass,String fldName, String idFldName,String fldValue,String id) {
        Query q = entityManager.createQuery("update "+entityClass.getSimpleName()+" set "+ fldName+" ='"+fldValue+"' WHERE "+idFldName+"=:id");
        q.setParameter( "id", id);

So in my method approveRequest(..), I will call this method update with the parameters required to do the job. My question I have different modules that will use this workflow.. for e.g Parking, leave, loan and so on. So I will need to call this method update in my method approve request several times with the corresponding parameter. As such there will be lot of if statement.

 public void approveRequest(String workflowId){
         //some code here
         //update status 
            update (param1,param2,param3,......);
        elseif (application="loan")
        elseif .....

So how can I avoid such things? It does not look good professionally writing codes with lots of if statement. I would be glad if someone can help on this.

You can use a switch statement instead of if statements, but you'll need someway to evaluate conditions.

I also think about switch statement. But I want something more professional (like pro programmers) If possible.

a switch would be appropriate here. And what makes you think chained if statements like that aren't "professional"?
Whether something's appropriate to use or not depends entirely on the scenario you're coding, and in this case it's entirely appropriate.

p.s. check your conditions. "=" in Java is an assignment, "==" is a reference comparison. You don't want either when comparing strings, you want .equals().

Well I will have about 20 application ..so 20 if statement or 20 switch statement. It can be more than 20. is that ok?

why wouldn't it be?

what exactly do you think professional programmers use, if they wouldn't use if-statements or a switch block?

I don't know. I wanted to implement something dynamic. I think with these switch or if it would not be that dynamic!

ah. You could create a class with an interface with a single method, then have for each case a class implementing that interface.
In your approveRequest method you'd create an instance of the required class based on the incoming request using reflection, the name of the class being somehow inferred from the parameters to the request (maybe a mapping in a properties file or database table).

Tricky to get right, not usually something you'd do unless the actual mappings and implementations of methods to be executed aren't known at the time the application is written, but it can and has been done.

samantha: I wouldn't recommend this. it will work no doubt, but it's a lot more advanced compared to the code you are looking at now, and the chance of it actually being needed is very slim.

yup. Unless you're planning on making a system that allows plugging in new types of actions to handle on the fly, or an application framework like Spring, it's almost certainly overkill.
But you wanted alternatives to hardcoding stuff, and that's what you'd end up with.

If it is the case that:
Parking, Loan etc are all a kind-of Workflow
There is a lot that's common to all of those, but each has it's own variations
You don't need to add new Workflows without updating the program
Define an abstract Workflow class with Parking etc as subclasses

otherwize, you could define a data structure with "Parking", "Loan" etc as keys, and data that tells you what parameters to use in the update calls. Probably the easiest way to do that is simply to hold the appropriate calls as lambdas.

@jwenting this is what I'm planning to do later. I'm currently working on a spring application.

@JamesCherill This solution sounds much better. Could you please provide an example? What you mean by data structure? All the application have an entity class.

HArd to create a elevant example without a lit mroe info, but the essence of it is just

abstract class Workflow {
   // methods common to all workflows

   abstract public void update();
   // same for any other methods that vary by kind of workflow

class Parking extends Workflow {
   public void update() {
      // version of update code for Parking

class Loan extends Workflow {
   public void update() {
      // version of update code for Loan

now you can instantiate Parking or Loan objects (etc) and subsequently call update() for any such object, and the appropriate version will be executed.

Edited 1 Year Ago by JamesCherrill

This question has already been answered. Start a new discussion instead.