• Like
  • Save
Smelling your code
Upcoming SlideShare
Loading in...5
×
 

Smelling your code

on

  • 2,588 views

Code smell is any symptom in the source code of a program that possibly indicates a deeper problem.

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.

Statistics

Views

Total Views
2,588
Views on SlideShare
2,588
Embed Views
0

Actions

Likes
3
Downloads
31
Comments
1

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Smelling your code Smelling your code Presentation Transcript

    • Smelling Your CodeRaju MazumderSr. Software EngineerTasawr InteractiveRubyist, Java and PHP Developerlike to work fun and challenging environmentWeb: www.rajumazumder.comTwitter: @rajuniit
    • 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 Obsession Duplicated Code Long Parameter List Meaningful NamesDivergent Change Shotgun Surgery
    • 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}
    • Comment is smellWhich one is clearer?//check is employee activeif ( $employee->isActive() ) { //do something;}if ( $employee->isActive() ) { //do something;}
    • Duplicate CodeCode repeated in multiple places.
    • 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);
    • 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
    • Long MethodLonger Methods are difficult to understandToo many conditions and loopsLong ClassSingle responsibility Principle Violated
    • Bad Examplefunction gotoPhpExpertsSeminar2011() { //wake up morning //taking my breakfast //take vehicle //check confirmation code //take a seat}
    • Refactor Continue...function gotoPhpExpertsSeminar2011() { wakeUpMorning(); takeBreakFast(); takeVehicle(); checkConfirmationCode(); takeSeat();} Note: We can imrove it more. Its just an example
    • Long Parameter ListLots of parameters will make code harder tounderstand.
    • 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);
    • 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
    • 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
    • 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
    • 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/
    • 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?