Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Čtvrtkon #1 - Refactoring
Search
Václav Novotný
June 07, 2013
Technology
0
43
Čtvrtkon #1 - Refactoring
Václav Novotný
June 07, 2013
Tweet
Share
More Decks by Václav Novotný
See All by Václav Novotný
Čtvrtkon 06.06. 2013 - Composer
ven
1
730
Čtvrtkon #1 - Čistý kód
ven
0
60
Čtvrtkon #3 - Git
ven
0
28
Other Decks in Technology
See All in Technology
フルカイテン株式会社 エンジニア向け採用資料
fullkaiten
0
9.9k
Knowledge Work の AI Backend
kworkdev
PRO
0
100
意外と知らない状態遷移テストの世界
nihonbuson
PRO
1
190
マイクロサービスへの5年間 ぶっちゃけ何をしてどうなったか
joker1007
18
7.4k
アプリにAIを正しく組み込むための アーキテクチャ── 国産LLMの現実と実践
kohju
0
190
SQLだけでマイグレーションしたい!
makki_d
0
1.2k
20251218_AIを活用した開発生産性向上の全社的な取り組みの進め方について / How to proceed with company-wide initiatives to improve development productivity using AI
yayoi_dd
0
610
コンテキスト情報を活用し個社最適化されたAI Agentを実現する4つのポイント
kworkdev
PRO
1
1.8k
AI時代のワークフロー設計〜Durable Functions / Step Functions / Strands Agents を添えて〜
yakumo
3
1.9k
IAMユーザーゼロの運用は果たして可能なのか
yama3133
2
520
ペアーズにおけるAIエージェント 基盤とText to SQLツールの紹介
hisamouna
2
1.4k
特別捜査官等研修会
nomizone
0
530
Featured
See All Featured
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
320
Bash Introduction
62gerente
615
210k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
31
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
0
290
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
1
200
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
0
210
WENDY [Excerpt]
tessaabrams
8
35k
Designing for Timeless Needs
cassininazir
0
88
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.6k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
The Cult of Friendly URLs
andyhume
79
6.7k
Speed Design
sergeychernyshev
33
1.4k
Transcript
Refactoring Václav Novotný, 6. září 2012, Čtvrtkon #1 (http://www.ctvrtkon.cz)
Problém Máme špatný kód. Jak z toho ven?
Řešením je refactoring zlepšování kvality bez změny funkčnosti
Příklad špatného kódu <?php $articles = $articleRepository->findAll(); foreach ($articles as
$article) { echo $article->getTitle(); echo $article->getText(); }
Trochu vylepšený kód <?php $articles = $articleRepository->findAll(); foreach ($articles as
$article) { printArticle($article); } function printArticle(Article $article) { echo $article->getTitle(); echo $article->getText(); }
Co jsme udělali? Snížili jsme složitost programu, aniž bychom změnili
jeho funkcionalitu.
Další příklad <?php // ArticleController.php if ($article->getPublishingDate() == date('Y-m-d') &&
$article->getStatus() == 2 && count($article->getComments()) > 0) { $article->setScore(10); }
Další příklad <?php // ArticleController.php if ($article->isPublishedToday() && $article->getStatus() ==
2 && count($article->getComments()) > 0) { $article->setScore(10); }
Další příklad <?php // ArticleController.php if ($article->isPublishedToday() && $article->isPublished() &&
count($article->getComments()) > 0) { $article->setScore(10); }
Další příklad <?php // ArticleController.php if ($article->isPublishedToday() && $article->isPublished() &&
$article->hasComments()) { $article->setScore(10); }
Další příklad <?php // ArticleController.php if ($article->isPublishedToday() && $article->isPublished() &&
$article->hasComments()) { $article->promoteArticle(); }
Další příklad <?php // ArticleController.php $articlePromoter->promoteArticle($article); // ArticlePromoter.php const TOP_ARTICLE
= 10; public function promoteArticle(Article $article) { if ($article->isPublishedToday() && $article->isPublished() && $article->hasComments()) { $article->setScore(self::TOP_ARTICLE); }
Kam až mám jít? Je to hodně o citu a
o domluvě v týmu.
Existuje chytrá kniha!
None
Je ale trochu nudná
Existuje chytrý web (trochu méně nudný) http://www.sourcemaking.com
Jak poznat, kde to hnije?
Pear rozšíření PHPMD PHP Mess Detector
None
Co PHPMD dělá? Pomocí statistické analýzy kódu ukazuje místa vhodná
pro refactoring.
Code Size Rules • Cyclomatic complexity • N-path complexity •
ExcessiveMethodLength • ExcessiveClassLength • ExcessiveParameterList • ExcessivePublicCount • TooManyFields • TooManyMethods • ExcessiveClassComplexity
Design Rules • ExitExpression • EvalExpression • GotoStatement • NumberOfChildren
• DepthOfInheritance • CouplingBetweenObjects
Naming Rules • ShortVariable • LongVariable • ShortMethodName • ConstructorWithNameAsEnclosingClass
• ConstantNamingConventions • BooleanGetMethodName
Vlastní pravidla Můžete si napsat vlastní pravidla. Třída implementující rozhraní
PHP_PMD_Rule.
Refactoring
Pamatujte • nesmíte přidávat novou funkcionalitu • dělejte malé kroky
• opravdu dělejte malé kroky!
Pamatujte #2 • bez testů se refactoruje těžko • jde
o nedílnou součást vývoje • refactorovaný kód co nejčastěji začleňujte zpět do hlavní vývojové větve
Dotazy?
Děkuji za pozornost @VaclavNovotny