Refuctoring
(The process of taking a well-designed piece of code and, through a series of
small, reversible changes, making it completely unmaintainable by anyone
except yourself.)
Code Smells
Well it doesn't have a nose... but it
definitely can stink!
Ingredients
● Comments
● Long Method, Large Class
● Long parameter list
● Duplicated code
● Dead Code
● Temporary variables
● Conditional Complexity
● Combinatorial Explosion
● Uncommunicative/Inconsistent
Names
Comments
● The best comment is a good name for a
method or class.
● The code is is supposed to explain itself. In
that case comments are not needed
Comments can be useful
● When explaining why something is being
implemented in a particular way.
● When explaining complex algorithms (when all
other methods for simplifying the algorithm
have been tried and come up short).
Long Methods &
Long Classes (God Class)
Classes usually start small. But over
time, they get bloated as the program
grows.
As is the case with long methods as
well,
Long Parameters
Duplicated
Code
Duplicated code is the bane of
software development. Stamp
out duplication whenever
possible. You should always be
on the lookout for more subtle
cases of near-duplication, too.
Don't Repeat Yourself!
Dead Code
● When requirements for the software
have changed or corrections have been
made, nobody had time to clean up the
old code.
● Such code could also be found in
complex conditionals, when one of
the branches becomes unreachable
(due to error or other circumstances).
Temporary
Variables
● Temporary variables are
quick and easy way to store
and manipulate data around.
● But over time they stick
around you with bad smell.
Conditional Complexity
● Watch out for large conditional logic
blocks, particularly blocks that tend to
grow larger or change significantly over
time. Consider alternative object-
oriented approaches such as decorator,
strategy.
Combinatorial
Explosion
● You have lots of code that
does almost the same thing..
but with tiny variations in
data or behavior.
Uncommunicative/Inconsistent Names
● Does the name of the method succinctly
describe what that method does? Could
you read the method's name to another
developer and have them explain to you
what it does? If not, rename it or rewrite
it.
● $category or $cat ?
● $cat or $c ?
● $subcategory or $SubCategory or
$sub_category or $s_cat or $s_c
There are two ways to write
error-free programs;
Only the third one works.(Alan Jay Perlis)
Shobi P P
shobi@storat.com
Reference
https://blog.codinghorror.com/code-smells/

Code Smells - Refactoring

  • 1.
    Refuctoring (The process oftaking a well-designed piece of code and, through a series of small, reversible changes, making it completely unmaintainable by anyone except yourself.)
  • 2.
    Code Smells Well itdoesn't have a nose... but it definitely can stink! Ingredients ● Comments ● Long Method, Large Class ● Long parameter list ● Duplicated code ● Dead Code ● Temporary variables ● Conditional Complexity ● Combinatorial Explosion ● Uncommunicative/Inconsistent Names
  • 3.
    Comments ● The bestcomment is a good name for a method or class. ● The code is is supposed to explain itself. In that case comments are not needed Comments can be useful ● When explaining why something is being implemented in a particular way. ● When explaining complex algorithms (when all other methods for simplifying the algorithm have been tried and come up short).
  • 4.
    Long Methods & LongClasses (God Class) Classes usually start small. But over time, they get bloated as the program grows. As is the case with long methods as well,
  • 5.
  • 6.
    Duplicated Code Duplicated code isthe bane of software development. Stamp out duplication whenever possible. You should always be on the lookout for more subtle cases of near-duplication, too. Don't Repeat Yourself!
  • 7.
    Dead Code ● Whenrequirements for the software have changed or corrections have been made, nobody had time to clean up the old code. ● Such code could also be found in complex conditionals, when one of the branches becomes unreachable (due to error or other circumstances).
  • 8.
    Temporary Variables ● Temporary variablesare quick and easy way to store and manipulate data around. ● But over time they stick around you with bad smell.
  • 9.
    Conditional Complexity ● Watchout for large conditional logic blocks, particularly blocks that tend to grow larger or change significantly over time. Consider alternative object- oriented approaches such as decorator, strategy.
  • 10.
    Combinatorial Explosion ● You havelots of code that does almost the same thing.. but with tiny variations in data or behavior.
  • 11.
    Uncommunicative/Inconsistent Names ● Doesthe name of the method succinctly describe what that method does? Could you read the method's name to another developer and have them explain to you what it does? If not, rename it or rewrite it. ● $category or $cat ? ● $cat or $c ? ● $subcategory or $SubCategory or $sub_category or $s_cat or $s_c
  • 12.
    There are twoways to write error-free programs; Only the third one works.(Alan Jay Perlis) Shobi P P shobi@storat.com Reference https://blog.codinghorror.com/code-smells/

Editor's Notes

  • #6 getListingBanners()