कोड से XOOPS विकास की गंध आती है
अवलोकन
Section titled “अवलोकन”कोड गंध कोड में संभावित समस्याओं के संकेतक हैं। उनका मतलब यह नहीं है कि कोड टूट गया है, लेकिन वे ऐसे क्षेत्रों का सुझाव देते हैं जो रिफैक्टरिंग से लाभान्वित हो सकते हैं।
सामान्य कोड गंध
Section titled “सामान्य कोड गंध”flowchart TD A[Code Smell Detected] --> B{Type?} B --> C[Bloaters] B --> D[Object-Orientation Abusers] B --> E[Change Preventers] B --> F[Dispensables] B --> G[Couplers]
C --> C1[Long Method] C --> C2[Large Class] C --> C3[Long Parameter List]
D --> D1[Switch Statements] D --> D2[Temporary Field]
E --> E1[Divergent Change] E --> E2[Shotgun Surgery]
F --> F1[Dead Code] F --> F2[Duplicate Code]
G --> G1[Feature Envy] G --> G2[Inappropriate Intimacy]ब्लोटर्स
Section titled “ब्लोटर्स”लंबी विधि
Section titled “लंबी विधि”// Smell: Method does too muchfunction processArticleSubmission($data) { // 100+ lines of validation, saving, notification, etc.}
// Solution: Extract into focused methodsfunction processArticleSubmission(array $data): Article{ $this->validateInput($data); $article = $this->createArticle($data); $this->saveArticle($article); $this->notifySubscribers($article); return $article;}बड़ा वर्ग (भगवान वस्तु)
Section titled “बड़ा वर्ग (भगवान वस्तु)”// Smell: Class does everythingclass ArticleManager { public function create() { ... } public function delete() { ... } public function sendEmail() { ... } public function generatePDF() { ... } public function exportToExcel() { ... } public function validateUser() { ... } public function checkPermissions() { ... } // ... 50 more methods}
// Solution: Split into focused classesclass ArticleService { ... }class ArticleExporter { ... }class ArticleNotifier { ... }class PermissionChecker { ... }लंबी पैरामीटर सूची
Section titled “लंबी पैरामीटर सूची”// Smell: Too many parametersfunction createArticle($title, $content, $author, $category, $tags, $status, $publishDate, $featured, $image) { ... }
// Solution: Use parameter objectclass CreateArticleCommand { public string $title; public string $content; public int $authorId; public int $categoryId; public array $tags; public string $status; public ?DateTime $publishDate; public bool $featured; public ?string $image;}
function createArticle(CreateArticleCommand $command): Article { ... }वस्तु-अभिविन्यास का दुरुपयोग करने वाले
Section titled “वस्तु-अभिविन्यास का दुरुपयोग करने वाले”स्टेटमेंट स्विच करें
Section titled “स्टेटमेंट स्विच करें”// Smell: Type checking with switchfunction getDiscount($userType) { switch ($userType) { case 'regular': return 0; case 'premium': return 10; case 'vip': return 20; default: return 0; }}
// Solution: Use polymorphisminterface UserType { public function getDiscount(): int;}
class RegularUser implements UserType { public function getDiscount(): int { return 0; }}
class PremiumUser implements UserType { public function getDiscount(): int { return 10; }}
class VipUser implements UserType { public function getDiscount(): int { return 20; }}अस्थायी क्षेत्र
Section titled “अस्थायी क्षेत्र”// Smell: Fields only used in certain situationsclass Article { private $tempCalculatedScore;
public function search($terms) { $this->tempCalculatedScore = $this->calculateScore($terms); // ... use score }}
// Solution: Pass as parameter or return valueclass Article { public function getSearchScore(array $terms): float { return $this->calculateScore($terms); }}परिवर्तन निरोधक
Section titled “परिवर्तन निरोधक”भिन्न परिवर्तन
Section titled “भिन्न परिवर्तन”// Smell: One class changed for many different reasonsclass Article { public function save() { ... } // Database change public function toJson() { ... } // API format change public function validate() { ... } // Business rule change public function render() { ... } // UI change}
// Solution: Separate responsibilitiesclass Article { ... } // Domain object onlyclass ArticleRepository { public function save() { ... } }class ArticleSerializer { public function toJson() { ... } }class ArticleValidator { public function validate() { ... } }शॉटगन सर्जरी
Section titled “शॉटगन सर्जरी”// Smell: One change requires many file edits// Changing date format requires editing:// - ArticleController.php// - ArticleView.php// - ArticleAPI.php// - ArticleExport.php
// Solution: Centralizeclass DateFormatter { public function format(DateTime $date): string { return $date->format($this->config->get('date_format')); }}डिस्पेंसेबल्स
Section titled “डिस्पेंसेबल्स”डेड कोड
Section titled “डेड कोड”// Smell: Unreachable or unused codefunction processData($data) { if (true) { return $this->handleData($data); } // This never executes return $this->legacyHandler($data);}
// Old unused method still in codebasefunction oldMethod() { // Not called anywhere}
// Solution: Remove dead codefunction processData($data) { return $this->handleData($data);}डुप्लिकेट कोड
Section titled “डुप्लिकेट कोड”// Smell: Same logic in multiple placesclass ArticleHandler { public function getActive() { $criteria = new CriteriaCompo(); $criteria->add(new Criteria('status', 'active')); return $this->getObjects($criteria); }}
class NewsHandler { public function getActive() { $criteria = new CriteriaCompo(); $criteria->add(new Criteria('status', 'active')); return $this->getObjects($criteria); }}
// Solution: Extract common behaviortrait ActiveRecordsTrait { public function getActive(): array { $criteria = new CriteriaCompo(); $criteria->add(new Criteria('status', 'active')); return $this->getObjects($criteria); }}कप्लर्स
Section titled “कप्लर्स”फ़ीचर ईर्ष्या
Section titled “फ़ीचर ईर्ष्या”// Smell: Method uses another object's data more than its ownclass Invoice { public function calculateTotal(Customer $customer) { $total = 0; foreach ($this->items as $item) { $total += $item->price; } // Uses customer data extensively if ($customer->isPremium()) { $total *= (1 - $customer->getDiscountRate()); } if ($customer->getCountry() === 'US') { $total *= 1.08; // Tax } return $total; }}
// Solution: Move behavior to the object with the dataclass Customer { public function applyDiscount(float $amount): float { return $this->isPremium() ? $amount * (1 - $this->discountRate) : $amount; }
public function applyTax(float $amount): float { return $this->country === 'US' ? $amount * 1.08 : $amount; }}रिफैक्टरिंग चेकलिस्ट
Section titled “रिफैक्टरिंग चेकलिस्ट”जब आपको कोई कोड गंध दिखे:
- पहचानें - यह कौन सी गंध है?
- आकलन - प्रभाव कितना गंभीर है?
- योजना - कौन सी रीफैक्टरिंग तकनीक लागू होती है?
- परीक्षण - सुनिश्चित करें कि रिफैक्टरिंग से पहले परीक्षण मौजूद हैं
- रिफैक्टर - छोटे, वृद्धिशील परिवर्तन करें
- सत्यापित करें - प्रत्येक परिवर्तन के बाद परीक्षण चलाएँ
संबंधित दस्तावेज़ीकरण
Section titled “संबंधित दस्तावेज़ीकरण”- स्वच्छ कोड सिद्धांत
- कोड संगठन
- सर्वोत्तम प्रथाओं का परीक्षण