AI|AI Hiring Strategy

Refining Business Logic Through Dialogue

5
Refining Business Logic Through Dialogue

"Create a simple To-Do List app."

GPT-4 can now handle a request like this in 30 seconds. However, the problems we face in the field are rarely this simple.

"A user changes their subscription plan from 'Basic ($10)' to 'Pro ($30)' in the middle of the month. Calculate the prorated difference for the remaining days, charge that amount, maintain the existing billing cycle, and if the payment fails, revert to the existing plan."

There is no AI (yet) that can perfectly implement such complex business requirements with a single prompt. If you force it, it will merely spit out code that looks plausible on the surface but is riddled with bugs.

When implementing complex features, what you need is not "One-Shot Coding," but obsessive dialogue (Ping-Pong) with the AI. Today, we will explore how to design robust business logic using the Chain of Thought (CoT) technique to maximize the AI's reasoning capabilities.

1. Do Not Write Code First (Pseudo-code First)

Opening the IDE immediately when facing a complex problem is a habit of junior developers. Seniors start by drawing on a whiteboard. Collaboration with AI is no different. If you immediately say, "Write this in Python," the AI becomes obsessed with getting the syntax right and loses track of the logic.

First, reach a consensus on the flow of logic using natural language or Pseudo-code.

πŸ’¬ Dialogue Example: First-Come-First-Served Coupon System

Developer:

"I'm going to build a feature that issues coupons to the first 100 people. Concurrency issues are key. We will use Redis, but don't write the code yet. Just explain the logic Flow step-by-step."

AI (CoT Activation):

  • Receive user request.
  • Query the current number of issued coupons (Redis GET).
  • If less than 100, increment the count (Redis INCR).
  • Process coupon issuance (Save to DB).
  • Developer (Critique & Refine):

    "Wait. The Gap between steps 2 and 3 will cause a Race Condition. If another request comes in between the GET and INCR, more than 100 coupons could be issued. Modify the logic to use the fact that Redis INCR is Atomic, or use a Lua Script."

    This process is CoT. We haven't written a single line of code, but we've already caught the most fatal bug in advance.

    2. Torment the AI with "Edge Cases"

    Once the big picture of the logic is drawn, you need to push the AI to its limits. AI tends to default to the "Happy Path" (scenarios where everything succeeds without issues).

    Your job is to become the Devil's Advocate.

    πŸ“ The Obsessive Question List (Prompting)

  • "If the DB transaction succeeds but the Redis cache update fails, how do you resolve the data inconsistency?"
  • "If a user mashes the payment button 5 times in 1 second, how will you prevent double billing?"
  • "There seems to be an O(N^2) section in this logic; is it safe with large datasets?"
  • When you ask these questions, the AI will apologize ("Ah, I missed that part.") and start adding Defensive Code to the logic. As this Tiki-Taka repeats, the logic becomes solid.

    3. Implementation: Translation Comes Later

    Once the consensus on logic and exception handling is finished, converting it into actual code is merely "translation." Only then do you ask for code in a specific language.

    "Based on the logic we agreed upon above (Redis control using Lua Script + DB transaction rollback handling), write the actual service code using Node.js and TypeORM."

    The code produced this way is likely to be "Senior-level code" that reflects far more considerations than if you had written it alone.

    4. Real-World Scenario: Complex Subscription Proration

    Let's assume you are building the "Subscription Upgrade" logic, which is the biggest headache in SaaS development, with AI.

  • Step 1 (Draft): "Write logic to calculate the difference when changing from a $10/mo plan to a $20/mo plan."
  • Step 2 (Inducing CoT): "No, we need Proration. If the month has 30 days and the user used 15, we should only charge the difference for the remaining 15 days. Set up the formula first."
  • Step 3 (Precision Strike): "What about decimals? Round or floor? Also, the payment gateway (Stripe) has a minimum charge of 50 cents. What if the amount is less than that?"
  • Step 4 (Coding): "Perfect. Now turn this into a TypeScript function."
  • Conclusion: The Developer is a "Director," Not a "Coder"

    In this process, you barely did any typing yourself. Instead, you asked, criticized, and verified.

    In the AI era, development capability depends not on "how fast you type code," but on "how logically you lead the AI so it doesn't write stupid code."

    Trust your AI partner. But doubt endlessly and question everything. Within that tense dialogue, masterpiece code is born.


    [Next Step]

    The logic looks perfect. But is this code actually "Clean Code"? In Part 3, we will introduce a sadistic (?) but effective refactoring technique where you throw your code at the AI and command, "This is messy, fix it all."

    Comments (0)

    0/1000 characters
    Loading comments...