The Three Strike Rule

I would like to ask for some examples regarding the application of the 2nd best practice in the ebook, "10* Best Practices to deliver better Apps", which is "Use the 3 strikes rule to encapsulate 'Simple' Queries in user actions.".

Thank you! :)
Hi Ju-chan

I believe that was the title of a presentation during the NextStep 2011.

 "Use the 3 strikes rule to encapsulate 'Simple' Queries in user actions."

That rule pretended to solve some maintenance issues that usually I found when audit some projects.

Sometimes a simple query can be too complex, with too much business login on it (that's why the quotation mark in the 'Simple'). We kept seeing copy paste the same query in an application (the top scenario was around 345 times the same query), and as a best pratice for this scenarios, when you copy more then 3 times the same query you should think what is the best approach, to copy paste as much as needed or to encapsulate it in a user action that you reuse all over the app, this way if you need to update the query you'll only have to update in one place.

Did that anwer your question?


Hi Ricardo,

Now I understand what it means. Thank you!

Ricardo, let me just add something that might be relevant for the discussion.

You should be aware that when you have a Simple Query in the Preparation, the OutSystems Platform analyses how that query is used on the screen and automatically optimizes the query.

To exemplify, imagine that the Entity customer has the attributes {Id, FirstName, LastName, Email, Phone Number, Address}

Scenario 1:
Your screen only displays the customers' {FirstName, LastName}, so the query is optimized to use a Select FirstName, LastName instead of Select *

Scenario 2:
The Table Records you use to display the query result has the LineCount property set to 10. Since you will only display 10 records, there is no need to fetch all of them if you are not going to display them, so the platform optimizes the query to use Select Top (TableRecords.LineCount + 1) instead of a Select.

Why TableRecords.LineCount + 1?
The platform will always fetch one more record than what you will display, this way, when you implement pagination you can test whether the query result has more records than you are currently showing.

Now...When you refactor this Simple Query and move it into a User Action, the platform does not know in which context the query is used, so it cannot optimize it.

So you just have to keep in mind these two factors when developing: optimization vs code maintenance.

And I agree with Ricardo, if you have 345 copies of the same piece of logic, something must be wrong.