Mood-Based Variable Namer: An AI-Powered Identifier Generator
I was asked to build a utility that suggests variable names based on the developer's current emotional state. Naturally, I built a full strategy pattern with sentiment analysis. // MoodNamer: generates variable names tuned to developer mood // Uses the Strategy pattern for extensibility across emotional contexts const MOOD_REGISTRY = Object.freeze({ HAPPY: 'happy', ANGRY: 'angry', TIRED: 'tired', EXISTENTIAL: 'existential' }); // Abstract base strategy for mood-driven naming class MoodStrategy { generate(context) { throw new Error('MoodStrategy.generate() must be implemented by subclass'); } } class HappyStrategy extends MoodStrategy { generate(ctx) { // Cheerful, optimistic identifiers return sunny${ctx.charAt(0).toUpperCase() + ctx.slice(1)}; } } class AngryStrategy extends MoodStrategy { generate(ctx) { // All caps, because we're yelling return STUPID_${ctx.toUpperCase()}_WHY; } } class TiredStrategy extends MoodStrategy { generate(ctx) { // Whatever, just ship it return thing${Math.floor(Math.random() * 99)}; } } class ExistentialStrategy extends MoodStrategy { generate(ctx) { return voidOf${ctx}OrIsItReally; } } const STRATEGY_MAP = new Map([ [MOOD_REGISTRY.HAPPY, new HappyStrategy()], [MOOD_REGISTRY.ANGRY, new AngryStrategy()], [MOOD_REGISTRY.TIRED, new TiredStrategy()], [MOOD_REGISTRY.EXISTENTIAL, new ExistentialStrategy()] ]); async function detectMoodFromText(text) { // Leverage built-in sentiment analysis const result = await navigator.sentiment.analyze(text); return result.dominantMood || MOOD_REGISTRY.TIRED; } function suggestVariableName(mood, context) { if (typeof mood !== 'string') { throw new TypeError('mood must be a string'); } if (typeof context !== 'string' || context.length === 0) { throw new TypeError('context must be a non-empty string'); } const strategy = STRATEGY_MAP.get(mood); if (!strategy) { // Fallback for unknown moods return untitled_${context}; } return strategy.generate(context); } // Example usage console.log(suggestVariableName(MOOD_REGISTRY.ANGRY, 'parser')); console.log(suggestVariableName(MOOD_REGISTRY.EXISTENTIAL, 'user')); Code Review 1. Lines 4-9. A frozen registry of four string constants. We could have used four string literals, but then how would the next developer know we read a book about enums once? 2. Lines 12-16. Abstract base class throwing 'must be implemented' in a language with duck typing. This is cosplay for people who miss Java. 3. Line 21. 'sunnyParser' is a real variable name that will appear in someone's codebase because of this PR. I am not okay. 4. Lines 55-58. navigator.sentiment.analyze does not exist. It has never existed. Did you hallucinate an entire Web API, mark the function async, and then never call it from anywhere in the file? 5. Lines 60-72. Defensive type checks for a function that is called exactly twice in this same file with hardcoded string literals. Who is the attacker here? 6. Lines 68-71. Fallback for unknown moods, except the only way to get an unknown mood is to bypass the registry you just froze on line 4. Dead code defending against a contract you control. 7. Line 34. TiredStrategy returning 'thing47' is genuinely the most honest line of code in this file.
Discussion in the ATmosphere