𝗧𝗵𝗲 𝗖𝗼𝗱𝗲 𝗔𝗜 𝗪𝗼𝗻'𝘁 𝗪𝗿𝗶𝘁𝗲
I use form validation as a technical interview question. It looks simple. The answers reveal how people think.
I tested this problem on Claude, ChatGPT, and Gemini. They all reached for the same solutions.
Most people use a single function with a type parameter to handle different addresses. It works. But every new rule adds a new branch to that same function. The differences stay hidden.
The cleverest human answer I saw used recursion. It walks through the data shape. It is elegant. But it has a flaw. It only validates fields that exist. If a key is missing, the function never sees it. It has no source of truth.
All three AIs made this same mistake. When I pointed out the flaw, they all suggested a schema. A schema-driven approach is technically sound. It handles missing keys and scales well.
But there is a better way: Composition.
Instead of one giant function or a complex schema, you create specific functions for each type.
- One function for a standard address.
- One function for shipping.
- One function for billing.
You combine them to build your validator.
This approach solves the missing key problem. The billing validator always checks for a VAT number, even if the key is missing. It works because a billing address is a real business concept. It is not just a pattern in your data.
The differences are expressed clearly. When a new address type arrives, you add a new function. You do not change old code.
The AI and the best engineers often fall into the same trap. We are taught to find patterns and centralize logic. We try to eliminate duplication at all costs.
AI inherits this instinct from our training data. It prioritizes generalization.
The problem is not that the AI is wrong. It is that the AI rarely asks the most important question: Does this variability live in my code or in my data?
If address types are stable, use composition. If address types change via external data, use a schema.
The simplest solution is not the one with the fewest lines. It is the one that reflects your business domain.
Source: https://dev.to/iceonfire/the-code-ai-wont-write-1ieb
Optional learning community: https://t.me/GyaanSetuAi