Smelling your code
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Smelling your code

  • 3,284 views
Uploaded on

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.

More in: Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
3,284
On Slideshare
3,283
From Embeds
1
Number of Embeds
1

Actions

Shares
Downloads
33
Comments
1
Likes
3

Embeds 1

https://twitter.com 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Smelling Your CodeRaju MazumderSr. Software EngineerTasawr InteractiveRubyist, Java and PHP Developerlike to work fun and challenging environmentWeb: www.rajumazumder.comTwitter: @rajuniit
  • 2. What is Code Smell?
  • 3. Wikipedia Definition“ code smell is any symptom in the source code of a program that possibly indicates a deeper problem. ”
  • 4. Speculative GeneralityLarge Class Long MethodDead Code CommentSwitch Case Primitive Obsession Duplicated Code Long Parameter List Meaningful NamesDivergent Change Shotgun Surgery
  • 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. Comment is smellWhich one is clearer?//check is employee activeif ( $employee->isActive() ) { //do something;}if ( $employee->isActive() ) { //do something;}
  • 7. Duplicate CodeCode repeated in multiple places.
  • 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. 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. Long MethodLonger Methods are difficult to understandToo many conditions and loopsLong ClassSingle responsibility Principle Violated
  • 11. Bad Examplefunction gotoPhpExpertsSeminar2011() { //wake up morning //taking my breakfast //take vehicle //check confirmation code //take a seat}
  • 12. Refactor Continue...function gotoPhpExpertsSeminar2011() { wakeUpMorning(); takeBreakFast(); takeVehicle(); checkConfirmationCode(); takeSeat();} Note: We can imrove it more. Its just an example
  • 13. Long Parameter ListLots of parameters will make code harder tounderstand.
  • 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. 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
  • 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. ResourcesClean Code– Robert C. MartinRefactoring: Improved the design of existing code– Martin Fowler
  • 20. Thanks“Code is the mirror of the developer”“So lets Write Beautiful Code”Questions?