O
O
onami2022-02-05 07:20:19
Programming
onami, 2022-02-05 07:20:19

How to get rid of the if heap in a method?

At the interview, they asked the following question: there is a method that deals with confirmation of orders. Then a demand comes from the business: upon confirmation of an order worth more than 1,000 rubles, give this client a discount coupon for new purchases. Further, from the business comes the requirement for orders from 10,000 rubles to give the client a permanent discount, and so on.

The question is: how can we implement this logic without turning the method into a bunch of ifs?

I answered that we can pull this logic into a table where the conditions and the list of operations that need to be performed when the given condition is met will be written.

I think the interviewer was expecting a different answer. Maybe it was necessary to mention some design pattern (which one, by the way)?

How would you answer this question? It is interesting to know your opinion.

Answer the question

In order to leave comments, you need to log in

8 answer(s)
D
Dmitry Roo, 2022-02-05
@xez

Similar to chain of responsibility .
The point is to create order handlers that will pass it to each other along the chain until it is processed in one of them.

A
Alexander Skusnov, 2022-02-05
@AlexSku

Usually, instead of a bunch of ifs, they make a swich case.

V
Vasily Bannikov, 2022-02-05
@vabka

I think the interviewer is a little more specific and detailed answer, since the table itself will not assign discounts and will not change the status of the client - some very specific code should do this.
And again - how should conditions and actions be recorded in this table?

W
Wataru, 2022-02-05
@wataru

Maybe I wanted to hear from you that the table can be searched by binary search.

T
Therapyx, 2022-02-05
@Therapyx

it was in this case that the following got into the headway, where there will be one if :)
Firstly, this list should be filled in and changed by someone, and certainly not by a hard-code programmer.
those. there should be a blitz by type key value
1000 - bun
10000 - another bun
20000 - ...
n - m
we make some adapter where the order amount will be transferred - we load it, for example, into a set that will be sorted with this table.
after which we look for a number that and through iteration we check
if the number is greater than the current index and less than the index +1, then this is our range from where we need to get a bun from value.
And what the interviewer expected, alas, I do not know.

V
vitaly_74, 2022-02-06
@vitaly_74

I think it's a decorator or a chain of responsibility .
But since you are talking about one object (order) then I think this is a decorator.

Handlers in the Chain of Responsibility can perform arbitrary actions independent of each other, as well as interrupt further transfer along the chain at any time. On the other hand, Decorators extend a specific action without breaking the interface of the underlying operation and without interrupting the execution of other decorators.

The decorator and the chain of responsibilities are very similar to each other. Learn more about decorators .

A
Alexey Baranoshnikov, 2022-03-05
@itPiligrim

Simply create methods like "IssueDiscount", "IssuePermanentDiscount", "IssueCoupon" and are called sequentially. The logic of each method is hardwired into the method. There are no ifs in the root method. The code is read from top to bottom in a linear fashion. Each method at the beginning has a condition check and if the conditions are not met, then the method immediately exits. It will be more difficult if the methods are interconnected, but that's another story.

A
Alexander Interesting, 2022-03-17
@Swimergg

As an option to score on what he says and do it so that it just works :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question