Smelling Your CodeRaju MazumderSr. Software EngineerTasawr InteractiveRubyist, Java and PHP Developerlike to work fun and ...
What is Code Smell?
Wikipedia Definition“ code smell is any symptom in the source code of a program  that possibly indicates a deeper problem. ”
Speculative GeneralityLarge Class            Long MethodDead Code             CommentSwitch Case        Primitive Obsessio...
Dead CodeHarder to comprehend the code base.Wasted time - reading through, or even changing dead code.Meaningful Namespubl...
Comment is smellWhich one is clearer?//check is employee activeif ( $employee->isActive() ) {      //do something;}if ( $e...
Duplicate CodeCode repeated in multiple places.
Bad Example<?phpfunction displayPHPExperSpeakerInfo($speakerId){    $speaker = new Speaker();    $speaker = $speaker->find...
Refactor continue...<?phpfunction displayPHPExperSpeakerInfo($speakerId){    $speaker = new Speaker();    $speaker = $spea...
Long MethodLonger Methods are difficult to understandToo many conditions and loopsLong ClassSingle responsibility Principl...
Bad Examplefunction gotoPhpExpertsSeminar2011() {   //wake up morning    //taking my breakfast    //take vehicle    //chec...
Refactor Continue...function gotoPhpExpertsSeminar2011() {    wakeUpMorning();    takeBreakFast();    takeVehicle();    ch...
Long Parameter ListLots of parameters will make code harder tounderstand.
Bad Example..function getSpeakers($type = , $experience = 0,  $zce = null, $newSpeaker = null){  $criteria = array();  if ...
Refactor continue..function getSpeakersByType($type = six_minute){  $speakers = $db->fetchQuery("select * from speakers   ...
Refactor continue..function getSpeakersByOptions(array $options){  $condition = prepareWhere($options);  $speakers = $db->...
Refactor Continue...$options = array(type => six_minute);$sixFameSpeakers = getSpeakersByOptions($options);$options = arra...
Smell Detection ToolsPhpcpdhttps://github.com/sebastianbergmann/phpcpdPHP Code Snifferhttp://pear.php.net/package/PHP_Code...
ResourcesClean Code– Robert C. MartinRefactoring: Improved the design of existing code– Martin Fowler
Thanks“Code is the mirror of the developer”“So lets Write Beautiful Code”Questions?
Upcoming SlideShare
Loading in …5
×

Smelling your code

3,725 views

Published on

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.

Published in: Technology, Business
1 Comment
3 Likes
Statistics
Notes
No Downloads
Views
Total views
3,725
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
36
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

Smelling your code

  1. 1. Smelling Your CodeRaju MazumderSr. Software EngineerTasawr InteractiveRubyist, Java and PHP Developerlike to work fun and challenging environmentWeb: www.rajumazumder.comTwitter: @rajuniit
  2. 2. What is Code Smell?
  3. 3. Wikipedia Definition“ code smell is any symptom in the source code of a program that possibly indicates a deeper problem. ”
  4. 4. Speculative GeneralityLarge Class Long MethodDead Code CommentSwitch Case Primitive Obsession Duplicated Code Long Parameter List Meaningful NamesDivergent Change Shotgun Surgery
  5. 5. Dead CodeHarder to comprehend the code base.Wasted time - reading through, or even changing dead code.Meaningful Namespublic $d; // what does it mean? Days or diameterpublic $days; // cleanpublic function getinvcdtlmt() { //do something}public function getInvoiceableCreditLimit() { //do something}
  6. 6. Comment is smellWhich one is clearer?//check is employee activeif ( $employee->isActive() ) { //do something;}if ( $employee->isActive() ) { //do something;}
  7. 7. Duplicate CodeCode repeated in multiple places.
  8. 8. Bad Example<?phpfunction displayPHPExperSpeakerInfo($speakerId){ $speaker = new Speaker(); $speaker = $speaker->find($speakerId); if($speaker->isSixMinuteSpeaker()) { $name = $speaker->getName(); $designation = $speaker->getDesignation(); echo "Name: ". $name; echo "Designation:". $designation; } else { $name = $speaker->getName(); $designation = $speaker->getDesignation(); $topic = $speaker->getTopicName(); echo "Name:". $name; echo "Designation: " . $designation; echo "Topic: ". $topic; }}displayPHPExperSpeakerInfo(2);
  9. 9. Refactor continue...<?phpfunction 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. 10. Long MethodLonger Methods are difficult to understandToo many conditions and loopsLong ClassSingle responsibility Principle Violated
  11. 11. Bad Examplefunction gotoPhpExpertsSeminar2011() { //wake up morning //taking my breakfast //take vehicle //check confirmation code //take a seat}
  12. 12. Refactor Continue...function gotoPhpExpertsSeminar2011() { wakeUpMorning(); takeBreakFast(); takeVehicle(); checkConfirmationCode(); takeSeat();} Note: We can imrove it more. Its just an example
  13. 13. Long Parameter ListLots of parameters will make code harder tounderstand.
  14. 14. Bad Example..function getSpeakers($type = , $experience = 0, $zce = null, $newSpeaker = null){ $criteria = array(); if ($type) { $criteria[] = "type = {$type} "; } ....... ........ if ($criteria) { $condition = implode(AND , $criteria); } else { $condition = 1=1; } $speakers = $db->fetchQuery("select * from speakers WHERE {$condition}"); return $speakers;}$sixMinuteSpeakers = getSpeakers(six_minute);$zecSpeakers = getSpeakers(,0,true);$tweleveMinuteSpeakers = getSpeakers(twelve_minute);
  15. 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. 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. 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
  18. 18. Smell Detection ToolsPhpcpdhttps://github.com/sebastianbergmann/phpcpdPHP Code Snifferhttp://pear.php.net/package/PHP_CodeSniffer/redirectedPhpdependhttp://pdepend.org/download/index.htmlphplochttps://github.com/sebastianbergmann/phplocphpmdhttp://phpmd.org/
  19. 19. ResourcesClean Code– Robert C. MartinRefactoring: Improved the design of existing code– Martin Fowler
  20. 20. Thanks“Code is the mirror of the developer”“So lets Write Beautiful Code”Questions?

×