Claude Code के साथ Implement Feature Flags कैसे करें
Claude Code का उपयोग करके implement feature flags सीखें। Practical code examples और step-by-step guidance शामिल है।
フィーチャーフラグ क्या है
フィーチャーフラグ(Feature Flags)は、code deployしたबादでもfeaturesのオン・オフを切り替えられる仕組み है।段階的なリリースやA/Btestに不可欠な技術であり、Claude Code का उपयोग करकेrobustなimplementationを素早くbuild किया जा सकता है。
basic フィーチャーフラグシステム
> TypeScriptでフィーチャーフラグのmanagementシステムを作って。
> user属性に基づく条件付きフラグにもsupportして。
type FlagValue = boolean | string | number;
interface FlagRule {
conditions: Record<string, any>;
value: FlagValue;
}
interface FeatureFlag {
key: string;
defaultValue: FlagValue;
description: string;
rules: FlagRule[];
enabled: boolean;
}
class FeatureFlagManager {
private flags: Map<string, FeatureFlag> = new Map();
register(flag: FeatureFlag) {
this.flags.set(flag.key, flag);
}
evaluate(key: string, context: Record<string, any> = {}): FlagValue {
const flag = this.flags.get(key);
if (!flag || !flag.enabled) return flag?.defaultValue ?? false;
for (const rule of flag.rules) {
if (this.matchConditions(rule.conditions, context)) {
return rule.value;
}
}
return flag.defaultValue;
}
private matchConditions(conditions: Record<string, any>, context: Record<string, any>): boolean {
return Object.entries(conditions).every(([key, value]) => {
if (Array.isArray(value)) return value.includes(context[key]);
return context[key] === value;
});
}
isEnabled(key: string, context?: Record<string, any>): boolean {
return Boolean(this.evaluate(key, context));
}
}
React用hookのimplementation
> Reactcomponentでフィーチャーフラグ use करनाためのカスタムhookを作って。
import { createContext, useContext, ReactNode } from 'react';
const FlagContext = createContext<FeatureFlagManager | null>(null);
export function FlagProvider({ manager, children }: { manager: FeatureFlagManager; children: ReactNode }) {
return <FlagContext.Provider value={manager}>{children}</FlagContext.Provider>;
}
export function useFeatureFlag(key: string, context?: Record<string, any>): boolean {
const manager = useContext(FlagContext);
if (!manager) throw new Error('FlagProviderがज़रूरीです');
return manager.isEnabled(key, context);
}
// Usage example
function NewDashboard() {
const showNewUI = useFeatureFlag('new-dashboard', { userId: currentUser.id });
if (showNewUI) {
return <ModernDashboard />;
}
return <LegacyDashboard />;
}
パーセンテージロールアウト
特定の割合のuserにだけfeaturesを公開する段階的リリースもimplementationでき है।
function percentageRollout(userId: string, percentage: number): boolean {
const hash = Array.from(userId).reduce((acc, char) => {
return ((acc << 5) - acc + char.charCodeAt(0)) | 0;
}, 0);
return Math.abs(hash % 100) < percentage;
}
// フラグ登録時にパーセンテージをsettings
flagManager.register({
key: 'new-checkout',
defaultValue: false,
description: 'नयाcheckアウトフロー',
enabled: true,
rules: [{
conditions: { rolloutPercentage: 20 },
value: true,
}],
});
フラグのmanagement画面
運用を楽にするため、フラグの状態をAPIでmanagementする仕組みもbuild किया जा सकता है。
import express from 'express';
const router = express.Router();
router.get('/api/flags', (req, res) => {
const flags = Array.from(flagManager.getAllFlags()).map(([key, flag]) => ({
key,
enabled: flag.enabled,
description: flag.description,
}));
res.json(flags);
});
router.patch('/api/flags/:key', (req, res) => {
const { enabled } = req.body;
flagManager.setEnabled(req.params.key, enabled);
res.json({ success: true });
});
Summary
フィーチャーフラグをintroductionする बातで、safeな段階的リリースとリスクの低減がpossibleになり है।Claude Code से条件分岐ロジックやReacthook、managementAPI तक一貫してgenerateでき है।A/Btestとの組み合わせはA/Btestimplementationガイドを、状態managementの設計は状態managementの記事をदेखें。
フィーチャーフラグの設計patternके बारे मेंはMartin Fowler - Feature Togglesが詳しい है।
Related Posts
Claude Code से Productivity 3 गुना बढ़ाने की 10 Tips
Claude Code से ज़्यादा पाने की 10 practical tips जानें। Prompt strategies से workflow shortcuts तक, ये techniques आज से ही आपकी efficiency boost करेंगी।
Claude Code के साथ Canvas/WebGL Optimization
Claude Code का उपयोग करके Canvas/WebGL optimization के बारे में जानें। Practical tips और code examples शामिल हैं।
Claude Code के साथ Markdown Implementation
Claude Code का उपयोग करके markdown implementation सीखें। Practical tips और code examples शामिल हैं।