Sr. Software Engineer, Consultant em Tasawr Interactive
18 de Dec de 2011•0 gostou•3,307 visualizações
1 de 20
Smelling your code
18 de Dec de 2011•0 gostou•3,307 visualizações
Baixar para ler offline
Denunciar
Tecnologia
Negócios
Code smell is any symptom in the source code of a program that possibly indicates a deeper problem.
So let's see what are code smell we can remove from our code.
1. Smelling Your Code
Raju Mazumder
Sr. Software Engineer
Tasawr Interactive
Rubyist, Java and PHP Developer
like to work fun and challenging environment
Web: www.rajumazumder.com
Twitter: @rajuniit
3. Wikipedia Definition
“ code smell is any symptom in the source code of a program
that possibly indicates a deeper problem. ”
4. Speculative Generality
Large Class Long Method
Dead Code Comment
Switch Case Primitive Obsession
Duplicated Code
Long Parameter List
Meaningful Names
Divergent Change Shotgun Surgery
5. Dead Code
Harder to comprehend the code base.
Wasted time - reading through, or even changing dead code.
Meaningful Names
public $d; // what does it mean? Days or diameter
public $days; // clean
public function getinvcdtlmt() {
//do something
}
public function getInvoiceableCreditLimit() {
//do something
}
6. Comment is smell
Which one is clearer?
//check is employee active
if ( $employee->isActive() ) {
//do something;
}
if ( $employee->isActive() ) {
//do something;
}
9. Refactor continue...
<?php
function displayPHPExperSpeakerInfo($speakerId)
{
$speaker = new Speaker();
$speaker = $speaker->find($speakerId);
displayGeneralInformation($speaker);
if ($speaker->isTwelveMinuteSpeaker()) {
$topic = $speaker->getTopicName();
echo "Topic: " . $topic;
}
}
function displayGeneralInformation($speaker)
{
$name = $speaker->getName();
$designation = $speaker->getDesignation();
echo "Name: " . $name;
echo "Designation:" . $designation;
}
displayPHPExperSpeakerInfo(2);
Note: We can imrove it more. Its just an example
10. Long Method
Longer Methods are difficult to understand
Too many conditions and loops
Long Class
Single responsibility Principle Violated
15. Refactor continue..
function getSpeakersByType($type = 'six_minute')
{
$speakers = $db->fetchQuery("select * from speakers
WHERE type = '{$type}'");
return $speakers;
}
function getZceSpeakers()
{
$speakers = $db->fetchQuery("select * from speakers
WHERE zce = 1");
return $speakers;
}
$sixMinuteSpeakers = getSpeakersByType();
$zecSpeakers = getZceSpeakers();
$tweleveMinuteSpeakers = getSpeakersByType('twelve_minute');
Note: We can imrove it more. Its just an example
16. Refactor continue..
function getSpeakersByOptions(array $options)
{
$condition = prepareWhere($options);
$speakers = $db->fetchQuery("select * from speakers
WHERE {$condition}");
return $speakers;
}
function prepareWhere($options)
{
$criteria = array();
foreach ($options as $option => $value) {
switch ($option) {
case 'type': $criteria[] = "type = '{$value}' "; break;
case 'zce': $criteria[] = "zce = '{$value}' "; break;
case 'experience': $criteria[] = "experience = '{$value}' "; break;
default: break;
}
}
if ($criteria) {
$condition = implode('AND ', $criteria);
} else {
$condition = '1=1';
}
return $condition;
} Note: We can imrove it more. Its just an example
17. Refactor Continue...
$options = array('type' => 'six_minute');
$sixFameSpeakers = getSpeakersByOptions($options);
$options = array('zce' => 1);
$zecSpeakers = getSpeakersByOptions($options);
$options = array('type' => 'twelve_minute');
$tweleveMinuteSpeakers = getSpeakersByOptions($options);
Note: We can imrove it more. Its just an example