Is the there a more efficient way to write the below IF statement so I only need 2 lines of code or can this be solved with a ternary operator ?

        if(postIds.size()>0) {
    if(putIds.size()>0) {

We use the coding convention that the curly brackets can be skipped if the condition is placed on a single line to make these common cases less cluttered looking. (Such conventions generally cause great debate though.)

So in your case it would be:

if (postIds.size() > 0) PostProperty(postIds);
if (putIds.size() > 0) PutProperty(putIds);

Your logic has 4 outcomes so a simple ternary if won't work as that only has 2 outcomes; ternary if is really for methods that return a value that is to be assigned to a variable.

Guards like those could be put inside the methods, particularly if the methods are called from several places. Then the calling code reads more cleanly.

If by "efficiency" you are thinking about execution speed then don't: making it easy for people to understand what the code is doing is the most important thing most of the time.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.