0

Let's say that in a project we have a function (in PHP for example) that checks whether a particular user is eligible for a discount.

Also, we have a report (written in SQL) that pulls all users that are eligible for discount.

At the beginning of the project, the logic in the function and the SQL is identical, no issues.

One year later, we decide to make changes to the logic, so we do it in our function. Now, what is the proper way to remind ourselves that the SQL in the report has to be updated as well (especially in large projects, where the duplicate logic might not be easy to track)?

Obviously, I can just put a comment, but is this the proper way?

1
  • As your question was originally written, it contained multiple questions. It would have been closed for sure as "needs focus". I removed all but one of the questions, if you don't mind taking a look. Apr 27, 2021 at 0:56

1 Answer 1

1

There is no "proper way" to handle this when logic is split across layers of the tech stack. Ideally you want the logic centralized, but pulling reports can be its own beast, and I understand that sometimes this stuff just gets separated for the sake of performance.

An option would be to create a database view that returns just the IDs of the users eligible for a discount. Your report could join on this view, and you could refactor your PHP code to call this view as well. This assumes that no runtime parameters are necessary in order to determine that someone is eligible.

If parameters are needed then a function or stored procedure in the database could work. Again, since a SQL function is executed in the database your report could call this function, and your PHP code could be refactored to call this function as well. Performance of the report might suffer. It also might not suffer. You'll just have to try and measure. If performance isn't too bad then maybe the maintenance benefits of centralizing the logic outweigh the drawbacks of reduced performance. It is a judgement call.

2
  • Thank you for the ideas, but it just seems weird to make a connection to the database just to retrieve business logic. Apr 27, 2021 at 5:43
  • @MonkeyBusiness: weird? Yes. Centralized? Also yes. Sometimes you make trade-offs in one area for a gain in another. This is one of those cases. Apr 27, 2021 at 11:07

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.