Removing Java code ... is it code that can be optimized?

So, I'm using an API that is somewhat unfriendly in a certain way. Basically, this API creates a resource that can be obtained later. This resource may or may not exist when we get to it later.

To extract a previously created resource, you must use the results directive as follows:

String resultKey = "12345";
PersistedResult r = mFactory.getPersistedResult(resultKey);

Now the hardest part is that it getPersistedResultDOES NOT throw an exception when called with an invalid director ... it PersistedResultis a lazy loader and will fail only when one of its methods is called (by calling the object to load).

So, to determine if the resource is really valid, I do the following:

PersistedResult r = null;

if (!StringUtils.isEmpty(resultKey)) {
    try {
       r = mFactory.getPersistedResult(resultKey);
       r.getResultCount(); // Triggers exception if result key was invalid.    
    } catch (Exception e) {
       // handle exception
    }
 }

getResultCount , ?

PersistedResult , .

+3
4

, getResultCount() - .

+6

, ? getResultCount , , - , , , , .

, , . , , .

+3

( , ) , ( ). - , , , .

( , , .)

+3

. .

+1

Source: https://habr.com/ru/post/1794289/


All Articles