<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Rjlloyd</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Rjlloyd"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Rjlloyd"/>
	<updated>2026-08-15T14:56:50Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83511</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83511"/>
		<updated>2014-02-19T05:42:54Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Metrics */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
For the page with a history of edits, go to [http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2014/ch1a_1w1e_rm this page].&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve its readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
In their Refactoring book&amp;lt;ref name=&amp;quot;Refactoring book&amp;quot;&amp;gt;http://books.google.com/books?id=1MsETFPD3I0C&amp;lt;/ref&amp;gt;, Martin Fowler and Kent Beck define refactoring as 'A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior… It is a disciplined way to clean up code that minimizes the chances of introducing bugs'.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of errors that can be made during refactoring, so it is pertinent to determine when it is necessary to refactor. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
Two main types of refactoring are &amp;lt;ref&amp;gt;http://web.cecs.pdx.edu/~black/publications/IEEESoftwareRefact.pdf&amp;lt;/ref&amp;gt;: &lt;br /&gt;
# Floss refactoring: refactoring performed regularly to keep code clean; gives healthy benefits in the long run&lt;br /&gt;
# Root canal refactoring: infrequent and protracted periods of refactoring; makes it painful and expensive (similar to dental analogy); mainly performed to clean unhealthy code&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by its children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def foo&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Method Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def greet&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def farewell&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def foo&lt;br /&gt;
  greet&lt;br /&gt;
  farewell&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  greet&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractSuperclass.html Example &amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Class Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class FullName&lt;br /&gt;
  def initialize(f,m,l)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @name = FullName.new(f,m,l)&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
==Metric Tools==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with its own functionality.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list for code coverage. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
For cyclomatic complexity (the number of possible paths),  Saikuro is commonly employed and made to be easy to implement. For code complexity, Flog is the typical choice by the open source community of users.&amp;lt;ref&amp;gt;http://www.sitepoint.com/code-metrics-and-you/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://books.google.com/books?id=6jyOUrJBJHAC&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
===When NOT to refactor:===&lt;br /&gt;
* Avoid refactoring when you should ideally be rewriting the entire code. Although a tough call to make, rewriting is inevitable when current code does not work at all or is too buggy to stabilize. &lt;br /&gt;
* Avoid refactoring when close to a deadline since productivity gain by refactoring is mostly seen after the deadline and might just cause the project to miss the deadline.&lt;br /&gt;
* Avoid refactoring for academic purposes, i.e. avoid changing code in working condition because you do not agree with those lines of code or that is not how you would have implemented it.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://ghendry.net/refactor.html A description of smells with their techniques exists]&lt;br /&gt;
&lt;br /&gt;
*[http://www.refactoring.com/catalog/index.html A list of techniques with examples in Ruby are listed]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://books.google.com/books?id=i6mZ0HBDPzsC Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83510</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83510"/>
		<updated>2014-02-19T05:42:08Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* More Techniques */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
For the page with a history of edits, go to [http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2014/ch1a_1w1e_rm this page].&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve its readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
In their Refactoring book&amp;lt;ref name=&amp;quot;Refactoring book&amp;quot;&amp;gt;http://books.google.com/books?id=1MsETFPD3I0C&amp;lt;/ref&amp;gt;, Martin Fowler and Kent Beck define refactoring as 'A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior… It is a disciplined way to clean up code that minimizes the chances of introducing bugs'.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of errors that can be made during refactoring, so it is pertinent to determine when it is necessary to refactor. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
Two main types of refactoring are &amp;lt;ref&amp;gt;http://web.cecs.pdx.edu/~black/publications/IEEESoftwareRefact.pdf&amp;lt;/ref&amp;gt;: &lt;br /&gt;
# Floss refactoring: refactoring performed regularly to keep code clean; gives healthy benefits in the long run&lt;br /&gt;
# Root canal refactoring: infrequent and protracted periods of refactoring; makes it painful and expensive (similar to dental analogy); mainly performed to clean unhealthy code&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by its children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def foo&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Method Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def greet&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def farewell&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def foo&lt;br /&gt;
  greet&lt;br /&gt;
  farewell&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  greet&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractSuperclass.html Example &amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Class Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class FullName&lt;br /&gt;
  def initialize(f,m,l)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @name = FullName.new(f,m,l)&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with its own functionality.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list for code coverage. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
For cyclomatic complexity (the number of possible paths),  Saikuro is commonly employed and made to be easy to implement. For code complexity, Flog is the typical choice by the open source community of users.&amp;lt;ref&amp;gt;http://www.sitepoint.com/code-metrics-and-you/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://books.google.com/books?id=6jyOUrJBJHAC&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
===When NOT to refactor:===&lt;br /&gt;
* Avoid refactoring when you should ideally be rewriting the entire code. Although a tough call to make, rewriting is inevitable when current code does not work at all or is too buggy to stabilize. &lt;br /&gt;
* Avoid refactoring when close to a deadline since productivity gain by refactoring is mostly seen after the deadline and might just cause the project to miss the deadline.&lt;br /&gt;
* Avoid refactoring for academic purposes, i.e. avoid changing code in working condition because you do not agree with those lines of code or that is not how you would have implemented it.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://ghendry.net/refactor.html A description of smells with their techniques exists]&lt;br /&gt;
&lt;br /&gt;
*[http://www.refactoring.com/catalog/index.html A list of techniques with examples in Ruby are listed]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://books.google.com/books?id=i6mZ0HBDPzsC Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83509</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83509"/>
		<updated>2014-02-19T05:37:05Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Best Practices */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
For the page with a history of edits, go to [http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2014/ch1a_1w1e_rm this page].&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve its readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
In their Refactoring book&amp;lt;ref name=&amp;quot;Refactoring book&amp;quot;&amp;gt;http://books.google.com/books?id=1MsETFPD3I0C&amp;lt;/ref&amp;gt;, Martin Fowler and Kent Beck define refactoring as 'A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior… It is a disciplined way to clean up code that minimizes the chances of introducing bugs'.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of errors that can be made during refactoring, so it is pertinent to determine when it is necessary to refactor. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
Two main types of refactoring are &amp;lt;ref&amp;gt;http://web.cecs.pdx.edu/~black/publications/IEEESoftwareRefact.pdf&amp;lt;/ref&amp;gt;: &lt;br /&gt;
# Floss refactoring: refactoring performed regularly to keep code clean; gives healthy benefits in the long run&lt;br /&gt;
# Root canal refactoring: infrequent and protracted periods of refactoring; makes it painful and expensive (similar to dental analogy); mainly performed to clean unhealthy code&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by its children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def foo&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Method Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def greet&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def farewell&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def foo&lt;br /&gt;
  greet&lt;br /&gt;
  farewell&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  greet&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractSuperclass.html Example &amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Class Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class FullName&lt;br /&gt;
  def initialize(f,m,l)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @name = FullName.new(f,m,l)&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There many more techniques than those that are listed above. Each has its specific purpose, though one must take care to make sure that it is a necessary refactoring.&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with its own functionality.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list for code coverage. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
For cyclomatic complexity (the number of possible paths),  Saikuro is commonly employed and made to be easy to implement. For code complexity, Flog is the typical choice by the open source community of users.&amp;lt;ref&amp;gt;http://www.sitepoint.com/code-metrics-and-you/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://books.google.com/books?id=6jyOUrJBJHAC&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
===When NOT to refactor:===&lt;br /&gt;
* Avoid refactoring when you should ideally be rewriting the entire code. Although a tough call to make, rewriting is inevitable when current code does not work at all or is too buggy to stabilize. &lt;br /&gt;
* Avoid refactoring when close to a deadline since productivity gain by refactoring is mostly seen after the deadline and might just cause the project to miss the deadline.&lt;br /&gt;
* Avoid refactoring for academic purposes, i.e. avoid changing code in working condition because you do not agree with those lines of code or that is not how you would have implemented it.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://ghendry.net/refactor.html A description of smells with their techniques exists]&lt;br /&gt;
&lt;br /&gt;
*[http://www.refactoring.com/catalog/index.html A list of techniques with examples in Ruby are listed]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://books.google.com/books?id=i6mZ0HBDPzsC Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83507</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83507"/>
		<updated>2014-02-19T05:32:08Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Metrics */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
For the page with a history of edits, go to [http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2014/ch1a_1w1e_rm this page].&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve its readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
In their Refactoring book&amp;lt;ref name=&amp;quot;Refactoring book&amp;quot;&amp;gt;http://books.google.com/books?id=1MsETFPD3I0C&amp;lt;/ref&amp;gt;, Martin Fowler and Kent Beck define refactoring as 'A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior… It is a disciplined way to clean up code that minimizes the chances of introducing bugs'.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of errors that can be made during refactoring, so it is pertinent to determine when it is necessary to refactor. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
Two main types of refactoring are &amp;lt;ref&amp;gt;http://web.cecs.pdx.edu/~black/publications/IEEESoftwareRefact.pdf&amp;lt;/ref&amp;gt;: &lt;br /&gt;
# Floss refactoring: refactoring performed regularly to keep code clean; gives healthy benefits in the long run&lt;br /&gt;
# Root canal refactoring: infrequent and protracted periods of refactoring; makes it painful and expensive (similar to dental analogy); mainly performed to clean unhealthy code&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by its children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def foo&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Method Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def greet&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def farewell&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def foo&lt;br /&gt;
  greet&lt;br /&gt;
  farewell&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  greet&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractSuperclass.html Example &amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Class Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class FullName&lt;br /&gt;
  def initialize(f,m,l)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @name = FullName.new(f,m,l)&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There many more techniques than those that are listed above. Each has its specific purpose, though one must take care to make sure that it is a necessary refactoring.&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with its own functionality.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list for code coverage. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
For cyclomatic complexity (the number of possible paths),  Saikuro is commonly employed and made to be easy to implement. For code complexity, Flog is the typical choice by the open source community of users.&amp;lt;ref&amp;gt;http://www.sitepoint.com/code-metrics-and-you/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Best Practices===&lt;br /&gt;
Because&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://books.google.com/books?id=6jyOUrJBJHAC&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
===When NOT to refactor:===&lt;br /&gt;
* Avoid refactoring when you should ideally be rewriting the entire code. Although a tough call to make, rewriting is inevitable when current code does not work at all or is too buggy to stabilize. &lt;br /&gt;
* Avoid refactoring when close to a deadline since productivity gain by refactoring is mostly seen after the deadline and might just cause the project to miss the deadline.&lt;br /&gt;
* Avoid refactoring for academic purposes, i.e. avoid changing code in working condition because you do not agree with those lines of code or that is not how you would have implemented it.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://ghendry.net/refactor.html A description of smells with their techniques exists]&lt;br /&gt;
&lt;br /&gt;
*[http://www.refactoring.com/catalog/index.html A list of techniques with examples in Ruby are listed]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://books.google.com/books?id=i6mZ0HBDPzsC Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83506</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83506"/>
		<updated>2014-02-19T05:31:47Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Metrics */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
For the page with a history of edits, go to [http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2014/ch1a_1w1e_rm this page].&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve its readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
In their Refactoring book&amp;lt;ref name=&amp;quot;Refactoring book&amp;quot;&amp;gt;http://books.google.com/books?id=1MsETFPD3I0C&amp;lt;/ref&amp;gt;, Martin Fowler and Kent Beck define refactoring as 'A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior… It is a disciplined way to clean up code that minimizes the chances of introducing bugs'.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of errors that can be made during refactoring, so it is pertinent to determine when it is necessary to refactor. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
Two main types of refactoring are &amp;lt;ref&amp;gt;http://web.cecs.pdx.edu/~black/publications/IEEESoftwareRefact.pdf&amp;lt;/ref&amp;gt;: &lt;br /&gt;
# Floss refactoring: refactoring performed regularly to keep code clean; gives healthy benefits in the long run&lt;br /&gt;
# Root canal refactoring: infrequent and protracted periods of refactoring; makes it painful and expensive (similar to dental analogy); mainly performed to clean unhealthy code&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by its children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def foo&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Method Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def greet&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def farewell&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def foo&lt;br /&gt;
  greet&lt;br /&gt;
  farewell&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  greet&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractSuperclass.html Example &amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Class Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class FullName&lt;br /&gt;
  def initialize(f,m,l)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @name = FullName.new(f,m,l)&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There many more techniques than those that are listed above. Each has its specific purpose, though one must take care to make sure that it is a necessary refactoring.&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list for code coverage. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
For cyclomatic complexity (the number of possible paths),  Saikuro is commonly employed and made to be easy to implement. For code complexity, Flog is the typical choice by the open source community of users.&amp;lt;ref&amp;gt;http://www.sitepoint.com/code-metrics-and-you/&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with its own functionality.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Best Practices===&lt;br /&gt;
Because&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://books.google.com/books?id=6jyOUrJBJHAC&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
===When NOT to refactor:===&lt;br /&gt;
* Avoid refactoring when you should ideally be rewriting the entire code. Although a tough call to make, rewriting is inevitable when current code does not work at all or is too buggy to stabilize. &lt;br /&gt;
* Avoid refactoring when close to a deadline since productivity gain by refactoring is mostly seen after the deadline and might just cause the project to miss the deadline.&lt;br /&gt;
* Avoid refactoring for academic purposes, i.e. avoid changing code in working condition because you do not agree with those lines of code or that is not how you would have implemented it.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://ghendry.net/refactor.html A description of smells with their techniques exists]&lt;br /&gt;
&lt;br /&gt;
*[http://www.refactoring.com/catalog/index.html A list of techniques with examples in Ruby are listed]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://books.google.com/books?id=i6mZ0HBDPzsC Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83501</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83501"/>
		<updated>2014-02-19T05:13:09Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Code Climate */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
For the page with a history of edits, go to [http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2014/ch1a_1w1e_rm this page].&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve its readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
In their Refactoring book&amp;lt;ref name=&amp;quot;Refactoring book&amp;quot;&amp;gt;http://books.google.com/books?id=1MsETFPD3I0C&amp;lt;/ref&amp;gt;, Martin Fowler and Kent Beck define refactoring as 'A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior… It is a disciplined way to clean up code that minimizes the chances of introducing bugs'.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of errors that can be made during refactoring, so it is pertinent to determine when it is necessary to refactor. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by its children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def foo&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Method Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def greet&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def farewell&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def foo&lt;br /&gt;
  greet&lt;br /&gt;
  farewell&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  greet&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractSuperclass.html Example &amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Class Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class FullName&lt;br /&gt;
  def initialize(f,m,l)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @name = FullName.new(f,m,l)&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There many more techniques than those that are listed above. Each has its specific purpose, though one must take care to make sure that it is a necessary refactoring.&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with its own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://books.google.com/books?id=6jyOUrJBJHAC&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
===When NOT to refactor:===&lt;br /&gt;
* Avoid refactoring when you should ideally be rewriting the entire code. Although a tough call to make, rewriting is inevitable when current code does not work at all or is too buggy to stabilize. &lt;br /&gt;
* Avoid refactoring when close to a deadline since productivity gain by refactoring is mostly seen after the deadline and might just cause the project to miss the deadline.&lt;br /&gt;
* Avoid refactoring for academic purposes, i.e. avoid changing code in working condition because you do not agree with those lines of code or that is not how you would have implemented it.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://ghendry.net/refactor.html A description of smells with their techniques exists]&lt;br /&gt;
&lt;br /&gt;
*[http://www.refactoring.com/catalog/index.html A list of techniques with examples in Ruby are listed]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://books.google.com/books?id=i6mZ0HBDPzsC Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83500</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83500"/>
		<updated>2014-02-19T05:12:39Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Background */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
For the page with a history of edits, go to [http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2014/ch1a_1w1e_rm this page].&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve its readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
In their Refactoring book&amp;lt;ref name=&amp;quot;Refactoring book&amp;quot;&amp;gt;http://books.google.com/books?id=1MsETFPD3I0C&amp;lt;/ref&amp;gt;, Martin Fowler and Kent Beck define refactoring as 'A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior… It is a disciplined way to clean up code that minimizes the chances of introducing bugs'.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of errors that can be made during refactoring, so it is pertinent to determine when it is necessary to refactor. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by its children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def foo&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Method Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def greet&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def farewell&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def foo&lt;br /&gt;
  greet&lt;br /&gt;
  farewell&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  greet&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractSuperclass.html Example &amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Class Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class FullName&lt;br /&gt;
  def initialize(f,m,l)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @name = FullName.new(f,m,l)&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There many more techniques than those that are listed above. Each has its specific purpose, though one must take care to make sure that it is a necessary refactoring.&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://books.google.com/books?id=6jyOUrJBJHAC&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
===When NOT to refactor:===&lt;br /&gt;
* Avoid refactoring when you should ideally be rewriting the entire code. Although a tough call to make, rewriting is inevitable when current code does not work at all or is too buggy to stabilize. &lt;br /&gt;
* Avoid refactoring when close to a deadline since productivity gain by refactoring is mostly seen after the deadline and might just cause the project to miss the deadline.&lt;br /&gt;
* Avoid refactoring for academic purposes, i.e. avoid changing code in working condition because you do not agree with those lines of code or that is not how you would have implemented it.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://ghendry.net/refactor.html A description of smells with their techniques exists]&lt;br /&gt;
&lt;br /&gt;
*[http://www.refactoring.com/catalog/index.html A list of techniques with examples in Ruby are listed]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://books.google.com/books?id=i6mZ0HBDPzsC Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83499</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83499"/>
		<updated>2014-02-19T05:11:22Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Further Reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
For the page with a history of edits, go to [http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2014/ch1a_1w1e_rm this page].&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
In their Refactoring book&amp;lt;ref name=&amp;quot;Refactoring book&amp;quot;&amp;gt;http://books.google.com/books?id=1MsETFPD3I0C&amp;lt;/ref&amp;gt;, Martin Fowler and Kent Beck define refactoring as 'A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior… It is a disciplined way to clean up code that minimizes the chances of introducing bugs'.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by its children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def foo&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Method Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def greet&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def farewell&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def foo&lt;br /&gt;
  greet&lt;br /&gt;
  farewell&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  greet&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractSuperclass.html Example &amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Class Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class FullName&lt;br /&gt;
  def initialize(f,m,l)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @name = FullName.new(f,m,l)&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There many more techniques than those that are listed above. Each has its specific purpose, though one must take care to make sure that it is a necessary refactoring.&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://books.google.com/books?id=6jyOUrJBJHAC&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
===When NOT to refactor:===&lt;br /&gt;
* Avoid refactoring when you should ideally be rewriting the entire code. Although a tough call to make, rewriting is inevitable when current code does not work at all or is too buggy to stabilize. &lt;br /&gt;
* Avoid refactoring when close to a deadline since productivity gain by refactoring is mostly seen after the deadline and might just cause the project to miss the deadline.&lt;br /&gt;
* Avoid refactoring for academic purposes, i.e. avoid changing code in working condition because you do not agree with those lines of code or that is not how you would have implemented it.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://ghendry.net/refactor.html A description of smells with their techniques exists]&lt;br /&gt;
&lt;br /&gt;
*[http://www.refactoring.com/catalog/index.html A list of techniques with examples in Ruby are listed]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://books.google.com/books?id=i6mZ0HBDPzsC Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83498</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83498"/>
		<updated>2014-02-19T05:10:52Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Refactoring Techniques */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
For the page with a history of edits, go to [http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2014/ch1a_1w1e_rm this page].&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
In their Refactoring book&amp;lt;ref name=&amp;quot;Refactoring book&amp;quot;&amp;gt;http://books.google.com/books?id=1MsETFPD3I0C&amp;lt;/ref&amp;gt;, Martin Fowler and Kent Beck define refactoring as 'A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior… It is a disciplined way to clean up code that minimizes the chances of introducing bugs'.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by its children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def foo&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Method Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def greet&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def farewell&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def foo&lt;br /&gt;
  greet&lt;br /&gt;
  farewell&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  greet&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractSuperclass.html Example &amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Class Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class FullName&lt;br /&gt;
  def initialize(f,m,l)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @name = FullName.new(f,m,l)&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There many more techniques than those that are listed above. Each has its specific purpose, though one must take care to make sure that it is a necessary refactoring.&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://books.google.com/books?id=6jyOUrJBJHAC&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
===When NOT to refactor:===&lt;br /&gt;
* Avoid refactoring when you should ideally be rewriting the entire code. Although a tough call to make, rewriting is inevitable when current code does not work at all or is too buggy to stabilize. &lt;br /&gt;
* Avoid refactoring when close to a deadline since productivity gain by refactoring is mostly seen after the deadline and might just cause the project to miss the deadline.&lt;br /&gt;
* Avoid refactoring for academic purposes, i.e. avoid changing code in working condition because you do not agree with those lines of code or that is not how you would have implemented it.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://books.google.com/books?id=i6mZ0HBDPzsC Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83496</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83496"/>
		<updated>2014-02-19T05:02:04Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Background */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve its readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of errors that can be made during refactoring, so it is pertinent to determine when it is necessary to refactor. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by its children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def foo&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Method Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def greet&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def farewell&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def foo&lt;br /&gt;
  greet&lt;br /&gt;
  farewell&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  greet&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractSuperclass.html Example &amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Class Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class FullName&lt;br /&gt;
  def initialize(f,m,l)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @name = FullName.new(f,m,l)&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There many more techniques than those that are listed above. Each has its specific purpose, though one must take care to make sure that it is a necessary refactoring.&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with its own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://ghendry.net/refactor.html A description of smells with their techniques exists]&lt;br /&gt;
&lt;br /&gt;
*[http://www.refactoring.com/catalog/index.html A list of techniques with examples in Ruby are listed]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83494</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83494"/>
		<updated>2014-02-19T04:55:29Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Further Reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve its readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by its children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def foo&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Method Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def greet&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def farewell&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def foo&lt;br /&gt;
  greet&lt;br /&gt;
  farewell&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  greet&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractSuperclass.html Example &amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Class Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class FullName&lt;br /&gt;
  def initialize(f,m,l)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @name = FullName.new(f,m,l)&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There many more techniques than those that are listed above. Each has its specific purpose, though one must take care to make sure that it is a necessary refactoring.&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with its own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://ghendry.net/refactor.html A description of smells with their techniques exists]&lt;br /&gt;
&lt;br /&gt;
*[http://www.refactoring.com/catalog/index.html A list of techniques with examples in Ruby are listed]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83493</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83493"/>
		<updated>2014-02-19T04:54:55Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* More Techniques */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve its readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by its children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def foo&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Method Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def greet&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def farewell&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def foo&lt;br /&gt;
  greet&lt;br /&gt;
  farewell&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  greet&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractSuperclass.html Example &amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Class Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class FullName&lt;br /&gt;
  def initialize(f,m,l)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @name = FullName.new(f,m,l)&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There many more techniques than those that are listed above. Each has its specific purpose, though one must take care to make sure that it is a necessary refactoring.&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with its own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83487</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83487"/>
		<updated>2014-02-19T04:51:01Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Code Climate */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve its readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by its children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def foo&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Method Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def greet&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def farewell&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def foo&lt;br /&gt;
  greet&lt;br /&gt;
  farewell&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  greet&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractSuperclass.html Example &amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Class Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class FullName&lt;br /&gt;
  def initialize(f,m,l)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @name = FullName.new(f,m,l)&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with its own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83486</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83486"/>
		<updated>2014-02-19T04:50:40Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Duplicate Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve its readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by its children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def foo&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Method Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def greet&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def farewell&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def foo&lt;br /&gt;
  greet&lt;br /&gt;
  farewell&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  greet&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractSuperclass.html Example &amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Class Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class FullName&lt;br /&gt;
  def initialize(f,m,l)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @name = FullName.new(f,m,l)&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83484</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83484"/>
		<updated>2014-02-19T04:50:26Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Background */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve its readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def foo&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Method Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def greet&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def farewell&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def foo&lt;br /&gt;
  greet&lt;br /&gt;
  farewell&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  greet&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractSuperclass.html Example &amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Class Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class FullName&lt;br /&gt;
  def initialize(f,m,l)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @name = FullName.new(f,m,l)&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83483</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83483"/>
		<updated>2014-02-19T04:49:29Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Large Method/Class */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def foo&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Method Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def greet&lt;br /&gt;
  puts &amp;quot;hey&amp;quot;&lt;br /&gt;
  puts &amp;quot;how are you&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def farewell&lt;br /&gt;
  puts &amp;quot;bye!&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
def foo&lt;br /&gt;
  greet&lt;br /&gt;
  farewell&lt;br /&gt;
end&lt;br /&gt;
def bar&lt;br /&gt;
  greet&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractSuperclass.html Example &amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Class Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class FullName&lt;br /&gt;
  def initialize(f,m,l)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @name = FullName.new(f,m,l)&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83480</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83480"/>
		<updated>2014-02-19T04:41:46Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Large Method/Class */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractSuperclass.html Example &amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Class Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class FullName&lt;br /&gt;
  def initialize(f,m,l)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @name = FullName.new(f,m,l)&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83479</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83479"/>
		<updated>2014-02-19T04:41:19Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Large Method/Class */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractSuperclass.html Example &amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Extract Class Refactoring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class FullName&lt;br /&gt;
  def initialize(f,m,l)&lt;br /&gt;
    @firstName = f&lt;br /&gt;
    @middleName = m&lt;br /&gt;
    @lastName = l&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class Person&lt;br /&gt;
  def initialize(f,m,l,s)&lt;br /&gt;
    @name = FullName.new(f,m,l)&lt;br /&gt;
    @SSN = s&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83478</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83478"/>
		<updated>2014-02-19T04:40:02Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Duplicate Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83477</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83477"/>
		<updated>2014-02-19T04:39:41Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Duplicate Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
**Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
**Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83476</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83476"/>
		<updated>2014-02-19T04:39:10Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Duplicate Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
**Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
**After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
**Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
**After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83475</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83475"/>
		<updated>2014-02-19T04:38:17Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Duplicate Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
    puts “Sleep in sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After Form Template Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat&lt;br /&gt;
  def live&lt;br /&gt;
    puts “Wake up”&lt;br /&gt;
    do_morning_routine&lt;br /&gt;
    puts “Sleep in the sun”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class Tiger &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Catch deer”&lt;br /&gt;
    puts “Eat deer”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
class HouseCat &amp;lt; Cat&lt;br /&gt;
  def do_morning_routine&lt;br /&gt;
    puts “Wake human up”&lt;br /&gt;
    puts “Eat food”&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83474</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83474"/>
		<updated>2014-02-19T04:36:06Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Duplicate Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
  ... &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
  ... &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
**[http://www.integralist.co.uk/posts/refactoring-techniques/#form-template-method Form Template Method Example]&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83473</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83473"/>
		<updated>2014-02-19T04:35:12Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Duplicate Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/extractMethod.html Extract Method Example&amp;lt;/ref&amp;gt;&lt;br /&gt;
Before Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
  ....&lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
... &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
... &lt;br /&gt;
  def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
After Pull Up Refactor&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Cat  &lt;br /&gt;
...&lt;br /&gt;
 def likes  &lt;br /&gt;
    puts &amp;quot;Boxes&amp;quot;  &lt;br /&gt;
  end  &lt;br /&gt;
end  &lt;br /&gt;
  &lt;br /&gt;
class Lion &amp;lt; Mammal &lt;br /&gt;
... &lt;br /&gt;
end &lt;br /&gt;
class HouseCat &amp;lt; Mammal &lt;br /&gt;
...  &lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
**[http://www.integralist.co.uk/posts/refactoring-techniques/#form-template-method Form Template Method Example]&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014&amp;diff=83360</id>
		<title>CSC/ECE 517 Spring 2014</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014&amp;diff=83360"/>
		<updated>2014-02-12T05:47:17Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE_517_Fall_2012/example_page]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1a 1e rm]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1 1w1h jg ]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1 1w1b np]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1 1w1f mj]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1a 1d mm]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1a 1c yj]]&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm1&amp;diff=83359</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1e rm1</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm1&amp;diff=83359"/>
		<updated>2014-02-12T05:42:54Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: moved CSC/ECE 517 Spring 2014/ch1a 1e rm1 to CSC/ECE 517 Spring 2014/ch1a 1e rm over redirect&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#REDIRECT [[CSC/ECE 517 Spring 2014/ch1a 1e rm]]&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83358</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83358"/>
		<updated>2014-02-12T05:42:54Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: moved CSC/ECE 517 Spring 2014/ch1a 1e rm1 to CSC/ECE 517 Spring 2014/ch1a 1e rm over redirect&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
For the page with a history of edits, go to [http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2014/ch1a_1w1e_rm this page].&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&lt;br /&gt;
**[http://www.refactoring.com/catalog/extractMethod.html Extract Method Example]&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
**[http://www.integralist.co.uk/posts/refactoring-techniques/#form-template-method Form Template Method Example]&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83357</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83357"/>
		<updated>2014-02-12T05:42:45Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
For the page with a history of edits, go to [http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Spring_2014/ch1a_1w1e_rm this page].&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&lt;br /&gt;
**[http://www.refactoring.com/catalog/extractMethod.html Extract Method Example]&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
**[http://www.integralist.co.uk/posts/refactoring-techniques/#form-template-method Form Template Method Example]&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014&amp;diff=83356</id>
		<title>CSC/ECE 517 Spring 2014</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014&amp;diff=83356"/>
		<updated>2014-02-12T05:40:14Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE_517_Fall_2012/example_page]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1a 1w1e rm]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1 1w1h jg ]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1 1w1b np]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1 1w1f mj]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1a 1d mm]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1a 1c yj]]&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83354</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83354"/>
		<updated>2014-02-12T05:34:52Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: moved CSC/ECE 517 Spring 2014/ch1a 1e rm to CSC/ECE 517 Spring 2014/ch1a 1e rm1&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83353</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1e_rm&amp;diff=83353"/>
		<updated>2014-02-12T05:34:41Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: Blanked the page&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1_1w1e_rm&amp;diff=83352</id>
		<title>CSC/ECE 517 Spring 2014/ch1 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1_1w1e_rm&amp;diff=83352"/>
		<updated>2014-02-12T05:33:37Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: moved CSC/ECE 517 Spring 2014/ch1 1w1e rm to CSC/ECE 517 Spring 2014/ch1a 1w1e rm: wrong title&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#REDIRECT [[CSC/ECE 517 Spring 2014/ch1a 1w1e rm]]&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83351</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83351"/>
		<updated>2014-02-12T05:33:37Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: moved CSC/ECE 517 Spring 2014/ch1 1w1e rm to CSC/ECE 517 Spring 2014/ch1a 1w1e rm: wrong title&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&lt;br /&gt;
**[http://www.refactoring.com/catalog/extractMethod.html Extract Method Example]&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
**[http://www.integralist.co.uk/posts/refactoring-techniques/#form-template-method Form Template Method Example]&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
#Complexity Metric - This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
#Duplication Metric - This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
#Churn Method - This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014&amp;diff=83350</id>
		<title>CSC/ECE 517 Spring 2014</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014&amp;diff=83350"/>
		<updated>2014-02-12T05:31:16Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE_517_Fall_2012/example_page]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1 1w1e rm]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1 1w1h jg ]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1 1w1b np]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1 1w1f mj]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1a 1d mm]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1a 1c yj]]&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014&amp;diff=83349</id>
		<title>CSC/ECE 517 Spring 2014</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014&amp;diff=83349"/>
		<updated>2014-02-12T05:31:02Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE_517_Fall_2012/example_page]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1a 1w1e rm]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1 1w1h jg ]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1 1w1b np]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1 1w1f mj]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1a 1d mm]]&lt;br /&gt;
*[[CSC/ECE 517 Spring 2014/ch1a 1c yj]]&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83216</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83216"/>
		<updated>2014-02-11T01:31:13Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Code Climate */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&lt;br /&gt;
**[http://www.refactoring.com/catalog/extractMethod.html Extract Method Example]&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
**[http://www.integralist.co.uk/posts/refactoring-techniques/#form-template-method Form Template Method Example]&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
*Complexity Metric&lt;br /&gt;
This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
*Duplication Metric&lt;br /&gt;
This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
*Churn Method&lt;br /&gt;
This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83215</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83215"/>
		<updated>2014-02-11T01:26:22Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Refactoring views */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&lt;br /&gt;
**[http://www.refactoring.com/catalog/extractMethod.html Extract Method Example]&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
**[http://www.integralist.co.uk/posts/refactoring-techniques/#form-template-method Form Template Method Example]&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
====Complexity Metric====&lt;br /&gt;
This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
====Duplication Metric====&lt;br /&gt;
This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Churn Method====&lt;br /&gt;
This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83214</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83214"/>
		<updated>2014-02-11T01:26:10Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Refactoring fat models */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&lt;br /&gt;
**[http://www.refactoring.com/catalog/extractMethod.html Extract Method Example]&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
**[http://www.integralist.co.uk/posts/refactoring-techniques/#form-template-method Form Template Method Example]&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
====Complexity Metric====&lt;br /&gt;
This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
====Duplication Metric====&lt;br /&gt;
This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Churn Method====&lt;br /&gt;
This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
===Refactoring views===&lt;br /&gt;
In this [https://speakerdeck.com/brianvh/refactoring-views-in-rails talk], Brian Hughes explains use of patterns to refactor views with bad code smells like using conditionals or formatting data.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83213</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83213"/>
		<updated>2014-02-11T01:25:59Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Further Reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&lt;br /&gt;
**[http://www.refactoring.com/catalog/extractMethod.html Extract Method Example]&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
**[http://www.integralist.co.uk/posts/refactoring-techniques/#form-template-method Form Template Method Example]&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
====Complexity Metric====&lt;br /&gt;
This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
====Duplication Metric====&lt;br /&gt;
This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Churn Method====&lt;br /&gt;
This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
===Refactoring fat models===&lt;br /&gt;
As more features get added to the application, the models tend to become bulkier. Such fat models cause maintenance issues. Refer to [http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ CodeClimate blog] for 7 patterns to refactor such fat models.&lt;br /&gt;
&lt;br /&gt;
===Refactoring views===&lt;br /&gt;
In this [https://speakerdeck.com/brianvh/refactoring-views-in-rails talk], Brian Hughes explains use of patterns to refactor views with bad code smells like using conditionals or formatting data.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Refactoring Fat Models]&lt;br /&gt;
&lt;br /&gt;
*[https://speakerdeck.com/brianvh/refactoring-views-in-rails Talk on Refactoring Fat Views]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83211</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83211"/>
		<updated>2014-02-11T01:22:36Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Refactoring views */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&lt;br /&gt;
**[http://www.refactoring.com/catalog/extractMethod.html Extract Method Example]&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
**[http://www.integralist.co.uk/posts/refactoring-techniques/#form-template-method Form Template Method Example]&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
====Complexity Metric====&lt;br /&gt;
This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
====Duplication Metric====&lt;br /&gt;
This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Churn Method====&lt;br /&gt;
This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
===Refactoring fat models===&lt;br /&gt;
As more features get added to the application, the models tend to become bulkier. Such fat models cause maintenance issues. Refer to [http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ CodeClimate blog] for 7 patterns to refactor such fat models.&lt;br /&gt;
&lt;br /&gt;
===Refactoring views===&lt;br /&gt;
In this [https://speakerdeck.com/brianvh/refactoring-views-in-rails talk], Brian Hughes explains use of patterns to refactor views with bad code smells like using conditionals or formatting data.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83210</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83210"/>
		<updated>2014-02-11T01:22:01Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Refactoring fat models */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&lt;br /&gt;
**[http://www.refactoring.com/catalog/extractMethod.html Extract Method Example]&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
**[http://www.integralist.co.uk/posts/refactoring-techniques/#form-template-method Form Template Method Example]&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
====Complexity Metric====&lt;br /&gt;
This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
====Duplication Metric====&lt;br /&gt;
This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Churn Method====&lt;br /&gt;
This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
===Refactoring fat models===&lt;br /&gt;
As more features get added to the application, the models tend to become bulkier. Such fat models cause maintenance issues. Refer to [http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ CodeClimate blog] for 7 patterns to refactor such fat models.&lt;br /&gt;
&lt;br /&gt;
===Refactoring views===&lt;br /&gt;
In this [https://speakerdeck.com/brianvh/refactoring-views-in-rails talk], Brian Hughes explains use of patterns to refactor views with bad code smells like use of conditionals or formatting data.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83207</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83207"/>
		<updated>2014-02-11T01:18:47Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* First step: Writing tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&lt;br /&gt;
**[http://www.refactoring.com/catalog/extractMethod.html Extract Method Example]&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
**[http://www.integralist.co.uk/posts/refactoring-techniques/#form-template-method Form Template Method Example]&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
====Complexity Metric====&lt;br /&gt;
This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
====Duplication Metric====&lt;br /&gt;
This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Churn Method====&lt;br /&gt;
This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, in order to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
===Refactoring fat models===&lt;br /&gt;
As more features get added to the application, the models tend to become bulkier. Such fat models cause maintenance issues. Refer [http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ CodeClimate blog] for 7 patterns to refactor such fat models.&lt;br /&gt;
&lt;br /&gt;
===Refactoring views===&lt;br /&gt;
In this [https://speakerdeck.com/brianvh/refactoring-views-in-rails talk], Brian Hughes explains use of patterns to refactor views with bad code smells like use of conditionals or formatting data.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83206</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83206"/>
		<updated>2014-02-11T01:18:00Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Background */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
&lt;br /&gt;
The idea behind [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] is to take a code base and improve it's readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability]. The practice is focused on having a better quality and more standardized code base, and not on fixing bugs or changing functionality.&lt;br /&gt;
&lt;br /&gt;
Refactoring has its difficulties, whether or not it is done using an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or whether it is done by hand. There are lots of pitfalls and errors that can be made during refactoring, so it is pertinent to determine how important a refactor is to the code. Code metrics are one method used to determine how badly a file needs refactoring.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&lt;br /&gt;
**[http://www.refactoring.com/catalog/extractMethod.html Extract Method Example]&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
**[http://www.integralist.co.uk/posts/refactoring-techniques/#form-template-method Form Template Method Example]&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
====Complexity Metric====&lt;br /&gt;
This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
====Duplication Metric====&lt;br /&gt;
This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Churn Method====&lt;br /&gt;
This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
===Refactoring fat models===&lt;br /&gt;
As more features get added to the application, the models tend to become bulkier. Such fat models cause maintenance issues. Refer [http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ CodeClimate blog] for 7 patterns to refactor such fat models.&lt;br /&gt;
&lt;br /&gt;
===Refactoring views===&lt;br /&gt;
In this [https://speakerdeck.com/brianvh/refactoring-views-in-rails talk], Brian Hughes explains use of patterns to refactor views with bad code smells like use of conditionals or formatting data.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83201</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83201"/>
		<updated>2014-02-11T01:00:42Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Large Method/Class */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
The practice of [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] deals with changing the content or structure of code without changing the code's function in its execution.  Code refactoring has become a standard programming practice, as it potentially promotes readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability] of code.&lt;br /&gt;
&lt;br /&gt;
Whether done through an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or by hand, large-scale code projects can prove tedious to refactor.  If minimal non-functional benefits are achieved through refactoring, time is wasted.  Furthermore, if not done properly, code refactoring can actually break the functionality of the code.  In the extreme case, code could be structured so badly that starting over completely may be more viable than refactoring.  As such, it is important to be able to know when and what to refactor.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&lt;br /&gt;
**[http://www.refactoring.com/catalog/extractMethod.html Extract Method Example]&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
**[http://www.integralist.co.uk/posts/refactoring-techniques/#form-template-method Form Template Method Example]&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While functionality of a block of code may be necessary for the project, the code block might not really pertain to the method or class it is included in. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
====Complexity Metric====&lt;br /&gt;
This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
====Duplication Metric====&lt;br /&gt;
This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Churn Method====&lt;br /&gt;
This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
===Refactoring fat models===&lt;br /&gt;
As more features get added to the application, the models tend to become bulkier. Such fat models cause maintenance issues. Refer [http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ CodeClimate blog] for 7 patterns to refactor such fat models.&lt;br /&gt;
&lt;br /&gt;
===Refactoring views===&lt;br /&gt;
In this [https://speakerdeck.com/brianvh/refactoring-views-in-rails talk], Brian Hughes explains use of patterns to refactor views with bad code smells like use of conditionals or formatting data.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83200</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83200"/>
		<updated>2014-02-11T00:58:51Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Open Source Ruby Tools */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses refactoring techniques and metrics. It also includes tips on refactoring in Ruby and list of tools for automated refactoring.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
The practice of [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] deals with changing the content or structure of code without changing the code's function in its execution.  Code refactoring has become a standard programming practice, as it potentially promotes readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability] of code.&lt;br /&gt;
&lt;br /&gt;
Whether done through an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or by hand, large-scale code projects can prove tedious to refactor.  If minimal non-functional benefits are achieved through refactoring, time is wasted.  Furthermore, if not done properly, code refactoring can actually break the functionality of the code.  In the extreme case, code could be structured so badly that starting over completely may be more viable than refactoring.  As such, it is important to be able to know when and what to refactor.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren't typically bugs, but can increase the chance of bugs later on if not fixed. Based on the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&lt;br /&gt;
**[http://www.refactoring.com/catalog/extractMethod.html Extract Method Example]&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
**[http://www.integralist.co.uk/posts/refactoring-techniques/#form-template-method Form Template Method Example]&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While the functionality may be necessary for the project, it might not be in the method or class. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
Many times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Metrics==&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
===Open Source Ruby Metric Tools===&lt;br /&gt;
There are a variety of open source code metric tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the code coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
===Code Climate===&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
====Complexity Metric====&lt;br /&gt;
This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
====Duplication Metric====&lt;br /&gt;
This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Churn Method====&lt;br /&gt;
This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
&lt;br /&gt;
===First step: Writing tests===&lt;br /&gt;
The first step in refactoring is writing solid set of tests for the section of code under consideration, to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test, make small changes, test again and so on&amp;lt;ref name='Refactoring Ruby Edition'&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;. This ensures that we can revert back to the working version of code if any small refactoring causes the application to stop working.&lt;br /&gt;
&lt;br /&gt;
===When to refactor===&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code&amp;lt;ref name='Refactoring Ruby Edition'/&amp;gt;:&lt;br /&gt;
* The Rule of Three - If any code segment gives trouble more than two times, it is time to refactor it&lt;br /&gt;
* Adding new function - It might be helpful to refactor when adding new functionality as it would give better understanding of code. Sometimes, revisiting old design might help to accommodate new features better&lt;br /&gt;
* Fixing a bug - It is useful to refactor code which had a bug; this might save bug reports from the same piece of code in future &lt;br /&gt;
* During code review - Refactoring is useful during code reviews as it can be applied immediately making code reviews more fruitful&lt;br /&gt;
* Understanding other's code - If a new member joins the team and is asked to refactor existing code, it helps them to get a better understanding of the code base. It might take extra time to get them started, but trying to refactor will help them contribute to the project better in the long run. &lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] suggests some other conditions to decide when to refactor code.&lt;br /&gt;
&lt;br /&gt;
===Refactoring fat models===&lt;br /&gt;
As more features get added to the application, the models tend to become bulkier. Such fat models cause maintenance issues. Refer [http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ CodeClimate blog] for 7 patterns to refactor such fat models.&lt;br /&gt;
&lt;br /&gt;
===Refactoring views===&lt;br /&gt;
In this [https://speakerdeck.com/brianvh/refactoring-views-in-rails talk], Brian Hughes explains use of patterns to refactor views with bad code smells like use of conditionals or formatting data.&lt;br /&gt;
&lt;br /&gt;
==Automated Code Refactoring==&lt;br /&gt;
=== RubyMine ===&lt;br /&gt;
*[http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine] has a built in refactoring menu.  &lt;br /&gt;
*To perform refactoring, select a code fragment to refactor. Refactorings available for your selection appear under the Refactor Menu in RubyMine. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
&lt;br /&gt;
===RFactor ===&lt;br /&gt;
*[http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor] is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
*The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
*It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
=== Other IDEs ===&lt;br /&gt;
*Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin])&lt;br /&gt;
*A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best, followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
*[http://www.amazon.com/Rails-AntiPatterns-Refactoring-Addison-Wesley-Professional/dp/0321604814 Rails AntiPatterns: Best Practice Ruby on Rails Refactoring]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83107</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83107"/>
		<updated>2014-02-10T08:03:32Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Duplication Metric */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses how to elegantly refactor code, including several common metrics used in determining the potential quality of refactoring code, as well as which refactoring techniques to use in coordination with such metrics.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
The practice of [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] deals with changing the content or structure of code without changing the code's function in its execution.  Code refactoring has become a standard programming practice, as it potentially promotes readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability] of code.&lt;br /&gt;
&lt;br /&gt;
Whether done through an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or by hand, large-scale code projects can prove tedious to refactor.  If minimal non-functional benefits are achieved through refactoring, time is wasted.  Furthermore, if not done properly, code refactoring can actually break the functionality of the code.  In the extreme case, code could be structured so badly that starting over completely may be more viable than refactoring.  As such, it is important to be able to know when and what to refactor.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren’t typically bugs, but can increase the chance of bugs later on if not fixed. Based off of the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&lt;br /&gt;
**[http://www.refactoring.com/catalog/extractMethod.html Extract Method Example]&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
**[http://www.integralist.co.uk/posts/refactoring-techniques/#form-template-method Form Template Method Example]&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While the functionality may be necessary for the project, it might not be in the method or class. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
A lot of times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can are able to be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
The first step in refactoring is writing solid set of tests for that section of code to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test again, make small changes and so on. &amp;lt;ref&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To start refactoring in Ruby, a [http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ CodeClimate blog] suggests 7 patterns to refactor fat models in Ruby: &lt;br /&gt;
# Extract Value Objects&lt;br /&gt;
# Extract Service Objects&lt;br /&gt;
# Extract Form Objects&lt;br /&gt;
# Extract Query Objects&lt;br /&gt;
# Introduce View Objects&lt;br /&gt;
# Extract Policy Objects&lt;br /&gt;
# Extract Decorators&lt;br /&gt;
&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code:&lt;br /&gt;
* The Rule of Three&lt;br /&gt;
* When you add function&lt;br /&gt;
* When you need to fix a bug&lt;br /&gt;
* During code review, &lt;br /&gt;
* For greater understanding&lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] also suggests some other conditions to identify the need to refactor code.&lt;br /&gt;
&lt;br /&gt;
=Metrics=&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
==Open Source Ruby Tools==&lt;br /&gt;
There are a variety of open source tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
==Code Climate==&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
===Complexity Metric===&lt;br /&gt;
This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
===Duplication Metric===&lt;br /&gt;
This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. &amp;lt;ref&amp;gt;https://codeclimate.com/docs#quality-metrics&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Churn Method===&lt;br /&gt;
This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
=Automated Code Refactoring=&lt;br /&gt;
* [http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine]&lt;br /&gt;
** RubyMine has a built in refactoring menu.  &lt;br /&gt;
** Select a symbol or code fragment to refactor. Refactorings available for your selection appears. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
 &lt;br /&gt;
* [http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor]&lt;br /&gt;
** RFactor is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
** The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
** It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
* Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin]). &lt;br /&gt;
** A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code.]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83106</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83106"/>
		<updated>2014-02-10T08:02:58Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Duplicate Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses how to elegantly refactor code, including several common metrics used in determining the potential quality of refactoring code, as well as which refactoring techniques to use in coordination with such metrics.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
The practice of [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] deals with changing the content or structure of code without changing the code's function in its execution.  Code refactoring has become a standard programming practice, as it potentially promotes readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability] of code.&lt;br /&gt;
&lt;br /&gt;
Whether done through an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or by hand, large-scale code projects can prove tedious to refactor.  If minimal non-functional benefits are achieved through refactoring, time is wasted.  Furthermore, if not done properly, code refactoring can actually break the functionality of the code.  In the extreme case, code could be structured so badly that starting over completely may be more viable than refactoring.  As such, it is important to be able to know when and what to refactor.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren’t typically bugs, but can increase the chance of bugs later on if not fixed. Based off of the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&lt;br /&gt;
**[http://www.refactoring.com/catalog/extractMethod.html Extract Method Example]&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. &amp;lt;ref&amp;gt;http://www.refactoring.com/catalog/formTemplateMethod.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
**[http://www.integralist.co.uk/posts/refactoring-techniques/#form-template-method Form Template Method Example]&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While the functionality may be necessary for the project, it might not be in the method or class. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
A lot of times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can are able to be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
The first step in refactoring is writing solid set of tests for that section of code to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test again, make small changes and so on. &amp;lt;ref&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To start refactoring in Ruby, a [http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ CodeClimate blog] suggests 7 patterns to refactor fat models in Ruby: &lt;br /&gt;
# Extract Value Objects&lt;br /&gt;
# Extract Service Objects&lt;br /&gt;
# Extract Form Objects&lt;br /&gt;
# Extract Query Objects&lt;br /&gt;
# Introduce View Objects&lt;br /&gt;
# Extract Policy Objects&lt;br /&gt;
# Extract Decorators&lt;br /&gt;
&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code:&lt;br /&gt;
* The Rule of Three&lt;br /&gt;
* When you add function&lt;br /&gt;
* When you need to fix a bug&lt;br /&gt;
* During code review, &lt;br /&gt;
* For greater understanding&lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] also suggests some other conditions to identify the need to refactor code.&lt;br /&gt;
&lt;br /&gt;
=Metrics=&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
==Open Source Ruby Tools==&lt;br /&gt;
There are a variety of open source tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
==Code Climate==&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
===Complexity Metric===&lt;br /&gt;
This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
===Duplication Metric===&lt;br /&gt;
This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. [https://codeclimate.com/docs#quality-metrics]&lt;br /&gt;
===Churn Method===&lt;br /&gt;
This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
=Automated Code Refactoring=&lt;br /&gt;
* [http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine]&lt;br /&gt;
** RubyMine has a built in refactoring menu.  &lt;br /&gt;
** Select a symbol or code fragment to refactor. Refactorings available for your selection appears. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
 &lt;br /&gt;
* [http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor]&lt;br /&gt;
** RFactor is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
** The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
** It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
* Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin]). &lt;br /&gt;
** A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code.]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83105</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83105"/>
		<updated>2014-02-10T08:02:17Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses how to elegantly refactor code, including several common metrics used in determining the potential quality of refactoring code, as well as which refactoring techniques to use in coordination with such metrics.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
The practice of [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] deals with changing the content or structure of code without changing the code's function in its execution.  Code refactoring has become a standard programming practice, as it potentially promotes readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability] of code.&lt;br /&gt;
&lt;br /&gt;
Whether done through an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or by hand, large-scale code projects can prove tedious to refactor.  If minimal non-functional benefits are achieved through refactoring, time is wasted.  Furthermore, if not done properly, code refactoring can actually break the functionality of the code.  In the extreme case, code could be structured so badly that starting over completely may be more viable than refactoring.  As such, it is important to be able to know when and what to refactor.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren’t typically bugs, but can increase the chance of bugs later on if not fixed. Based off of the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&lt;br /&gt;
**[http://www.refactoring.com/catalog/extractMethod.html Extract Method Example]&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. [http://www.refactoring.com/catalog/formTemplateMethod.html]&lt;br /&gt;
**[http://www.integralist.co.uk/posts/refactoring-techniques/#form-template-method Form Template Method Example]&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While the functionality may be necessary for the project, it might not be in the method or class. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
A lot of times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can are able to be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
The first step in refactoring is writing solid set of tests for that section of code to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test again, make small changes and so on. &amp;lt;ref&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To start refactoring in Ruby, a [http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ CodeClimate blog] suggests 7 patterns to refactor fat models in Ruby: &lt;br /&gt;
# Extract Value Objects&lt;br /&gt;
# Extract Service Objects&lt;br /&gt;
# Extract Form Objects&lt;br /&gt;
# Extract Query Objects&lt;br /&gt;
# Introduce View Objects&lt;br /&gt;
# Extract Policy Objects&lt;br /&gt;
# Extract Decorators&lt;br /&gt;
&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code:&lt;br /&gt;
* The Rule of Three&lt;br /&gt;
* When you add function&lt;br /&gt;
* When you need to fix a bug&lt;br /&gt;
* During code review, &lt;br /&gt;
* For greater understanding&lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] also suggests some other conditions to identify the need to refactor code.&lt;br /&gt;
&lt;br /&gt;
=Metrics=&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
==Open Source Ruby Tools==&lt;br /&gt;
There are a variety of open source tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
==Code Climate==&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
===Complexity Metric===&lt;br /&gt;
This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
===Duplication Metric===&lt;br /&gt;
This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. [https://codeclimate.com/docs#quality-metrics]&lt;br /&gt;
===Churn Method===&lt;br /&gt;
This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
=Automated Code Refactoring=&lt;br /&gt;
* [http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine]&lt;br /&gt;
** RubyMine has a built in refactoring menu.  &lt;br /&gt;
** Select a symbol or code fragment to refactor. Refactorings available for your selection appears. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
 &lt;br /&gt;
* [http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor]&lt;br /&gt;
** RFactor is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
** The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
** It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
* Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin]). &lt;br /&gt;
** A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code.]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83104</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83104"/>
		<updated>2014-02-10T08:02:08Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Further Reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses how to elegantly refactor code, including several common metrics used in determining the potential quality of refactoring code, as well as which refactoring techniques to use in coordination with such metrics.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
The practice of [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] deals with changing the content or structure of code without changing the code's function in its execution.  Code refactoring has become a standard programming practice, as it potentially promotes readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability] of code.&lt;br /&gt;
&lt;br /&gt;
Whether done through an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or by hand, large-scale code projects can prove tedious to refactor.  If minimal non-functional benefits are achieved through refactoring, time is wasted.  Furthermore, if not done properly, code refactoring can actually break the functionality of the code.  In the extreme case, code could be structured so badly that starting over completely may be more viable than refactoring.  As such, it is important to be able to know when and what to refactor.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren’t typically bugs, but can increase the chance of bugs later on if not fixed. Based off of the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&lt;br /&gt;
**[http://www.refactoring.com/catalog/extractMethod.html Extract Method Example]&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. [http://www.refactoring.com/catalog/formTemplateMethod.html]&lt;br /&gt;
**[http://www.integralist.co.uk/posts/refactoring-techniques/#form-template-method Form Template Method Example]&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While the functionality may be necessary for the project, it might not be in the method or class. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
A lot of times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can are able to be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
The first step in refactoring is writing solid set of tests for that section of code to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test again, make small changes and so on. &amp;lt;ref&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To start refactoring in Ruby, a [http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ CodeClimate blog] suggests 7 patterns to refactor fat models in Ruby: &lt;br /&gt;
# Extract Value Objects&lt;br /&gt;
# Extract Service Objects&lt;br /&gt;
# Extract Form Objects&lt;br /&gt;
# Extract Query Objects&lt;br /&gt;
# Introduce View Objects&lt;br /&gt;
# Extract Policy Objects&lt;br /&gt;
# Extract Decorators&lt;br /&gt;
&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code:&lt;br /&gt;
* The Rule of Three&lt;br /&gt;
* When you add function&lt;br /&gt;
* When you need to fix a bug&lt;br /&gt;
* During code review, &lt;br /&gt;
* For greater understanding&lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] also suggests some other conditions to identify the need to refactor code.&lt;br /&gt;
&lt;br /&gt;
=Metrics=&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
==Open Source Ruby Tools==&lt;br /&gt;
There are a variety of open source tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
==Code Climate==&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
===Complexity Metric===&lt;br /&gt;
This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
===Duplication Metric===&lt;br /&gt;
This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. [https://codeclimate.com/docs#quality-metrics]&lt;br /&gt;
===Churn Method===&lt;br /&gt;
This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
=Automated Code Refactoring=&lt;br /&gt;
* [http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine]&lt;br /&gt;
** RubyMine has a built in refactoring menu.  &lt;br /&gt;
** Select a symbol or code fragment to refactor. Refactorings available for your selection appears. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
 &lt;br /&gt;
* [http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor]&lt;br /&gt;
** RFactor is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
** The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
** It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
* Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin]). &lt;br /&gt;
** A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code.]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83103</id>
		<title>CSC/ECE 517 Spring 2014/ch1a 1w1e rm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2014/ch1a_1w1e_rm&amp;diff=83103"/>
		<updated>2014-02-10T08:00:24Z</updated>

		<summary type="html">&lt;p&gt;Rjlloyd: /* Further Reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page discusses how to elegantly refactor code, including several common metrics used in determining the potential quality of refactoring code, as well as which refactoring techniques to use in coordination with such metrics.&lt;br /&gt;
&lt;br /&gt;
==Background==&lt;br /&gt;
The practice of [http://en.wikipedia.org/wiki/Code_refactoring code refactoring] deals with changing the content or structure of code without changing the code's function in its execution.  Code refactoring has become a standard programming practice, as it potentially promotes readability, [http://en.wikipedia.org/wiki/Extensibility extensibility], and [http://en.wikipedia.org/wiki/Reusability reusability] of code.&lt;br /&gt;
&lt;br /&gt;
Whether done through an [http://en.wikipedia.org/wiki/Integrated_development_environment IDE] or by hand, large-scale code projects can prove tedious to refactor.  If minimal non-functional benefits are achieved through refactoring, time is wasted.  Furthermore, if not done properly, code refactoring can actually break the functionality of the code.  In the extreme case, code could be structured so badly that starting over completely may be more viable than refactoring.  As such, it is important to be able to know when and what to refactor.&lt;br /&gt;
&lt;br /&gt;
==Refactoring Techniques==&lt;br /&gt;
	Before a coder performs a refactor, they must, either formally or informally, identify their ‘code smells’. A code smell refers to a negative quality of a code base that either implements bad programming practices or slows down code development or runtime. These aren’t typically bugs, but can increase the chance of bugs later on if not fixed. Based off of the type of code smell, a different refactoring technique is used to fix it.&lt;br /&gt;
&lt;br /&gt;
===Duplicate Code===&lt;br /&gt;
	Duplicate code can be a tricky concept when refactoring. Large sections of duplicated code can be easy to find and fixed by pulling it out and creating a single centralized method to call, however it is usually not that easy. Sometimes it may only be one or two lines of code that are duplicated which calls for an assessment of whether or not it is in the best interest to create a new method for a couple lines of code. Other times, code is not duplicate, rather it is similar enough that a generic method could be created to serve various purposes.&lt;br /&gt;
Some specific techniques to deal with this are:&lt;br /&gt;
*Extract Method  / Pull-up Method&lt;br /&gt;
**If common code is used in multiple places, simply pull it out and make a method that can be called from all of the necessary places. Variables can be passed in if slight variations are needed between calls.&lt;br /&gt;
**If the code is used in various subclasses, put the common code in a method in the superclass, so that it can be seen and called by it’s children.&lt;br /&gt;
**[http://www.refactoring.com/catalog/extractMethod.html Extract Method Example]&lt;br /&gt;
&lt;br /&gt;
*Form Template Method&lt;br /&gt;
**If two methods in subclasses perform similar steps in the same order, but the steps are different, then get the steps into methods with the same signature, so that the original methods become the same. Then pull them up. [http://www.refactoring.com/catalog/formTemplateMethod.html]&lt;br /&gt;
**[http://www.integralist.co.uk/posts/refactoring-techniques/#form-template-method Form Template Method Example]&lt;br /&gt;
&lt;br /&gt;
===Large Method/Class===&lt;br /&gt;
If a project is not planned out well enough in advance, it is easy for methods and classes to become populated with excess functionality. While the functionality may be necessary for the project, it might not be in the method or class. &lt;br /&gt;
*Extract Method&lt;br /&gt;
**This can be used when duplicate code occurs, as discussed above. But it can also be used when a method performs multiple functions that have the ability to be split up into various functions that serve a single purpose. &lt;br /&gt;
*Extract Class/Subclass/Superclass&lt;br /&gt;
**If there are class variables or methods that don’t directly pertain to a class, then it may be necessary to create a new class for those pieces.[http://www.refactoring.com/catalog/extractClass.html Example]&lt;br /&gt;
**If there are pieces of a class that are only for a specific subset of instances, then a subclass can be constructed to contain these.[http://www.refactoring.com/catalog/extractSubclass.html Example]&lt;br /&gt;
**If there are pieces that multiple classes use, then a superclass can be constructed to handle these generic functions, leaving the subclasses to deal with the remaining differences [http://www.refactoring.com/catalog/extractSuperclass.html Example]&lt;br /&gt;
&lt;br /&gt;
===Improving Readability and Clarity===&lt;br /&gt;
A lot of times, refactoring can be used to do simple, yet necessary changes like renaming variables, methods, or classes. As features get added to a project, classes tend to get charged with more uses than originally planned, so sometimes, the original naming scheme no longer applies and a new one needs to be instilled.&lt;br /&gt;
&lt;br /&gt;
Moving methods and parameters around to where they have the best accessibility also has its uses. Classes also tend to be promoted or demoted to super and sub classes after their ultimate functional purpose is determined.&lt;br /&gt;
&lt;br /&gt;
===More Techniques===&lt;br /&gt;
There is an extensive list of coding smells that can are able to be improved through refactoring.&lt;br /&gt;
&lt;br /&gt;
A description of smells with their techniques exists [http://ghendry.net/refactor.html here].&lt;br /&gt;
&lt;br /&gt;
A list of techniques with examples in Ruby are listed [http://www.refactoring.com/catalog/index.html here].&lt;br /&gt;
&lt;br /&gt;
==Getting Started with Refactoring in Ruby==&lt;br /&gt;
The first step in refactoring is writing solid set of tests for that section of code to avoid introducing bugs. In Ruby, this can be done using [http://www.ruby-doc.org/stdlib-2.1.0/libdoc/test/unit/rdoc/Test/Unit.html Test::Unit] or [http://rspec.info/ Rspec]. Next step is to make small changes, test again, make small changes and so on. &amp;lt;ref&amp;gt;http://www.amazon.com/Refactoring-Edition-Addison-Wesley-Professional-Series/dp/0321984137&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To start refactoring in Ruby, a [http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ CodeClimate blog] suggests 7 patterns to refactor fat models in Ruby: &lt;br /&gt;
# Extract Value Objects&lt;br /&gt;
# Extract Service Objects&lt;br /&gt;
# Extract Form Objects&lt;br /&gt;
# Extract Query Objects&lt;br /&gt;
# Introduce View Objects&lt;br /&gt;
# Extract Policy Objects&lt;br /&gt;
# Extract Decorators&lt;br /&gt;
&lt;br /&gt;
It is always confusing when to start refactoring. If either one of the following conditions is true, it is a good time to start refactoring code:&lt;br /&gt;
* The Rule of Three&lt;br /&gt;
* When you add function&lt;br /&gt;
* When you need to fix a bug&lt;br /&gt;
* During code review, &lt;br /&gt;
* For greater understanding&lt;br /&gt;
&lt;br /&gt;
A [http://blog.codeclimate.com/blog/2014/01/09/when-is-it-time-to-refactor/ CodeClimate blog] also suggests some other conditions to identify the need to refactor code.&lt;br /&gt;
&lt;br /&gt;
=Metrics=&lt;br /&gt;
It can be hard to decide whether or not to refactor, especially when it’s for something that appears to be working fine with just a surface level smell. Program analysis tools are used to derive various types of code metrics, which allow coders to identify problem areas and future pitfalls of their code base. These tools look at the code in several different ways, including the number of times a source file has been edited to help determine if it is a possible target of [feature envy], duplicate code structure to identify replicated lines, and block depths to suggest possible complexity issues.&lt;br /&gt;
&lt;br /&gt;
==Open Source Ruby Tools==&lt;br /&gt;
There are a variety of open source tools that can be employed specifically for Ruby programs. [https://www.ruby-toolbox.com/categories/code_metrics The Ruby Toolbox] contains the most popular of these tools along with ratings.&lt;br /&gt;
&lt;br /&gt;
[http://rubydoc.info/gems/simplecov/frames Simple Cov] is at the top of this list. It was developed specifically to be of use to anyone using any framework. It integrates itself with the project’s own test cases to check the coverage of them in addition to cucumber features.&lt;br /&gt;
&lt;br /&gt;
==Code Climate==&lt;br /&gt;
[https://codeclimate.com Code Climate] is a hosted code metrics tool that analyzes projects in a multitdue of ways. It produces three main metric ratings with grades from A-F that comprise of the methods and classes contained in each file and project. &lt;br /&gt;
===Complexity Metric===&lt;br /&gt;
This is based off of the Assignment, Branches, and Conditions ([http://c2.com/cgi/wiki?AbcMetric ABC]) metric, where the number of assignments, branches, and conditions are counted and analyzed. However, because Code Climate is constructed for Ruby programs, it also takes into account certain types of Ruby constructs that may increase the metric score, but are actually beneficial to the project.&lt;br /&gt;
===Duplication Metric===&lt;br /&gt;
This looks at the syntax trees of the code in order to identify identical and similar code structures. Because the syntax trees are being analyzed, code formatting and different method and class names do not affect the score. [https://codeclimate.com/docs#quality-metrics]&lt;br /&gt;
===Churn Method===&lt;br /&gt;
This integrates with the Git repository to look at the change history of the project’s files. Files with excessively high change histories have a tendency to have a high complexity rating as well, as they can be the result of feature envy, where extra functions are added into a file instead of being added to a new file with it’s own functionality.&lt;br /&gt;
&lt;br /&gt;
=Automated Code Refactoring=&lt;br /&gt;
* [http://www.jetbrains.com/ruby/webhelp/refactoring.html RubyMine]&lt;br /&gt;
** RubyMine has a built in refactoring menu.  &lt;br /&gt;
** Select a symbol or code fragment to refactor. Refactorings available for your selection appears. &amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/refactoring-source-code.html#common&amp;lt;/ref&amp;gt;&amp;lt;br/&amp;gt; [[File:RubyMine_refactoring_menu.png]]&lt;br /&gt;
 &lt;br /&gt;
* [http://fabiokung.com/2009/02/04/rfactor-ruby-refactoring-for-your-loved-editor/ RFactor]&lt;br /&gt;
** RFactor is a Ruby gem, which aims to provide common and simple refactorings for Ruby code for text editors like TextMate&lt;br /&gt;
** The first release has only Extract method implemented while other refactorings are coming soon&lt;br /&gt;
** It is available on [http://github.com/fabiokung/rfactor-tmbundle/tree/master GitHub] &lt;br /&gt;
&lt;br /&gt;
* Some other IDEs with built-in refactoring include [http://www.aptana.com/products/studio3 Aptana Studio], [http://www.aptana.com/products/radrails Aptana RadRails] and [https://netbeans.org/features/ruby/index.html NetBeans 7] (which requires Ruby and Rails [http://plugins.netbeans.org/plugin/38549 plugin]). &lt;br /&gt;
** A StackOverflow [http://stackoverflow.com/questions/8705144/ide-with-refactoring-support-for-ruby-on-rails post] compares these tools ranking them as RubyMine being the best followed by NetBeans, RadRails 2 and Aptana Studio 3&lt;br /&gt;
&lt;br /&gt;
==Further Reading==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Code_smell Ways to identify problems in code.]&lt;br /&gt;
&lt;br /&gt;
*[http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring/ Resistance With Refactoring Ruby Class Methods]&lt;br /&gt;
&lt;br /&gt;
*[http://www.railsinside.com/uncategorized/460-the-first-step-of-refactoring-a-rails-application.html Refactoring a Rails Application]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rjlloyd</name></author>
	</entry>
</feed>