Business Logic in Application Code

Learn how to embed business logic in the application code.

What we see in the previous lesson is a direct translation from the business case (or user story if we prefer that term) into a SQL query. The SQL implementation uses joins and computations specific to both the data model and the use case we’re solving.

Use case: Business logic in the application

Another implementation could be done with several queries and the computation in the application’s main code:

  1. Fetch the list of albums for the selected artist.
  2. For each album, fetch the duration of every track in the album.
  3. In the application, sum up the durations per album.

Here’s a very quick way to write such an application. It’s important to include it here because we might recognize patterns found in our own applications, and we want to explain why those patterns should be avoided. Let’s see the following application code in Python:

Get hands-on with 1400+ tech skills courses.