D
D
Dmitry2016-08-19 10:27:54
Java
Dmitry, 2016-08-19 10:27:54

What is the best way to remove code repetition?

Oop geniuses, tell me how best to get rid of repeated pieces of code in methods, the methods are different, they return different information, but the point is that you have to write the same piece every time

if (mGetRequest != null && mGetRequest.isExecuting())
            return;

        ParamsBuilder paramsBuilder = new ParamsBuilder();
        paramsBuilder.setMethod(BOOKS_METHOD);
        paramsBuilder.setEnumParse(EnumParse.BOOKS);

        if (page != null) paramsBuilder.addParams("page", String.valueOf(page));
        if (search != null) paramsBuilder.addParams("s", search);
        if (userId != null) paramsBuilder.addParams("uid", userId.toString());

        mGetRequest = new HttpRequest(listener);
        mGetRequest.execute(paramsBuilder);

this piece (mGetRequest != null && mGetRequest.isExecuting()) has to be constantly checked

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Kornachev, 2016-08-19
@Scotik

A method that takes a functional interface (a lambda with code) as input, a condition is checked inside the method, and depending on the condition, the lambda is executed or not.

M
maaGames, 2016-08-19
@maaGames

bool isExecuting()
{
return mGetRequest != null && mGetRequest.isExecuting();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question