A Paradigm Shift in Software Development: Leveraging GenAI for Direct Business Logic Processing
Tags: tutorial, programming, softwaredevelopment, ai
Published: 2024-07-17 01:49:15

# A Paradigm Shift in Software Development: Leveraging GenAI for Direct Business Logic Processing

> Generative AI transforms the way we handle business logic

If you read the title and thought I am going to introduce Copilot, you are wrong.

Before we start the topic, let's start with a case study of an e-commerce platform.

Suppose the shopping cart looks like the following.

```json
[
  {
    "product_id": 123,
    "amount": 2,
    "price": 10.99,
    "category_id": 1
  },
  {
    "product_id": 456,
    "amount": 1,
    "price": 29.99,
    "category_id": 2
  },
  {
    "product_id": 789,
    "amount": 5,
    "price": 1.99,
    "category_id": 3
  }
]
```

Each field should be simple enough to contain the item purchased, the quantity purchased, the price of the single item, and the category it belongs to.

I have 3 promotions.

1. $5 off a $20 purchase, which continues to accrue after qualifying.
2. buy 2 get 1 free on category_id 1 items.
3. 30% off the total price of category_id 3 items.

- What is the total price after calculation?
- How much of the discount is allocated to each item?

To implement such promotions, please answer the following questions.

- How long would it take you to write this logic?
- Can you make the logic better than O(n^2)?

The first question is easy to understand, but what does the second question mean?


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lte76lwkkou95fgoe80x.png)



We have three promotions, and to be able to determine the impact of every promotion we need to scan the entire shopping cart for every item. So, in the example above, that's `3 * 3 = 9`, i.e. O(n^2).

### Then

What if I told you:
- I could do it in just a few minutes.
- And it's O(1).

Would you believe me?

Guess how I did it.

### GenAI can help

Although GenAI was mentioned, if you thought I am going to introduce Copilot or similar tools you are very wrong.

It's true that those code generation tools can produce business logic in a matter of minutes, but the business logic they produce will still work the way we think, which means it will still be O(n^2).

So what do we do with GenAI? The answer is simple: let GenAI learn business logic and then answer the results directly.

Sounds unbelievable, right? Let's see how I did it.


[GEMINI DEMO LINK](https://aistudio.google.com/app/prompts?state=%7B%22ids%22:%5B%221l57m9Y-lzOoiVLVSej8RSPlquod_QnFl%22%5D,%22action%22:%22open%22,%22userId%22:%22112447852159053274838%22,%22resourceKeys%22:%7B%7D%7D&usp=sharing)

Even though I'm using Gemini as an example, actually, you can use any model.

### Step 1

First, I'll tell Gemini his role using `System Instructions`.

> You are an e-commerce expert who is well versed in all kinds of promotions and understands how shopping cart profits are calculated.

### Step 2

Next, ask Gemini to explain the structure of a shopping cart that I dropped in. It's important to ask him to explain this. Instead of telling him what it is, it's better to let him understand it for himself so that he can get a more accurate mental model.

> Let's describe a shopping cart in JSON, here's an example.

### Step 3

Tell Gemini what he needs to know about the promotion, and explain in detail what we need. This echoes the question at the beginning of this article. The point of this step, by the way, is not just to explain the promotion, but also to tell him what results to send back.

### Step 4

Based on Gemini's thought process, we have to keep correcting it until his understanding and calculations are correct. Fortunately, GenAI doesn't hide anything. He tells us step by step what he's thinking, so it's easy to find mistakes in the middle. I have to say, it's much easier to debug a natural language than a programmed language.

### Step 5

Ask Gemini to generate a response structure that corresponds to the requirements in step 3, which is why I said we should tell him what we want as early as possible. If we don't tell him as early as possible, we may need to go back and adjust his thought process at this step, which would be very ineffective.

### Final step

Because GenAI will still keep "describing" his answer, we have to tell him, "I don't want to see the process, I just want to see the result, and I don't want any description".

Finally, the business logic is complete.

Wait, that's a little weird. We don't deal with business logic this way by interacting with GenAI.

Yes, that's right! The first step to the last step are all pre-defined "prompts", and we can get the result by wrapping all these prompts and business logic inputs in the same question and asking GenAI.

In fact, it looks like this.

https://gist.github.com/wirelessr/85ed1e1616513a4fcd4bdc3ad5f7874b

The `INSERT_INPUT_HERE` is actually the original structure of our shopping cart promotion calculation.

This process is exactly the same as the popular prompt engineering nowadays.

## Conclusion

In this article we have shown a case study of using GenAI to accomplish business logic.

Let's organize the whole process again.

1. Inform about the role of GenAI
2. Explain the input of the business logic.
3. Describe the requirements of the business logic.
4. Correct GenAI's errors.
5. Generate the output of the business logic.
6. Prune all descriptive statements.

These steps are all centered around prompt engineering, and the more you are familiar with prompt engineering, the quicker the process will be.

The benefits of this process are not only that we can make O(n^2) business logic processing become O(1) as mentioned at the beginning, but also that we can make business logic easier to debug. As I said, it's much easier to catch human speech defects than it is to find bugs in a program.

Nevertheless, there is one important thing to realize about this development process. We have to realize that GenAI is actually a Large Language Model, or LLM, which is not good at computation. So when we use GenAI to write business logic, we still need to have full unit testing to make sure the results are what we expect.

In other words, the importance of unit testing increases rather than decreases with this development process.

When we think of GenAI for software development, we always think of Copilot, but it's much simpler to let GenAI implement business logic directly without generating code.