Advice please: Which of these 2 methods looks better ? like professional code?
Both methods are doing the same thing(simpleUpdate). Except the way am passing the parameters are different. The method manageJob is used to call the method simpleUpdate to update the status in DB.

public <T> void manageJob(Class<T> entityClass,String name,String idName, Car selectedCar){
        switch (selectedtask.getGwtApplicationType()) {
        case "Cars":
            simpleUpdate(entityClass, name, idName, selectedCar.getCarStatus(), selectedCar.getCarId());
            break;

        default:
            break;
        }

    }

Method 1:

public <T> Boolean simpleUpdate(Object...params) {
        Query q = entityManager.createQuery("update "+params[0]+" set "+ params[1]+" ='"+params[3]+"' WHERE "+params[2]+"=:id");
        q.setParameter( "id", params[4]);
        try {
            q.executeUpdate();
            return true;
        } catch (TransactionRequiredException  ex) {
            return false;
        } catch (IllegalStateException ex) {
            return false;
        }

    }

Method 2:

public <T> Boolean updateStatus(Class<T> entityClass,String name, String idName,String value,String id) {
        Query q = entityManager.createQuery("update "+entityClass.getSimpleName()+" set "+ name+" ='"+value+"' WHERE "+idName+"=:id");
        q.setParameter( "id", id);
        try {
            q.executeUpdate();
            return true;
        } catch (TransactionRequiredException  ex) {
            return false;
        } catch (IllegalStateException ex) {
            return false;
        }

    }

Recommended Answers

All 3 Replies

Method 1: (Object...params)
That's as unhelpful and error-prone method signature as I can imagine. You have no idea what the arguments should be, and the compiler can do nothing to help you.
Method 2: (Class<T> entityClass,String name, String idName,String value,String id)
Now at least I know what the arguments are supposed to be, and the compiler can check that the first is a class<T>, the second a String etc

For me it's 100% the second version. No contest.

Okay James . Thanks a lot

An extra thought: if that combination of arguments is common (and especially if they logically make up something) it's often a good idea to make a class to represent the combination.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.