𝗧𝗵𝗲 𝗖𝗼𝗱𝗲 𝗔𝗜 𝗪𝗼𝗻'𝘁 𝗪𝗿𝗶𝘁𝗲
I use form validation as a technical interview question. It looks simple. The way people solve it shows how they think.
I tested this problem on Claude, ChatGPT, and Gemini. They all reached for the same solution.
Most people use one of three ways to solve it:
- Using a single function with flags to handle different types.
- Using recursion to walk through the data shape.
- Using a schema to drive the validation.
The AI models all chose the schema approach. It is technically sound. It handles missing keys. It scales well. If your data changes at runtime, the schema is the right choice.
But there is a better way for most software.
Instead of trying to find a single pattern for everything, use composition. Give each business concept its own function.
- Create a basic address validator.
- Create a shipping validator that uses the basic one.
- Create a billing validator that uses the basic one.
This approach does not use recursion. It does not use a schema. It uses clear, separate functions.
The benefit is simple. The code reflects the business. A billing address is a distinct concept. It deserves its own logic. When you add a new type, you do not change an existing function. You add a new one. This keeps your changes local.
AI rarely chooses this path. It chooses the pattern that centralizes logic. Our education teaches us to eliminate duplication and generalize. AI learns from that culture.
The AI is not failing. It is simply following the same engineering instincts we taught it.
The best solution is not the one with the fewest lines of code. It is the one that reflects your business domain most honestly.
Next time you build a solution, ask yourself one question:
Is this variability in my code, or is it in my data?
The answer changes your entire design.
Source: https://dev.to/iceonfire/the-code-ai-wont-write-1ieb