<?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=Hsure</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=Hsure"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Hsure"/>
	<updated>2026-06-02T06:07:04Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=82375</id>
		<title>CSC/ECE 517 Fall 2013/oss E811 syn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=82375"/>
		<updated>2013-11-07T20:26:39Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Testing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Refactor &amp;amp; test submitted_content_controller &amp;amp; submitted_content_helper''' ==&lt;br /&gt;
Expertiza is a web application that is used for Project/Assignment submissions. Students can form teams, select topics, submit assignments, review other's work and give feedback for reviews. Submission of work can be done in two ways:&lt;br /&gt;
*Hyperlinks&lt;br /&gt;
*Files/Folders&lt;br /&gt;
&lt;br /&gt;
Submitted_Content_Controller and Submitted_Content_Helper are designed to handle operations on hyperlinks and files. They include methods to submit, verify and remove hyperlinks; and create, move, rename, submit and remove files.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Project Description''' ==&lt;br /&gt;
&lt;br /&gt;
Classes: submitted_content_controller.rb (250 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
	 submitted_content_helper.rb (98 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
What they do: Allow users to submit files and links to Expertiza. &amp;lt;br&amp;gt;&lt;br /&gt;
What needs to be done:  The methods are long and complex, and contain a lot of duplicated code.  In particular, there are two different ways of submitting files: as a homework submission, and uploading a review file requested by a review rubric.  These should use common code as much as possible.  The approach to deleting submitted links and submitted files should be analogous.  There have been bugs in deleting hyperlinks, so be sure to test the code thoroughly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== '''Design Changes''' ==&lt;br /&gt;
&lt;br /&gt;
*Code duplication&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Duplicate_code&amp;lt;/ref&amp;gt; is generally considered a mark of poor or lazy programming style. Good coding style is generally associated with code reuse&amp;lt;ref&amp;gt;http://voices.yahoo.com/programming-code-reusability-11278802.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
:We identified a sequence of operations that are repetitively used in methods which are used to submit files for the first time and again in review. The code to check if a file exists or not and creating a new directory path for any new submission is common for both submission and review pages. Thus we separated it out as a different method ‘check_file_exists’ as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def check_file_exists  curr_directory&lt;br /&gt;
    #check if file exists. If not, create a new file. Else, remove the existing file and then create new file.&lt;br /&gt;
    if !File.exists? curr_directory&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    else&lt;br /&gt;
      FileUtils.rm_rf(curr_directory)&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method can now be called from both submit_file and custom_submit_file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def submit_file&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    file = params[:uploaded_file]&lt;br /&gt;
    participant.set_student_directory_num&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    curr_directory = participant.get_path.to_s+@current_folder.name&lt;br /&gt;
    check_file_exists(curr_directory)&lt;br /&gt;
&lt;br /&gt;
    safe_filename = file.original_filename.gsub(/\\/,&amp;quot;/&amp;quot;)&lt;br /&gt;
    safe_filename = FileHelper::sanitize_filename(safe_filename) # new code to sanitize file path before upload*&lt;br /&gt;
    full_filename =  curr_directory + File.split(safe_filename).last.gsub(&amp;quot; &amp;quot;,'_') #safe_filename #curr_directory +&lt;br /&gt;
    File.open(full_filename, &amp;quot;wb&amp;quot;) { |f| f.write(file.read) }&lt;br /&gt;
&lt;br /&gt;
    if params['unzip']&lt;br /&gt;
      SubmittedContentHelper::unzip_file(full_filename, curr_directory, true) if get_file_type(safe_filename) == &amp;quot;zip&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    participant.update_resubmit_times&lt;br /&gt;
&lt;br /&gt;
    #send message to reviewers when submission has been updated&lt;br /&gt;
    participant.assignment.email(participant.id) rescue nil # If the user has no team: 1) there are no reviewers to notify; 2) calling email will throw an exception. So rescue and ignore it.&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Long methods&amp;lt;ref&amp;gt;http://sourcemaking.com/refactoring/long-method&amp;lt;/ref&amp;gt; are often caused due to unnecessary parameters. Long list of parameters reduce readability&amp;lt;ref&amp;gt;http://simpleprogrammer.com/2013/04/14/what-makes-code-readable-not-what-you-think/&amp;lt;/ref&amp;gt; of code. Following changes are made to remove such unnecessary parameters:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file.JPG|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
Here the variable safename is declared and used only once. The assignment can be made inline by replacing it in the method call with actual logic in the below way:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file mod.JPG|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Below is another name change from ‘fpath’ to ‘file_path’ which is made to eliminate the most common code smell&amp;lt;ref&amp;gt;http://www.codinghorror.com/blog/2006/05/code-smells.html&amp;lt;/ref&amp;gt; ‘Excessively short identifiers’.&lt;br /&gt;
 &lt;br /&gt;
[[File:Fpath.png|frame|none|alt=Before Refactoring|Before Refactoring]] [[File:Filepath.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Consecutive assignments or modifications of the same variable can lead to “Long method” or “Excessive use of literals”. Such code smells are removed. For example, variable ‘newloc’ which is modified consecutively is refactored in the following way:&lt;br /&gt;
 &lt;br /&gt;
:Before Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_loc = @participant.get_path &lt;br /&gt;
    new_loc+= &amp;quot;/&amp;quot; &lt;br /&gt;
    new_loc+= params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_loc)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:After Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_location = @participant.get_path + &amp;quot;/&amp;quot; + params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_location)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Modified code reduces the method length and increases Readability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Unnecessary variable declarations which are never used in the method increase the length of code and confuse the readers. For example, the variable ‘display’ shown in the code snippet below was defined but never used in the method. Such variables can be removed.&lt;br /&gt;
&lt;br /&gt;
[[File:Display variable.png|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
The actual variable that was used which is ‘disp’ can now be renamed to ‘display’ which is more meaningful.&lt;br /&gt;
&lt;br /&gt;
[[File:Display change.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Many more minor code changes are made following the Ruby Coding guidelines. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if !File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We should prefer to use 'unless' for such conditions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== List of Code Smells identified and removed ==&lt;br /&gt;
*'''Duplicated code''': Identical or very similar code exists in more than one location. Code duplication frequently creates long, repeated sections of code that differ in only a few lines or characters. The length of such routines can make it difficult to quickly understand them. The repetition of largely identical code sections can conceal how they differ from one another, and therefore, what the specific purpose of each code section is. Often, the only difference is in a parameter value. The best practice in such cases is a reusable subroutine.&lt;br /&gt;
*'''Long method''': A method, function, or procedure that has grown too large. Since the early days of programming people have realized that the longer a procedure is, the more difficult it is to understand. Older languages carried an overhead in subroutine calls, which deterred people from small methods. Modern OO languages have pretty much eliminated that overhead for in-process calls.&lt;br /&gt;
*'''Too many parameters''': A long list of parameters in a procedure or function make readability and code quality worse. The more parameters a method has, the more complex it is. Limit the number of parameters you need in a given method, or use an object to combine the parameters.&lt;br /&gt;
*'''Excessively long identifiers''': In particular, the use of naming conventions to provide disambiguation that should be implicit in the software architecture.&lt;br /&gt;
*'''Excessively short identifiers''': The name of a variable should reflect its function unless the function is obvious. Pick a set of standard terminology and stick to it throughout your methods. For example, if you have Open(), you should probably have Close().&lt;br /&gt;
*'''Excessive use of literals''': These should be coded as named constants, to improve readability and to avoid programming errors. Additionally, literals can and should be externalized into resource files/scripts where possible, to facilitate localization of software if it is intended to be deployed in different regions.&lt;br /&gt;
*'''Complex conditionals''': Branches that check lots of unrelated conditions and edge cases that don't seem to capture the meaning of a block of code. Large conditional logic blocks, particularly blocks that tend to grow larger or change significantly over time have to be eliminated. Consider alternative object-oriented approaches such as decorator, strategy, or state.&lt;br /&gt;
*'''Dead Code'''&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Dead_code&amp;lt;/ref&amp;gt;: Ruthlessly delete code that isn't being used. Unused code will result in Long classes or long methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
&lt;br /&gt;
To verify that the changes didn't break anything. One can login to the VCL instance using username/password: sample/sample. One can go to your work page in an assignment. Upload a hyperlink and delete it.&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
*Polymorphism&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Polymorphism_(computer_science)&amp;lt;/ref&amp;gt; can be implemented by pushing some methods to relevant classes. Following section of code contains certain method calls which are implemented in the same class. These can be abstracted into a different class for file operations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def file_folder_action&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
    if params[:faction][:delete]        #call method to delete selected files&lt;br /&gt;
      delete_selected_files&lt;br /&gt;
    elsif params[:faction][:rename]     #call method to rename selected files&lt;br /&gt;
      rename_selected_file&lt;br /&gt;
    elsif params[:faction][:move]       #call method to move selected files&lt;br /&gt;
      move_selected_file&lt;br /&gt;
    elsif params[:faction][:copy]       #call method to copy selected files&lt;br /&gt;
      copy_selected_file&lt;br /&gt;
    elsif params[:faction][:create]     #call method to create new folder&lt;br /&gt;
      create_new_folder&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; @participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Submit file functionality currently breaks with the latest expertiza master branch code which can be fixed by further exploration.&lt;br /&gt;
*Design changes made in partial files can be extended such that code reuse can be maximized.&lt;br /&gt;
*Many more code smells can be identified and removed to improve elegance.&lt;br /&gt;
*RSpec test cases for the changes made can be added.&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;br /&gt;
&lt;br /&gt;
Steps to setup the project:&lt;br /&gt;
*Extract source code in RubyMine using the following url: https://github.com/hsure/expertiza&lt;br /&gt;
:(A) VCS -&amp;gt; Checkout from version control -&amp;gt; Github&lt;br /&gt;
:(B) Give this url:  https://github.com/hsure/expertiza -&amp;gt; Finish&lt;br /&gt;
*Confirm the sdk for RubyMine to ruby1.9.3 using File -&amp;gt; Settings &lt;br /&gt;
*Run bundle install &lt;br /&gt;
*Run - rake db:create:all&lt;br /&gt;
*Run - rake db:migrate&lt;br /&gt;
*From the Expertiza folder (project root) execute the command “rails server”&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
You should be able to view the test Database in MySQL.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Use the following credentials to explore the application:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Username: admin (Administrator)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: admin&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Username: sample (User)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: sample&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=82374</id>
		<title>CSC/ECE 517 Fall 2013/oss E811 syn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=82374"/>
		<updated>2013-11-07T20:25:20Z</updated>

		<summary type="html">&lt;p&gt;Hsure: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Refactor &amp;amp; test submitted_content_controller &amp;amp; submitted_content_helper''' ==&lt;br /&gt;
Expertiza is a web application that is used for Project/Assignment submissions. Students can form teams, select topics, submit assignments, review other's work and give feedback for reviews. Submission of work can be done in two ways:&lt;br /&gt;
*Hyperlinks&lt;br /&gt;
*Files/Folders&lt;br /&gt;
&lt;br /&gt;
Submitted_Content_Controller and Submitted_Content_Helper are designed to handle operations on hyperlinks and files. They include methods to submit, verify and remove hyperlinks; and create, move, rename, submit and remove files.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Project Description''' ==&lt;br /&gt;
&lt;br /&gt;
Classes: submitted_content_controller.rb (250 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
	 submitted_content_helper.rb (98 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
What they do: Allow users to submit files and links to Expertiza. &amp;lt;br&amp;gt;&lt;br /&gt;
What needs to be done:  The methods are long and complex, and contain a lot of duplicated code.  In particular, there are two different ways of submitting files: as a homework submission, and uploading a review file requested by a review rubric.  These should use common code as much as possible.  The approach to deleting submitted links and submitted files should be analogous.  There have been bugs in deleting hyperlinks, so be sure to test the code thoroughly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== '''Design Changes''' ==&lt;br /&gt;
&lt;br /&gt;
*Code duplication&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Duplicate_code&amp;lt;/ref&amp;gt; is generally considered a mark of poor or lazy programming style. Good coding style is generally associated with code reuse&amp;lt;ref&amp;gt;http://voices.yahoo.com/programming-code-reusability-11278802.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
:We identified a sequence of operations that are repetitively used in methods which are used to submit files for the first time and again in review. The code to check if a file exists or not and creating a new directory path for any new submission is common for both submission and review pages. Thus we separated it out as a different method ‘check_file_exists’ as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def check_file_exists  curr_directory&lt;br /&gt;
    #check if file exists. If not, create a new file. Else, remove the existing file and then create new file.&lt;br /&gt;
    if !File.exists? curr_directory&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    else&lt;br /&gt;
      FileUtils.rm_rf(curr_directory)&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method can now be called from both submit_file and custom_submit_file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def submit_file&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    file = params[:uploaded_file]&lt;br /&gt;
    participant.set_student_directory_num&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    curr_directory = participant.get_path.to_s+@current_folder.name&lt;br /&gt;
    check_file_exists(curr_directory)&lt;br /&gt;
&lt;br /&gt;
    safe_filename = file.original_filename.gsub(/\\/,&amp;quot;/&amp;quot;)&lt;br /&gt;
    safe_filename = FileHelper::sanitize_filename(safe_filename) # new code to sanitize file path before upload*&lt;br /&gt;
    full_filename =  curr_directory + File.split(safe_filename).last.gsub(&amp;quot; &amp;quot;,'_') #safe_filename #curr_directory +&lt;br /&gt;
    File.open(full_filename, &amp;quot;wb&amp;quot;) { |f| f.write(file.read) }&lt;br /&gt;
&lt;br /&gt;
    if params['unzip']&lt;br /&gt;
      SubmittedContentHelper::unzip_file(full_filename, curr_directory, true) if get_file_type(safe_filename) == &amp;quot;zip&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    participant.update_resubmit_times&lt;br /&gt;
&lt;br /&gt;
    #send message to reviewers when submission has been updated&lt;br /&gt;
    participant.assignment.email(participant.id) rescue nil # If the user has no team: 1) there are no reviewers to notify; 2) calling email will throw an exception. So rescue and ignore it.&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Long methods&amp;lt;ref&amp;gt;http://sourcemaking.com/refactoring/long-method&amp;lt;/ref&amp;gt; are often caused due to unnecessary parameters. Long list of parameters reduce readability&amp;lt;ref&amp;gt;http://simpleprogrammer.com/2013/04/14/what-makes-code-readable-not-what-you-think/&amp;lt;/ref&amp;gt; of code. Following changes are made to remove such unnecessary parameters:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file.JPG|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
Here the variable safename is declared and used only once. The assignment can be made inline by replacing it in the method call with actual logic in the below way:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file mod.JPG|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Below is another name change from ‘fpath’ to ‘file_path’ which is made to eliminate the most common code smell&amp;lt;ref&amp;gt;http://www.codinghorror.com/blog/2006/05/code-smells.html&amp;lt;/ref&amp;gt; ‘Excessively short identifiers’.&lt;br /&gt;
 &lt;br /&gt;
[[File:Fpath.png|frame|none|alt=Before Refactoring|Before Refactoring]] [[File:Filepath.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Consecutive assignments or modifications of the same variable can lead to “Long method” or “Excessive use of literals”. Such code smells are removed. For example, variable ‘newloc’ which is modified consecutively is refactored in the following way:&lt;br /&gt;
 &lt;br /&gt;
:Before Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_loc = @participant.get_path &lt;br /&gt;
    new_loc+= &amp;quot;/&amp;quot; &lt;br /&gt;
    new_loc+= params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_loc)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:After Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_location = @participant.get_path + &amp;quot;/&amp;quot; + params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_location)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Modified code reduces the method length and increases Readability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Unnecessary variable declarations which are never used in the method increase the length of code and confuse the readers. For example, the variable ‘display’ shown in the code snippet below was defined but never used in the method. Such variables can be removed.&lt;br /&gt;
&lt;br /&gt;
[[File:Display variable.png|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
The actual variable that was used which is ‘disp’ can now be renamed to ‘display’ which is more meaningful.&lt;br /&gt;
&lt;br /&gt;
[[File:Display change.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Many more minor code changes are made following the Ruby Coding guidelines. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if !File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We should prefer to use 'unless' for such conditions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== List of Code Smells identified and removed ==&lt;br /&gt;
*'''Duplicated code''': Identical or very similar code exists in more than one location. Code duplication frequently creates long, repeated sections of code that differ in only a few lines or characters. The length of such routines can make it difficult to quickly understand them. The repetition of largely identical code sections can conceal how they differ from one another, and therefore, what the specific purpose of each code section is. Often, the only difference is in a parameter value. The best practice in such cases is a reusable subroutine.&lt;br /&gt;
*'''Long method''': A method, function, or procedure that has grown too large. Since the early days of programming people have realized that the longer a procedure is, the more difficult it is to understand. Older languages carried an overhead in subroutine calls, which deterred people from small methods. Modern OO languages have pretty much eliminated that overhead for in-process calls.&lt;br /&gt;
*'''Too many parameters''': A long list of parameters in a procedure or function make readability and code quality worse. The more parameters a method has, the more complex it is. Limit the number of parameters you need in a given method, or use an object to combine the parameters.&lt;br /&gt;
*'''Excessively long identifiers''': In particular, the use of naming conventions to provide disambiguation that should be implicit in the software architecture.&lt;br /&gt;
*'''Excessively short identifiers''': The name of a variable should reflect its function unless the function is obvious. Pick a set of standard terminology and stick to it throughout your methods. For example, if you have Open(), you should probably have Close().&lt;br /&gt;
*'''Excessive use of literals''': These should be coded as named constants, to improve readability and to avoid programming errors. Additionally, literals can and should be externalized into resource files/scripts where possible, to facilitate localization of software if it is intended to be deployed in different regions.&lt;br /&gt;
*'''Complex conditionals''': Branches that check lots of unrelated conditions and edge cases that don't seem to capture the meaning of a block of code. Large conditional logic blocks, particularly blocks that tend to grow larger or change significantly over time have to be eliminated. Consider alternative object-oriented approaches such as decorator, strategy, or state.&lt;br /&gt;
*'''Dead Code'''&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Dead_code&amp;lt;/ref&amp;gt;: Ruthlessly delete code that isn't being used. Unused code will result in Long classes or long methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
&lt;br /&gt;
To verify that the changes didn't break anything. One can login to the VCL instance using username/password: sample/sample. One can upload a hyperlink and delete it.&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
*Polymorphism&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Polymorphism_(computer_science)&amp;lt;/ref&amp;gt; can be implemented by pushing some methods to relevant classes. Following section of code contains certain method calls which are implemented in the same class. These can be abstracted into a different class for file operations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def file_folder_action&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
    if params[:faction][:delete]        #call method to delete selected files&lt;br /&gt;
      delete_selected_files&lt;br /&gt;
    elsif params[:faction][:rename]     #call method to rename selected files&lt;br /&gt;
      rename_selected_file&lt;br /&gt;
    elsif params[:faction][:move]       #call method to move selected files&lt;br /&gt;
      move_selected_file&lt;br /&gt;
    elsif params[:faction][:copy]       #call method to copy selected files&lt;br /&gt;
      copy_selected_file&lt;br /&gt;
    elsif params[:faction][:create]     #call method to create new folder&lt;br /&gt;
      create_new_folder&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; @participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Submit file functionality currently breaks with the latest expertiza master branch code which can be fixed by further exploration.&lt;br /&gt;
*Design changes made in partial files can be extended such that code reuse can be maximized.&lt;br /&gt;
*Many more code smells can be identified and removed to improve elegance.&lt;br /&gt;
*RSpec test cases for the changes made can be added.&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;br /&gt;
&lt;br /&gt;
Steps to setup the project:&lt;br /&gt;
*Extract source code in RubyMine using the following url: https://github.com/hsure/expertiza&lt;br /&gt;
:(A) VCS -&amp;gt; Checkout from version control -&amp;gt; Github&lt;br /&gt;
:(B) Give this url:  https://github.com/hsure/expertiza -&amp;gt; Finish&lt;br /&gt;
*Confirm the sdk for RubyMine to ruby1.9.3 using File -&amp;gt; Settings &lt;br /&gt;
*Run bundle install &lt;br /&gt;
*Run - rake db:create:all&lt;br /&gt;
*Run - rake db:migrate&lt;br /&gt;
*From the Expertiza folder (project root) execute the command “rails server”&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
You should be able to view the test Database in MySQL.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Use the following credentials to explore the application:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Username: admin (Administrator)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: admin&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Username: sample (User)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: sample&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81491</id>
		<title>CSC/ECE 517 Fall 2013/oss E811 syn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81491"/>
		<updated>2013-10-30T22:50:33Z</updated>

		<summary type="html">&lt;p&gt;Hsure: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Refactor &amp;amp; test submitted_content_controller &amp;amp; submitted_content_helper''' ==&lt;br /&gt;
Expertiza is a web application that is used for Project/Assignment submissions. Students can form teams, select topics, submit assignments, review other's work and give feedback for reviews. Submission of work can be done in two ways:&lt;br /&gt;
*Hyperlinks&lt;br /&gt;
*Files/Folders&lt;br /&gt;
&lt;br /&gt;
Submitted_Content_Controller and Submitted_Content_Helper are designed to handle operations on hyperlinks and files. They include methods to submit, verify and remove hyperlinks; and create, move, rename, submit and remove files.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Project Description''' ==&lt;br /&gt;
&lt;br /&gt;
Classes: submitted_content_controller.rb (250 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
	 submitted_content_helper.rb (98 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
What they do: Allow users to submit files and links to Expertiza. &amp;lt;br&amp;gt;&lt;br /&gt;
What needs to be done:  The methods are long and complex, and contain a lot of duplicated code.  In particular, there are two different ways of submitting files: as a homework submission, and uploading a review file requested by a review rubric.  These should use common code as much as possible.  The approach to deleting submitted links and submitted files should be analogous.  There have been bugs in deleting hyperlinks, so be sure to test the code thoroughly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== '''Design Changes''' ==&lt;br /&gt;
&lt;br /&gt;
*Code duplication&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Duplicate_code&amp;lt;/ref&amp;gt; is generally considered a mark of poor or lazy programming style. Good coding style is generally associated with code reuse&amp;lt;ref&amp;gt;http://voices.yahoo.com/programming-code-reusability-11278802.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
:We identified a sequence of operations that are repetitively used in methods which are used to submit files for the first time and again in review. The code to check if a file exists or not and creating a new directory path for any new submission is common for both submission and review pages. Thus we separated it out as a different method ‘check_file_exists’ as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def check_file_exists  curr_directory&lt;br /&gt;
    #check if file exists. If not, create a new file. Else, remove the existing file and then create new file.&lt;br /&gt;
    if !File.exists? curr_directory&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    else&lt;br /&gt;
      FileUtils.rm_rf(curr_directory)&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method can now be called from both submit_file and custom_submit_file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def submit_file&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    file = params[:uploaded_file]&lt;br /&gt;
    participant.set_student_directory_num&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    curr_directory = participant.get_path.to_s+@current_folder.name&lt;br /&gt;
    check_file_exists(curr_directory)&lt;br /&gt;
&lt;br /&gt;
    safe_filename = file.original_filename.gsub(/\\/,&amp;quot;/&amp;quot;)&lt;br /&gt;
    safe_filename = FileHelper::sanitize_filename(safe_filename) # new code to sanitize file path before upload*&lt;br /&gt;
    full_filename =  curr_directory + File.split(safe_filename).last.gsub(&amp;quot; &amp;quot;,'_') #safe_filename #curr_directory +&lt;br /&gt;
    File.open(full_filename, &amp;quot;wb&amp;quot;) { |f| f.write(file.read) }&lt;br /&gt;
&lt;br /&gt;
    if params['unzip']&lt;br /&gt;
      SubmittedContentHelper::unzip_file(full_filename, curr_directory, true) if get_file_type(safe_filename) == &amp;quot;zip&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    participant.update_resubmit_times&lt;br /&gt;
&lt;br /&gt;
    #send message to reviewers when submission has been updated&lt;br /&gt;
    participant.assignment.email(participant.id) rescue nil # If the user has no team: 1) there are no reviewers to notify; 2) calling email will throw an exception. So rescue and ignore it.&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Long methods&amp;lt;ref&amp;gt;http://sourcemaking.com/refactoring/long-method&amp;lt;/ref&amp;gt; are often caused due to unnecessary parameters. Long list of parameters reduce readability&amp;lt;ref&amp;gt;http://simpleprogrammer.com/2013/04/14/what-makes-code-readable-not-what-you-think/&amp;lt;/ref&amp;gt; of code. Following changes are made to remove such unnecessary parameters:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file.JPG|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
Here the variable safename is declared and used only once. The assignment can be made inline by replacing it in the method call with actual logic in the below way:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file mod.JPG|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Below is another name change from ‘fpath’ to ‘file_path’ which is made to eliminate the most common code smell&amp;lt;ref&amp;gt;http://www.codinghorror.com/blog/2006/05/code-smells.html&amp;lt;/ref&amp;gt; ‘Excessively short identifiers’.&lt;br /&gt;
 &lt;br /&gt;
[[File:Fpath.png|frame|none|alt=Before Refactoring|Before Refactoring]] [[File:Filepath.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Consecutive assignments or modifications of the same variable can lead to “Long method” or “Excessive use of literals”. Such code smells are removed. For example, variable ‘newloc’ which is modified consecutively is refactored in the following way:&lt;br /&gt;
 &lt;br /&gt;
:Before Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_loc = @participant.get_path &lt;br /&gt;
    new_loc+= &amp;quot;/&amp;quot; &lt;br /&gt;
    new_loc+= params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_loc)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:After Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_location = @participant.get_path + &amp;quot;/&amp;quot; + params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_location)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Modified code reduces the method length and increases Readability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Unnecessary variable declarations which are never used in the method increase the length of code and confuse the readers. For example, the variable ‘display’ shown in the code snippet below was defined but never used in the method. Such variables can be removed.&lt;br /&gt;
&lt;br /&gt;
[[File:Display variable.png|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
The actual variable that was used which is ‘disp’ can now be renamed to ‘display’ which is more meaningful.&lt;br /&gt;
&lt;br /&gt;
[[File:Display change.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Many more minor code changes are made following the Ruby Coding guidelines. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if !File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We should prefer to use 'unless' for such conditions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== List of Code Smells identified and removed ==&lt;br /&gt;
*'''Duplicated code''': Identical or very similar code exists in more than one location. Code duplication frequently creates long, repeated sections of code that differ in only a few lines or characters. The length of such routines can make it difficult to quickly understand them. The repetition of largely identical code sections can conceal how they differ from one another, and therefore, what the specific purpose of each code section is. Often, the only difference is in a parameter value. The best practice in such cases is a reusable subroutine.&lt;br /&gt;
*'''Long method''': A method, function, or procedure that has grown too large. Since the early days of programming people have realized that the longer a procedure is, the more difficult it is to understand. Older languages carried an overhead in subroutine calls, which deterred people from small methods. Modern OO languages have pretty much eliminated that overhead for in-process calls.&lt;br /&gt;
*'''Too many parameters''': A long list of parameters in a procedure or function make readability and code quality worse. The more parameters a method has, the more complex it is. Limit the number of parameters you need in a given method, or use an object to combine the parameters.&lt;br /&gt;
*'''Excessively long identifiers''': In particular, the use of naming conventions to provide disambiguation that should be implicit in the software architecture.&lt;br /&gt;
*'''Excessively short identifiers''': The name of a variable should reflect its function unless the function is obvious. Pick a set of standard terminology and stick to it throughout your methods. For example, if you have Open(), you should probably have Close().&lt;br /&gt;
*'''Excessive use of literals''': These should be coded as named constants, to improve readability and to avoid programming errors. Additionally, literals can and should be externalized into resource files/scripts where possible, to facilitate localization of software if it is intended to be deployed in different regions.&lt;br /&gt;
*'''Complex conditionals''': Branches that check lots of unrelated conditions and edge cases that don't seem to capture the meaning of a block of code. Large conditional logic blocks, particularly blocks that tend to grow larger or change significantly over time have to be eliminated. Consider alternative object-oriented approaches such as decorator, strategy, or state.&lt;br /&gt;
*'''Dead Code'''&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Dead_code&amp;lt;/ref&amp;gt;: Ruthlessly delete code that isn't being used. Unused code will result in Long classes or long methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
*Polymorphism&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Polymorphism_(computer_science)&amp;lt;/ref&amp;gt; can be implemented by pushing some methods to relevant classes. Following section of code contains certain method calls which are implemented in the same class. These can be abstracted into a different class for file operations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def file_folder_action&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
    if params[:faction][:delete]        #call method to delete selected files&lt;br /&gt;
      delete_selected_files&lt;br /&gt;
    elsif params[:faction][:rename]     #call method to rename selected files&lt;br /&gt;
      rename_selected_file&lt;br /&gt;
    elsif params[:faction][:move]       #call method to move selected files&lt;br /&gt;
      move_selected_file&lt;br /&gt;
    elsif params[:faction][:copy]       #call method to copy selected files&lt;br /&gt;
      copy_selected_file&lt;br /&gt;
    elsif params[:faction][:create]     #call method to create new folder&lt;br /&gt;
      create_new_folder&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; @participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Submit file functionality currently breaks with the latest expertiza master branch code which can be fixed by further exploration.&lt;br /&gt;
*Design changes made in partial files can be extended such that code reuse can be maximized.&lt;br /&gt;
*Many more code smells can be identified and removed to improve elegance.&lt;br /&gt;
*RSpec test cases for the changes made can be added.&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;br /&gt;
&lt;br /&gt;
Steps to setup the project:&lt;br /&gt;
*Extract source code in RubyMine using the following url: https://github.com/hsure/expertiza&lt;br /&gt;
:(A) VCS -&amp;gt; Checkout from version control -&amp;gt; Github&lt;br /&gt;
:(B) Give this url:  https://github.com/hsure/expertiza -&amp;gt; Finish&lt;br /&gt;
*Confirm the sdk for RubyMine to ruby1.9.3 using File -&amp;gt; Settings &lt;br /&gt;
*Run bundle install &lt;br /&gt;
*Run - rake db:create:all&lt;br /&gt;
*Run - rake db:migrate&lt;br /&gt;
*From the Expertiza folder (project root) execute the command “rails server”&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
You should be able to view the test Database in MySQL.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Use the following credentials to explore the application:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Username: admin (Administrator)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: admin&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Username: sample (User)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: sample&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81490</id>
		<title>CSC/ECE 517 Fall 2013/oss E811 syn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81490"/>
		<updated>2013-10-30T22:50:03Z</updated>

		<summary type="html">&lt;p&gt;Hsure: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Refactor &amp;amp; test submitted_content_controller &amp;amp; submitted_content_helper''' ==&lt;br /&gt;
Expertiza is a web application that is used for Project/Assignment submissions. Students can form teams, select topics, submit assignments, review other's work and give feedback for reviews. Submission of work can be done in two ways:&lt;br /&gt;
*Hyperlinks&lt;br /&gt;
*Files/Folders&lt;br /&gt;
&lt;br /&gt;
Submitted_Content_Controller and Submitted_Content_Helper are designed to handle operations on hyperlinks and files. They include methods to submit, verify and remove hyperlinks; and create, move, rename, submit and remove files.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Project Description''' ==&lt;br /&gt;
&lt;br /&gt;
Classes: submitted_content_controller.rb (250 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
	 submitted_content_helper.rb (98 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
What they do: Allow users to submit files and links to Expertiza. &amp;lt;br&amp;gt;&lt;br /&gt;
What needs to be done:  The methods are long and complex, and contain a lot of duplicated code.  In particular, there are two different ways of submitting files: as a homework submission, and uploading a review file requested by a review rubric.  These should use common code as much as possible.  The approach to deleting submitted links and submitted files should be analogous.  There have been bugs in deleting hyperlinks, so be sure to test the code thoroughly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== '''Design Changes''' ==&lt;br /&gt;
&lt;br /&gt;
*Code duplication&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Duplicate_code&amp;lt;/ref&amp;gt; is generally considered a mark of poor or lazy programming style. Good coding style is generally associated with code reuse&amp;lt;ref&amp;gt;http://voices.yahoo.com/programming-code-reusability-11278802.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
:We identified a sequence of operations that are repetitively used in methods which are used to submit files for the first time and again in review. The code to check if a file exists or not and creating a new directory path for any new submission is common for both submission and review pages. Thus we separated it out as a different method ‘check_file_exists’ as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def check_file_exists  curr_directory&lt;br /&gt;
    #check if file exists. If not, create a new file. Else, remove the existing file and then create new file.&lt;br /&gt;
    if !File.exists? curr_directory&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    else&lt;br /&gt;
      FileUtils.rm_rf(curr_directory)&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method can now be called from both submit_file and custom_submit_file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def submit_file&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    file = params[:uploaded_file]&lt;br /&gt;
    participant.set_student_directory_num&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    curr_directory = participant.get_path.to_s+@current_folder.name&lt;br /&gt;
    check_file_exists(curr_directory)&lt;br /&gt;
&lt;br /&gt;
    safe_filename = file.original_filename.gsub(/\\/,&amp;quot;/&amp;quot;)&lt;br /&gt;
    safe_filename = FileHelper::sanitize_filename(safe_filename) # new code to sanitize file path before upload*&lt;br /&gt;
    full_filename =  curr_directory + File.split(safe_filename).last.gsub(&amp;quot; &amp;quot;,'_') #safe_filename #curr_directory +&lt;br /&gt;
    File.open(full_filename, &amp;quot;wb&amp;quot;) { |f| f.write(file.read) }&lt;br /&gt;
&lt;br /&gt;
    if params['unzip']&lt;br /&gt;
      SubmittedContentHelper::unzip_file(full_filename, curr_directory, true) if get_file_type(safe_filename) == &amp;quot;zip&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    participant.update_resubmit_times&lt;br /&gt;
&lt;br /&gt;
    #send message to reviewers when submission has been updated&lt;br /&gt;
    participant.assignment.email(participant.id) rescue nil # If the user has no team: 1) there are no reviewers to notify; 2) calling email will throw an exception. So rescue and ignore it.&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Long methods&amp;lt;ref&amp;gt;http://sourcemaking.com/refactoring/long-method&amp;lt;/ref&amp;gt; are often caused due to unnecessary parameters. Long list of parameters reduce readability&amp;lt;ref&amp;gt;http://simpleprogrammer.com/2013/04/14/what-makes-code-readable-not-what-you-think/&amp;lt;/ref&amp;gt; of code. Following changes are made to remove such unnecessary parameters:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file.JPG|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
Here the variable safename is declared and used only once. The assignment can be made inline by replacing it in the method call with actual logic in the below way:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file mod.JPG|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Below is another name change from ‘fpath’ to ‘file_path’ which is made to eliminate the most common code smell&amp;lt;ref&amp;gt;http://www.codinghorror.com/blog/2006/05/code-smells.html&amp;lt;/ref&amp;gt; ‘Excessively short identifiers’.&lt;br /&gt;
 &lt;br /&gt;
[[File:Fpath.png|frame|none|alt=Before Refactoring|Before Refactoring]] [[File:Filepath.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Consecutive assignments or modifications of the same variable can lead to “Long method” or “Excessive use of literals”. Such code smells are removed. For example, variable ‘newloc’ which is modified consecutively is refactored in the following way:&lt;br /&gt;
 &lt;br /&gt;
:Before Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_loc = @participant.get_path &lt;br /&gt;
    new_loc+= &amp;quot;/&amp;quot; &lt;br /&gt;
    new_loc+= params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_loc)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:After Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_location = @participant.get_path + &amp;quot;/&amp;quot; + params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_location)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Modified code reduces the method length and increases Readability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Unnecessary variable declarations which are never used in the method increase the length of code and confuse the readers. For example, the variable ‘display’ shown in the code snippet below was defined but never used in the method. Such variables can be removed.&lt;br /&gt;
&lt;br /&gt;
[[File:Display variable.png|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
The actual variable that was used which is ‘disp’ can now be renamed to ‘display’ which is more meaningful.&lt;br /&gt;
&lt;br /&gt;
[[File:Display change.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Many more minor code changes are made following the Ruby Coding guidelines. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if !File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We should prefer to use 'unless' for such conditions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== List of Code Smells identified and removed ==&lt;br /&gt;
*'''Duplicated code''': Identical or very similar code exists in more than one location. Code duplication frequently creates long, repeated sections of code that differ in only a few lines or characters. The length of such routines can make it difficult to quickly understand them. The repetition of largely identical code sections can conceal how they differ from one another, and therefore, what the specific purpose of each code section is. Often, the only difference is in a parameter value. The best practice in such cases is a reusable subroutine.&lt;br /&gt;
*'''Long method''': A method, function, or procedure that has grown too large. Since the early days of programming people have realized that the longer a procedure is, the more difficult it is to understand. Older languages carried an overhead in subroutine calls, which deterred people from small methods. Modern OO languages have pretty much eliminated that overhead for in-process calls.&lt;br /&gt;
*'''Too many parameters''': A long list of parameters in a procedure or function make readability and code quality worse. The more parameters a method has, the more complex it is. Limit the number of parameters you need in a given method, or use an object to combine the parameters.&lt;br /&gt;
*'''Excessively long identifiers''': In particular, the use of naming conventions to provide disambiguation that should be implicit in the software architecture.&lt;br /&gt;
*'''Excessively short identifiers''': The name of a variable should reflect its function unless the function is obvious. Pick a set of standard terminology and stick to it throughout your methods. For example, if you have Open(), you should probably have Close().&lt;br /&gt;
*'''Excessive use of literals''': These should be coded as named constants, to improve readability and to avoid programming errors. Additionally, literals can and should be externalized into resource files/scripts where possible, to facilitate localization of software if it is intended to be deployed in different regions.&lt;br /&gt;
*'''Complex conditionals''': Branches that check lots of unrelated conditions and edge cases that don't seem to capture the meaning of a block of code. Large conditional logic blocks, particularly blocks that tend to grow larger or change significantly over time have to be eliminated. Consider alternative object-oriented approaches such as decorator, strategy, or state.&lt;br /&gt;
*'''Dead Code'''&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Dead_code&amp;lt;/ref&amp;gt;: Ruthlessly delete code that isn't being used. Unused code will result in Long classes or long methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
*Polymorphism&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Polymorphism_(computer_science)&amp;lt;/ref&amp;gt; can be implemented by pushing some methods to relevant classes. Following section of code contains certain method calls which are implemented in the same class. These can be abstracted into a different class for file operations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def file_folder_action&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
    if params[:faction][:delete]        #call method to delete selected files&lt;br /&gt;
      delete_selected_files&lt;br /&gt;
    elsif params[:faction][:rename]     #call method to rename selected files&lt;br /&gt;
      rename_selected_file&lt;br /&gt;
    elsif params[:faction][:move]       #call method to move selected files&lt;br /&gt;
      move_selected_file&lt;br /&gt;
    elsif params[:faction][:copy]       #call method to copy selected files&lt;br /&gt;
      copy_selected_file&lt;br /&gt;
    elsif params[:faction][:create]     #call method to create new folder&lt;br /&gt;
      create_new_folder&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; @participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Submit file functionality currently breaks with the latest expertiza master branch code which can be fixed by further exploration.&lt;br /&gt;
*Design changes made in partial files can be extended such that code reuse can be maximized.&lt;br /&gt;
*Many more code smells can be identified and removed to improve elegance.&lt;br /&gt;
*RSpec test cases for the changes made can be added.&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;br /&gt;
&lt;br /&gt;
Steps to setup the project:&lt;br /&gt;
*Extract source code in RubyMine using the following url: https://github.com/hsure/expertiza&lt;br /&gt;
:(A) VCS -&amp;gt; Checkout from version control -&amp;gt; Github&lt;br /&gt;
:(B) Give this url:  https://github.com/hsure/expertiza -&amp;gt; Finish&lt;br /&gt;
*Confirm the sdk for RubyMine to ruby1.9.3 using File -&amp;gt; Settings &lt;br /&gt;
*Run bundle install &lt;br /&gt;
*Run - rake db:create:all&lt;br /&gt;
*Run - rake db:migrate&lt;br /&gt;
*From the Expertiza folder (project root) execute the command “rails server”&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
You should be able to view the test Database in MySQL.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Use the following credentials to explore the application:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Username: admin (Administrator)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: admin&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Username: sample (User)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: sample&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.codinghorror.com/blog/2006/05/code-smells.html&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81426</id>
		<title>CSC/ECE 517 Fall 2013/oss E811 syn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81426"/>
		<updated>2013-10-30T22:19:09Z</updated>

		<summary type="html">&lt;p&gt;Hsure: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Refactor &amp;amp; test submitted_content_controller &amp;amp; submitted_content_helper''' ==&lt;br /&gt;
Expertiza is a web application that is used for Project/Assignment submissions. Students can form teams, select topics, submit assignments, review other's work and give feedback for reviews. Submission of work can be done in two ways:&lt;br /&gt;
*Hyperlinks&lt;br /&gt;
*Files/Folders&lt;br /&gt;
&lt;br /&gt;
Submitted_Content_Controller and Submitted_Content_Helper are designed to handle operations on hyperlinks and files. They include methods to submit, verify and remove hyperlinks; and create, move, rename, submit and remove files.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Project Description''' ==&lt;br /&gt;
&lt;br /&gt;
Classes: submitted_content_controller.rb (250 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
	 submitted_content_helper.rb (98 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
What they do: Allow users to submit files and links to Expertiza. &amp;lt;br&amp;gt;&lt;br /&gt;
What needs to be done:  The methods are long and complex, and contain a lot of duplicated code&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Duplicate_code&amp;lt;/ref&amp;gt;.  In particular, there are two different ways of submitting files: as a homework submission, and uploading a review file requested by a review rubric.  These should use common code as much as possible.  The approach to deleting submitted links and submitted files should be analogous.  There have been bugs in deleting hyperlinks, so be sure to test the code thoroughly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== '''Design Changes''' ==&lt;br /&gt;
&lt;br /&gt;
*Code duplication is generally considered a mark of poor or lazy programming style. Good coding style is generally associated with code reuse.&lt;br /&gt;
&lt;br /&gt;
:We identified a sequence of operations that are repetitively used in methods which are used to submit files for the first time and again in review. The code to check if a file exists or not and creating a new directory path for any new submission is common for both submission and review pages. Thus we separated it out as a different method ‘check_file_exists’ as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def check_file_exists  curr_directory&lt;br /&gt;
    #check if file exists. If not, create a new file. Else, remove the existing file and then create new file.&lt;br /&gt;
    if !File.exists? curr_directory&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    else&lt;br /&gt;
      FileUtils.rm_rf(curr_directory)&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method can now be called from both submit_file and custom_submit_file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def submit_file&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    file = params[:uploaded_file]&lt;br /&gt;
    participant.set_student_directory_num&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    curr_directory = participant.get_path.to_s+@current_folder.name&lt;br /&gt;
    check_file_exists(curr_directory)&lt;br /&gt;
&lt;br /&gt;
    safe_filename = file.original_filename.gsub(/\\/,&amp;quot;/&amp;quot;)&lt;br /&gt;
    safe_filename = FileHelper::sanitize_filename(safe_filename) # new code to sanitize file path before upload*&lt;br /&gt;
    full_filename =  curr_directory + File.split(safe_filename).last.gsub(&amp;quot; &amp;quot;,'_') #safe_filename #curr_directory +&lt;br /&gt;
    File.open(full_filename, &amp;quot;wb&amp;quot;) { |f| f.write(file.read) }&lt;br /&gt;
&lt;br /&gt;
    if params['unzip']&lt;br /&gt;
      SubmittedContentHelper::unzip_file(full_filename, curr_directory, true) if get_file_type(safe_filename) == &amp;quot;zip&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    participant.update_resubmit_times&lt;br /&gt;
&lt;br /&gt;
    #send message to reviewers when submission has been updated&lt;br /&gt;
    participant.assignment.email(participant.id) rescue nil # If the user has no team: 1) there are no reviewers to notify; 2) calling email will throw an exception. So rescue and ignore it.&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Long methods&amp;lt;ref&amp;gt;http://sourcemaking.com/refactoring/long-method&amp;lt;/ref&amp;gt; are often caused due to unnecessary parameters. Long list of parameters reduce readability of code. Following changes are made to remove such unnecessary parameters:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file.JPG|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
Here the variable safename is declared and used only once. The assignment can be made inline by replacing it in the method call with actual logic in the below way:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file mod.JPG|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Below is another name change from ‘fpath’ to ‘file_path’ which is made to eliminate the most common code smell&amp;lt;ref&amp;gt;http://www.codinghorror.com/blog/2006/05/code-smells.html&amp;lt;/ref&amp;gt; ‘Excessively short identifiers’.&lt;br /&gt;
 &lt;br /&gt;
[[File:Fpath.png|frame|none|alt=Before Refactoring|Before Refactoring]] [[File:Filepath.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Consecutive assignments or modifications of the same variable can lead to “Long method” or “Excessive use of literals”. Such code smells are removed. For example, variable ‘newloc’ which is modified consecutively is refactored in the following way:&lt;br /&gt;
 &lt;br /&gt;
:Before Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_loc = @participant.get_path &lt;br /&gt;
    new_loc+= &amp;quot;/&amp;quot; &lt;br /&gt;
    new_loc+= params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_loc)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:After Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_location = @participant.get_path + &amp;quot;/&amp;quot; + params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_location)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Modified code reduces the method length and increases Readability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Unnecessary variable declarations which are never used in the method increase the length of code and confuse the readers. For example, the variable ‘display’ shown in the code snippet below was defined but never used in the method. Such variables can be removed.&lt;br /&gt;
&lt;br /&gt;
[[File:Display variable.png|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
The actual variable that was used which is ‘disp’ can now be renamed to ‘display’ which is more meaningful.&lt;br /&gt;
&lt;br /&gt;
[[File:Display change.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Many more minor code changes are made following the Ruby Coding guidelines. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if !File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We should prefer to use 'unless' for such conditions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== List of Code Smells identified and removed ==&lt;br /&gt;
*'''Duplicated code''': Identical or very similar code exists in more than one location. Code duplication frequently creates long, repeated sections of code that differ in only a few lines or characters. The length of such routines can make it difficult to quickly understand them. The repetition of largely identical code sections can conceal how they differ from one another, and therefore, what the specific purpose of each code section is. Often, the only difference is in a parameter value. The best practice in such cases is a reusable subroutine.&lt;br /&gt;
*'''Long method''': A method, function, or procedure that has grown too large. Since the early days of programming people have realized that the longer a procedure is, the more difficult it is to understand. Older languages carried an overhead in subroutine calls, which deterred people from small methods. Modern OO languages have pretty much eliminated that overhead for in-process calls.&lt;br /&gt;
*'''Too many parameters''': A long list of parameters in a procedure or function make readability and code quality worse. The more parameters a method has, the more complex it is. Limit the number of parameters you need in a given method, or use an object to combine the parameters.&lt;br /&gt;
*'''Excessively long identifiers''': In particular, the use of naming conventions to provide disambiguation that should be implicit in the software architecture.&lt;br /&gt;
*'''Excessively short identifiers''': The name of a variable should reflect its function unless the function is obvious. Pick a set of standard terminology and stick to it throughout your methods. For example, if you have Open(), you should probably have Close().&lt;br /&gt;
*'''Excessive use of literals''': These should be coded as named constants, to improve readability and to avoid programming errors. Additionally, literals can and should be externalized into resource files/scripts where possible, to facilitate localization of software if it is intended to be deployed in different regions.&lt;br /&gt;
*'''Complex conditionals''': Branches that check lots of unrelated conditions and edge cases that don't seem to capture the meaning of a block of code. Large conditional logic blocks, particularly blocks that tend to grow larger or change significantly over time have to be eliminated. Consider alternative object-oriented approaches such as decorator, strategy, or state.&lt;br /&gt;
*'''Dead Code''': Ruthlessly delete code that isn't being used. Unused code will result in Long classes or long methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
*Polymorphism can be implemented by pushing some methods to relevant classes. Following section of code contains certain method calls which are implemented in the same class. These can be abstracted into a different class for file operations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def file_folder_action&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
    if params[:faction][:delete]        #call method to delete selected files&lt;br /&gt;
      delete_selected_files&lt;br /&gt;
    elsif params[:faction][:rename]     #call method to rename selected files&lt;br /&gt;
      rename_selected_file&lt;br /&gt;
    elsif params[:faction][:move]       #call method to move selected files&lt;br /&gt;
      move_selected_file&lt;br /&gt;
    elsif params[:faction][:copy]       #call method to copy selected files&lt;br /&gt;
      copy_selected_file&lt;br /&gt;
    elsif params[:faction][:create]     #call method to create new folder&lt;br /&gt;
      create_new_folder&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; @participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Submit file functionality currently breaks with the latest expertiza master branch code which can be fixed by further exploration.&lt;br /&gt;
*Design changes made in partial files can be extended such that code reuse can be maximized.&lt;br /&gt;
*Many more code smells can be identified and removed to improve elegance.&lt;br /&gt;
*RSpec test cases for the changes made can be added.&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;br /&gt;
&lt;br /&gt;
Steps to setup the project:&lt;br /&gt;
*Extract source code in RubyMine using the following url: https://github.com/hsure/expertiza&lt;br /&gt;
:(A) VCS -&amp;gt; Checkout from version control -&amp;gt; Github&lt;br /&gt;
:(B) Give this url:  https://github.com/hsure/expertiza -&amp;gt; Finish&lt;br /&gt;
*Confirm the sdk for RubyMine to ruby1.9.3 using File -&amp;gt; Settings &lt;br /&gt;
*Run bundle install &lt;br /&gt;
*Run - rake db:create:all&lt;br /&gt;
*Run - rake db:migrate&lt;br /&gt;
*From the Expertiza folder (project root) execute the command “rails server”&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
You should be able to view the test Database in MySQL.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Use the following credentials to explore the application:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Username: admin (Administrator)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: admin&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Username: sample (User)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: sample&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.codinghorror.com/blog/2006/05/code-smells.html&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81420</id>
		<title>CSC/ECE 517 Fall 2013/oss E811 syn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81420"/>
		<updated>2013-10-30T22:16:25Z</updated>

		<summary type="html">&lt;p&gt;Hsure: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Refactor &amp;amp; test submitted_content_controller &amp;amp; submitted_content_helper''' ==&lt;br /&gt;
Expertiza is a web application that is used for Project/Assignment submissions. Students can form teams, select topics, submit assignments, review other's work and give feedback for reviews. Submission of work can be done in two ways:&lt;br /&gt;
*Hyperlinks&lt;br /&gt;
*Files/Folders&lt;br /&gt;
&lt;br /&gt;
Submitted_Content_Controller and Submitted_Content_Helper are designed to handle operations on hyperlinks and files. They include methods to submit, verify and remove hyperlinks; and create, move, rename, submit and remove files.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Project Description''' ==&lt;br /&gt;
&lt;br /&gt;
Classes: submitted_content_controller.rb (250 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
	 submitted_content_helper.rb (98 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
What they do: Allow users to submit files and links to Expertiza. &amp;lt;br&amp;gt;&lt;br /&gt;
What needs to be done:  The methods are long and complex, and contain a lot of duplicated code.  In particular, there are two different ways of submitting files: as a homework submission, and uploading a review file requested by a review rubric.  These should use common code as much as possible.  The approach to deleting submitted links and submitted files should be analogous.  There have been bugs in deleting hyperlinks, so be sure to test the code thoroughly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== '''Design Changes''' ==&lt;br /&gt;
&lt;br /&gt;
*Code duplication is generally considered a mark of poor or lazy programming style. Good coding style is generally associated with code reuse.&lt;br /&gt;
&lt;br /&gt;
:We identified a sequence of operations that are repetitively used in methods which are used to submit files for the first time and again in review. The code to check if a file exists or not and creating a new directory path for any new submission is common for both submission and review pages. Thus we separated it out as a different method ‘check_file_exists’ as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def check_file_exists  curr_directory&lt;br /&gt;
    #check if file exists. If not, create a new file. Else, remove the existing file and then create new file.&lt;br /&gt;
    if !File.exists? curr_directory&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    else&lt;br /&gt;
      FileUtils.rm_rf(curr_directory)&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method can now be called from both submit_file and custom_submit_file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def submit_file&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    file = params[:uploaded_file]&lt;br /&gt;
    participant.set_student_directory_num&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    curr_directory = participant.get_path.to_s+@current_folder.name&lt;br /&gt;
    check_file_exists(curr_directory)&lt;br /&gt;
&lt;br /&gt;
    safe_filename = file.original_filename.gsub(/\\/,&amp;quot;/&amp;quot;)&lt;br /&gt;
    safe_filename = FileHelper::sanitize_filename(safe_filename) # new code to sanitize file path before upload*&lt;br /&gt;
    full_filename =  curr_directory + File.split(safe_filename).last.gsub(&amp;quot; &amp;quot;,'_') #safe_filename #curr_directory +&lt;br /&gt;
    File.open(full_filename, &amp;quot;wb&amp;quot;) { |f| f.write(file.read) }&lt;br /&gt;
&lt;br /&gt;
    if params['unzip']&lt;br /&gt;
      SubmittedContentHelper::unzip_file(full_filename, curr_directory, true) if get_file_type(safe_filename) == &amp;quot;zip&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    participant.update_resubmit_times&lt;br /&gt;
&lt;br /&gt;
    #send message to reviewers when submission has been updated&lt;br /&gt;
    participant.assignment.email(participant.id) rescue nil # If the user has no team: 1) there are no reviewers to notify; 2) calling email will throw an exception. So rescue and ignore it.&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Long methods&amp;lt;ref&amp;gt;http://sourcemaking.com/refactoring/long-method&amp;lt;/ref&amp;gt; are often caused due to unnecessary parameters. Long list of parameters reduce readability of code. Following changes are made to remove such unnecessary parameters:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file.JPG|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
Here the variable safename is declared and used only once. The assignment can be made inline by replacing it in the method call with actual logic in the below way:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file mod.JPG|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Below is another name change from ‘fpath’ to ‘file_path’ which is made to eliminate the most common code smell&amp;lt;ref&amp;gt;http://www.codinghorror.com/blog/2006/05/code-smells.html&amp;lt;/ref&amp;gt; ‘Excessively short identifiers’.&lt;br /&gt;
 &lt;br /&gt;
[[File:Fpath.png|frame|none|alt=Before Refactoring|Before Refactoring]] [[File:Filepath.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Consecutive assignments or modifications of the same variable can lead to “Long method” or “Excessive use of literals”. Such code smells are removed. For example, variable ‘newloc’ which is modified consecutively is refactored in the following way:&lt;br /&gt;
 &lt;br /&gt;
:Before Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_loc = @participant.get_path &lt;br /&gt;
    new_loc+= &amp;quot;/&amp;quot; &lt;br /&gt;
    new_loc+= params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_loc)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:After Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_location = @participant.get_path + &amp;quot;/&amp;quot; + params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_location)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Modified code reduces the method length and increases Readability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Unnecessary variable declarations which are never used in the method increase the length of code and confuse the readers. For example, the variable ‘display’ shown in the code snippet below was defined but never used in the method. Such variables can be removed.&lt;br /&gt;
&lt;br /&gt;
[[File:Display variable.png|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
The actual variable that was used which is ‘disp’ can now be renamed to ‘display’ which is more meaningful.&lt;br /&gt;
&lt;br /&gt;
[[File:Display change.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Many more minor code changes are made following the Ruby Coding guidelines. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if !File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We should prefer to use 'unless' for such conditions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== List of Code Smells identified and removed ==&lt;br /&gt;
*'''Duplicated code''': Identical or very similar code exists in more than one location. Code duplication frequently creates long, repeated sections of code that differ in only a few lines or characters. The length of such routines can make it difficult to quickly understand them. The repetition of largely identical code sections can conceal how they differ from one another, and therefore, what the specific purpose of each code section is. Often, the only difference is in a parameter value. The best practice in such cases is a reusable subroutine.&lt;br /&gt;
*'''Long method''': A method, function, or procedure that has grown too large. Since the early days of programming people have realized that the longer a procedure is, the more difficult it is to understand. Older languages carried an overhead in subroutine calls, which deterred people from small methods. Modern OO languages have pretty much eliminated that overhead for in-process calls.&lt;br /&gt;
*'''Too many parameters''': A long list of parameters in a procedure or function make readability and code quality worse. The more parameters a method has, the more complex it is. Limit the number of parameters you need in a given method, or use an object to combine the parameters.&lt;br /&gt;
*'''Excessively long identifiers''': In particular, the use of naming conventions to provide disambiguation that should be implicit in the software architecture.&lt;br /&gt;
*'''Excessively short identifiers''': The name of a variable should reflect its function unless the function is obvious. Pick a set of standard terminology and stick to it throughout your methods. For example, if you have Open(), you should probably have Close().&lt;br /&gt;
*'''Excessive use of literals''': These should be coded as named constants, to improve readability and to avoid programming errors. Additionally, literals can and should be externalized into resource files/scripts where possible, to facilitate localization of software if it is intended to be deployed in different regions.&lt;br /&gt;
*'''Complex conditionals''': Branches that check lots of unrelated conditions and edge cases that don't seem to capture the meaning of a block of code. Large conditional logic blocks, particularly blocks that tend to grow larger or change significantly over time have to be eliminated. Consider alternative object-oriented approaches such as decorator, strategy, or state.&lt;br /&gt;
*'''Dead Code''': Ruthlessly delete code that isn't being used. Unused code will result in Long classes or long methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
*Polymorphism can be implemented by pushing some methods to relevant classes. Following section of code contains certain method calls which are implemented in the same class. These can be abstracted into a different class for file operations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def file_folder_action&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
    if params[:faction][:delete]        #call method to delete selected files&lt;br /&gt;
      delete_selected_files&lt;br /&gt;
    elsif params[:faction][:rename]     #call method to rename selected files&lt;br /&gt;
      rename_selected_file&lt;br /&gt;
    elsif params[:faction][:move]       #call method to move selected files&lt;br /&gt;
      move_selected_file&lt;br /&gt;
    elsif params[:faction][:copy]       #call method to copy selected files&lt;br /&gt;
      copy_selected_file&lt;br /&gt;
    elsif params[:faction][:create]     #call method to create new folder&lt;br /&gt;
      create_new_folder&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; @participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Submit file functionality currently breaks with the latest expertiza master branch code which can be fixed by further exploration.&lt;br /&gt;
*Design changes made in partial files can be extended such that code reuse can be maximized.&lt;br /&gt;
*Many more code smells can be identified and removed to improve elegance.&lt;br /&gt;
*RSpec test cases for the changes made can be added.&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;br /&gt;
&lt;br /&gt;
Steps to setup the project:&lt;br /&gt;
*Extract source code in RubyMine using the following url: https://github.com/hsure/expertiza&lt;br /&gt;
:(A) VCS -&amp;gt; Checkout from version control -&amp;gt; Github&lt;br /&gt;
:(B) Give this url:  https://github.com/hsure/expertiza -&amp;gt; Finish&lt;br /&gt;
*Confirm the sdk for RubyMine to ruby1.9.3 using File -&amp;gt; Settings &lt;br /&gt;
*Run bundle install &lt;br /&gt;
*Run - rake db:create:all&lt;br /&gt;
*Run - rake db:migrate&lt;br /&gt;
*From the Expertiza folder (project root) execute the command “rails server”&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
You should be able to view the test Database in MySQL.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Use the following credentials to explore the application:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Username: admin (Administrator)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: admin&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Username: sample (User)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: sample&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.codinghorror.com/blog/2006/05/code-smells.html&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81415</id>
		<title>CSC/ECE 517 Fall 2013/oss E811 syn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81415"/>
		<updated>2013-10-30T22:14:36Z</updated>

		<summary type="html">&lt;p&gt;Hsure: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Refactor &amp;amp; test submitted_content_controller &amp;amp; submitted_content_helper''' ==&lt;br /&gt;
Expertiza is a web application that is used for Project/Assignment submissions. Students can form teams, select topics, submit assignments, review other's work and give feedback for reviews. Submission of work can be done in two ways:&lt;br /&gt;
*Hyperlinks&lt;br /&gt;
*Files/Folders&lt;br /&gt;
&lt;br /&gt;
Submitted_Content_Controller and Submitted_Content_Helper are designed to handle operations on hyperlinks and files. They include methods to submit, verify and remove hyperlinks; and create, move, rename, submit and remove files.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Project Description''' ==&lt;br /&gt;
&lt;br /&gt;
Classes: submitted_content_controller.rb (250 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
	 submitted_content_helper.rb (98 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
What they do: Allow users to submit files and links to Expertiza. &amp;lt;br&amp;gt;&lt;br /&gt;
What needs to be done:  The methods are long and complex, and contain a lot of duplicated code.  In particular, there are two different ways of submitting files: as a homework submission, and uploading a review file requested by a review rubric.  These should use common code as much as possible.  The approach to deleting submitted links and submitted files should be analogous.  There have been bugs in deleting hyperlinks, so be sure to test the code thoroughly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== '''Design Changes''' ==&lt;br /&gt;
&lt;br /&gt;
*Code duplication is generally considered a mark of poor or lazy programming style. Good coding style is generally associated with code reuse.&lt;br /&gt;
&lt;br /&gt;
:We identified a sequence of operations that are repetitively used in methods which are used to submit files for the first time and again in review. The code to check if a file exists or not and creating a new directory path for any new submission is common for both submission and review pages. Thus we separated it out as a different method ‘check_file_exists’ as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def check_file_exists  curr_directory&lt;br /&gt;
    #check if file exists. If not, create a new file. Else, remove the existing file and then create new file.&lt;br /&gt;
    if !File.exists? curr_directory&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    else&lt;br /&gt;
      FileUtils.rm_rf(curr_directory)&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method can now be called from both submit_file and custom_submit_file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def submit_file&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    file = params[:uploaded_file]&lt;br /&gt;
    participant.set_student_directory_num&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    curr_directory = participant.get_path.to_s+@current_folder.name&lt;br /&gt;
    check_file_exists(curr_directory)&lt;br /&gt;
&lt;br /&gt;
    safe_filename = file.original_filename.gsub(/\\/,&amp;quot;/&amp;quot;)&lt;br /&gt;
    safe_filename = FileHelper::sanitize_filename(safe_filename) # new code to sanitize file path before upload*&lt;br /&gt;
    full_filename =  curr_directory + File.split(safe_filename).last.gsub(&amp;quot; &amp;quot;,'_') #safe_filename #curr_directory +&lt;br /&gt;
    File.open(full_filename, &amp;quot;wb&amp;quot;) { |f| f.write(file.read) }&lt;br /&gt;
&lt;br /&gt;
    if params['unzip']&lt;br /&gt;
      SubmittedContentHelper::unzip_file(full_filename, curr_directory, true) if get_file_type(safe_filename) == &amp;quot;zip&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    participant.update_resubmit_times&lt;br /&gt;
&lt;br /&gt;
    #send message to reviewers when submission has been updated&lt;br /&gt;
    participant.assignment.email(participant.id) rescue nil # If the user has no team: 1) there are no reviewers to notify; 2) calling email will throw an exception. So rescue and ignore it.&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Long methods are often caused due to unnecessary parameters. Long list of parameters reduce readability of code. Following changes are made to remove such unnecessary parameters:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file.JPG|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
Here the variable safename is declared and used only once. The assignment can be made inline by replacing it in the method call with actual logic in the below way:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file mod.JPG|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Below is another name change from ‘fpath’ to ‘file_path’ which is made to eliminate the most common code smell&amp;lt;ref&amp;gt;http://www.codinghorror.com/blog/2006/05/code-smells.html&amp;lt;/ref&amp;gt; ‘Excessively short identifiers’.&lt;br /&gt;
 &lt;br /&gt;
[[File:Fpath.png|frame|none|alt=Before Refactoring|Before Refactoring]] [[File:Filepath.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Consecutive assignments or modifications of the same variable can lead to “Long method” or “Excessive use of literals”. Such code smells are removed. For example, variable ‘newloc’ which is modified consecutively is refactored in the following way:&lt;br /&gt;
 &lt;br /&gt;
:Before Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_loc = @participant.get_path &lt;br /&gt;
    new_loc+= &amp;quot;/&amp;quot; &lt;br /&gt;
    new_loc+= params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_loc)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:After Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_location = @participant.get_path + &amp;quot;/&amp;quot; + params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_location)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Modified code reduces the method length and increases Readability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Unnecessary variable declarations which are never used in the method increase the length of code and confuse the readers. For example, the variable ‘display’ shown in the code snippet below was defined but never used in the method. Such variables can be removed.&lt;br /&gt;
&lt;br /&gt;
[[File:Display variable.png|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
The actual variable that was used which is ‘disp’ can now be renamed to ‘display’ which is more meaningful.&lt;br /&gt;
&lt;br /&gt;
[[File:Display change.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Many more minor code changes are made following the Ruby Coding guidelines. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if !File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We should prefer to use 'unless' for such conditions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== List of Code Smells identified and removed ==&lt;br /&gt;
*'''Duplicated code''': Identical or very similar code exists in more than one location. Code duplication frequently creates long, repeated sections of code that differ in only a few lines or characters. The length of such routines can make it difficult to quickly understand them. The repetition of largely identical code sections can conceal how they differ from one another, and therefore, what the specific purpose of each code section is. Often, the only difference is in a parameter value. The best practice in such cases is a reusable subroutine.&lt;br /&gt;
*'''Long method''': A method, function, or procedure that has grown too large. Since the early days of programming people have realized that the longer a procedure is, the more difficult it is to understand. Older languages carried an overhead in subroutine calls, which deterred people from small methods. Modern OO languages have pretty much eliminated that overhead for in-process calls.&lt;br /&gt;
*'''Too many parameters''': A long list of parameters in a procedure or function make readability and code quality worse. The more parameters a method has, the more complex it is. Limit the number of parameters you need in a given method, or use an object to combine the parameters.&lt;br /&gt;
*'''Excessively long identifiers''': In particular, the use of naming conventions to provide disambiguation that should be implicit in the software architecture.&lt;br /&gt;
*'''Excessively short identifiers''': The name of a variable should reflect its function unless the function is obvious. Pick a set of standard terminology and stick to it throughout your methods. For example, if you have Open(), you should probably have Close().&lt;br /&gt;
*'''Excessive use of literals''': These should be coded as named constants, to improve readability and to avoid programming errors. Additionally, literals can and should be externalized into resource files/scripts where possible, to facilitate localization of software if it is intended to be deployed in different regions.&lt;br /&gt;
*'''Complex conditionals''': Branches that check lots of unrelated conditions and edge cases that don't seem to capture the meaning of a block of code. Large conditional logic blocks, particularly blocks that tend to grow larger or change significantly over time have to be eliminated. Consider alternative object-oriented approaches such as decorator, strategy, or state.&lt;br /&gt;
*'''Dead Code''': Ruthlessly delete code that isn't being used. Unused code will result in Long classes or long methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
*Polymorphism can be implemented by pushing some methods to relevant classes. Following section of code contains certain method calls which are implemented in the same class. These can be abstracted into a different class for file operations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def file_folder_action&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
    if params[:faction][:delete]        #call method to delete selected files&lt;br /&gt;
      delete_selected_files&lt;br /&gt;
    elsif params[:faction][:rename]     #call method to rename selected files&lt;br /&gt;
      rename_selected_file&lt;br /&gt;
    elsif params[:faction][:move]       #call method to move selected files&lt;br /&gt;
      move_selected_file&lt;br /&gt;
    elsif params[:faction][:copy]       #call method to copy selected files&lt;br /&gt;
      copy_selected_file&lt;br /&gt;
    elsif params[:faction][:create]     #call method to create new folder&lt;br /&gt;
      create_new_folder&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; @participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Submit file functionality currently breaks with the latest expertiza master branch code which can be fixed by further exploration.&lt;br /&gt;
*Design changes made in partial files can be extended such that code reuse can be maximized.&lt;br /&gt;
*Many more code smells can be identified and removed to improve elegance.&lt;br /&gt;
*RSpec test cases for the changes made can be added.&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;br /&gt;
&lt;br /&gt;
Steps to setup the project:&lt;br /&gt;
*Extract source code in RubyMine using the following url: https://github.com/hsure/expertiza&lt;br /&gt;
:(A) VCS -&amp;gt; Checkout from version control -&amp;gt; Github&lt;br /&gt;
:(B) Give this url:  https://github.com/hsure/expertiza -&amp;gt; Finish&lt;br /&gt;
*Confirm the sdk for RubyMine to ruby1.9.3 using File -&amp;gt; Settings &lt;br /&gt;
*Run bundle install &lt;br /&gt;
*Run - rake db:create:all&lt;br /&gt;
*Run - rake db:migrate&lt;br /&gt;
*From the Expertiza folder (project root) execute the command “rails server”&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
You should be able to view the test Database in MySQL.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Use the following credentials to explore the application:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Username: admin (Administrator)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: admin&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Username: sample (User)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: sample&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
*http://github.com/styleguide/ruby&lt;br /&gt;
*http://www.ruby-doc.org/docs/ProgrammingRuby/&lt;br /&gt;
*http://www.ruby-lang.org/en/documentation/&lt;br /&gt;
*http://en.wikibooks.org/wiki/Ruby_programming_language&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81396</id>
		<title>CSC/ECE 517 Fall 2013/oss E811 syn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81396"/>
		<updated>2013-10-30T21:59:23Z</updated>

		<summary type="html">&lt;p&gt;Hsure: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Refactor &amp;amp; test submitted_content_controller &amp;amp; submitted_content_helper''' ==&lt;br /&gt;
Expertiza is a web application that is used for Project/Assignment submissions. Students can form teams, select topics, submit assignments, review other's work and give feedback for reviews. Submission of work can be done in two ways:&lt;br /&gt;
*Hyperlinks&lt;br /&gt;
*Files/Folders&lt;br /&gt;
&lt;br /&gt;
Submitted_Content_Controller and Submitted_Content_Helper are designed to handle operations on hyperlinks and files. They include methods to submit, verify and remove hyperlinks; and create, move, rename, submit and remove files.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Project Description''' ==&lt;br /&gt;
&lt;br /&gt;
Classes: submitted_content_controller.rb (250 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
	 submitted_content_helper.rb (98 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
What they do: Allow users to submit files and links to Expertiza. &amp;lt;br&amp;gt;&lt;br /&gt;
What needs to be done:  The methods are long and complex, and contain a lot of duplicated code.  In particular, there are two different ways of submitting files: as a homework submission, and uploading a review file requested by a review rubric.  These should use common code as much as possible.  The approach to deleting submitted links and submitted files should be analogous.  There have been bugs in deleting hyperlinks, so be sure to test the code thoroughly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== '''Design Changes''' ==&lt;br /&gt;
&lt;br /&gt;
*Code duplication is generally considered a mark of poor or lazy programming style. Good coding style is generally associated with code reuse.&lt;br /&gt;
&lt;br /&gt;
:We identified a sequence of operations that are repetitively used in methods which are used to submit files for the first time and again in review. The code to check if a file exists or not and creating a new directory path for any new submission is common for both submission and review pages. Thus we separated it out as a different method ‘check_file_exists’ as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def check_file_exists  curr_directory&lt;br /&gt;
    #check if file exists. If not, create a new file. Else, remove the existing file and then create new file.&lt;br /&gt;
    if !File.exists? curr_directory&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    else&lt;br /&gt;
      FileUtils.rm_rf(curr_directory)&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method can now be called from both submit_file and custom_submit_file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def submit_file&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    file = params[:uploaded_file]&lt;br /&gt;
    participant.set_student_directory_num&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    curr_directory = participant.get_path.to_s+@current_folder.name&lt;br /&gt;
    check_file_exists(curr_directory)&lt;br /&gt;
&lt;br /&gt;
    safe_filename = file.original_filename.gsub(/\\/,&amp;quot;/&amp;quot;)&lt;br /&gt;
    safe_filename = FileHelper::sanitize_filename(safe_filename) # new code to sanitize file path before upload*&lt;br /&gt;
    full_filename =  curr_directory + File.split(safe_filename).last.gsub(&amp;quot; &amp;quot;,'_') #safe_filename #curr_directory +&lt;br /&gt;
    File.open(full_filename, &amp;quot;wb&amp;quot;) { |f| f.write(file.read) }&lt;br /&gt;
&lt;br /&gt;
    if params['unzip']&lt;br /&gt;
      SubmittedContentHelper::unzip_file(full_filename, curr_directory, true) if get_file_type(safe_filename) == &amp;quot;zip&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    participant.update_resubmit_times&lt;br /&gt;
&lt;br /&gt;
    #send message to reviewers when submission has been updated&lt;br /&gt;
    participant.assignment.email(participant.id) rescue nil # If the user has no team: 1) there are no reviewers to notify; 2) calling email will throw an exception. So rescue and ignore it.&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Long methods are often caused due to unnecessary parameters. Long list of parameters reduce readability of code. Following changes are made to remove such unnecessary parameters:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file.JPG|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
Here the variable safename is declared and used only once. The assignment can be made inline by replacing it in the method call with actual logic in the below way:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file mod.JPG|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Below is another name change from ‘fpath’ to ‘file_path’ which is made to eliminate the most common code smell ‘Excessively short identifiers’.&lt;br /&gt;
 &lt;br /&gt;
[[File:Fpath.png|frame|none|alt=Before Refactoring|Before Refactoring]] [[File:Filepath.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Consecutive assignments or modifications of the same variable can lead to “Long method” or “Excessive use of literals”. Such code smells are removed. For example, variable ‘newloc’ which is modified consecutively is refactored in the following way:&lt;br /&gt;
 &lt;br /&gt;
:Before Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_loc = @participant.get_path &lt;br /&gt;
    new_loc+= &amp;quot;/&amp;quot; &lt;br /&gt;
    new_loc+= params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_loc)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:After Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_location = @participant.get_path + &amp;quot;/&amp;quot; + params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_location)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Modified code reduces the method length and increases Readability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Unnecessary variable declarations which are never used in the method increase the length of code and confuse the readers. For example, the variable ‘display’ shown in the code snippet below was defined but never used in the method. Such variables can be removed.&lt;br /&gt;
&lt;br /&gt;
[[File:Display variable.png|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
The actual variable that was used which is ‘disp’ can now be renamed to ‘display’ which is more meaningful.&lt;br /&gt;
&lt;br /&gt;
[[File:Display change.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Many more minor code changes are made following the Ruby Coding guidelines. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if !File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We should prefer to use 'unless' for such conditions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== List of Code Smells identified and removed ==&lt;br /&gt;
*'''Duplicated code''': Identical or very similar code exists in more than one location. Code duplication frequently creates long, repeated sections of code that differ in only a few lines or characters. The length of such routines can make it difficult to quickly understand them. The repetition of largely identical code sections can conceal how they differ from one another, and therefore, what the specific purpose of each code section is. Often, the only difference is in a parameter value. The best practice in such cases is a reusable subroutine.&lt;br /&gt;
*'''Long method''': A method, function, or procedure that has grown too large. Since the early days of programming people have realized that the longer a procedure is, the more difficult it is to understand. Older languages carried an overhead in subroutine calls, which deterred people from small methods. Modern OO languages have pretty much eliminated that overhead for in-process calls.&lt;br /&gt;
*'''Too many parameters''': A long list of parameters in a procedure or function make readability and code quality worse. The more parameters a method has, the more complex it is. Limit the number of parameters you need in a given method, or use an object to combine the parameters.&lt;br /&gt;
*'''Excessively long identifiers''': In particular, the use of naming conventions to provide disambiguation that should be implicit in the software architecture.&lt;br /&gt;
*'''Excessively short identifiers''': The name of a variable should reflect its function unless the function is obvious. Pick a set of standard terminology and stick to it throughout your methods. For example, if you have Open(), you should probably have Close().&lt;br /&gt;
*'''Excessive use of literals''': These should be coded as named constants, to improve readability and to avoid programming errors. Additionally, literals can and should be externalized into resource files/scripts where possible, to facilitate localization of software if it is intended to be deployed in different regions.&lt;br /&gt;
*'''Complex conditionals''': Branches that check lots of unrelated conditions and edge cases that don't seem to capture the meaning of a block of code. Large conditional logic blocks, particularly blocks that tend to grow larger or change significantly over time have to be eliminated. Consider alternative object-oriented approaches such as decorator, strategy, or state.&lt;br /&gt;
*'''Dead Code''': Ruthlessly delete code that isn't being used. Unused code will result in Long classes or long methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
*Polymorphism can be implemented by pushing some methods to relevant classes. Following section of code contains certain method calls which are implemented in the same class. These can be abstracted into a different class for file operations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def file_folder_action&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
    if params[:faction][:delete]        #call method to delete selected files&lt;br /&gt;
      delete_selected_files&lt;br /&gt;
    elsif params[:faction][:rename]     #call method to rename selected files&lt;br /&gt;
      rename_selected_file&lt;br /&gt;
    elsif params[:faction][:move]       #call method to move selected files&lt;br /&gt;
      move_selected_file&lt;br /&gt;
    elsif params[:faction][:copy]       #call method to copy selected files&lt;br /&gt;
      copy_selected_file&lt;br /&gt;
    elsif params[:faction][:create]     #call method to create new folder&lt;br /&gt;
      create_new_folder&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; @participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Submit file functionality currently breaks with the latest expertiza master branch code which can be fixed by further exploration.&lt;br /&gt;
*Design changes made in partial files can be extended such that code reuse can be maximized.&lt;br /&gt;
*Many more code smells can be identified and removed to improve elegance.&lt;br /&gt;
*RSpec test cases for the changes made can be added.&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;br /&gt;
&lt;br /&gt;
Steps to setup the project:&lt;br /&gt;
*Extract source code in RubyMine using the following url: https://github.com/hsure/expertiza&lt;br /&gt;
:(A) VCS -&amp;gt; Checkout from version control -&amp;gt; Github&lt;br /&gt;
:(B) Give this url:  https://github.com/hsure/expertiza -&amp;gt; Finish&lt;br /&gt;
*Confirm the sdk for RubyMine to ruby1.9.3 using File -&amp;gt; Settings &lt;br /&gt;
*Run bundle install &lt;br /&gt;
*Run - rake db:create:all&lt;br /&gt;
*Run - rake db:migrate&lt;br /&gt;
*From the Expertiza folder (project root) execute the command “rails server”&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
You should be able to view the test Database in MySQL.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Use the following credentials to explore the application:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Username: admin (Administrator)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: admin&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Username: sample (User)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: sample&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
*http://github.com/styleguide/ruby&lt;br /&gt;
*http://www.ruby-doc.org/docs/ProgrammingRuby/&lt;br /&gt;
*http://www.ruby-lang.org/en/documentation/&lt;br /&gt;
*http://en.wikibooks.org/wiki/Ruby_programming_language&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81392</id>
		<title>CSC/ECE 517 Fall 2013/oss E811 syn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81392"/>
		<updated>2013-10-30T21:58:21Z</updated>

		<summary type="html">&lt;p&gt;Hsure: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Refactor &amp;amp; test submitted_content_controller &amp;amp; submitted_content_helper''' ==&lt;br /&gt;
Expertiza is a web application that is used for Project/Assignment submissions. Students can form teams, select topics, submit assignments, review other's work and give feedback for reviews. Submission of work can be done in two ways:&lt;br /&gt;
*Hyperlinks&lt;br /&gt;
*Files/Folders&lt;br /&gt;
&lt;br /&gt;
Submitted_Content_Controller and Submitted_Content_Helper are designed to handle operations on hyperlinks and files. They include methods to submit, verify and remove hyperlinks; and create, move, rename, submit and remove files.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Project Description''' ==&lt;br /&gt;
&lt;br /&gt;
Classes: submitted_content_controller.rb (250 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
	 submitted_content_helper.rb (98 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
What they do: Allow users to submit files and links to Expertiza. &amp;lt;br&amp;gt;&lt;br /&gt;
What needs to be done:  The methods are long and complex, and contain a lot of duplicated code.  In particular, there are two different ways of submitting files: as a homework submission, and uploading a review file requested by a review rubric.  These should use common code as much as possible.  The approach to deleting submitted links and submitted files should be analogous.  There have been bugs in deleting hyperlinks, so be sure to test the code thoroughly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== '''Design Changes''' ==&lt;br /&gt;
&lt;br /&gt;
*Code duplication is generally considered a mark of poor or lazy programming style. Good coding style is generally associated with code reuse.&lt;br /&gt;
&lt;br /&gt;
:We identified a sequence of operations that are repetitively used in methods which are used to submit files for the first time and again in review. The code to check if a file exists or not and creating a new directory path for any new submission is common for both submission and review pages. Thus we separated it out as a different method ‘check_file_exists’ as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def check_file_exists  curr_directory&lt;br /&gt;
    #check if file exists. If not, create a new file. Else, remove the existing file and then create new file.&lt;br /&gt;
    if !File.exists? curr_directory&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    else&lt;br /&gt;
      FileUtils.rm_rf(curr_directory)&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method can now be called from both submit_file and custom_submit_file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def submit_file&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    file = params[:uploaded_file]&lt;br /&gt;
    participant.set_student_directory_num&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    curr_directory = participant.get_path.to_s+@current_folder.name&lt;br /&gt;
    check_file_exists(curr_directory)&lt;br /&gt;
&lt;br /&gt;
    safe_filename = file.original_filename.gsub(/\\/,&amp;quot;/&amp;quot;)&lt;br /&gt;
    safe_filename = FileHelper::sanitize_filename(safe_filename) # new code to sanitize file path before upload*&lt;br /&gt;
    full_filename =  curr_directory + File.split(safe_filename).last.gsub(&amp;quot; &amp;quot;,'_') #safe_filename #curr_directory +&lt;br /&gt;
    File.open(full_filename, &amp;quot;wb&amp;quot;) { |f| f.write(file.read) }&lt;br /&gt;
&lt;br /&gt;
    if params['unzip']&lt;br /&gt;
      SubmittedContentHelper::unzip_file(full_filename, curr_directory, true) if get_file_type(safe_filename) == &amp;quot;zip&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    participant.update_resubmit_times&lt;br /&gt;
&lt;br /&gt;
    #send message to reviewers when submission has been updated&lt;br /&gt;
    participant.assignment.email(participant.id) rescue nil # If the user has no team: 1) there are no reviewers to notify; 2) calling email will throw an exception. So rescue and ignore it.&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Long methods are often caused due to unnecessary parameters. Long list of parameters reduce readability of code. Following changes are made to remove such unnecessary parameters:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file.JPG|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
Here the variable safename is declared and used only once. The assignment can be made inline by replacing it in the method call with actual logic in the below way:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file mod.JPG|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Below is another name change from ‘fpath’ to ‘file_path’ which is made to eliminate the most common code smell ‘Excessively short identifiers’.&lt;br /&gt;
 &lt;br /&gt;
[[File:Fpath.png|frame|none|alt=Before Refactoring|Before Refactoring]] [[File:Filepath.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Consecutive assignments or modifications of the same variable can lead to “Long method” or “Excessive use of literals”. Such code smells are removed. For example, variable ‘newloc’ which is modified consecutively is refactored in the following way:&lt;br /&gt;
 &lt;br /&gt;
:Before Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_loc = @participant.get_path &lt;br /&gt;
    new_loc+= &amp;quot;/&amp;quot; &lt;br /&gt;
    new_loc+= params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_loc)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:After Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_location = @participant.get_path + &amp;quot;/&amp;quot; + params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_location)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Modified code reduces the method length and increases Readability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Unnecessary variable declarations which are never used in the method increase the length of code and confuse the readers. For example, the variable ‘display’ shown in the code snippet below was defined but never used in the method. Such variables can be removed.&lt;br /&gt;
&lt;br /&gt;
[[File:Display variable.png|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
The actual variable that was used which is ‘disp’ can now be renamed to ‘display’ which is more meaningful.&lt;br /&gt;
&lt;br /&gt;
[[File:Display change.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Many more minor code changes are made following the Ruby Coding guidelines. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if !File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We should prefer to use 'unless' for such conditions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== List of Code Smells identified and removed ==&lt;br /&gt;
*'''Duplicated code''': Identical or very similar code exists in more than one location. Code duplication frequently creates long, repeated sections of code that differ in only a few lines or characters. The length of such routines can make it difficult to quickly understand them. The repetition of largely identical code sections can conceal how they differ from one another, and therefore, what the specific purpose of each code section is. Often, the only difference is in a parameter value. The best practice in such cases is a reusable subroutine.&lt;br /&gt;
*'''Long method''': A method, function, or procedure that has grown too large. Since the early days of programming people have realized that the longer a procedure is, the more difficult it is to understand. Older languages carried an overhead in subroutine calls, which deterred people from small methods. Modern OO languages have pretty much eliminated that overhead for in-process calls.&lt;br /&gt;
*'''Too many parameters''': A long list of parameters in a procedure or function make readability and code quality worse. The more parameters a method has, the more complex it is. Limit the number of parameters you need in a given method, or use an object to combine the parameters.&lt;br /&gt;
*'''Excessively long identifiers''': In particular, the use of naming conventions to provide disambiguation that should be implicit in the software architecture.&lt;br /&gt;
*'''Excessively short identifiers''': The name of a variable should reflect its function unless the function is obvious. Pick a set of standard terminology and stick to it throughout your methods. For example, if you have Open(), you should probably have Close().&lt;br /&gt;
*'''Excessive use of literals''': These should be coded as named constants, to improve readability and to avoid programming errors. Additionally, literals can and should be externalized into resource files/scripts where possible, to facilitate localization of software if it is intended to be deployed in different regions.&lt;br /&gt;
*'''Complex conditionals''': Branches that check lots of unrelated conditions and edge cases that don't seem to capture the meaning of a block of code. Large conditional logic blocks, particularly blocks that tend to grow larger or change significantly over time have to be eliminated. Consider alternative object-oriented approaches such as decorator, strategy, or state.&lt;br /&gt;
*'''Dead Code''': Ruthlessly delete code that isn't being used. Unused code will result in Long classes or long methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
*Polymorphism can be implemented by pushing some methods to relevant classes. Following section of code contains certain method calls which are implemented in the same class. These can be abstracted into a different class for file operations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def file_folder_action&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
    if params[:faction][:delete]        #call method to delete selected files&lt;br /&gt;
      delete_selected_files&lt;br /&gt;
    elsif params[:faction][:rename]     #call method to rename selected files&lt;br /&gt;
      rename_selected_file&lt;br /&gt;
    elsif params[:faction][:move]       #call method to move selected files&lt;br /&gt;
      move_selected_file&lt;br /&gt;
    elsif params[:faction][:copy]       #call method to copy selected files&lt;br /&gt;
      copy_selected_file&lt;br /&gt;
    elsif params[:faction][:create]     #call method to create new folder&lt;br /&gt;
      create_new_folder&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; @participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Submit file functionality currently breaks with the latest expertiza master branch code which can be fixed by further exploration.&lt;br /&gt;
*Design changes made in partial files can be extended such that code reuse can be maximized.&lt;br /&gt;
*Many more code smells can be identified and removed to improve elegance.&lt;br /&gt;
*RSpec test cases for the changes made can be added.&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;br /&gt;
&lt;br /&gt;
Steps to setup the project:&lt;br /&gt;
*Extract source code in RubyMine using the following url: https://github.com/hsure/expertiza&lt;br /&gt;
:(A) VCS -&amp;gt; Checkout from version control -&amp;gt; Github&lt;br /&gt;
:(B) Give this url:  https://github.com/hsure/expertiza -&amp;gt; Finish&lt;br /&gt;
*Confirm the sdk for RubyMine to ruby1.9.3 using File -&amp;gt; Settings &lt;br /&gt;
*Run bundle install &lt;br /&gt;
*Run - rake db:create:all&lt;br /&gt;
*Run - rake db:migrate&lt;br /&gt;
*From the Expertiza folder (project root) execute the command “rails server”&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
You should be able to view the test Database in MySQL.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Use the following credentials to explore the application:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Username: admin (Administrator)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: admin&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Username: sample (User)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: sample&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
*http://github.com/styleguide/ruby&lt;br /&gt;
*http://www.ruby-doc.org/docs/ProgrammingRuby/&lt;br /&gt;
*http://www.ruby-lang.org/en/documentation/&lt;br /&gt;
*http://en.wikibooks.org/wiki/Ruby_programming_language&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81388</id>
		<title>CSC/ECE 517 Fall 2013/oss E811 syn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81388"/>
		<updated>2013-10-30T21:54:32Z</updated>

		<summary type="html">&lt;p&gt;Hsure: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Refactor &amp;amp; test submitted_content_controller &amp;amp; submitted_content_helper''' ==&lt;br /&gt;
Expertiza is a web application that is used for Project/Assignment submissions. Students can form teams, select topics, submit assignments, review other's work and give feedback for reviews. Submission of work can be done in two ways:&lt;br /&gt;
*Hyperlinks&lt;br /&gt;
*Files/Folders&lt;br /&gt;
&lt;br /&gt;
Submitted_Content_Controller and Submitted_Content_Helper are designed to handle operations on hyperlinks and files. They include methods to submit, verify and remove hyperlinks; and create, move, rename, submit and remove files.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Project Description''' ==&lt;br /&gt;
&lt;br /&gt;
Classes: submitted_content_controller.rb (250 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
	 submitted_content_helper.rb (98 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
What they do: Allow users to submit files and links to Expertiza. &amp;lt;br&amp;gt;&lt;br /&gt;
What needs to be done:  The methods are long and complex, and contain a lot of duplicated code.  In particular, there are two different ways of submitting files: as a homework submission, and uploading a review file requested by a review rubric.  These should use common code as much as possible.  The approach to deleting submitted links and submitted files should be analogous.  There have been bugs in deleting hyperlinks, so be sure to test the code thoroughly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== '''Design Changes''' ==&lt;br /&gt;
&lt;br /&gt;
*Code duplication is generally considered a mark of poor or lazy programming style. Good coding style is generally associated with code reuse.&lt;br /&gt;
&lt;br /&gt;
:We identified a sequence of operations that are repetitively used in methods which are used to submit files for the first time and again in review. The code to check if a file exists or not and creating a new directory path for any new submission is common for both submission and review pages. Thus we separated it out as a different method ‘check_file_exists’ as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def check_file_exists  curr_directory&lt;br /&gt;
    #check if file exists. If not, create a new file. Else, remove the existing file and then create new file.&lt;br /&gt;
    if !File.exists? curr_directory&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    else&lt;br /&gt;
      FileUtils.rm_rf(curr_directory)&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method can now be called from both submit_file and custom_submit_file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def submit_file&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    file = params[:uploaded_file]&lt;br /&gt;
    participant.set_student_directory_num&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    curr_directory = participant.get_path.to_s+@current_folder.name&lt;br /&gt;
    check_file_exists(curr_directory)&lt;br /&gt;
&lt;br /&gt;
    safe_filename = file.original_filename.gsub(/\\/,&amp;quot;/&amp;quot;)&lt;br /&gt;
    safe_filename = FileHelper::sanitize_filename(safe_filename) # new code to sanitize file path before upload*&lt;br /&gt;
    full_filename =  curr_directory + File.split(safe_filename).last.gsub(&amp;quot; &amp;quot;,'_') #safe_filename #curr_directory +&lt;br /&gt;
    File.open(full_filename, &amp;quot;wb&amp;quot;) { |f| f.write(file.read) }&lt;br /&gt;
&lt;br /&gt;
    if params['unzip']&lt;br /&gt;
      SubmittedContentHelper::unzip_file(full_filename, curr_directory, true) if get_file_type(safe_filename) == &amp;quot;zip&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    participant.update_resubmit_times&lt;br /&gt;
&lt;br /&gt;
    #send message to reviewers when submission has been updated&lt;br /&gt;
    participant.assignment.email(participant.id) rescue nil # If the user has no team: 1) there are no reviewers to notify; 2) calling email will throw an exception. So rescue and ignore it.&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Long methods are often caused due to unnecessary parameters. Long list of parameters reduce readability of code. Following changes are made to remove such unnecessary parameters:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file.JPG|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
Here the variable safename is declared and used only once. The assignment can be made inline by replacing it in the method call with actual logic in the below way:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file mod.JPG|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Below is another name change from ‘fpath’ to ‘file_path’ which is made to eliminate the most common code smell ‘Excessively short identifiers’.&lt;br /&gt;
 &lt;br /&gt;
[[File:Fpath.png|frame|none|alt=Before Refactoring|Before Refactoring]] [[File:Filepath.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Consecutive assignments or modifications of the same variable can lead to “Long method” or “Excessive use of literals”. Such code smells are removed. For example, variable ‘newloc’ which is modified consecutively is refactored in the following way:&lt;br /&gt;
 &lt;br /&gt;
:Before Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_loc = @participant.get_path &lt;br /&gt;
    new_loc+= &amp;quot;/&amp;quot; &lt;br /&gt;
    new_loc+= params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_loc)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:After Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_location = @participant.get_path + &amp;quot;/&amp;quot; + params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_location)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Modified code reduces the method length and increases Readability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Unnecessary variable declarations which are never used in the method increase the length of code and confuse the readers. For example, the variable ‘display’ shown in the code snippet below was defined but never used in the method. Such variables can be removed.&lt;br /&gt;
&lt;br /&gt;
[[File:Display variable.png|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
The actual variable that was used which is ‘disp’ can now be renamed to ‘display’ which is more meaningful.&lt;br /&gt;
&lt;br /&gt;
[[File:Display change.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Many more minor code changes are made following the Ruby Coding guidelines. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if !File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We should prefer to use 'unless' for such conditions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== List of Code Smells identified and removed ==&lt;br /&gt;
*'''Duplicated code''': Identical or very similar code exists in more than one location. Code duplication frequently creates long, repeated sections of code that differ in only a few lines or characters. The length of such routines can make it difficult to quickly understand them. The repetition of largely identical code sections can conceal how they differ from one another, and therefore, what the specific purpose of each code section is. Often, the only difference is in a parameter value. The best practice in such cases is a reusable subroutine.&lt;br /&gt;
*'''Long method''': A method, function, or procedure that has grown too large. Since the early days of programming people have realized that the longer a procedure is, the more difficult it is to understand. Older languages carried an overhead in subroutine calls, which deterred people from small methods. Modern OO languages have pretty much eliminated that overhead for in-process calls.&lt;br /&gt;
*'''Too many parameters''': A long list of parameters in a procedure or function make readability and code quality worse. The more parameters a method has, the more complex it is. Limit the number of parameters you need in a given method, or use an object to combine the parameters.&lt;br /&gt;
*'''Excessively long identifiers''': In particular, the use of naming conventions to provide disambiguation that should be implicit in the software architecture.&lt;br /&gt;
*'''Excessively short identifiers''': The name of a variable should reflect its function unless the function is obvious. Pick a set of standard terminology and stick to it throughout your methods. For example, if you have Open(), you should probably have Close().&lt;br /&gt;
*'''Excessive use of literals''': These should be coded as named constants, to improve readability and to avoid programming errors. Additionally, literals can and should be externalized into resource files/scripts where possible, to facilitate localization of software if it is intended to be deployed in different regions.&lt;br /&gt;
*'''Complex conditionals''': Branches that check lots of unrelated conditions and edge cases that don't seem to capture the meaning of a block of code. Large conditional logic blocks, particularly blocks that tend to grow larger or change significantly over time have to be eliminated. Consider alternative object-oriented approaches such as decorator, strategy, or state.&lt;br /&gt;
*'''Dead Code''': Ruthlessly delete code that isn't being used. Unused code will result in Long classes or long methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
*Polymorphism can be implemented by pushing some methods to relevant classes. Following section of code contains certain method calls which are implemented in the same class. These can be abstracted into a different class for file operations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def file_folder_action&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
    if params[:faction][:delete]        #call method to delete selected files&lt;br /&gt;
      delete_selected_files&lt;br /&gt;
    elsif params[:faction][:rename]     #call method to rename selected files&lt;br /&gt;
      rename_selected_file&lt;br /&gt;
    elsif params[:faction][:move]       #call method to move selected files&lt;br /&gt;
      move_selected_file&lt;br /&gt;
    elsif params[:faction][:copy]       #call method to copy selected files&lt;br /&gt;
      copy_selected_file&lt;br /&gt;
    elsif params[:faction][:create]     #call method to create new folder&lt;br /&gt;
      create_new_folder&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; @participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Submit file functionality currently breaks with the latest expertiza master branch code which can be fixed by further exploration.&lt;br /&gt;
*Design changes made in partial files can be extended such that code reuse can be maximized.&lt;br /&gt;
*Many more code smells can be identified and removed to improve elegance.&lt;br /&gt;
*RSpec test cases for the changes made can be added.&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;br /&gt;
&lt;br /&gt;
Steps to setup the project:&lt;br /&gt;
*Extract source code in RubyMine using the following url: https://github.com/hsure/expertiza&lt;br /&gt;
:(A) VCS -&amp;gt; Checkout from version control -&amp;gt; Github&lt;br /&gt;
:(B) Give this url:  https://github.com/hsure/expertiza -&amp;gt; Finish&lt;br /&gt;
*Confirm the sdk for RubyMine to ruby1.9.3 using File -&amp;gt; Settings &lt;br /&gt;
*Run bundle install &lt;br /&gt;
*Run - rake db:create:all&lt;br /&gt;
*Run - rake db:migrate&lt;br /&gt;
*From the Expertiza folder (project root) execute the command “rails server”&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
You should be able to view the test Database in MySQL.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Use the following credentials to explore the application:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Username: admin (Administrator)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: admin&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Username: sample (User)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: sample&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81387</id>
		<title>CSC/ECE 517 Fall 2013/oss E811 syn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81387"/>
		<updated>2013-10-30T21:53:58Z</updated>

		<summary type="html">&lt;p&gt;Hsure: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Refactor &amp;amp; test submitted_content_controller &amp;amp; submitted_content_helper ==&lt;br /&gt;
Expertiza is a web application that is used for Project/Assignment submissions. Students can form teams, select topics, submit assignments, review other's work and give feedback for reviews. Submission of work can be done in two ways:&lt;br /&gt;
*Hyperlinks&lt;br /&gt;
*Files/Folders&lt;br /&gt;
&lt;br /&gt;
Submitted_Content_Controller and Submitted_Content_Helper are designed to handle operations on hyperlinks and files. They include methods to submit, verify and remove hyperlinks; and create, move, rename, submit and remove files.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Project Description''' ==&lt;br /&gt;
&lt;br /&gt;
Classes: submitted_content_controller.rb (250 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
	 submitted_content_helper.rb (98 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
What they do: Allow users to submit files and links to Expertiza. &amp;lt;br&amp;gt;&lt;br /&gt;
What needs to be done:  The methods are long and complex, and contain a lot of duplicated code.  In particular, there are two different ways of submitting files: as a homework submission, and uploading a review file requested by a review rubric.  These should use common code as much as possible.  The approach to deleting submitted links and submitted files should be analogous.  There have been bugs in deleting hyperlinks, so be sure to test the code thoroughly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== '''Design Changes''' ==&lt;br /&gt;
&lt;br /&gt;
*Code duplication is generally considered a mark of poor or lazy programming style. Good coding style is generally associated with code reuse.&lt;br /&gt;
&lt;br /&gt;
:We identified a sequence of operations that are repetitively used in methods which are used to submit files for the first time and again in review. The code to check if a file exists or not and creating a new directory path for any new submission is common for both submission and review pages. Thus we separated it out as a different method ‘check_file_exists’ as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def check_file_exists  curr_directory&lt;br /&gt;
    #check if file exists. If not, create a new file. Else, remove the existing file and then create new file.&lt;br /&gt;
    if !File.exists? curr_directory&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    else&lt;br /&gt;
      FileUtils.rm_rf(curr_directory)&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method can now be called from both submit_file and custom_submit_file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def submit_file&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    file = params[:uploaded_file]&lt;br /&gt;
    participant.set_student_directory_num&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    curr_directory = participant.get_path.to_s+@current_folder.name&lt;br /&gt;
    check_file_exists(curr_directory)&lt;br /&gt;
&lt;br /&gt;
    safe_filename = file.original_filename.gsub(/\\/,&amp;quot;/&amp;quot;)&lt;br /&gt;
    safe_filename = FileHelper::sanitize_filename(safe_filename) # new code to sanitize file path before upload*&lt;br /&gt;
    full_filename =  curr_directory + File.split(safe_filename).last.gsub(&amp;quot; &amp;quot;,'_') #safe_filename #curr_directory +&lt;br /&gt;
    File.open(full_filename, &amp;quot;wb&amp;quot;) { |f| f.write(file.read) }&lt;br /&gt;
&lt;br /&gt;
    if params['unzip']&lt;br /&gt;
      SubmittedContentHelper::unzip_file(full_filename, curr_directory, true) if get_file_type(safe_filename) == &amp;quot;zip&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    participant.update_resubmit_times&lt;br /&gt;
&lt;br /&gt;
    #send message to reviewers when submission has been updated&lt;br /&gt;
    participant.assignment.email(participant.id) rescue nil # If the user has no team: 1) there are no reviewers to notify; 2) calling email will throw an exception. So rescue and ignore it.&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Long methods are often caused due to unnecessary parameters. Long list of parameters reduce readability of code. Following changes are made to remove such unnecessary parameters:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file.JPG|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
Here the variable safename is declared and used only once. The assignment can be made inline by replacing it in the method call with actual logic in the below way:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file mod.JPG|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Below is another name change from ‘fpath’ to ‘file_path’ which is made to eliminate the most common code smell ‘Excessively short identifiers’.&lt;br /&gt;
 &lt;br /&gt;
[[File:Fpath.png|frame|none|alt=Before Refactoring|Before Refactoring]] [[File:Filepath.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Consecutive assignments or modifications of the same variable can lead to “Long method” or “Excessive use of literals”. Such code smells are removed. For example, variable ‘newloc’ which is modified consecutively is refactored in the following way:&lt;br /&gt;
 &lt;br /&gt;
:Before Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_loc = @participant.get_path &lt;br /&gt;
    new_loc+= &amp;quot;/&amp;quot; &lt;br /&gt;
    new_loc+= params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_loc)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:After Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_location = @participant.get_path + &amp;quot;/&amp;quot; + params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_location)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Modified code reduces the method length and increases Readability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Unnecessary variable declarations which are never used in the method increase the length of code and confuse the readers. For example, the variable ‘display’ shown in the code snippet below was defined but never used in the method. Such variables can be removed.&lt;br /&gt;
&lt;br /&gt;
[[File:Display variable.png|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
The actual variable that was used which is ‘disp’ can now be renamed to ‘display’ which is more meaningful.&lt;br /&gt;
&lt;br /&gt;
[[File:Display change.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Many more minor code changes are made following the Ruby Coding guidelines. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if !File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We should prefer to use 'unless' for such conditions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== List of Code Smells identified and removed ==&lt;br /&gt;
*'''Duplicated code''': Identical or very similar code exists in more than one location. Code duplication frequently creates long, repeated sections of code that differ in only a few lines or characters. The length of such routines can make it difficult to quickly understand them. The repetition of largely identical code sections can conceal how they differ from one another, and therefore, what the specific purpose of each code section is. Often, the only difference is in a parameter value. The best practice in such cases is a reusable subroutine.&lt;br /&gt;
*'''Long method''': A method, function, or procedure that has grown too large. Since the early days of programming people have realized that the longer a procedure is, the more difficult it is to understand. Older languages carried an overhead in subroutine calls, which deterred people from small methods. Modern OO languages have pretty much eliminated that overhead for in-process calls.&lt;br /&gt;
*'''Too many parameters''': A long list of parameters in a procedure or function make readability and code quality worse. The more parameters a method has, the more complex it is. Limit the number of parameters you need in a given method, or use an object to combine the parameters.&lt;br /&gt;
*'''Excessively long identifiers''': In particular, the use of naming conventions to provide disambiguation that should be implicit in the software architecture.&lt;br /&gt;
*'''Excessively short identifiers''': The name of a variable should reflect its function unless the function is obvious. Pick a set of standard terminology and stick to it throughout your methods. For example, if you have Open(), you should probably have Close().&lt;br /&gt;
*'''Excessive use of literals''': These should be coded as named constants, to improve readability and to avoid programming errors. Additionally, literals can and should be externalized into resource files/scripts where possible, to facilitate localization of software if it is intended to be deployed in different regions.&lt;br /&gt;
*'''Complex conditionals''': Branches that check lots of unrelated conditions and edge cases that don't seem to capture the meaning of a block of code. Large conditional logic blocks, particularly blocks that tend to grow larger or change significantly over time have to be eliminated. Consider alternative object-oriented approaches such as decorator, strategy, or state.&lt;br /&gt;
*'''Dead Code''': Ruthlessly delete code that isn't being used. Unused code will result in Long classes or long methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
*Polymorphism can be implemented by pushing some methods to relevant classes. Following section of code contains certain method calls which are implemented in the same class. These can be abstracted into a different class for file operations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def file_folder_action&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
    if params[:faction][:delete]        #call method to delete selected files&lt;br /&gt;
      delete_selected_files&lt;br /&gt;
    elsif params[:faction][:rename]     #call method to rename selected files&lt;br /&gt;
      rename_selected_file&lt;br /&gt;
    elsif params[:faction][:move]       #call method to move selected files&lt;br /&gt;
      move_selected_file&lt;br /&gt;
    elsif params[:faction][:copy]       #call method to copy selected files&lt;br /&gt;
      copy_selected_file&lt;br /&gt;
    elsif params[:faction][:create]     #call method to create new folder&lt;br /&gt;
      create_new_folder&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; @participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Submit file functionality currently breaks with the latest expertiza master branch code which can be fixed by further exploration.&lt;br /&gt;
*Design changes made in partial files can be extended such that code reuse can be maximized.&lt;br /&gt;
*Many more code smells can be identified and removed to improve elegance.&lt;br /&gt;
*RSpec test cases for the changes made can be added.&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;br /&gt;
&lt;br /&gt;
Steps to setup the project:&lt;br /&gt;
*Extract source code in RubyMine using the following url: https://github.com/hsure/expertiza&lt;br /&gt;
:(A) VCS -&amp;gt; Checkout from version control -&amp;gt; Github&lt;br /&gt;
:(B) Give this url:  https://github.com/hsure/expertiza -&amp;gt; Finish&lt;br /&gt;
*Confirm the sdk for RubyMine to ruby1.9.3 using File -&amp;gt; Settings &lt;br /&gt;
*Run bundle install &lt;br /&gt;
*Run - rake db:create:all&lt;br /&gt;
*Run - rake db:migrate&lt;br /&gt;
*From the Expertiza folder (project root) execute the command “rails server”&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
You should be able to view the test Database in MySQL.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Use the following credentials to explore the application:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Username: admin (Administrator)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: admin&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Username: sample (User)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: sample&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81386</id>
		<title>CSC/ECE 517 Fall 2013/oss E811 syn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81386"/>
		<updated>2013-10-30T21:53:19Z</updated>

		<summary type="html">&lt;p&gt;Hsure: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Refactor &amp;amp; test submitted_content_controller &amp;amp; submitted_content_helper ==&lt;br /&gt;
Expertiza is a web application that is used for Project/Assignment submissions. Students can form teams, select topics, submit assignments, review other's work and give feedback for reviews. Submission of work can be done in two ways:&lt;br /&gt;
*Hyperlinks&lt;br /&gt;
*Files/Folders&lt;br /&gt;
&lt;br /&gt;
Submitted_Content_Controller and Submitted_Content_Helper are designed to handle operations on hyperlinks and files. They include methods to submit, verify and remove hyperlinks; and create, move, rename, submit and remove files.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Project Description''' ==&lt;br /&gt;
&lt;br /&gt;
Classes: submitted_content_controller.rb (250 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
	 submitted_content_helper.rb (98 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
What they do: Allow users to submit files and links to Expertiza. &amp;lt;br&amp;gt;&lt;br /&gt;
What needs to be done:  The methods are long and complex, and contain a lot of duplicated code.  In particular, there are two different ways of submitting files: as a homework submission, and uploading a review file requested by a review rubric.  These should use common code as much as possible.  The approach to deleting submitted links and submitted files should be analogous.  There have been bugs in deleting hyperlinks, so be sure to test the code thoroughly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== '''Design Changes''' ==&lt;br /&gt;
&lt;br /&gt;
*Code duplication is generally considered a mark of poor or lazy programming style. Good coding style is generally associated with code reuse.&lt;br /&gt;
&lt;br /&gt;
:We identified a sequence of operations that are repetitively used in methods which are used to submit files for the first time and again in review. The code to check if a file exists or not and creating a new directory path for any new submission is common for both submission and review pages. Thus we separated it out as a different method ‘check_file_exists’ as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def check_file_exists  curr_directory&lt;br /&gt;
    #check if file exists. If not, create a new file. Else, remove the existing file and then create new file.&lt;br /&gt;
    if !File.exists? curr_directory&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    else&lt;br /&gt;
      FileUtils.rm_rf(curr_directory)&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method can now be called from both submit_file and custom_submit_file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def submit_file&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    file = params[:uploaded_file]&lt;br /&gt;
    participant.set_student_directory_num&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    curr_directory = participant.get_path.to_s+@current_folder.name&lt;br /&gt;
    check_file_exists(curr_directory)&lt;br /&gt;
&lt;br /&gt;
    safe_filename = file.original_filename.gsub(/\\/,&amp;quot;/&amp;quot;)&lt;br /&gt;
    safe_filename = FileHelper::sanitize_filename(safe_filename) # new code to sanitize file path before upload*&lt;br /&gt;
    full_filename =  curr_directory + File.split(safe_filename).last.gsub(&amp;quot; &amp;quot;,'_') #safe_filename #curr_directory +&lt;br /&gt;
    File.open(full_filename, &amp;quot;wb&amp;quot;) { |f| f.write(file.read) }&lt;br /&gt;
&lt;br /&gt;
    if params['unzip']&lt;br /&gt;
      SubmittedContentHelper::unzip_file(full_filename, curr_directory, true) if get_file_type(safe_filename) == &amp;quot;zip&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    participant.update_resubmit_times&lt;br /&gt;
&lt;br /&gt;
    #send message to reviewers when submission has been updated&lt;br /&gt;
    participant.assignment.email(participant.id) rescue nil # If the user has no team: 1) there are no reviewers to notify; 2) calling email will throw an exception. So rescue and ignore it.&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Long methods are often caused due to unnecessary parameters. Long list of parameters reduce readability of code. Following changes are made to remove such unnecessary parameters:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file.JPG|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
Here the variable safename is declared and used only once. The assignment can be made inline by replacing it in the method call with actual logic in the below way:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file mod.JPG|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Below is another name change from ‘fpath’ to ‘file_path’ which is made to eliminate the most common code smell ‘Excessively short identifiers’.&lt;br /&gt;
 &lt;br /&gt;
[[File:Fpath.png|frame|none|alt=Before Refactoring|Before Refactoring]] [[File:Filepath.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Consecutive assignments or modifications of the same variable can lead to “Long method” or “Excessive use of literals”. Such code smells are removed. For example, variable ‘newloc’ which is modified consecutively is refactored in the following way:&lt;br /&gt;
 &lt;br /&gt;
:Before Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_loc = @participant.get_path &lt;br /&gt;
    new_loc+= &amp;quot;/&amp;quot; &lt;br /&gt;
    new_loc+= params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_loc)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:After Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_location = @participant.get_path + &amp;quot;/&amp;quot; + params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_location)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Modified code reduces the method length and increases Readability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Unnecessary variable declarations which are never used in the method increase the length of code and confuse the readers. For example, the variable ‘display’ shown in the code snippet below was defined but never used in the method. Such variables can be removed.&lt;br /&gt;
&lt;br /&gt;
[[File:Display variable.png|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
The actual variable that was used which is ‘disp’ can now be renamed to ‘display’ which is more meaningful.&lt;br /&gt;
&lt;br /&gt;
[[File:Display change.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Many more minor code changes are made following the Ruby Coding guidelines. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if !File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We should prefer to use 'unless' for such conditions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== List of Code Smells identified and removed ==&lt;br /&gt;
*'''Duplicated code''': Identical or very similar code exists in more than one location. Code duplication frequently creates long, repeated sections of code that differ in only a few lines or characters. The length of such routines can make it difficult to quickly understand them. The repetition of largely identical code sections can conceal how they differ from one another, and therefore, what the specific purpose of each code section is. Often, the only difference is in a parameter value. The best practice in such cases is a reusable subroutine.&lt;br /&gt;
*'''Long method''': A method, function, or procedure that has grown too large. Since the early days of programming people have realized that the longer a procedure is, the more difficult it is to understand. Older languages carried an overhead in subroutine calls, which deterred people from small methods. Modern OO languages have pretty much eliminated that overhead for in-process calls.&lt;br /&gt;
*'''Too many parameters''': A long list of parameters in a procedure or function make readability and code quality worse. The more parameters a method has, the more complex it is. Limit the number of parameters you need in a given method, or use an object to combine the parameters.&lt;br /&gt;
*'''Excessively long identifiers''': In particular, the use of naming conventions to provide disambiguation that should be implicit in the software architecture.&lt;br /&gt;
*'''Excessively short identifiers''': The name of a variable should reflect its function unless the function is obvious. Pick a set of standard terminology and stick to it throughout your methods. For example, if you have Open(), you should probably have Close().&lt;br /&gt;
*'''Excessive use of literals''': These should be coded as named constants, to improve readability and to avoid programming errors. Additionally, literals can and should be externalized into resource files/scripts where possible, to facilitate localization of software if it is intended to be deployed in different regions.&lt;br /&gt;
*'''Complex conditionals''': Branches that check lots of unrelated conditions and edge cases that don't seem to capture the meaning of a block of code. Large conditional logic blocks, particularly blocks that tend to grow larger or change significantly over time have to be eliminated. Consider alternative object-oriented approaches such as decorator, strategy, or state.&lt;br /&gt;
*'''Dead Code''': Ruthlessly delete code that isn't being used. Unused code will result in Long classes or long methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
*Polymorphism can be implemented by pushing some methods to relevant classes. Following section of code contains certain method calls which are implemented in the same class. These can be abstracted into a different class for file operations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def file_folder_action&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
    if params[:faction][:delete]        #call method to delete selected files&lt;br /&gt;
      delete_selected_files&lt;br /&gt;
    elsif params[:faction][:rename]     #call method to rename selected files&lt;br /&gt;
      rename_selected_file&lt;br /&gt;
    elsif params[:faction][:move]       #call method to move selected files&lt;br /&gt;
      move_selected_file&lt;br /&gt;
    elsif params[:faction][:copy]       #call method to copy selected files&lt;br /&gt;
      copy_selected_file&lt;br /&gt;
    elsif params[:faction][:create]     #call method to create new folder&lt;br /&gt;
      create_new_folder&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; @participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Submit file functionality currently breaks with the latest expertiza master branch code which can be fixed by further exploration.&lt;br /&gt;
*Design changes made in partial files can be extended such that code reuse can be maximized.&lt;br /&gt;
*Many more code smells can be identified and removed to improve elegance.&lt;br /&gt;
*RSpec test cases for the changes made can be added.&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;br /&gt;
&lt;br /&gt;
Steps to setup the project:&lt;br /&gt;
*Extract source code in RubyMine using the following url: https://github.com/hsure/expertiza&lt;br /&gt;
:(A) VCS -&amp;gt; Checkout from version control -&amp;gt; Github&lt;br /&gt;
:(B) Give this url:  https://github.com/hsure/expertiza -&amp;gt; Finish&lt;br /&gt;
*Confirm the sdk for RubyMine to ruby1.9.3 using File -&amp;gt; Settings &lt;br /&gt;
*Run bundle install &lt;br /&gt;
*Run - rake db:create:all&lt;br /&gt;
*Run - rake db:migrate&lt;br /&gt;
*From the Expertiza folder (project root) execute the command “rails server”&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
You should be able to view the test Database in MySQL.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Use the following credentials to explore the application:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Username: admin (Administrator)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: admin&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Username: sample (User)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: sample&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81385</id>
		<title>CSC/ECE 517 Fall 2013/oss E811 syn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81385"/>
		<updated>2013-10-30T21:53:01Z</updated>

		<summary type="html">&lt;p&gt;Hsure: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Refactor &amp;amp; test submitted_content_controller &amp;amp; submitted_content_helper ==&lt;br /&gt;
Expertiza is a web application that is used for Project/Assignment submissions. Students can form teams, select topics, submit assignments, review other's work and give feedback for reviews. Submission of work can be done in two ways:&lt;br /&gt;
*Hyperlinks&lt;br /&gt;
*Files/Folders&lt;br /&gt;
&lt;br /&gt;
Submitted_Content_Controller and Submitted_Content_Helper are designed to handle operations on hyperlinks and files. They include methods to submit, verify and remove hyperlinks; and create, move, rename, submit and remove files.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
== '''Project Description''' ==&lt;br /&gt;
&lt;br /&gt;
Classes: submitted_content_controller.rb (250 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
	 submitted_content_helper.rb (98 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
What they do: Allow users to submit files and links to Expertiza. &amp;lt;br&amp;gt;&lt;br /&gt;
What needs to be done:  The methods are long and complex, and contain a lot of duplicated code.  In particular, there are two different ways of submitting files: as a homework submission, and uploading a review file requested by a review rubric.  These should use common code as much as possible.  The approach to deleting submitted links and submitted files should be analogous.  There have been bugs in deleting hyperlinks, so be sure to test the code thoroughly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== '''Design Changes''' ==&lt;br /&gt;
&lt;br /&gt;
*Code duplication is generally considered a mark of poor or lazy programming style. Good coding style is generally associated with code reuse.&lt;br /&gt;
&lt;br /&gt;
:We identified a sequence of operations that are repetitively used in methods which are used to submit files for the first time and again in review. The code to check if a file exists or not and creating a new directory path for any new submission is common for both submission and review pages. Thus we separated it out as a different method ‘check_file_exists’ as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def check_file_exists  curr_directory&lt;br /&gt;
    #check if file exists. If not, create a new file. Else, remove the existing file and then create new file.&lt;br /&gt;
    if !File.exists? curr_directory&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    else&lt;br /&gt;
      FileUtils.rm_rf(curr_directory)&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method can now be called from both submit_file and custom_submit_file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def submit_file&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    file = params[:uploaded_file]&lt;br /&gt;
    participant.set_student_directory_num&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    curr_directory = participant.get_path.to_s+@current_folder.name&lt;br /&gt;
    check_file_exists(curr_directory)&lt;br /&gt;
&lt;br /&gt;
    safe_filename = file.original_filename.gsub(/\\/,&amp;quot;/&amp;quot;)&lt;br /&gt;
    safe_filename = FileHelper::sanitize_filename(safe_filename) # new code to sanitize file path before upload*&lt;br /&gt;
    full_filename =  curr_directory + File.split(safe_filename).last.gsub(&amp;quot; &amp;quot;,'_') #safe_filename #curr_directory +&lt;br /&gt;
    File.open(full_filename, &amp;quot;wb&amp;quot;) { |f| f.write(file.read) }&lt;br /&gt;
&lt;br /&gt;
    if params['unzip']&lt;br /&gt;
      SubmittedContentHelper::unzip_file(full_filename, curr_directory, true) if get_file_type(safe_filename) == &amp;quot;zip&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    participant.update_resubmit_times&lt;br /&gt;
&lt;br /&gt;
    #send message to reviewers when submission has been updated&lt;br /&gt;
    participant.assignment.email(participant.id) rescue nil # If the user has no team: 1) there are no reviewers to notify; 2) calling email will throw an exception. So rescue and ignore it.&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Long methods are often caused due to unnecessary parameters. Long list of parameters reduce readability of code. Following changes are made to remove such unnecessary parameters:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file.JPG|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
Here the variable safename is declared and used only once. The assignment can be made inline by replacing it in the method call with actual logic in the below way:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file mod.JPG|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Below is another name change from ‘fpath’ to ‘file_path’ which is made to eliminate the most common code smell ‘Excessively short identifiers’.&lt;br /&gt;
 &lt;br /&gt;
[[File:Fpath.png|frame|none|alt=Before Refactoring|Before Refactoring]] [[File:Filepath.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Consecutive assignments or modifications of the same variable can lead to “Long method” or “Excessive use of literals”. Such code smells are removed. For example, variable ‘newloc’ which is modified consecutively is refactored in the following way:&lt;br /&gt;
 &lt;br /&gt;
:Before Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_loc = @participant.get_path &lt;br /&gt;
    new_loc+= &amp;quot;/&amp;quot; &lt;br /&gt;
    new_loc+= params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_loc)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:After Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_location = @participant.get_path + &amp;quot;/&amp;quot; + params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_location)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Modified code reduces the method length and increases Readability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Unnecessary variable declarations which are never used in the method increase the length of code and confuse the readers. For example, the variable ‘display’ shown in the code snippet below was defined but never used in the method. Such variables can be removed.&lt;br /&gt;
&lt;br /&gt;
[[File:Display variable.png|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
The actual variable that was used which is ‘disp’ can now be renamed to ‘display’ which is more meaningful.&lt;br /&gt;
&lt;br /&gt;
[[File:Display change.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Many more minor code changes are made following the Ruby Coding guidelines. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if !File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We should prefer to use 'unless' for such conditions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== List of Code Smells identified and removed ==&lt;br /&gt;
*'''Duplicated code''': Identical or very similar code exists in more than one location. Code duplication frequently creates long, repeated sections of code that differ in only a few lines or characters. The length of such routines can make it difficult to quickly understand them. The repetition of largely identical code sections can conceal how they differ from one another, and therefore, what the specific purpose of each code section is. Often, the only difference is in a parameter value. The best practice in such cases is a reusable subroutine.&lt;br /&gt;
*'''Long method''': A method, function, or procedure that has grown too large. Since the early days of programming people have realized that the longer a procedure is, the more difficult it is to understand. Older languages carried an overhead in subroutine calls, which deterred people from small methods. Modern OO languages have pretty much eliminated that overhead for in-process calls.&lt;br /&gt;
*'''Too many parameters''': A long list of parameters in a procedure or function make readability and code quality worse. The more parameters a method has, the more complex it is. Limit the number of parameters you need in a given method, or use an object to combine the parameters.&lt;br /&gt;
*'''Excessively long identifiers''': In particular, the use of naming conventions to provide disambiguation that should be implicit in the software architecture.&lt;br /&gt;
*'''Excessively short identifiers''': The name of a variable should reflect its function unless the function is obvious. Pick a set of standard terminology and stick to it throughout your methods. For example, if you have Open(), you should probably have Close().&lt;br /&gt;
*'''Excessive use of literals''': These should be coded as named constants, to improve readability and to avoid programming errors. Additionally, literals can and should be externalized into resource files/scripts where possible, to facilitate localization of software if it is intended to be deployed in different regions.&lt;br /&gt;
*'''Complex conditionals''': Branches that check lots of unrelated conditions and edge cases that don't seem to capture the meaning of a block of code. Large conditional logic blocks, particularly blocks that tend to grow larger or change significantly over time have to be eliminated. Consider alternative object-oriented approaches such as decorator, strategy, or state.&lt;br /&gt;
*'''Dead Code''': Ruthlessly delete code that isn't being used. Unused code will result in Long classes or long methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
*Polymorphism can be implemented by pushing some methods to relevant classes. Following section of code contains certain method calls which are implemented in the same class. These can be abstracted into a different class for file operations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def file_folder_action&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
    if params[:faction][:delete]        #call method to delete selected files&lt;br /&gt;
      delete_selected_files&lt;br /&gt;
    elsif params[:faction][:rename]     #call method to rename selected files&lt;br /&gt;
      rename_selected_file&lt;br /&gt;
    elsif params[:faction][:move]       #call method to move selected files&lt;br /&gt;
      move_selected_file&lt;br /&gt;
    elsif params[:faction][:copy]       #call method to copy selected files&lt;br /&gt;
      copy_selected_file&lt;br /&gt;
    elsif params[:faction][:create]     #call method to create new folder&lt;br /&gt;
      create_new_folder&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; @participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Submit file functionality currently breaks with the latest expertiza master branch code which can be fixed by further exploration.&lt;br /&gt;
*Design changes made in partial files can be extended such that code reuse can be maximized.&lt;br /&gt;
*Many more code smells can be identified and removed to improve elegance.&lt;br /&gt;
*RSpec test cases for the changes made can be added.&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;br /&gt;
&lt;br /&gt;
Steps to setup the project:&lt;br /&gt;
*Extract source code in RubyMine using the following url: https://github.com/hsure/expertiza&lt;br /&gt;
:(A) VCS -&amp;gt; Checkout from version control -&amp;gt; Github&lt;br /&gt;
:(B) Give this url:  https://github.com/hsure/expertiza -&amp;gt; Finish&lt;br /&gt;
*Confirm the sdk for RubyMine to ruby1.9.3 using File -&amp;gt; Settings &lt;br /&gt;
*Run bundle install &lt;br /&gt;
*Run - rake db:create:all&lt;br /&gt;
*Run - rake db:migrate&lt;br /&gt;
*From the Expertiza folder (project root) execute the command “rails server”&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
You should be able to view the test Database in MySQL.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Use the following credentials to explore the application:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Username: admin (Administrator)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: admin&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Username: sample (User)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: sample&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81381</id>
		<title>CSC/ECE 517 Fall 2013/oss E811 syn</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E811_syn&amp;diff=81381"/>
		<updated>2013-10-30T21:38:15Z</updated>

		<summary type="html">&lt;p&gt;Hsure: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Refactor &amp;amp; test submitted_content_controller &amp;amp; submitted_content_helper ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Introduction''' ==&lt;br /&gt;
&lt;br /&gt;
Expertiza is a web application that is used for Project/Assignment submissions. Students can form teams, select topics, submit assignments, review other's work and give feedback for reviews. Submission of work can be done in two ways:&lt;br /&gt;
*Hyperlinks&lt;br /&gt;
*Files/Folders&lt;br /&gt;
&lt;br /&gt;
Submitted_Content_Controller and Submitted_Content_Helper are designed to handle operations on hyperlinks and files. They include methods to submit, verify and remove hyperlinks; and create, move, rename, submit and remove files.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== '''Project Description''' ==&lt;br /&gt;
&lt;br /&gt;
Classes: submitted_content_controller.rb (250 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
	 submitted_content_helper.rb (98 lines) &amp;lt;br&amp;gt;&lt;br /&gt;
What they do: Allow users to submit files and links to Expertiza. &amp;lt;br&amp;gt;&lt;br /&gt;
What needs to be done:  The methods are long and complex, and contain a lot of duplicated code.  In particular, there are two different ways of submitting files: as a homework submission, and uploading a review file requested by a review rubric.  These should use common code as much as possible.  The approach to deleting submitted links and submitted files should be analogous.  There have been bugs in deleting hyperlinks, so be sure to test the code thoroughly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== '''Design Changes''' ==&lt;br /&gt;
&lt;br /&gt;
*Code duplication is generally considered a mark of poor or lazy programming style. Good coding style is generally associated with code reuse.&lt;br /&gt;
&lt;br /&gt;
:We identified a sequence of operations that are repetitively used in methods which are used to submit files for the first time and again in review. The code to check if a file exists or not and creating a new directory path for any new submission is common for both submission and review pages. Thus we separated it out as a different method ‘check_file_exists’ as shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def check_file_exists  curr_directory&lt;br /&gt;
    #check if file exists. If not, create a new file. Else, remove the existing file and then create new file.&lt;br /&gt;
    if !File.exists? curr_directory&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    else&lt;br /&gt;
      FileUtils.rm_rf(curr_directory)&lt;br /&gt;
      FileUtils.mkdir_p(curr_directory)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method can now be called from both submit_file and custom_submit_file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 def submit_file&lt;br /&gt;
    participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    file = params[:uploaded_file]&lt;br /&gt;
    participant.set_student_directory_num&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    curr_directory = participant.get_path.to_s+@current_folder.name&lt;br /&gt;
    check_file_exists(curr_directory)&lt;br /&gt;
&lt;br /&gt;
    safe_filename = file.original_filename.gsub(/\\/,&amp;quot;/&amp;quot;)&lt;br /&gt;
    safe_filename = FileHelper::sanitize_filename(safe_filename) # new code to sanitize file path before upload*&lt;br /&gt;
    full_filename =  curr_directory + File.split(safe_filename).last.gsub(&amp;quot; &amp;quot;,'_') #safe_filename #curr_directory +&lt;br /&gt;
    File.open(full_filename, &amp;quot;wb&amp;quot;) { |f| f.write(file.read) }&lt;br /&gt;
&lt;br /&gt;
    if params['unzip']&lt;br /&gt;
      SubmittedContentHelper::unzip_file(full_filename, curr_directory, true) if get_file_type(safe_filename) == &amp;quot;zip&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    participant.update_resubmit_times&lt;br /&gt;
&lt;br /&gt;
    #send message to reviewers when submission has been updated&lt;br /&gt;
    participant.assignment.email(participant.id) rescue nil # If the user has no team: 1) there are no reviewers to notify; 2) calling email will throw an exception. So rescue and ignore it.&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Long methods are often caused due to unnecessary parameters. Long list of parameters reduce readability of code. Following changes are made to remove such unnecessary parameters:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file.JPG|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
Here the variable safename is declared and used only once. The assignment can be made inline by replacing it in the method call with actual logic in the below way:&lt;br /&gt;
&lt;br /&gt;
[[File:Unzip file mod.JPG|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Below is another name change from ‘fpath’ to ‘file_path’ which is made to eliminate the most common code smell ‘Excessively short identifiers’.&lt;br /&gt;
 &lt;br /&gt;
[[File:Fpath.png|frame|none|alt=Before Refactoring|Before Refactoring]] [[File:Filepath.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Consecutive assignments or modifications of the same variable can lead to “Long method” or “Excessive use of literals”. Such code smells are removed. For example, variable ‘newloc’ which is modified consecutively is refactored in the following way:&lt;br /&gt;
 &lt;br /&gt;
:Before Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_loc = @participant.get_path &lt;br /&gt;
    new_loc+= &amp;quot;/&amp;quot; &lt;br /&gt;
    new_loc+= params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_loc)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:After Refactoring:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def move_selected_file&lt;br /&gt;
    old_filename = params[:directories][params[:chk_files]] + &amp;quot;/&amp;quot; + params[:filenames][params[:chk_files]]&lt;br /&gt;
    new_location = @participant.get_path + &amp;quot;/&amp;quot; + params[:faction][:move]&lt;br /&gt;
    begin&lt;br /&gt;
      FileHelper::move_file(old_filename, new_location)&lt;br /&gt;
      flash[:note] = &amp;quot;The file was moved successfully from \&amp;quot;/#{params[:filenames][params[:chk_files]]}\&amp;quot; to \&amp;quot;/#{params[:faction][:move]}\&amp;quot;&amp;quot;&lt;br /&gt;
    rescue&lt;br /&gt;
      flash[:error] = &amp;quot;There was a problem moving the file: &amp;quot;+$!&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Modified code reduces the method length and increases Readability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Unnecessary variable declarations which are never used in the method increase the length of code and confuse the readers. For example, the variable ‘display’ shown in the code snippet below was defined but never used in the method. Such variables can be removed.&lt;br /&gt;
&lt;br /&gt;
[[File:Display variable.png|frame|none|alt=Before Refactoring|Before Refactoring]]&lt;br /&gt;
&lt;br /&gt;
The actual variable that was used which is ‘disp’ can now be renamed to ‘display’ which is more meaningful.&lt;br /&gt;
&lt;br /&gt;
[[File:Display change.png|frame|none|alt=After Refactoring|After Refactoring]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
*Many more minor code changes are made following the Ruby Coding guidelines. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
if !File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We should prefer to use 'unless' for such conditions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
unless File.exist?(new_filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
== List of Code Smells identified and removed ==&lt;br /&gt;
*'''Duplicated code''': Identical or very similar code exists in more than one location. Code duplication frequently creates long, repeated sections of code that differ in only a few lines or characters. The length of such routines can make it difficult to quickly understand them. The repetition of largely identical code sections can conceal how they differ from one another, and therefore, what the specific purpose of each code section is. Often, the only difference is in a parameter value. The best practice in such cases is a reusable subroutine.&lt;br /&gt;
*'''Long method''': A method, function, or procedure that has grown too large. Since the early days of programming people have realized that the longer a procedure is, the more difficult it is to understand. Older languages carried an overhead in subroutine calls, which deterred people from small methods. Modern OO languages have pretty much eliminated that overhead for in-process calls.&lt;br /&gt;
*'''Too many parameters''': A long list of parameters in a procedure or function make readability and code quality worse. The more parameters a method has, the more complex it is. Limit the number of parameters you need in a given method, or use an object to combine the parameters.&lt;br /&gt;
*'''Excessively long identifiers''': In particular, the use of naming conventions to provide disambiguation that should be implicit in the software architecture.&lt;br /&gt;
*'''Excessively short identifiers''': The name of a variable should reflect its function unless the function is obvious. Pick a set of standard terminology and stick to it throughout your methods. For example, if you have Open(), you should probably have Close().&lt;br /&gt;
*'''Excessive use of literals''': These should be coded as named constants, to improve readability and to avoid programming errors. Additionally, literals can and should be externalized into resource files/scripts where possible, to facilitate localization of software if it is intended to be deployed in different regions.&lt;br /&gt;
*'''Complex conditionals''': Branches that check lots of unrelated conditions and edge cases that don't seem to capture the meaning of a block of code. Large conditional logic blocks, particularly blocks that tend to grow larger or change significantly over time have to be eliminated. Consider alternative object-oriented approaches such as decorator, strategy, or state.&lt;br /&gt;
*'''Dead Code''': Ruthlessly delete code that isn't being used. Unused code will result in Long classes or long methods.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
*Polymorphism can be implemented by pushing some methods to relevant classes. Following section of code contains certain method calls which are implemented in the same class. These can be abstracted into a different class for file operations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  def file_folder_action&lt;br /&gt;
    @participant = AssignmentParticipant.find(params[:id])&lt;br /&gt;
    return unless current_user_id?(@participant.user_id)&lt;br /&gt;
&lt;br /&gt;
    @current_folder = DisplayOption.new&lt;br /&gt;
    @current_folder.name = &amp;quot;/&amp;quot;&lt;br /&gt;
    if params[:current_folder]&lt;br /&gt;
      @current_folder.name = FileHelper::sanitize_folder(params[:current_folder][:name])&lt;br /&gt;
    end&lt;br /&gt;
    if params[:faction][:delete]        #call method to delete selected files&lt;br /&gt;
      delete_selected_files&lt;br /&gt;
    elsif params[:faction][:rename]     #call method to rename selected files&lt;br /&gt;
      rename_selected_file&lt;br /&gt;
    elsif params[:faction][:move]       #call method to move selected files&lt;br /&gt;
      move_selected_file&lt;br /&gt;
    elsif params[:faction][:copy]       #call method to copy selected files&lt;br /&gt;
      copy_selected_file&lt;br /&gt;
    elsif params[:faction][:create]     #call method to create new folder&lt;br /&gt;
      create_new_folder&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    redirect_to :action =&amp;gt; 'edit', :id =&amp;gt; @participant.id&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Submit file functionality currently breaks with the latest expertiza master branch code which can be fixed by further exploration.&lt;br /&gt;
*Design changes made in partial files can be extended such that code reuse can be maximized.&lt;br /&gt;
*Many more code smells can be identified and removed to improve elegance.&lt;br /&gt;
*RSpec test cases for the changes made can be added.&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;br /&gt;
&lt;br /&gt;
Steps to setup the project:&lt;br /&gt;
*Extract source code in RubyMine using the following url: https://github.com/hsure/expertiza&lt;br /&gt;
:(A) VCS -&amp;gt; Checkout from version control -&amp;gt; Github&lt;br /&gt;
:(B) Give this url:  https://github.com/hsure/expertiza -&amp;gt; Finish&lt;br /&gt;
*Confirm the sdk for RubyMine to ruby1.9.3 using File -&amp;gt; Settings &lt;br /&gt;
*Run bundle install &lt;br /&gt;
*Run - rake db:create:all&lt;br /&gt;
*Run - rake db:migrate&lt;br /&gt;
*From the Expertiza folder (project root) execute the command “rails server”&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
You should be able to view the test Database in MySQL.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Use the following credentials to explore the application:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Username: admin (Administrator)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: admin&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Username: sample (User)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Password: sample&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=78816</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=78816"/>
		<updated>2013-09-24T20:24:46Z</updated>

		<summary type="html">&lt;p&gt;Hsure: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] Coding Guidelines include best practices followed generally for most of the [http://en.wikipedia.org/wiki/Category:Object-oriented_programming_languages object oriented programming languages] as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines&amp;lt;ref&amp;gt;http://itsignals.cascadia.com.au/?p=7&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on [http://en.wikipedia.org/wiki/Unix UNIX]-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than [http://en.wikipedia.org/wiki/CamelCase camelBack] for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of [http://en.wikipedia.org/wiki/Mixin mix-ins] (which are just modules), however,  should probably be adjectives, such as the standard [http://en.wikibooks.org/wiki/Ruby_Programming/Reference/Objects/Enumerable Enumerable] and [http://www.ruby-doc.org/core-2.0.0/Comparable.html Comparable] modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Visitor_pattern The Visitor pattern]''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Delegation_pattern Delegation]''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''[http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''[http://en.wikipedia.org/wiki/Observer_pattern Observer pattern]''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines&amp;lt;ref&amp;gt;http://blog.monitis.com/index.php/2012/02/08/20-ruby-performance-tips/&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Concatenated strings calls a method to get executed. So, it affects the performance as it is one of the most frequently used operation. Its better to replace them with Interpolated strings which runs faster as it doesn't invoke a method call.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put “Hello there, #{name}!”vs.puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that which modifies the actual value than working on a copy of the data are much faster. But this should be handled carefully as original data gets disturbed.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: .each uses an enumeration object behind the scene which adds a delay in execution. Using for instead of .each would improve performance but for short loops only.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both these calls are very expensive. using regular expressions would improve the time.&lt;br /&gt;
&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: All libraries are not efficient. Many gems may need to be removed to problem to fix a problem with the code. This is a common scenario when many gems are installed. So performing bench marking and testing a gem with others that perform same task would help in picking the right gem.&lt;br /&gt;
&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements results in evident improvements in performance of the code. It is always ideal to design an efficient algorithm without unwanted method calls.&lt;br /&gt;
&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: While using if statements or a case statement always test the cases that occur most frequently. This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: While accessing the global constants one should use namespace before the constants to avoid entire library search for the constant.&lt;br /&gt;
 &lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Even though the result of last operation is returned for a method, using explicit returns would speed up the code. Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/api_documentation_guidelines.html&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments&amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&amp;lt;/ref&amp;gt; can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines&amp;lt;ref&amp;gt;http://www.caliban.org/ruby/rubyguide.shtml&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&lt;br /&gt;
require statements&lt;br /&gt;
include statements&lt;br /&gt;
class and module definitions&lt;br /&gt;
main program section&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools&amp;lt;ref&amp;gt;http://www.ruby-toolbox.com/categories/code_metrics&amp;lt;/ref&amp;gt; can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[http://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[http://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[http://saikuro.rubyforge.org/ '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[http://ruby.sadi.st/Flog.html '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[http://ruby.sadi.st/Flay.html '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
*http://github.com/styleguide/ruby&lt;br /&gt;
*http://www.ruby-doc.org/docs/ProgrammingRuby/&lt;br /&gt;
*http://www.ruby-lang.org/en/documentation/&lt;br /&gt;
*http://en.wikibooks.org/wiki/Ruby_programming_language&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=78815</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=78815"/>
		<updated>2013-09-24T20:22:15Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Code Analysis Tools */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] Coding Guidelines include best practices followed generally for most of the [http://en.wikipedia.org/wiki/Category:Object-oriented_programming_languages object oriented programming languages] as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines&amp;lt;ref&amp;gt;http://itsignals.cascadia.com.au/?p=7&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on [http://en.wikipedia.org/wiki/Unix UNIX]-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than [http://en.wikipedia.org/wiki/CamelCase camelBack] for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of [http://en.wikipedia.org/wiki/Mixin mix-ins] (which are just modules), however,  should probably be adjectives, such as the standard [http://en.wikibooks.org/wiki/Ruby_Programming/Reference/Objects/Enumerable Enumerable] and [http://www.ruby-doc.org/core-2.0.0/Comparable.html Comparable] modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Visitor_pattern The Visitor pattern]''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Delegation_pattern Delegation]''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''[http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''[http://en.wikipedia.org/wiki/Observer_pattern Observer pattern]''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines&amp;lt;ref&amp;gt;http://blog.monitis.com/index.php/2012/02/08/20-ruby-performance-tips/&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Concatenated strings calls a method to get executed. So, it affects the performance as it is one of the most frequently used operation. Its better to replace them with Interpolated strings which runs faster as it doesn't invoke a method call.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put “Hello there, #{name}!”vs.puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that which modifies the actual value than working on a copy of the data are much faster. But this should be handled carefully as original data gets disturbed.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: .each uses an enumeration object behind the scene which adds a delay in execution. Using for instead of .each would improve performance but for short loops only.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both these calls are very expensive. using regular expressions would improve the time.&lt;br /&gt;
&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: All libraries are not efficient. Many gems may need to be removed to problem to fix a problem with the code. This is a common scenario when many gems are installed. So performing bench marking and testing a gem with others that perform same task would help in picking the right gem.&lt;br /&gt;
&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements results in evident improvements in performance of the code. It is always ideal to design an efficient algorithm without unwanted method calls.&lt;br /&gt;
&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: While using if statements or a case statement always test the cases that occur most frequently. This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: While accessing the global constants one should use namespace before the constants to avoid entire library search for the constant.&lt;br /&gt;
 &lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Even though the result of last operation is returned for a method, using explicit returns would speed up the code. Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/api_documentation_guidelines.html&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments&amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&amp;lt;/ref&amp;gt; can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines&amp;lt;ref&amp;gt;http://www.caliban.org/ruby/rubyguide.shtml&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&lt;br /&gt;
require statements&lt;br /&gt;
include statements&lt;br /&gt;
class and module definitions&lt;br /&gt;
main program section&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools&amp;lt;ref&amp;gt;https://www.ruby-toolbox.com/categories/code_metrics&amp;lt;/ref&amp;gt; can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[http://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[http://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[http://saikuro.rubyforge.org/ '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[http://ruby.sadi.st/Flog.html '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[http://ruby.sadi.st/Flay.html '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
*http://github.com/styleguide/ruby&lt;br /&gt;
*http://www.ruby-doc.org/docs/ProgrammingRuby/&lt;br /&gt;
*http://www.ruby-lang.org/en/documentation/&lt;br /&gt;
*http://en.wikibooks.org/wiki/Ruby_programming_language&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=78813</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=78813"/>
		<updated>2013-09-24T20:20:23Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Code Analysis Tools */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] Coding Guidelines include best practices followed generally for most of the [http://en.wikipedia.org/wiki/Category:Object-oriented_programming_languages object oriented programming languages] as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines&amp;lt;ref&amp;gt;http://itsignals.cascadia.com.au/?p=7&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on [http://en.wikipedia.org/wiki/Unix UNIX]-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than [http://en.wikipedia.org/wiki/CamelCase camelBack] for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of [http://en.wikipedia.org/wiki/Mixin mix-ins] (which are just modules), however,  should probably be adjectives, such as the standard [http://en.wikibooks.org/wiki/Ruby_Programming/Reference/Objects/Enumerable Enumerable] and [http://www.ruby-doc.org/core-2.0.0/Comparable.html Comparable] modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Visitor_pattern The Visitor pattern]''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Delegation_pattern Delegation]''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''[http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''[http://en.wikipedia.org/wiki/Observer_pattern Observer pattern]''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines&amp;lt;ref&amp;gt;http://blog.monitis.com/index.php/2012/02/08/20-ruby-performance-tips/&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Concatenated strings calls a method to get executed. So, it affects the performance as it is one of the most frequently used operation. Its better to replace them with Interpolated strings which runs faster as it doesn't invoke a method call.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put “Hello there, #{name}!”vs.puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that which modifies the actual value than working on a copy of the data are much faster. But this should be handled carefully as original data gets disturbed.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: .each uses an enumeration object behind the scene which adds a delay in execution. Using for instead of .each would improve performance but for short loops only.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both these calls are very expensive. using regular expressions would improve the time.&lt;br /&gt;
&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: All libraries are not efficient. Many gems may need to be removed to problem to fix a problem with the code. This is a common scenario when many gems are installed. So performing bench marking and testing a gem with others that perform same task would help in picking the right gem.&lt;br /&gt;
&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements results in evident improvements in performance of the code. It is always ideal to design an efficient algorithm without unwanted method calls.&lt;br /&gt;
&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: While using if statements or a case statement always test the cases that occur most frequently. This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: While accessing the global constants one should use namespace before the constants to avoid entire library search for the constant.&lt;br /&gt;
 &lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Even though the result of last operation is returned for a method, using explicit returns would speed up the code. Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/api_documentation_guidelines.html&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments&amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&amp;lt;/ref&amp;gt; can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines&amp;lt;ref&amp;gt;http://www.caliban.org/ruby/rubyguide.shtml&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&lt;br /&gt;
require statements&lt;br /&gt;
include statements&lt;br /&gt;
class and module definitions&lt;br /&gt;
main program section&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools&amp;lt;ref&amp;gt;https://www.ruby-toolbox.com/categories/code_metrics&amp;lt;/ref&amp;gt; can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[http://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[http://https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[http://saikuro.rubyforge.org/ '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[http://http://ruby.sadi.st/Flog.html '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[http://ruby.sadi.st/Flay.html '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
*http://github.com/styleguide/ruby&lt;br /&gt;
*http://www.ruby-doc.org/docs/ProgrammingRuby/&lt;br /&gt;
*http://www.ruby-lang.org/en/documentation/&lt;br /&gt;
*http://en.wikibooks.org/wiki/Ruby_programming_language&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=78809</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=78809"/>
		<updated>2013-09-24T20:15:57Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Code Analysis Tools */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] Coding Guidelines include best practices followed generally for most of the [http://en.wikipedia.org/wiki/Category:Object-oriented_programming_languages object oriented programming languages] as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines&amp;lt;ref&amp;gt;http://itsignals.cascadia.com.au/?p=7&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on [http://en.wikipedia.org/wiki/Unix UNIX]-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than [http://en.wikipedia.org/wiki/CamelCase camelBack] for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of [http://en.wikipedia.org/wiki/Mixin mix-ins] (which are just modules), however,  should probably be adjectives, such as the standard [http://en.wikibooks.org/wiki/Ruby_Programming/Reference/Objects/Enumerable Enumerable] and [http://www.ruby-doc.org/core-2.0.0/Comparable.html Comparable] modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Visitor_pattern The Visitor pattern]''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Delegation_pattern Delegation]''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''[http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''[http://en.wikipedia.org/wiki/Observer_pattern Observer pattern]''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines&amp;lt;ref&amp;gt;http://blog.monitis.com/index.php/2012/02/08/20-ruby-performance-tips/&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Concatenated strings calls a method to get executed. So, it affects the performance as it is one of the most frequently used operation. Its better to replace them with Interpolated strings which runs faster as it doesn't invoke a method call.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put “Hello there, #{name}!”vs.puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that which modifies the actual value than working on a copy of the data are much faster. But this should be handled carefully as original data gets disturbed.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: .each uses an enumeration object behind the scene which adds a delay in execution. Using for instead of .each would improve performance but for short loops only.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both these calls are very expensive. using regular expressions would improve the time.&lt;br /&gt;
&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: All libraries are not efficient. Many gems may need to be removed to problem to fix a problem with the code. This is a common scenario when many gems are installed. So performing bench marking and testing a gem with others that perform same task would help in picking the right gem.&lt;br /&gt;
&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements results in evident improvements in performance of the code. It is always ideal to design an efficient algorithm without unwanted method calls.&lt;br /&gt;
&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: While using if statements or a case statement always test the cases that occur most frequently. This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: While accessing the global constants one should use namespace before the constants to avoid entire library search for the constant.&lt;br /&gt;
 &lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Even though the result of last operation is returned for a method, using explicit returns would speed up the code. Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/api_documentation_guidelines.html&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments&amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&amp;lt;/ref&amp;gt; can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines&amp;lt;ref&amp;gt;http://www.caliban.org/ruby/rubyguide.shtml&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&lt;br /&gt;
require statements&lt;br /&gt;
include statements&lt;br /&gt;
class and module definitions&lt;br /&gt;
main program section&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools&amp;lt;ref&amp;gt;https://www.ruby-toolbox.com/categories/code_metrics&amp;lt;/ref&amp;gt; can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[http://www.ruby-toolbox.com/categories/code_metrics '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[http://www.ruby-toolbox.com/categories/code_metrics '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[http://www.ruby-toolbox.com/categories/code_metrics '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[http://www.ruby-toolbox.com/categories/code_metrics '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[http://www.ruby-toolbox.com/categories/code_metrics '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
*http://github.com/styleguide/ruby&lt;br /&gt;
*http://www.ruby-doc.org/docs/ProgrammingRuby/&lt;br /&gt;
*http://www.ruby-lang.org/en/documentation/&lt;br /&gt;
*http://en.wikibooks.org/wiki/Ruby_programming_language&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=78572</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=78572"/>
		<updated>2013-09-24T05:08:36Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Ruby Coding Guidelines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] Coding Guidelines include best practices followed generally for most of the [http://en.wikipedia.org/wiki/Category:Object-oriented_programming_languages object oriented programming languages] as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines&amp;lt;ref&amp;gt;http://itsignals.cascadia.com.au/?p=7&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on [http://en.wikipedia.org/wiki/Unix UNIX]-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than [http://en.wikipedia.org/wiki/CamelCase camelBack] for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of [http://en.wikipedia.org/wiki/Mixin mix-ins] (which are just modules), however,  should probably be adjectives, such as the standard [http://en.wikibooks.org/wiki/Ruby_Programming/Reference/Objects/Enumerable Enumerable] and [http://www.ruby-doc.org/core-2.0.0/Comparable.html Comparable] modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Visitor_pattern The Visitor pattern]''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Delegation_pattern Delegation]''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''[http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''[http://en.wikipedia.org/wiki/Observer_pattern Observer pattern]''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines&amp;lt;ref&amp;gt;http://blog.monitis.com/index.php/2012/02/08/20-ruby-performance-tips/&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Concatenated strings calls a method to get executed. So, it affects the performance as it is one of the most frequently used operation. Its better to replace them with Interpolated strings which runs faster as it doesn't invoke a method call.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put “Hello there, #{name}!”vs.puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that which modifies the actual value than working on a copy of the data are much faster. But this should be handled carefully as original data gets disturbed.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: .each uses an enumeration object behind the scene which adds a delay in execution. Using for instead of .each would improve performance but for short loops only.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both these calls are very expensive. using regular expressions would improve the time.&lt;br /&gt;
&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: All libraries are not efficient. Many gems may need to be removed to problem to fix a problem with the code. This is a common scenario when many gems are installed. So performing bench marking and testing a gem with others that perform same task would help in picking the right gem.&lt;br /&gt;
&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements results in evident improvements in performance of the code. It is always ideal to design an efficient algorithm without unwanted method calls.&lt;br /&gt;
&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: While using if statements or a case statement always test the cases that occur most frequently. This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: While accessing the global constants one should use namespace before the constants to avoid entire library search for the constant.&lt;br /&gt;
 &lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Even though the result of last operation is returned for a method, using explicit returns would speed up the code. Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/api_documentation_guidelines.html&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments&amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&amp;lt;/ref&amp;gt; can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines&amp;lt;ref&amp;gt;http://www.caliban.org/ruby/rubyguide.shtml&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&lt;br /&gt;
require statements&lt;br /&gt;
include statements&lt;br /&gt;
class and module definitions&lt;br /&gt;
main program section&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools&amp;lt;ref&amp;gt;https://www.ruby-toolbox.com/categories/code_metrics&amp;lt;/ref&amp;gt; can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://www.ruby-toolbox.com/categories/code_metrics '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek/wiki '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://www.ruby-toolbox.com/categories/code_metrics '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://www.ruby-toolbox.com/categories/code_metrics '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://www.ruby-toolbox.com/categories/code_metrics '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
*https://github.com/styleguide/ruby&lt;br /&gt;
*http://www.ruby-doc.org/docs/ProgrammingRuby/&lt;br /&gt;
*https://www.ruby-lang.org/en/documentation/&lt;br /&gt;
*http://en.wikibooks.org/wiki/Ruby_programming_language&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=78571</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=78571"/>
		<updated>2013-09-24T05:08:18Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Ruby Coding Guidelines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
div#content a[href ^=&amp;quot;https://&amp;quot;].external&lt;br /&gt;
    background: center right no-repeat; padding-right: 18px;&lt;br /&gt;
/div&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] Coding Guidelines include best practices followed generally for most of the [http://en.wikipedia.org/wiki/Category:Object-oriented_programming_languages object oriented programming languages] as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines&amp;lt;ref&amp;gt;http://itsignals.cascadia.com.au/?p=7&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on [http://en.wikipedia.org/wiki/Unix UNIX]-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than [http://en.wikipedia.org/wiki/CamelCase camelBack] for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of [http://en.wikipedia.org/wiki/Mixin mix-ins] (which are just modules), however,  should probably be adjectives, such as the standard [http://en.wikibooks.org/wiki/Ruby_Programming/Reference/Objects/Enumerable Enumerable] and [http://www.ruby-doc.org/core-2.0.0/Comparable.html Comparable] modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Visitor_pattern The Visitor pattern]''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Delegation_pattern Delegation]''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''[http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''[http://en.wikipedia.org/wiki/Observer_pattern Observer pattern]''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines&amp;lt;ref&amp;gt;http://blog.monitis.com/index.php/2012/02/08/20-ruby-performance-tips/&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Concatenated strings calls a method to get executed. So, it affects the performance as it is one of the most frequently used operation. Its better to replace them with Interpolated strings which runs faster as it doesn't invoke a method call.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put “Hello there, #{name}!”vs.puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that which modifies the actual value than working on a copy of the data are much faster. But this should be handled carefully as original data gets disturbed.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: .each uses an enumeration object behind the scene which adds a delay in execution. Using for instead of .each would improve performance but for short loops only.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both these calls are very expensive. using regular expressions would improve the time.&lt;br /&gt;
&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: All libraries are not efficient. Many gems may need to be removed to problem to fix a problem with the code. This is a common scenario when many gems are installed. So performing bench marking and testing a gem with others that perform same task would help in picking the right gem.&lt;br /&gt;
&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements results in evident improvements in performance of the code. It is always ideal to design an efficient algorithm without unwanted method calls.&lt;br /&gt;
&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: While using if statements or a case statement always test the cases that occur most frequently. This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: While accessing the global constants one should use namespace before the constants to avoid entire library search for the constant.&lt;br /&gt;
 &lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Even though the result of last operation is returned for a method, using explicit returns would speed up the code. Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/api_documentation_guidelines.html&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments&amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&amp;lt;/ref&amp;gt; can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines&amp;lt;ref&amp;gt;http://www.caliban.org/ruby/rubyguide.shtml&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&lt;br /&gt;
require statements&lt;br /&gt;
include statements&lt;br /&gt;
class and module definitions&lt;br /&gt;
main program section&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools&amp;lt;ref&amp;gt;https://www.ruby-toolbox.com/categories/code_metrics&amp;lt;/ref&amp;gt; can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://www.ruby-toolbox.com/categories/code_metrics '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek/wiki '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://www.ruby-toolbox.com/categories/code_metrics '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://www.ruby-toolbox.com/categories/code_metrics '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://www.ruby-toolbox.com/categories/code_metrics '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
*https://github.com/styleguide/ruby&lt;br /&gt;
*http://www.ruby-doc.org/docs/ProgrammingRuby/&lt;br /&gt;
*https://www.ruby-lang.org/en/documentation/&lt;br /&gt;
*http://en.wikibooks.org/wiki/Ruby_programming_language&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=78570</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=78570"/>
		<updated>2013-09-24T05:07:49Z</updated>

		<summary type="html">&lt;p&gt;Hsure: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
[[div#content a[href ^=&amp;quot;https://&amp;quot;].external]]&lt;br /&gt;
    background: center right no-repeat; padding-right: 18px;&lt;br /&gt;
[[/div]]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] Coding Guidelines include best practices followed generally for most of the [http://en.wikipedia.org/wiki/Category:Object-oriented_programming_languages object oriented programming languages] as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines&amp;lt;ref&amp;gt;http://itsignals.cascadia.com.au/?p=7&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on [http://en.wikipedia.org/wiki/Unix UNIX]-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than [http://en.wikipedia.org/wiki/CamelCase camelBack] for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of [http://en.wikipedia.org/wiki/Mixin mix-ins] (which are just modules), however,  should probably be adjectives, such as the standard [http://en.wikibooks.org/wiki/Ruby_Programming/Reference/Objects/Enumerable Enumerable] and [http://www.ruby-doc.org/core-2.0.0/Comparable.html Comparable] modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Visitor_pattern The Visitor pattern]''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Delegation_pattern Delegation]''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''[http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''[http://en.wikipedia.org/wiki/Observer_pattern Observer pattern]''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines&amp;lt;ref&amp;gt;http://blog.monitis.com/index.php/2012/02/08/20-ruby-performance-tips/&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Concatenated strings calls a method to get executed. So, it affects the performance as it is one of the most frequently used operation. Its better to replace them with Interpolated strings which runs faster as it doesn't invoke a method call.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put “Hello there, #{name}!”vs.puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that which modifies the actual value than working on a copy of the data are much faster. But this should be handled carefully as original data gets disturbed.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: .each uses an enumeration object behind the scene which adds a delay in execution. Using for instead of .each would improve performance but for short loops only.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both these calls are very expensive. using regular expressions would improve the time.&lt;br /&gt;
&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: All libraries are not efficient. Many gems may need to be removed to problem to fix a problem with the code. This is a common scenario when many gems are installed. So performing bench marking and testing a gem with others that perform same task would help in picking the right gem.&lt;br /&gt;
&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements results in evident improvements in performance of the code. It is always ideal to design an efficient algorithm without unwanted method calls.&lt;br /&gt;
&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: While using if statements or a case statement always test the cases that occur most frequently. This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: While accessing the global constants one should use namespace before the constants to avoid entire library search for the constant.&lt;br /&gt;
 &lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Even though the result of last operation is returned for a method, using explicit returns would speed up the code. Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/api_documentation_guidelines.html&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments&amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&amp;lt;/ref&amp;gt; can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines&amp;lt;ref&amp;gt;http://www.caliban.org/ruby/rubyguide.shtml&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&lt;br /&gt;
require statements&lt;br /&gt;
include statements&lt;br /&gt;
class and module definitions&lt;br /&gt;
main program section&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools&amp;lt;ref&amp;gt;https://www.ruby-toolbox.com/categories/code_metrics&amp;lt;/ref&amp;gt; can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://www.ruby-toolbox.com/categories/code_metrics '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek/wiki '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://www.ruby-toolbox.com/categories/code_metrics '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://www.ruby-toolbox.com/categories/code_metrics '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://www.ruby-toolbox.com/categories/code_metrics '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
*https://github.com/styleguide/ruby&lt;br /&gt;
*http://www.ruby-doc.org/docs/ProgrammingRuby/&lt;br /&gt;
*https://www.ruby-lang.org/en/documentation/&lt;br /&gt;
*http://en.wikibooks.org/wiki/Ruby_programming_language&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=78486</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=78486"/>
		<updated>2013-09-23T21:28:26Z</updated>

		<summary type="html">&lt;p&gt;Hsure: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
[[div#content a[href ^=&amp;quot;https://&amp;quot;].external]]&lt;br /&gt;
    background: center right no-repeat; padding-right: 18px;&lt;br /&gt;
[[/div]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] Coding Guidelines include best practices followed generally for most of the [http://en.wikipedia.org/wiki/Category:Object-oriented_programming_languages object oriented programming languages] as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines&amp;lt;ref&amp;gt;http://itsignals.cascadia.com.au/?p=7&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on [http://en.wikipedia.org/wiki/Unix UNIX]-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than [http://en.wikipedia.org/wiki/CamelCase camelBack] for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of [http://en.wikipedia.org/wiki/Mixin mix-ins] (which are just modules), however,  should probably be adjectives, such as the standard [http://en.wikibooks.org/wiki/Ruby_Programming/Reference/Objects/Enumerable Enumerable] and [http://www.ruby-doc.org/core-2.0.0/Comparable.html Comparable] modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Visitor_pattern The Visitor pattern]''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''[http://en.wikipedia.org/wiki/Delegation_pattern Delegation]''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''[http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''[http://en.wikipedia.org/wiki/Observer_pattern Observer pattern]''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines&amp;lt;ref&amp;gt;http://blog.monitis.com/index.php/2012/02/08/20-ruby-performance-tips/&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Concatenated strings calls a method to get executed. So, it affects the performance as it is one of the most frequently used operation. Its better to replace them with Interpolated strings which runs faster as it doesn't invoke a method call.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put “Hello there, #{name}!”vs.puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that which modifies the actual value than working on a copy of the data are much faster. But this should be handled carefully as original data gets disturbed.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: .each uses an enumeration object behind the scene which adds a delay in execution. Using for instead of .each would improve performance but for short loops only.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both these calls are very expensive. using regular expressions would improve the time.&lt;br /&gt;
&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: All libraries are not efficient. Many gems may need to be removed to problem to fix a problem with the code. This is a common scenario when many gems are installed. So performing bench marking and testing a gem with others that perform same task would help in picking the right gem.&lt;br /&gt;
&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements results in evident improvements in performance of the code. It is always ideal to design an efficient algorithm without unwanted method calls.&lt;br /&gt;
&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: While using if statements or a case statement always test the cases that occur most frequently. This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: While accessing the global constants one should use namespace before the constants to avoid entire library search for the constant.&lt;br /&gt;
 &lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Even though the result of last operation is returned for a method, using explicit returns would speed up the code. Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/api_documentation_guidelines.html&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments&amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&amp;lt;/ref&amp;gt; can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines&amp;lt;ref&amp;gt;http://www.caliban.org/ruby/rubyguide.shtml&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&lt;br /&gt;
require statements&lt;br /&gt;
include statements&lt;br /&gt;
class and module definitions&lt;br /&gt;
main program section&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools&amp;lt;ref&amp;gt;https://www.ruby-toolbox.com/categories/code_metrics&amp;lt;/ref&amp;gt; can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://www.ruby-toolbox.com/categories/code_metrics '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek/wiki '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://www.ruby-toolbox.com/categories/code_metrics '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://www.ruby-toolbox.com/categories/code_metrics '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://www.ruby-toolbox.com/categories/code_metrics '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
*https://github.com/styleguide/ruby&lt;br /&gt;
*http://www.ruby-doc.org/docs/ProgrammingRuby/&lt;br /&gt;
*https://www.ruby-lang.org/en/documentation/&lt;br /&gt;
*http://en.wikibooks.org/wiki/Ruby_programming_language&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77970</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77970"/>
		<updated>2013-09-18T17:54:33Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Ruby Coding Guidelines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines&amp;lt;ref&amp;gt;http://itsignals.cascadia.com.au/?p=7&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules), however,  should probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines&amp;lt;ref&amp;gt;http://blog.monitis.com/index.php/2012/02/08/20-ruby-performance-tips/&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Concatenated strings calls a method to get executed. So, it affects the performance as it is one of the most frequently used operation. Its better to replace them with Interpolated strings which runs faster as it doesn't invoke a method call.&lt;br /&gt;
put “Hello there, #{name}!”vs.puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that which modifies the actual value than working on a copy of the data are much faster. But this should be handled carefully as original data gets disturbed.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: .each uses an enumeration object behind the scene which adds a delay in execution. Using for instead of .each would improve performance but for short loops only.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both these calls are very expensive. using regular expressions would improve the time.&lt;br /&gt;
&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: All libraries are not efficient. Many gems may need to be removed to problem to fix a problem with the code. This is a common scenario when many gems are installed. So performing bench marking and testing a gem with others that perform same task would help in picking the right gem.&lt;br /&gt;
&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements results in evident improvements in performance of the code. It is always ideal to design an efficient algorithm without unwanted method calls.&lt;br /&gt;
&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: While using if statements or a case statement always test the cases that occur most frequently. This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: While accessing the global constants one should use namespace before the constants to avoid entire library search for the constant.&lt;br /&gt;
 &lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Even though the result of last operation is returned for a method, using explicit returns would speed up the code. Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/api_documentation_guidelines.html&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments&amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&amp;lt;/ref&amp;gt; can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines&amp;lt;ref&amp;gt;http://www.caliban.org/ruby/rubyguide.shtml&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools&amp;lt;ref&amp;gt;http://www.infoq.com/news/2009/09/code-quality-metric-fu&amp;lt;/ref&amp;gt; can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77968</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77968"/>
		<updated>2013-09-18T17:53:48Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Ruby Coding Guidelines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
[[http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby]] Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines&amp;lt;ref&amp;gt;http://itsignals.cascadia.com.au/?p=7&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules), however,  should probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines&amp;lt;ref&amp;gt;http://blog.monitis.com/index.php/2012/02/08/20-ruby-performance-tips/&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Concatenated strings calls a method to get executed. So, it affects the performance as it is one of the most frequently used operation. Its better to replace them with Interpolated strings which runs faster as it doesn't invoke a method call.&lt;br /&gt;
put “Hello there, #{name}!”vs.puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that which modifies the actual value than working on a copy of the data are much faster. But this should be handled carefully as original data gets disturbed.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: .each uses an enumeration object behind the scene which adds a delay in execution. Using for instead of .each would improve performance but for short loops only.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both these calls are very expensive. using regular expressions would improve the time.&lt;br /&gt;
&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: All libraries are not efficient. Many gems may need to be removed to problem to fix a problem with the code. This is a common scenario when many gems are installed. So performing bench marking and testing a gem with others that perform same task would help in picking the right gem.&lt;br /&gt;
&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements results in evident improvements in performance of the code. It is always ideal to design an efficient algorithm without unwanted method calls.&lt;br /&gt;
&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: While using if statements or a case statement always test the cases that occur most frequently. This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: While accessing the global constants one should use namespace before the constants to avoid entire library search for the constant.&lt;br /&gt;
 &lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Even though the result of last operation is returned for a method, using explicit returns would speed up the code. Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/api_documentation_guidelines.html&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments&amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&amp;lt;/ref&amp;gt; can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines&amp;lt;ref&amp;gt;http://www.caliban.org/ruby/rubyguide.shtml&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools&amp;lt;ref&amp;gt;http://www.infoq.com/news/2009/09/code-quality-metric-fu&amp;lt;/ref&amp;gt; can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77964</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77964"/>
		<updated>2013-09-18T17:51:10Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Performance Guidelines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines&amp;lt;ref&amp;gt;http://itsignals.cascadia.com.au/?p=7&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules), however,  should probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines&amp;lt;ref&amp;gt;http://blog.monitis.com/index.php/2012/02/08/20-ruby-performance-tips/&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Concatenated strings calls a method to get executed. So, it affects the performance as it is one of the most frequently used operation. Its better to replace them with Interpolated strings which runs faster as it doesn't invoke a method call.&lt;br /&gt;
put “Hello there, #{name}!”vs.puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that which modifies the actual value than working on a copy of the data are much faster. But this should be handled carefully as original data gets disturbed.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: .each uses an enumeration object behind the scene which adds a delay in execution. Using for instead of .each would improve performance but for short loops only.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both these calls are very expensive. using regular expressions would improve the time.&lt;br /&gt;
&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: All libraries are not efficient. Many gems may need to be removed to problem to fix a problem with the code. This is a common scenario when many gems are installed. So performing bench marking and testing a gem with others that perform same task would help in picking the right gem.&lt;br /&gt;
&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements results in evident improvements in performance of the code. It is always ideal to design an efficient algorithm without unwanted method calls.&lt;br /&gt;
&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: While using if statements or a case statement always test the cases that occur most frequently. This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: While accessing the global constants one should use namespace before the constants to avoid entire library search for the constant.&lt;br /&gt;
 &lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Even though the result of last operation is returned for a method, using explicit returns would speed up the code. Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/api_documentation_guidelines.html&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments&amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&amp;lt;/ref&amp;gt; can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines&amp;lt;ref&amp;gt;http://www.caliban.org/ruby/rubyguide.shtml&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools&amp;lt;ref&amp;gt;http://www.infoq.com/news/2009/09/code-quality-metric-fu&amp;lt;/ref&amp;gt; can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77958</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77958"/>
		<updated>2013-09-18T17:46:32Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Layout Guidelines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines&amp;lt;ref&amp;gt;http://itsignals.cascadia.com.au/?p=7&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules), however,  should probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Concatenated strings calls a method to get executed. So, it affects the performance as it is one of the most frequently used operation. Its better to replace them with Interpolated strings which runs faster as it doesn't invoke a method call.&lt;br /&gt;
put “Hello there, #{name}!”vs.puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that which modifies the actual value than working on a copy of the data are much faster. But this should be handled carefully as original data gets disturbed.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: .each uses an enumeration object behind the scene which adds a delay in execution. Using for instead of .each would improve performance but for short loops only.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both these calls are very expensive. using regular expressions would improve the time.&lt;br /&gt;
&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: All libraries are not efficient. Many gems may need to be removed to problem to fix a problem with the code. This is a common scenario when many gems are installed. So performing bench marking and testing a gem with others that perform same task would help in picking the right gem.&lt;br /&gt;
&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements results in evident improvements in performance of the code. It is always ideal to design an efficient algorithm without unwanted method calls.&lt;br /&gt;
&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: While using if statements or a case statement always test the cases that occur most frequently. This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: While accessing the global constants one should use namespace before the constants to avoid entire library search for the constant.&lt;br /&gt;
 &lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Even though the result of last operation is returned for a method, using explicit returns would speed up the code. Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/api_documentation_guidelines.html&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments&amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&amp;lt;/ref&amp;gt; can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines&amp;lt;ref&amp;gt;http://www.caliban.org/ruby/rubyguide.shtml&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools&amp;lt;ref&amp;gt;http://www.infoq.com/news/2009/09/code-quality-metric-fu&amp;lt;/ref&amp;gt; can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77957</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77957"/>
		<updated>2013-09-18T17:45:28Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Documentation Guidelineshttp://guides.rubyonrails.org/api_documentation_guidelines.html */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines&amp;lt;ref&amp;gt;http://itsignals.cascadia.com.au/?p=7&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules), however,  should probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Concatenated strings calls a method to get executed. So, it affects the performance as it is one of the most frequently used operation. Its better to replace them with Interpolated strings which runs faster as it doesn't invoke a method call.&lt;br /&gt;
put “Hello there, #{name}!”vs.puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that which modifies the actual value than working on a copy of the data are much faster. But this should be handled carefully as original data gets disturbed.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: .each uses an enumeration object behind the scene which adds a delay in execution. Using for instead of .each would improve performance but for short loops only.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both these calls are very expensive. using regular expressions would improve the time.&lt;br /&gt;
&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: All libraries are not efficient. Many gems may need to be removed to problem to fix a problem with the code. This is a common scenario when many gems are installed. So performing bench marking and testing a gem with others that perform same task would help in picking the right gem.&lt;br /&gt;
&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements results in evident improvements in performance of the code. It is always ideal to design an efficient algorithm without unwanted method calls.&lt;br /&gt;
&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: While using if statements or a case statement always test the cases that occur most frequently. This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: While accessing the global constants one should use namespace before the constants to avoid entire library search for the constant.&lt;br /&gt;
 &lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Even though the result of last operation is returned for a method, using explicit returns would speed up the code. Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/api_documentation_guidelines.html&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments&amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&amp;lt;/ref&amp;gt; can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools&amp;lt;ref&amp;gt;http://www.infoq.com/news/2009/09/code-quality-metric-fu&amp;lt;/ref&amp;gt; can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77954</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77954"/>
		<updated>2013-09-18T17:44:35Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Documentation Guidelines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines&amp;lt;ref&amp;gt;http://itsignals.cascadia.com.au/?p=7&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules), however,  should probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Concatenated strings calls a method to get executed. So, it affects the performance as it is one of the most frequently used operation. Its better to replace them with Interpolated strings which runs faster as it doesn't invoke a method call.&lt;br /&gt;
put “Hello there, #{name}!”vs.puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that which modifies the actual value than working on a copy of the data are much faster. But this should be handled carefully as original data gets disturbed.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: .each uses an enumeration object behind the scene which adds a delay in execution. Using for instead of .each would improve performance but for short loops only.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both these calls are very expensive. using regular expressions would improve the time.&lt;br /&gt;
&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: All libraries are not efficient. Many gems may need to be removed to problem to fix a problem with the code. This is a common scenario when many gems are installed. So performing bench marking and testing a gem with others that perform same task would help in picking the right gem.&lt;br /&gt;
&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements results in evident improvements in performance of the code. It is always ideal to design an efficient algorithm without unwanted method calls.&lt;br /&gt;
&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: While using if statements or a case statement always test the cases that occur most frequently. This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: While accessing the global constants one should use namespace before the constants to avoid entire library search for the constant.&lt;br /&gt;
 &lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Even though the result of last operation is returned for a method, using explicit returns would speed up the code. Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/api_documentation_guidelines.html&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools&amp;lt;ref&amp;gt;http://www.infoq.com/news/2009/09/code-quality-metric-fu&amp;lt;/ref&amp;gt; can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77951</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77951"/>
		<updated>2013-09-18T17:42:23Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Naming Guidelines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines&amp;lt;ref&amp;gt;http://itsignals.cascadia.com.au/?p=7&amp;lt;/ref&amp;gt; ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules), however,  should probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Concatenated strings calls a method to get executed. So, it affects the performance as it is one of the most frequently used operation. Its better to replace them with Interpolated strings which runs faster as it doesn't invoke a method call.&lt;br /&gt;
put “Hello there, #{name}!”vs.puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that which modifies the actual value than working on a copy of the data are much faster. But this should be handled carefully as original data gets disturbed.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: .each uses an enumeration object behind the scene which adds a delay in execution. Using for instead of .each would improve performance but for short loops only.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both these calls are very expensive. using regular expressions would improve the time.&lt;br /&gt;
&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: All libraries are not efficient. Many gems may need to be removed to problem to fix a problem with the code. This is a common scenario when many gems are installed. So performing bench marking and testing a gem with others that perform same task would help in picking the right gem.&lt;br /&gt;
&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements results in evident improvements in performance of the code. It is always ideal to design an efficient algorithm without unwanted method calls.&lt;br /&gt;
&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: While using if statements or a case statement always test the cases that occur most frequently. This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: While accessing the global constants one should use namespace before the constants to avoid entire library search for the constant.&lt;br /&gt;
 &lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Even though the result of last operation is returned for a method, using explicit returns would speed up the code. Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools&amp;lt;ref&amp;gt;http://www.infoq.com/news/2009/09/code-quality-metric-fu&amp;lt;/ref&amp;gt; can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77948</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77948"/>
		<updated>2013-09-18T17:40:55Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Code Analysis Tools */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules), however,  should probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Concatenated strings calls a method to get executed. So, it affects the performance as it is one of the most frequently used operation. Its better to replace them with Interpolated strings which runs faster as it doesn't invoke a method call.&lt;br /&gt;
put “Hello there, #{name}!”vs.puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that which modifies the actual value than working on a copy of the data are much faster. But this should be handled carefully as original data gets disturbed.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: .each uses an enumeration object behind the scene which adds a delay in execution. Using for instead of .each would improve performance but for short loops only.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both these calls are very expensive. using regular expressions would improve the time.&lt;br /&gt;
&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: All libraries are not efficient. Many gems may need to be removed to problem to fix a problem with the code. This is a common scenario when many gems are installed. So performing bench marking and testing a gem with others that perform same task would help in picking the right gem.&lt;br /&gt;
&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements results in evident improvements in performance of the code. It is always ideal to design an efficient algorithm without unwanted method calls.&lt;br /&gt;
&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: While using if statements or a case statement always test the cases that occur most frequently. This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: While accessing the global constants one should use namespace before the constants to avoid entire library search for the constant.&lt;br /&gt;
 &lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Even though the result of last operation is returned for a method, using explicit returns would speed up the code. Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools&amp;lt;ref&amp;gt;http://www.infoq.com/news/2009/09/code-quality-metric-fu&amp;lt;/ref&amp;gt; can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77947</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77947"/>
		<updated>2013-09-18T17:38:07Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Documentation Guidelines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules), however,  should probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Concatenated strings calls a method to get executed. So, it affects the performance as it is one of the most frequently used operation. Its better to replace them with Interpolated strings which runs faster as it doesn't invoke a method call.&lt;br /&gt;
put “Hello there, #{name}!”vs.puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that which modifies the actual value than working on a copy of the data are much faster. But this should be handled carefully as original data gets disturbed.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: .each uses an enumeration object behind the scene which adds a delay in execution. Using for instead of .each would improve performance but for short loops only.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both these calls are very expensive. using regular expressions would improve the time.&lt;br /&gt;
&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: All libraries are not efficient. Many gems may need to be removed to problem to fix a problem with the code. This is a common scenario when many gems are installed. So performing bench marking and testing a gem with others that perform same task would help in picking the right gem.&lt;br /&gt;
&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements results in evident improvements in performance of the code. It is always ideal to design an efficient algorithm without unwanted method calls.&lt;br /&gt;
&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: While using if statements or a case statement always test the cases that occur most frequently. This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: While accessing the global constants one should use namespace before the constants to avoid entire library search for the constant.&lt;br /&gt;
 &lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Even though the result of last operation is returned for a method, using explicit returns would speed up the code. Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&amp;lt;ref&amp;gt;http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77946</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77946"/>
		<updated>2013-09-18T17:36:52Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Performance Guidelines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules), however,  should probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Concatenated strings calls a method to get executed. So, it affects the performance as it is one of the most frequently used operation. Its better to replace them with Interpolated strings which runs faster as it doesn't invoke a method call.&lt;br /&gt;
put “Hello there, #{name}!”vs.puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that which modifies the actual value than working on a copy of the data are much faster. But this should be handled carefully as original data gets disturbed.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: .each uses an enumeration object behind the scene which adds a delay in execution. Using for instead of .each would improve performance but for short loops only.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both these calls are very expensive. using regular expressions would improve the time.&lt;br /&gt;
&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: All libraries are not efficient. Many gems may need to be removed to problem to fix a problem with the code. This is a common scenario when many gems are installed. So performing bench marking and testing a gem with others that perform same task would help in picking the right gem.&lt;br /&gt;
&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements results in evident improvements in performance of the code. It is always ideal to design an efficient algorithm without unwanted method calls.&lt;br /&gt;
&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: While using if statements or a case statement always test the cases that occur most frequently. This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: While accessing the global constants one should use namespace before the constants to avoid entire library search for the constant.&lt;br /&gt;
 &lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Even though the result of last operation is returned for a method, using explicit returns would speed up the code. Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77945</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77945"/>
		<updated>2013-09-18T17:36:18Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Performance Guidelines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules), however,  should probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Concatenated strings calls a method to get executed. So, it affects the performance as it is one of the most frequently used operation. Its better to replace them with Interpolated strings which runs faster as it doesn't invoke a method call.&lt;br /&gt;
put “Hello there, #{name}!”vs.puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that which modifies the actual value than working on a copy of the data are much faster. But this should be handled carefully as original data gets disturbed.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: .each uses an enumeration object behind the scene which adds a delay in execution. Using for instead of .each would improve performance but for short loops only.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&amp;lt;ref/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both these calls are very expensive. using regular expressions would improve the time.&lt;br /&gt;
&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: All libraries are not efficient. Many gems may need to be removed to problem to fix a problem with the code. This is a common scenario when many gems are installed. So performing bench marking and testing a gem with others that perform same task would help in picking the right gem.&lt;br /&gt;
&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements results in evident improvements in performance of the code. It is always ideal to design an efficient algorithm without unwanted method calls.&lt;br /&gt;
&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: While using if statements or a case statement always test the cases that occur most frequently. This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: While accessing the global constants one should use namespace before the constants to avoid entire library search for the constant.&lt;br /&gt;
 &lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Even though the result of last operation is returned for a method, using explicit returns would speed up the code. Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77943</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77943"/>
		<updated>2013-09-18T17:35:10Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules), however,  should probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Concatenated strings calls a method to get executed. So, it affects the performance as it is one of the most frequently used operation. Its better to replace them with Interpolated strings which runs faster as it doesn't invoke a method call.&lt;br /&gt;
put “Hello there, #{name}!”vs.puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that which modifies the actual value than working on a copy of the data are much faster. But this should be handled carefully as original data gets disturbed.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: .each uses an enumeration object behind the scene which adds a delay in execution. Using for instead of .each would improve performance but for short loops only.&lt;br /&gt;
&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both these calls are very expensive. using regular expressions would improve the time.&lt;br /&gt;
&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: All libraries are not efficient. Many gems may need to be removed to problem to fix a problem with the code. This is a common scenario when many gems are installed. So performing bench marking and testing a gem with others that perform same task would help in picking the right gem.&lt;br /&gt;
&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements results in evident improvements in performance of the code. It is always ideal to design an efficient algorithm without unwanted method calls.&lt;br /&gt;
&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: While using if statements or a case statement always test the cases that occur most frequently. This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: While accessing the global constants one should use namespace before the constants to avoid entire library search for the constant.&lt;br /&gt;
 &lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Even though the result of last operation is returned for a method, using explicit returns would speed up the code. Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77215</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77215"/>
		<updated>2013-09-17T23:21:34Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Performance Guidelines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules), however,  should probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Concatenated strings calls a method to get executed. So, it affects the performance as it is one of the most frequently used operation. Its better to replace them with Interpolated strings which runs faster as it doesn't invoke a method call.&lt;br /&gt;
put “Hello there, #{name}!”vs.puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that which modifies the actual value than working on a copy of the data are much faster. But this should be handled carefully as original data gets disturbed.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: .each uses an enumeration object behind the scene which adds a delay in execution. Using for instead of .each would improve performance but for short loops only.&lt;br /&gt;
&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both these calls are very expensive. using regular expressions would improve the time.&lt;br /&gt;
&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: All libraries are not efficient. Many gems may need to be removed to problem to fix a problem with the code. This is a common scenario when many gems are installed. So performing bench marking and testing a gem with others that perform same task would help in picking the right gem.&lt;br /&gt;
&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements results in evident improvements in performance of the code. It is always ideal to design an efficient algorithm without unwanted method calls.&lt;br /&gt;
&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: While using if statements or a case statement always test the cases that occur most frequently. This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: While accessing the global constants one should use namespace before the constants to avoid entire library search for the constant.&lt;br /&gt;
 &lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Even though the result of last operation is returned for a method, using explicit returns would speed up the code. Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
*http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&lt;br /&gt;
*http://www.infoq.com/news/2009/09/code-quality-metric-fu&lt;br /&gt;
*http://itsignals.cascadia.com.au/?p=7&lt;br /&gt;
*http://guides.rubyonrails.org/api_documentation_guidelines.html&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
*http://en.wikipedia.org/wiki/Ruby_(programming_language)&lt;br /&gt;
*http://graidengarrison.weebly.com/1/post/2013/03/the-importance-of-coding-standards-in-net-development.html&lt;br /&gt;
*http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77127</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77127"/>
		<updated>2013-09-17T21:55:19Z</updated>

		<summary type="html">&lt;p&gt;Hsure: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules), however,  should probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Interpolated strings are faster than concatenated strings in almost all interpreted languages; Ruby is no exception.  Using the &amp;lt;&amp;lt; operator makes a method call which and method calls should be avoided when possible.&lt;br /&gt;
put “Hello there, #{name}!”&lt;br /&gt;
vs.&lt;br /&gt;
puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that modify the actual value instead of working on a copy of it are much faster, but be careful as they sometimes behave strangely (i.e., for!)&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: When you use .each you encounter per-request execution; for loops avoid this expensive operation.&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both of these are very expensive operations.  Use regular expressions when parsing out date/time components.&lt;br /&gt;
*'''Don’t use unnecessary block parameters'''&lt;br /&gt;
: If you won’t be using the parameter in the block don’t specify it in the parameter list.  Go through your code and ensure that any parameters declared are used or removed.&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: Not all libraries are created with performance in mind.  Many gems are slapped together to solve a particular problem that author was having.  Before you introduce a new gem into your performance-oriented production codebase be sure to perform thorough benchmarking and testing against other gems that perform the same tasks.&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements are almost always going to have more of an impact on the performance of your code than tweaks to the way your code is written will.  Make sure your algorithm is designed to be efficient and has no extraneous methods or calls.  Also, test for most frequent cases first and exit loops as soon as possible.&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: When using if statements or a case statement always test the cases in the order that they occur most frequently.  This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: Be sure to precede a global constant with it’s namespace and the double colon operator (Namespace::constant_name) to reduce the time needed to query the library.&lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Although Ruby will automatically return the result of the last completed operation if no return value is provided you should use explicit return values.  Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
*http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&lt;br /&gt;
*http://www.infoq.com/news/2009/09/code-quality-metric-fu&lt;br /&gt;
*http://itsignals.cascadia.com.au/?p=7&lt;br /&gt;
*http://guides.rubyonrails.org/api_documentation_guidelines.html&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
*http://en.wikipedia.org/wiki/Ruby_(programming_language)&lt;br /&gt;
*http://graidengarrison.weebly.com/1/post/2013/03/the-importance-of-coding-standards-in-net-development.html&lt;br /&gt;
*http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77124</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77124"/>
		<updated>2013-09-17T21:51:09Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Performance Guidelines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules), however,  should probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby.&amp;lt;ref&amp;gt;http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&amp;lt;/ref&amp;gt; They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Interpolated strings are faster than concatenated strings in almost all interpreted languages; Ruby is no exception.  Using the &amp;lt;&amp;lt; operator makes a method call which and method calls should be avoided when possible.&lt;br /&gt;
put “Hello there, #{name}!”&lt;br /&gt;
vs.&lt;br /&gt;
puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that modify the actual value instead of working on a copy of it are much faster, but be careful as they sometimes behave strangely (i.e., for!)&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: When you use .each you encounter per-request execution; for loops avoid this expensive operation.&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both of these are very expensive operations.  Use regular expressions when parsing out date/time components.&lt;br /&gt;
*'''Don’t use unnecessary block parameters'''&lt;br /&gt;
: If you won’t be using the parameter in the block don’t specify it in the parameter list.  Go through your code and ensure that any parameters declared are used or removed.&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: Not all libraries are created with performance in mind.  Many gems are slapped together to solve a particular problem that author was having.  Before you introduce a new gem into your performance-oriented production codebase be sure to perform thorough benchmarking and testing against other gems that perform the same tasks.&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements are almost always going to have more of an impact on the performance of your code than tweaks to the way your code is written will.  Make sure your algorithm is designed to be efficient and has no extraneous methods or calls.  Also, test for most frequent cases first and exit loops as soon as possible.&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: When using if statements or a case statement always test the cases in the order that they occur most frequently.  This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: Be sure to precede a global constant with it’s namespace and the double colon operator (Namespace::constant_name) to reduce the time needed to query the library.&lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Although Ruby will automatically return the result of the last completed operation if no return value is provided you should use explicit return values.  Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
*http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&lt;br /&gt;
*http://www.infoq.com/news/2009/09/code-quality-metric-fu&lt;br /&gt;
*http://itsignals.cascadia.com.au/?p=7&lt;br /&gt;
*http://guides.rubyonrails.org/api_documentation_guidelines.html&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
*http://en.wikipedia.org/wiki/Ruby_(programming_language)&lt;br /&gt;
*http://graidengarrison.weebly.com/1/post/2013/03/the-importance-of-coding-standards-in-net-development.html&lt;br /&gt;
*http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77110</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77110"/>
		<updated>2013-09-17T21:45:11Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules), however,  should probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Interpolated strings are faster than concatenated strings in almost all interpreted languages; Ruby is no exception.  Using the &amp;lt;&amp;lt; operator makes a method call which and method calls should be avoided when possible.&lt;br /&gt;
put “Hello there, #{name}!”&lt;br /&gt;
vs.&lt;br /&gt;
puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that modify the actual value instead of working on a copy of it are much faster, but be careful as they sometimes behave strangely (i.e., for!)&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: When you use .each you encounter per-request execution; for loops avoid this expensive operation.&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both of these are very expensive operations.  Use regular expressions when parsing out date/time components.&lt;br /&gt;
*'''Don’t use unnecessary block parameters'''&lt;br /&gt;
: If you won’t be using the parameter in the block don’t specify it in the parameter list.  Go through your code and ensure that any parameters declared are used or removed.&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: Not all libraries are created with performance in mind.  Many gems are slapped together to solve a particular problem that author was having.  Before you introduce a new gem into your performance-oriented production codebase be sure to perform thorough benchmarking and testing against other gems that perform the same tasks.&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements are almost always going to have more of an impact on the performance of your code than tweaks to the way your code is written will.  Make sure your algorithm is designed to be efficient and has no extraneous methods or calls.  Also, test for most frequent cases first and exit loops as soon as possible.&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: When using if statements or a case statement always test the cases in the order that they occur most frequently.  This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: Be sure to precede a global constant with it’s namespace and the double colon operator (Namespace::constant_name) to reduce the time needed to query the library.&lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Although Ruby will automatically return the result of the last completed operation if no return value is provided you should use explicit return values.  Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
*http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&lt;br /&gt;
*http://www.infoq.com/news/2009/09/code-quality-metric-fu&lt;br /&gt;
*http://itsignals.cascadia.com.au/?p=7&lt;br /&gt;
*http://guides.rubyonrails.org/api_documentation_guidelines.html&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
*http://en.wikipedia.org/wiki/Ruby_(programming_language)&lt;br /&gt;
*http://graidengarrison.weebly.com/1/post/2013/03/the-importance-of-coding-standards-in-net-development.html&lt;br /&gt;
*http://stackoverflow.com/questions/6406112/why-are-ruby-method-calls-particularly-slow-in-comparison-to-other-languages&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77109</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77109"/>
		<updated>2013-09-17T21:44:39Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Performance Guidelines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules), however,  should probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are expensive operations in ruby. They should be avoided to keep up the performance of the application.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Interpolated strings are faster than concatenated strings in almost all interpreted languages; Ruby is no exception.  Using the &amp;lt;&amp;lt; operator makes a method call which and method calls should be avoided when possible.&lt;br /&gt;
put “Hello there, #{name}!”&lt;br /&gt;
vs.&lt;br /&gt;
puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that modify the actual value instead of working on a copy of it are much faster, but be careful as they sometimes behave strangely (i.e., for!)&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: When you use .each you encounter per-request execution; for loops avoid this expensive operation.&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both of these are very expensive operations.  Use regular expressions when parsing out date/time components.&lt;br /&gt;
*'''Don’t use unnecessary block parameters'''&lt;br /&gt;
: If you won’t be using the parameter in the block don’t specify it in the parameter list.  Go through your code and ensure that any parameters declared are used or removed.&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: Not all libraries are created with performance in mind.  Many gems are slapped together to solve a particular problem that author was having.  Before you introduce a new gem into your performance-oriented production codebase be sure to perform thorough benchmarking and testing against other gems that perform the same tasks.&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements are almost always going to have more of an impact on the performance of your code than tweaks to the way your code is written will.  Make sure your algorithm is designed to be efficient and has no extraneous methods or calls.  Also, test for most frequent cases first and exit loops as soon as possible.&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: When using if statements or a case statement always test the cases in the order that they occur most frequently.  This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: Be sure to precede a global constant with it’s namespace and the double colon operator (Namespace::constant_name) to reduce the time needed to query the library.&lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Although Ruby will automatically return the result of the last completed operation if no return value is provided you should use explicit return values.  Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
*http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&lt;br /&gt;
*http://www.infoq.com/news/2009/09/code-quality-metric-fu&lt;br /&gt;
*http://itsignals.cascadia.com.au/?p=7&lt;br /&gt;
*http://guides.rubyonrails.org/api_documentation_guidelines.html&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
*http://en.wikipedia.org/wiki/Ruby_(programming_language)&lt;br /&gt;
*http://graidengarrison.weebly.com/1/post/2013/03/the-importance-of-coding-standards-in-net-development.html&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77103</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77103"/>
		<updated>2013-09-17T21:38:22Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Performance Guidelines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules), however,  should probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting affects the performance of the code proportionally with the increasing levels in loops. Limiting nesting to three levels is one good practice to keep the code's performance well.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, often use unwanted variables in code.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is a very costly operation as far as computing is concerned. Keeping it to the minimal improves the application response time drastically. Using disk I/O, makes the application extremely slow. Using storage systems such as memcached reduces disk I/O operations to a great extent as lot of data is kept in memory. The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, to get benefited by this performance one needs to follow their guidelines.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are very expensive operations and should be avoided when necessary.&lt;br /&gt;
&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Interpolated strings are faster than concatenated strings in almost all interpreted languages; Ruby is no exception.  Using the &amp;lt;&amp;lt; operator makes a method call which and method calls should be avoided when possible.&lt;br /&gt;
put “Hello there, #{name}!”&lt;br /&gt;
vs.&lt;br /&gt;
puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that modify the actual value instead of working on a copy of it are much faster, but be careful as they sometimes behave strangely (i.e., for!)&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: When you use .each you encounter per-request execution; for loops avoid this expensive operation.&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both of these are very expensive operations.  Use regular expressions when parsing out date/time components.&lt;br /&gt;
*'''Don’t use unnecessary block parameters'''&lt;br /&gt;
: If you won’t be using the parameter in the block don’t specify it in the parameter list.  Go through your code and ensure that any parameters declared are used or removed.&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: Not all libraries are created with performance in mind.  Many gems are slapped together to solve a particular problem that author was having.  Before you introduce a new gem into your performance-oriented production codebase be sure to perform thorough benchmarking and testing against other gems that perform the same tasks.&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements are almost always going to have more of an impact on the performance of your code than tweaks to the way your code is written will.  Make sure your algorithm is designed to be efficient and has no extraneous methods or calls.  Also, test for most frequent cases first and exit loops as soon as possible.&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: When using if statements or a case statement always test the cases in the order that they occur most frequently.  This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: Be sure to precede a global constant with it’s namespace and the double colon operator (Namespace::constant_name) to reduce the time needed to query the library.&lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Although Ruby will automatically return the result of the last completed operation if no return value is provided you should use explicit return values.  Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
*http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&lt;br /&gt;
*http://www.infoq.com/news/2009/09/code-quality-metric-fu&lt;br /&gt;
*http://itsignals.cascadia.com.au/?p=7&lt;br /&gt;
*http://guides.rubyonrails.org/api_documentation_guidelines.html&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
*http://en.wikipedia.org/wiki/Ruby_(programming_language)&lt;br /&gt;
*http://graidengarrison.weebly.com/1/post/2013/03/the-importance-of-coding-standards-in-net-development.html&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77077</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=77077"/>
		<updated>2013-09-17T21:21:50Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Performance Guidelines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Should use lowercase letter followed by other characters. Naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. average, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should succeed @, e.g. @color &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. The name should possibly be a verb e.g. move, display_details&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@color&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names usually start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names are recommended to be be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules), however,  should probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. Class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
Ruby blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies. These strategies can be used to design classes accordingly as suitable for different types of  applications.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
The key parameter on which a software application is rated is by its performance. It is the time taken by the application to respond to a user's request. Performance of ruby can be improved significantly by following certain coding guidelines, as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting not only slows your code down but also can make maintenance of the codebase difficult if it goes too many levels deep.  Limiting nesting of loops and functions to three levels or less is a good rule of thumb to keep your code performant.&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, tend to assign variables more than necessary.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is one of the biggest bottlenecks remaining in computing.  Read/write operations to disk are extremely slow and it’s best to avoid using the disk whenever possible.  Many people are now using software such as memcached which allows data to be stored in memory and only periodically written to disk.  The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, in order to take advantage of these performance gains you must be sure to program according to their guidelines.&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are very expensive operations and should be avoided when necessary.&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Interpolated strings are faster than concatenated strings in almost all interpreted languages; Ruby is no exception.  Using the &amp;lt;&amp;lt; operator makes a method call which and method calls should be avoided when possible.&lt;br /&gt;
put “Hello there, #{name}!”&lt;br /&gt;
vs.&lt;br /&gt;
puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that modify the actual value instead of working on a copy of it are much faster, but be careful as they sometimes behave strangely (i.e., for!)&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: When you use .each you encounter per-request execution; for loops avoid this expensive operation.&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both of these are very expensive operations.  Use regular expressions when parsing out date/time components.&lt;br /&gt;
*'''Don’t use unnecessary block parameters'''&lt;br /&gt;
: If you won’t be using the parameter in the block don’t specify it in the parameter list.  Go through your code and ensure that any parameters declared are used or removed.&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: Not all libraries are created with performance in mind.  Many gems are slapped together to solve a particular problem that author was having.  Before you introduce a new gem into your performance-oriented production codebase be sure to perform thorough benchmarking and testing against other gems that perform the same tasks.&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements are almost always going to have more of an impact on the performance of your code than tweaks to the way your code is written will.  Make sure your algorithm is designed to be efficient and has no extraneous methods or calls.  Also, test for most frequent cases first and exit loops as soon as possible.&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: When using if statements or a case statement always test the cases in the order that they occur most frequently.  This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: Be sure to precede a global constant with it’s namespace and the double colon operator (Namespace::constant_name) to reduce the time needed to query the library.&lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Although Ruby will automatically return the result of the last completed operation if no return value is provided you should use explicit return values.  Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Documentation comments can be created in accordance with [http://rdoc.sourceforge.net/ RDoc] and [http://yardoc.org/ YARD] syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything meaningfully.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== '''Code Analysis Tools''' ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping developers write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual things one would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to increase. Automatic tools can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses Ruby code and warns about design issues from the list  configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of code written: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyzer, this can be used for duplication identification (a $99 license is needed for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== '''Summary''' ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== '''See Also''' ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
&lt;br /&gt;
== '''References''' ==&lt;br /&gt;
*http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&lt;br /&gt;
*http://www.infoq.com/news/2009/09/code-quality-metric-fu&lt;br /&gt;
*http://itsignals.cascadia.com.au/?p=7&lt;br /&gt;
*http://guides.rubyonrails.org/api_documentation_guidelines.html&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
*http://en.wikipedia.org/wiki/Ruby_(programming_language)&lt;br /&gt;
*http://graidengarrison.weebly.com/1/post/2013/03/the-importance-of-coding-standards-in-net-development.html&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=76588</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=76588"/>
		<updated>2013-09-17T02:27:57Z</updated>

		<summary type="html">&lt;p&gt;Hsure: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== '''Ruby Coding Guidelines''' ==&lt;br /&gt;
&lt;br /&gt;
Designed and developed in the mid-1990s by Yukihiro &amp;quot;Matz&amp;quot; Matsumoto in Japan, Ruby embodies syntax inspired by Perl with Smalltalk-like features and was also influenced by Eiffel and Lisp. Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== '''Types of Guidelines''' ==&lt;br /&gt;
Coding guidelines in Ruby can be defined for individually for major sections that code consists of. Naming , Class Design , Member Design , Maintainability, Performance, Documentation and Layout guidelines are illustrated below. These guidelines contain sections common to all of these as well as sections which apply to each one individually.&lt;br /&gt;
&lt;br /&gt;
=== Naming Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*'''Local Variables'''&lt;br /&gt;
:Lowercase letter followed by other characters, naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. mileage, variable_xyz&lt;br /&gt;
*'''Instance Variables'''&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should be used after the @, e.g. @colour &lt;br /&gt;
*'''Instance Methods'''&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. If possible, the name should be a verb e.g. paint, close_the_door&lt;br /&gt;
*'''Class Variables'''&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@colour&lt;br /&gt;
*'''Constant''' &lt;br /&gt;
:Constant names start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*'''Class and Module''' &lt;br /&gt;
:Class names should be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules) should, however, probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*'''Global Variables'''&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*'''Model Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Controller Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''View Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*'''Tests Naming Convention'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
To implement object-oriented programming by using Ruby, one needs to first learn how to create objects and classes in Ruby.&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. The class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
One of the interesting things about Ruby is the way it blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* '''Profile your code regularly'''&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Computer software performance, particularly software application response time, is an aspect of software quality that is important in human–computer interactions. Performance of Ruby based applications can be regulated by following certain coding guidelines, such as below.&lt;br /&gt;
&lt;br /&gt;
*'''Avoid nesting loops more than three levels deep'''&lt;br /&gt;
: Nesting not only slows your code down but also can make maintenance of the codebase difficult if it goes too many levels deep.  Limiting nesting of loops and functions to three levels or less is a good rule of thumb to keep your code performant.&lt;br /&gt;
*'''Avoid unnecessary variable assignments'''&lt;br /&gt;
: New programmers, tend to assign variables more than necessary.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
*'''Reduce usage of disk I/O'''&lt;br /&gt;
: Disk I/O is one of the biggest bottlenecks remaining in computing.  Read/write operations to disk are extremely slow and it’s best to avoid using the disk whenever possible.  Many people are now using software such as memcached which allows data to be stored in memory and only periodically written to disk.  The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
*'''Use Ruby Enterprise Edition'''&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, in order to take advantage of these performance gains you must be sure to program according to their guidelines.&lt;br /&gt;
*'''Avoid method calls as much as possible'''&lt;br /&gt;
: Method calls are very expensive operations and should be avoided when necessary.&lt;br /&gt;
*'''Use interpolated strings instead of concatenated strings'''&lt;br /&gt;
: Interpolated strings are faster than concatenated strings in almost all interpreted languages; Ruby is no exception.  Using the &amp;lt;&amp;lt; operator makes a method call which and method calls should be avoided when possible.&lt;br /&gt;
put “Hello there, #{name}!”&lt;br /&gt;
vs.&lt;br /&gt;
puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
*'''Destructive operations are faster'''&lt;br /&gt;
: Ruby’s in-place methods that modify the actual value instead of working on a copy of it are much faster, but be careful as they sometimes behave strangely (i.e., for!)&lt;br /&gt;
*'''Avoid unnecessary calls to uniq on arrays'''&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
*'''For loops are faster than .each'''&lt;br /&gt;
: When you use .each you encounter per-request execution; for loops avoid this expensive operation.&lt;br /&gt;
*'''Use x.blank? over x.nil? || x.empty?'''&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
*'''Avoid calls to parse_date and strftime'''&lt;br /&gt;
: Both of these are very expensive operations.  Use regular expressions when parsing out date/time components.&lt;br /&gt;
*'''Don’t use unnecessary block parameters'''&lt;br /&gt;
: If you won’t be using the parameter in the block don’t specify it in the parameter list.  Go through your code and ensure that any parameters declared are used or removed.&lt;br /&gt;
*'''Know your gems'''&lt;br /&gt;
: Not all libraries are created with performance in mind.  Many gems are slapped together to solve a particular problem that author was having.  Before you introduce a new gem into your performance-oriented production codebase be sure to perform thorough benchmarking and testing against other gems that perform the same tasks.&lt;br /&gt;
*'''Improve your algorithms before you try to improve your code'''&lt;br /&gt;
: Algorithmic improvements are almost always going to have more of an impact on the performance of your code than tweaks to the way your code is written will.  Make sure your algorithm is designed to be efficient and has no extraneous methods or calls.  Also, test for most frequent cases first and exit loops as soon as possible.&lt;br /&gt;
*'''Test the most frequently occurring case first'''&lt;br /&gt;
: When using if statements or a case statement always test the cases in the order that they occur most frequently.  This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
*'''Optimize the way you access global constants'''&lt;br /&gt;
: Be sure to precede a global constant with it’s namespace and the double colon operator (Namespace::constant_name) to reduce the time needed to query the library.&lt;br /&gt;
*'''Use explicit returns'''&lt;br /&gt;
: Although Ruby will automatically return the result of the last completed operation if no return value is provided you should use explicit return values.  Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
In languages files, RubyMine creates stubs of documentation comments on typing the opening tag and pressing Enter.&lt;br /&gt;
&lt;br /&gt;
Documentation comments can be created in accordance with RDoc and YARD syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
&lt;br /&gt;
When you create additional tags, RubyMine provides code completion that suggests the possible tag names. &lt;br /&gt;
&lt;br /&gt;
The most common Documentation guidelines are listed below.&lt;br /&gt;
*Write simple, declarative sentences. Brevity is a plus: get to the point.&lt;br /&gt;
*Write in present tense: &amp;quot;Returns a hash that...&amp;quot;, rather than &amp;quot;Returned a hash that...&amp;quot; or &amp;quot;Will return a hash that...&amp;quot;.&lt;br /&gt;
*Start comments in upper case. Follow regular punctuation rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Declares an attribute reader backed by an internally-named &lt;br /&gt;
# instance variable.&lt;br /&gt;
def attr_internal_reader(*attrs)&lt;br /&gt;
  ...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage.&lt;br /&gt;
&lt;br /&gt;
Documentation has to be concise but comprehensive. Explore and document edge cases.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*'''Spreading Code Out and Lining it Up'''&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything in a meaningful way.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalizing and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== Code Analysis Tools ==&lt;br /&gt;
&lt;br /&gt;
Ruby itself goes a long way towards helping one write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugger can do all the usual sorts of things you would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to rise. Automatic tools can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses your Ruby code and warns you about design issues from the list you configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of your code: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyser, this can be used for duplication identification (you need a $99 license for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:Code Analysis Outputs.png|frame|none|alt=Code Analysis Tool Results|Code Analysis Tool Results]]&lt;br /&gt;
&lt;br /&gt;
== Summary ==&lt;br /&gt;
Ruby developers should follow a certain criteria or guidelines during software development. Coding standards are set of rules, guidelines and regulations on the manner of writing a code that helps programmers and developers read and understand quickly the source code that conforms to style and help avoid introducing misunderstanding and faults.&lt;br /&gt;
&lt;br /&gt;
Particularly in Ruby development, coding standards are extremely important; therefore Ruby developers should put an importance to them. This is because these standards offer higher uniformity and consistency when writing code by different programmers. This could result in a code that's simple to know and preserve, thus reducing the project’s overall expenses.&lt;br /&gt;
&lt;br /&gt;
Some of the benefits of using coding standards are:&lt;br /&gt;
&lt;br /&gt;
*Easy to understand and maintained&lt;br /&gt;
*Boost the code’s readability&lt;br /&gt;
*Maintainable applications&lt;br /&gt;
*Eradicates complexity&lt;br /&gt;
*Separate documents look more appropriate&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
*http://www.jetbrains.com/ruby/webhelp/documenting-source-code-in-rubymine.html&lt;br /&gt;
*http://www.infoq.com/news/2009/09/code-quality-metric-fu&lt;br /&gt;
*http://itsignals.cascadia.com.au/?p=7&lt;br /&gt;
*http://guides.rubyonrails.org/api_documentation_guidelines.html&lt;br /&gt;
*http://www.caliban.org/ruby/rubyguide.shtml&lt;br /&gt;
*http://en.wikipedia.org/wiki/Ruby_(programming_language)&lt;br /&gt;
*http://graidengarrison.weebly.com/1/post/2013/03/the-importance-of-coding-standards-in-net-development.html&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=76286</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=76286"/>
		<updated>2013-09-16T23:27:18Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Importance of guidelines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Ruby Coding Guidelines'''&lt;br /&gt;
&lt;br /&gt;
Designed and developed in the mid-1990s by Yukihiro &amp;quot;Matz&amp;quot; Matsumoto in Japan, Ruby embodies syntax inspired by Perl with Smalltalk-like features and was also influenced by Eiffel and Lisp. Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Types of Guidelines == &lt;br /&gt;
=== Naming Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*Local Variables&lt;br /&gt;
:Lowercase letter followed by other characters, naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. mileage, variable_xyz&lt;br /&gt;
*Instance Variables&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should be used after the @, e.g. @colour &lt;br /&gt;
*Instance Methods&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. If possible, the name should be a verb e.g. paint, close_the_door&lt;br /&gt;
*Class Variables&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@colour&lt;br /&gt;
*Constant &lt;br /&gt;
:Constant names start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*Class and Module &lt;br /&gt;
:Class names should be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules) should, however, probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*Global Variables&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*Model Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Controller Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*View Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Tests Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
To implement object-oriented programming by using Ruby, one needs to first learn how to create objects and classes in Ruby.&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. The class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
One of the interesting things about Ruby is the way it blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.  &amp;lt;ref&amp;gt;Robert L. Glass: Facts and Fallacies of Software Engineering; Addison Wesley, 2003. &amp;lt;/ref&amp;gt;&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* Profile your code regularly&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Computer software performance, particularly software application response time, is an aspect of software quality that is important in human–computer interactions. Performance of Ruby based applications can be regulated by following certain coding guidelines, such as below.&lt;br /&gt;
&lt;br /&gt;
*Avoid nesting loops more than three levels deep&lt;br /&gt;
: Nesting not only slows your code down but also can make maintenance of the codebase difficult if it goes too many levels deep.  Limiting nesting of loops and functions to three levels or less is a good rule of thumb to keep your code performant.&lt;br /&gt;
*Avoid unnecessary variable assignments&lt;br /&gt;
: New programmers, tend to assign variables more than necessary.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
*Reduce usage of disk I/O&lt;br /&gt;
: Disk I/O is one of the biggest bottlenecks remaining in computing.  Read/write operations to disk are extremely slow and it’s best to avoid using the disk whenever possible.  Many people are now using software such as memcached which allows data to be stored in memory and only periodically written to disk.  The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
*Use Ruby Enterprise Edition&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, in order to take advantage of these performance gains you must be sure to program according to their guidelines.&lt;br /&gt;
*Avoid method calls as much as possible&lt;br /&gt;
: Method calls are very expensive operations and should be avoided when necessary.&lt;br /&gt;
*Use interpolated strings instead of concatenated strings&lt;br /&gt;
: Interpolated strings are faster than concatenated strings in almost all interpreted languages; Ruby is no exception.  Using the &amp;lt;&amp;lt; operator makes a method call which and method calls should be avoided when possible.&lt;br /&gt;
put “Hello there, #{name}!”&lt;br /&gt;
vs.&lt;br /&gt;
puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
*Destructive operations are faster&lt;br /&gt;
: Ruby’s in-place methods that modify the actual value instead of working on a copy of it are much faster, but be careful as they sometimes behave strangely (i.e., for!)&lt;br /&gt;
*Avoid unnecessary calls to uniq on arrays&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
*For loops are faster than .each&lt;br /&gt;
: When you use .each you encounter per-request execution; for loops avoid this expensive operation.&lt;br /&gt;
*Use x.blank? over x.nil? || x.empty?&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
*Avoid calls to parse_date and strftime&lt;br /&gt;
: Both of these are very expensive operations.  Use regular expressions when parsing out date/time components.&lt;br /&gt;
*Don’t use unnecessary block parameters&lt;br /&gt;
: If you won’t be using the parameter in the block don’t specify it in the parameter list.  Go through your code and ensure that any parameters declared are used or removed.&lt;br /&gt;
*Know your gems&lt;br /&gt;
: Not all libraries are created with performance in mind.  Many gems are slapped together to solve a particular problem that author was having.  Before you introduce a new gem into your performance-oriented production codebase be sure to perform thorough benchmarking and testing against other gems that perform the same tasks.&lt;br /&gt;
*Improve your algorithms before you try to improve your code&lt;br /&gt;
: Algorithmic improvements are almost always going to have more of an impact on the performance of your code than tweaks to the way your code is written will.  Make sure your algorithm is designed to be efficient and has no extraneous methods or calls.  Also, test for most frequent cases first and exit loops as soon as possible.&lt;br /&gt;
*Test the most frequently occurring case first&lt;br /&gt;
: When using if statements or a case statement always test the cases in the order that they occur most frequently.  This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
*Optimize the way you access global constants&lt;br /&gt;
: Be sure to precede a global constant with it’s namespace and the double colon operator (Namespace::constant_name) to reduce the time needed to query the library.&lt;br /&gt;
*Use explicit returns&lt;br /&gt;
: Although Ruby will automatically return the result of the last completed operation if no return value is provided you should use explicit return values.  Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
In languages files, RubyMine creates stubs of documentation comments on typing the opening tag and pressing Enter.&lt;br /&gt;
&lt;br /&gt;
Documentation comments can be created in accordance with RDoc and YARD syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
&lt;br /&gt;
When you create additional tags, RubyMine provides code completion that suggests the possible tag names.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Spreading Code Out and Lining it Up&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything in a meaningful way.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalising and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== Code Analysis Tools ==&lt;br /&gt;
Ruby itself goes a long way towards helping one write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The debugger can do all the usual sorts of things you would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to rise. Automatic tools can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses your Ruby code and warns you about design issues from the list you configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of your code: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyser, this can be used for duplication identification (you need a $99 license for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
== Summary ==&lt;br /&gt;
== See Also ==&lt;br /&gt;
== References ==&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=76285</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=76285"/>
		<updated>2013-09-16T23:26:56Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Ruby Coding Guidelines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Ruby Coding Guidelines'''&lt;br /&gt;
&lt;br /&gt;
Designed and developed in the mid-1990s by Yukihiro &amp;quot;Matz&amp;quot; Matsumoto in Japan, Ruby embodies syntax inspired by Perl with Smalltalk-like features and was also influenced by Eiffel and Lisp. Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Importance of guidelines ==&lt;br /&gt;
&lt;br /&gt;
== Types of Guidelines == &lt;br /&gt;
=== Naming Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*Local Variables&lt;br /&gt;
:Lowercase letter followed by other characters, naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. mileage, variable_xyz&lt;br /&gt;
*Instance Variables&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should be used after the @, e.g. @colour &lt;br /&gt;
*Instance Methods&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. If possible, the name should be a verb e.g. paint, close_the_door&lt;br /&gt;
*Class Variables&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@colour&lt;br /&gt;
*Constant &lt;br /&gt;
:Constant names start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*Class and Module &lt;br /&gt;
:Class names should be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules) should, however, probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*Global Variables&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*Model Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Controller Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*View Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Tests Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
To implement object-oriented programming by using Ruby, one needs to first learn how to create objects and classes in Ruby.&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. The class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
One of the interesting things about Ruby is the way it blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.  &amp;lt;ref&amp;gt;Robert L. Glass: Facts and Fallacies of Software Engineering; Addison Wesley, 2003. &amp;lt;/ref&amp;gt;&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* Profile your code regularly&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Computer software performance, particularly software application response time, is an aspect of software quality that is important in human–computer interactions. Performance of Ruby based applications can be regulated by following certain coding guidelines, such as below.&lt;br /&gt;
&lt;br /&gt;
*Avoid nesting loops more than three levels deep&lt;br /&gt;
: Nesting not only slows your code down but also can make maintenance of the codebase difficult if it goes too many levels deep.  Limiting nesting of loops and functions to three levels or less is a good rule of thumb to keep your code performant.&lt;br /&gt;
*Avoid unnecessary variable assignments&lt;br /&gt;
: New programmers, tend to assign variables more than necessary.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
*Reduce usage of disk I/O&lt;br /&gt;
: Disk I/O is one of the biggest bottlenecks remaining in computing.  Read/write operations to disk are extremely slow and it’s best to avoid using the disk whenever possible.  Many people are now using software such as memcached which allows data to be stored in memory and only periodically written to disk.  The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
*Use Ruby Enterprise Edition&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, in order to take advantage of these performance gains you must be sure to program according to their guidelines.&lt;br /&gt;
*Avoid method calls as much as possible&lt;br /&gt;
: Method calls are very expensive operations and should be avoided when necessary.&lt;br /&gt;
*Use interpolated strings instead of concatenated strings&lt;br /&gt;
: Interpolated strings are faster than concatenated strings in almost all interpreted languages; Ruby is no exception.  Using the &amp;lt;&amp;lt; operator makes a method call which and method calls should be avoided when possible.&lt;br /&gt;
put “Hello there, #{name}!”&lt;br /&gt;
vs.&lt;br /&gt;
puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
*Destructive operations are faster&lt;br /&gt;
: Ruby’s in-place methods that modify the actual value instead of working on a copy of it are much faster, but be careful as they sometimes behave strangely (i.e., for!)&lt;br /&gt;
*Avoid unnecessary calls to uniq on arrays&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
*For loops are faster than .each&lt;br /&gt;
: When you use .each you encounter per-request execution; for loops avoid this expensive operation.&lt;br /&gt;
*Use x.blank? over x.nil? || x.empty?&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
*Avoid calls to parse_date and strftime&lt;br /&gt;
: Both of these are very expensive operations.  Use regular expressions when parsing out date/time components.&lt;br /&gt;
*Don’t use unnecessary block parameters&lt;br /&gt;
: If you won’t be using the parameter in the block don’t specify it in the parameter list.  Go through your code and ensure that any parameters declared are used or removed.&lt;br /&gt;
*Know your gems&lt;br /&gt;
: Not all libraries are created with performance in mind.  Many gems are slapped together to solve a particular problem that author was having.  Before you introduce a new gem into your performance-oriented production codebase be sure to perform thorough benchmarking and testing against other gems that perform the same tasks.&lt;br /&gt;
*Improve your algorithms before you try to improve your code&lt;br /&gt;
: Algorithmic improvements are almost always going to have more of an impact on the performance of your code than tweaks to the way your code is written will.  Make sure your algorithm is designed to be efficient and has no extraneous methods or calls.  Also, test for most frequent cases first and exit loops as soon as possible.&lt;br /&gt;
*Test the most frequently occurring case first&lt;br /&gt;
: When using if statements or a case statement always test the cases in the order that they occur most frequently.  This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
*Optimize the way you access global constants&lt;br /&gt;
: Be sure to precede a global constant with it’s namespace and the double colon operator (Namespace::constant_name) to reduce the time needed to query the library.&lt;br /&gt;
*Use explicit returns&lt;br /&gt;
: Although Ruby will automatically return the result of the last completed operation if no return value is provided you should use explicit return values.  Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
In languages files, RubyMine creates stubs of documentation comments on typing the opening tag and pressing Enter.&lt;br /&gt;
&lt;br /&gt;
Documentation comments can be created in accordance with RDoc and YARD syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
&lt;br /&gt;
When you create additional tags, RubyMine provides code completion that suggests the possible tag names.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Spreading Code Out and Lining it Up&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything in a meaningful way.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalising and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== Code Analysis Tools ==&lt;br /&gt;
Ruby itself goes a long way towards helping one write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The debugger can do all the usual sorts of things you would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to rise. Automatic tools can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses your Ruby code and warns you about design issues from the list you configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of your code: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyser, this can be used for duplication identification (you need a $99 license for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
== Summary ==&lt;br /&gt;
== See Also ==&lt;br /&gt;
== References ==&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=76283</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=76283"/>
		<updated>2013-09-16T23:26:23Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Code Analysis Tools */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Ruby Coding Guidelines =&lt;br /&gt;
&lt;br /&gt;
Designed and developed in the mid-1990s by Yukihiro &amp;quot;Matz&amp;quot; Matsumoto in Japan, Ruby embodies syntax inspired by Perl with Smalltalk-like features and was also influenced by Eiffel and Lisp. Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Importance of guidelines ==&lt;br /&gt;
== Types of Guidelines == &lt;br /&gt;
=== Naming Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*Local Variables&lt;br /&gt;
:Lowercase letter followed by other characters, naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. mileage, variable_xyz&lt;br /&gt;
*Instance Variables&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should be used after the @, e.g. @colour &lt;br /&gt;
*Instance Methods&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. If possible, the name should be a verb e.g. paint, close_the_door&lt;br /&gt;
*Class Variables&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@colour&lt;br /&gt;
*Constant &lt;br /&gt;
:Constant names start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*Class and Module &lt;br /&gt;
:Class names should be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules) should, however, probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*Global Variables&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*Model Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Controller Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*View Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Tests Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
To implement object-oriented programming by using Ruby, one needs to first learn how to create objects and classes in Ruby.&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. The class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
One of the interesting things about Ruby is the way it blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.  &amp;lt;ref&amp;gt;Robert L. Glass: Facts and Fallacies of Software Engineering; Addison Wesley, 2003. &amp;lt;/ref&amp;gt;&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* Profile your code regularly&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Computer software performance, particularly software application response time, is an aspect of software quality that is important in human–computer interactions. Performance of Ruby based applications can be regulated by following certain coding guidelines, such as below.&lt;br /&gt;
&lt;br /&gt;
*Avoid nesting loops more than three levels deep&lt;br /&gt;
: Nesting not only slows your code down but also can make maintenance of the codebase difficult if it goes too many levels deep.  Limiting nesting of loops and functions to three levels or less is a good rule of thumb to keep your code performant.&lt;br /&gt;
*Avoid unnecessary variable assignments&lt;br /&gt;
: New programmers, tend to assign variables more than necessary.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
*Reduce usage of disk I/O&lt;br /&gt;
: Disk I/O is one of the biggest bottlenecks remaining in computing.  Read/write operations to disk are extremely slow and it’s best to avoid using the disk whenever possible.  Many people are now using software such as memcached which allows data to be stored in memory and only periodically written to disk.  The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
*Use Ruby Enterprise Edition&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, in order to take advantage of these performance gains you must be sure to program according to their guidelines.&lt;br /&gt;
*Avoid method calls as much as possible&lt;br /&gt;
: Method calls are very expensive operations and should be avoided when necessary.&lt;br /&gt;
*Use interpolated strings instead of concatenated strings&lt;br /&gt;
: Interpolated strings are faster than concatenated strings in almost all interpreted languages; Ruby is no exception.  Using the &amp;lt;&amp;lt; operator makes a method call which and method calls should be avoided when possible.&lt;br /&gt;
put “Hello there, #{name}!”&lt;br /&gt;
vs.&lt;br /&gt;
puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
*Destructive operations are faster&lt;br /&gt;
: Ruby’s in-place methods that modify the actual value instead of working on a copy of it are much faster, but be careful as they sometimes behave strangely (i.e., for!)&lt;br /&gt;
*Avoid unnecessary calls to uniq on arrays&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
*For loops are faster than .each&lt;br /&gt;
: When you use .each you encounter per-request execution; for loops avoid this expensive operation.&lt;br /&gt;
*Use x.blank? over x.nil? || x.empty?&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
*Avoid calls to parse_date and strftime&lt;br /&gt;
: Both of these are very expensive operations.  Use regular expressions when parsing out date/time components.&lt;br /&gt;
*Don’t use unnecessary block parameters&lt;br /&gt;
: If you won’t be using the parameter in the block don’t specify it in the parameter list.  Go through your code and ensure that any parameters declared are used or removed.&lt;br /&gt;
*Know your gems&lt;br /&gt;
: Not all libraries are created with performance in mind.  Many gems are slapped together to solve a particular problem that author was having.  Before you introduce a new gem into your performance-oriented production codebase be sure to perform thorough benchmarking and testing against other gems that perform the same tasks.&lt;br /&gt;
*Improve your algorithms before you try to improve your code&lt;br /&gt;
: Algorithmic improvements are almost always going to have more of an impact on the performance of your code than tweaks to the way your code is written will.  Make sure your algorithm is designed to be efficient and has no extraneous methods or calls.  Also, test for most frequent cases first and exit loops as soon as possible.&lt;br /&gt;
*Test the most frequently occurring case first&lt;br /&gt;
: When using if statements or a case statement always test the cases in the order that they occur most frequently.  This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
*Optimize the way you access global constants&lt;br /&gt;
: Be sure to precede a global constant with it’s namespace and the double colon operator (Namespace::constant_name) to reduce the time needed to query the library.&lt;br /&gt;
*Use explicit returns&lt;br /&gt;
: Although Ruby will automatically return the result of the last completed operation if no return value is provided you should use explicit return values.  Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
In languages files, RubyMine creates stubs of documentation comments on typing the opening tag and pressing Enter.&lt;br /&gt;
&lt;br /&gt;
Documentation comments can be created in accordance with RDoc and YARD syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
&lt;br /&gt;
When you create additional tags, RubyMine provides code completion that suggests the possible tag names.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Spreading Code Out and Lining it Up&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything in a meaningful way.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalising and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== Code Analysis Tools ==&lt;br /&gt;
Ruby itself goes a long way towards helping one write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The debugger can do all the usual sorts of things you would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to rise. Automatic tools can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses your Ruby code and warns you about design issues from the list you configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of your code: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyser, this can be used for duplication identification (you need a $99 license for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
== Summary ==&lt;br /&gt;
== See Also ==&lt;br /&gt;
== References ==&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=76281</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=76281"/>
		<updated>2013-09-16T23:26:05Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Code Analysis Tools */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Ruby Coding Guidelines =&lt;br /&gt;
&lt;br /&gt;
Designed and developed in the mid-1990s by Yukihiro &amp;quot;Matz&amp;quot; Matsumoto in Japan, Ruby embodies syntax inspired by Perl with Smalltalk-like features and was also influenced by Eiffel and Lisp. Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Importance of guidelines ==&lt;br /&gt;
== Types of Guidelines == &lt;br /&gt;
=== Naming Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*Local Variables&lt;br /&gt;
:Lowercase letter followed by other characters, naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. mileage, variable_xyz&lt;br /&gt;
*Instance Variables&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should be used after the @, e.g. @colour &lt;br /&gt;
*Instance Methods&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. If possible, the name should be a verb e.g. paint, close_the_door&lt;br /&gt;
*Class Variables&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@colour&lt;br /&gt;
*Constant &lt;br /&gt;
:Constant names start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*Class and Module &lt;br /&gt;
:Class names should be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules) should, however, probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*Global Variables&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*Model Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Controller Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*View Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Tests Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
To implement object-oriented programming by using Ruby, one needs to first learn how to create objects and classes in Ruby.&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. The class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
One of the interesting things about Ruby is the way it blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.  &amp;lt;ref&amp;gt;Robert L. Glass: Facts and Fallacies of Software Engineering; Addison Wesley, 2003. &amp;lt;/ref&amp;gt;&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* Profile your code regularly&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Computer software performance, particularly software application response time, is an aspect of software quality that is important in human–computer interactions. Performance of Ruby based applications can be regulated by following certain coding guidelines, such as below.&lt;br /&gt;
&lt;br /&gt;
*Avoid nesting loops more than three levels deep&lt;br /&gt;
: Nesting not only slows your code down but also can make maintenance of the codebase difficult if it goes too many levels deep.  Limiting nesting of loops and functions to three levels or less is a good rule of thumb to keep your code performant.&lt;br /&gt;
*Avoid unnecessary variable assignments&lt;br /&gt;
: New programmers, tend to assign variables more than necessary.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
*Reduce usage of disk I/O&lt;br /&gt;
: Disk I/O is one of the biggest bottlenecks remaining in computing.  Read/write operations to disk are extremely slow and it’s best to avoid using the disk whenever possible.  Many people are now using software such as memcached which allows data to be stored in memory and only periodically written to disk.  The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
*Use Ruby Enterprise Edition&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, in order to take advantage of these performance gains you must be sure to program according to their guidelines.&lt;br /&gt;
*Avoid method calls as much as possible&lt;br /&gt;
: Method calls are very expensive operations and should be avoided when necessary.&lt;br /&gt;
*Use interpolated strings instead of concatenated strings&lt;br /&gt;
: Interpolated strings are faster than concatenated strings in almost all interpreted languages; Ruby is no exception.  Using the &amp;lt;&amp;lt; operator makes a method call which and method calls should be avoided when possible.&lt;br /&gt;
put “Hello there, #{name}!”&lt;br /&gt;
vs.&lt;br /&gt;
puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
*Destructive operations are faster&lt;br /&gt;
: Ruby’s in-place methods that modify the actual value instead of working on a copy of it are much faster, but be careful as they sometimes behave strangely (i.e., for!)&lt;br /&gt;
*Avoid unnecessary calls to uniq on arrays&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
*For loops are faster than .each&lt;br /&gt;
: When you use .each you encounter per-request execution; for loops avoid this expensive operation.&lt;br /&gt;
*Use x.blank? over x.nil? || x.empty?&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
*Avoid calls to parse_date and strftime&lt;br /&gt;
: Both of these are very expensive operations.  Use regular expressions when parsing out date/time components.&lt;br /&gt;
*Don’t use unnecessary block parameters&lt;br /&gt;
: If you won’t be using the parameter in the block don’t specify it in the parameter list.  Go through your code and ensure that any parameters declared are used or removed.&lt;br /&gt;
*Know your gems&lt;br /&gt;
: Not all libraries are created with performance in mind.  Many gems are slapped together to solve a particular problem that author was having.  Before you introduce a new gem into your performance-oriented production codebase be sure to perform thorough benchmarking and testing against other gems that perform the same tasks.&lt;br /&gt;
*Improve your algorithms before you try to improve your code&lt;br /&gt;
: Algorithmic improvements are almost always going to have more of an impact on the performance of your code than tweaks to the way your code is written will.  Make sure your algorithm is designed to be efficient and has no extraneous methods or calls.  Also, test for most frequent cases first and exit loops as soon as possible.&lt;br /&gt;
*Test the most frequently occurring case first&lt;br /&gt;
: When using if statements or a case statement always test the cases in the order that they occur most frequently.  This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
*Optimize the way you access global constants&lt;br /&gt;
: Be sure to precede a global constant with it’s namespace and the double colon operator (Namespace::constant_name) to reduce the time needed to query the library.&lt;br /&gt;
*Use explicit returns&lt;br /&gt;
: Although Ruby will automatically return the result of the last completed operation if no return value is provided you should use explicit return values.  Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
In languages files, RubyMine creates stubs of documentation comments on typing the opening tag and pressing Enter.&lt;br /&gt;
&lt;br /&gt;
Documentation comments can be created in accordance with RDoc and YARD syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
&lt;br /&gt;
When you create additional tags, RubyMine provides code completion that suggests the possible tag names.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Spreading Code Out and Lining it Up&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything in a meaningful way.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalising and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== Code Analysis Tools ==&lt;br /&gt;
Ruby itself goes a long way towards helping one write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The debugger can do all the usual sorts of things you would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to rise. Automatic tools can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses your Ruby code and warns you about design issues from the list you configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of your code: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyser, this can be used for duplication identification (you need a $99 license for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:How to add an image.ogv|thumb|300px|Video tutorial showing how to add an image thumbnail to a Wikipedia page]]&lt;br /&gt;
&lt;br /&gt;
== Summary ==&lt;br /&gt;
== See Also ==&lt;br /&gt;
== References ==&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=76277</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=76277"/>
		<updated>2013-09-16T23:25:21Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Code Analysis Tools */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Ruby Coding Guidelines =&lt;br /&gt;
&lt;br /&gt;
Designed and developed in the mid-1990s by Yukihiro &amp;quot;Matz&amp;quot; Matsumoto in Japan, Ruby embodies syntax inspired by Perl with Smalltalk-like features and was also influenced by Eiffel and Lisp. Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Importance of guidelines ==&lt;br /&gt;
== Types of Guidelines == &lt;br /&gt;
=== Naming Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*Local Variables&lt;br /&gt;
:Lowercase letter followed by other characters, naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. mileage, variable_xyz&lt;br /&gt;
*Instance Variables&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should be used after the @, e.g. @colour &lt;br /&gt;
*Instance Methods&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. If possible, the name should be a verb e.g. paint, close_the_door&lt;br /&gt;
*Class Variables&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@colour&lt;br /&gt;
*Constant &lt;br /&gt;
:Constant names start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*Class and Module &lt;br /&gt;
:Class names should be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules) should, however, probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*Global Variables&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*Model Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Controller Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*View Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Tests Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
To implement object-oriented programming by using Ruby, one needs to first learn how to create objects and classes in Ruby.&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. The class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
One of the interesting things about Ruby is the way it blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.  &amp;lt;ref&amp;gt;Robert L. Glass: Facts and Fallacies of Software Engineering; Addison Wesley, 2003. &amp;lt;/ref&amp;gt;&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* Profile your code regularly&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Computer software performance, particularly software application response time, is an aspect of software quality that is important in human–computer interactions. Performance of Ruby based applications can be regulated by following certain coding guidelines, such as below.&lt;br /&gt;
&lt;br /&gt;
*Avoid nesting loops more than three levels deep&lt;br /&gt;
: Nesting not only slows your code down but also can make maintenance of the codebase difficult if it goes too many levels deep.  Limiting nesting of loops and functions to three levels or less is a good rule of thumb to keep your code performant.&lt;br /&gt;
*Avoid unnecessary variable assignments&lt;br /&gt;
: New programmers, tend to assign variables more than necessary.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
*Reduce usage of disk I/O&lt;br /&gt;
: Disk I/O is one of the biggest bottlenecks remaining in computing.  Read/write operations to disk are extremely slow and it’s best to avoid using the disk whenever possible.  Many people are now using software such as memcached which allows data to be stored in memory and only periodically written to disk.  The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
*Use Ruby Enterprise Edition&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, in order to take advantage of these performance gains you must be sure to program according to their guidelines.&lt;br /&gt;
*Avoid method calls as much as possible&lt;br /&gt;
: Method calls are very expensive operations and should be avoided when necessary.&lt;br /&gt;
*Use interpolated strings instead of concatenated strings&lt;br /&gt;
: Interpolated strings are faster than concatenated strings in almost all interpreted languages; Ruby is no exception.  Using the &amp;lt;&amp;lt; operator makes a method call which and method calls should be avoided when possible.&lt;br /&gt;
put “Hello there, #{name}!”&lt;br /&gt;
vs.&lt;br /&gt;
puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
*Destructive operations are faster&lt;br /&gt;
: Ruby’s in-place methods that modify the actual value instead of working on a copy of it are much faster, but be careful as they sometimes behave strangely (i.e., for!)&lt;br /&gt;
*Avoid unnecessary calls to uniq on arrays&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
*For loops are faster than .each&lt;br /&gt;
: When you use .each you encounter per-request execution; for loops avoid this expensive operation.&lt;br /&gt;
*Use x.blank? over x.nil? || x.empty?&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
*Avoid calls to parse_date and strftime&lt;br /&gt;
: Both of these are very expensive operations.  Use regular expressions when parsing out date/time components.&lt;br /&gt;
*Don’t use unnecessary block parameters&lt;br /&gt;
: If you won’t be using the parameter in the block don’t specify it in the parameter list.  Go through your code and ensure that any parameters declared are used or removed.&lt;br /&gt;
*Know your gems&lt;br /&gt;
: Not all libraries are created with performance in mind.  Many gems are slapped together to solve a particular problem that author was having.  Before you introduce a new gem into your performance-oriented production codebase be sure to perform thorough benchmarking and testing against other gems that perform the same tasks.&lt;br /&gt;
*Improve your algorithms before you try to improve your code&lt;br /&gt;
: Algorithmic improvements are almost always going to have more of an impact on the performance of your code than tweaks to the way your code is written will.  Make sure your algorithm is designed to be efficient and has no extraneous methods or calls.  Also, test for most frequent cases first and exit loops as soon as possible.&lt;br /&gt;
*Test the most frequently occurring case first&lt;br /&gt;
: When using if statements or a case statement always test the cases in the order that they occur most frequently.  This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
*Optimize the way you access global constants&lt;br /&gt;
: Be sure to precede a global constant with it’s namespace and the double colon operator (Namespace::constant_name) to reduce the time needed to query the library.&lt;br /&gt;
*Use explicit returns&lt;br /&gt;
: Although Ruby will automatically return the result of the last completed operation if no return value is provided you should use explicit return values.  Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
In languages files, RubyMine creates stubs of documentation comments on typing the opening tag and pressing Enter.&lt;br /&gt;
&lt;br /&gt;
Documentation comments can be created in accordance with RDoc and YARD syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
&lt;br /&gt;
When you create additional tags, RubyMine provides code completion that suggests the possible tag names.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Spreading Code Out and Lining it Up&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything in a meaningful way.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalising and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== Code Analysis Tools ==&lt;br /&gt;
Ruby itself goes a long way towards helping one write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The debugger can do all the usual sorts of things you would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to rise. Automatic tools can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses your Ruby code and warns you about design issues from the list you configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of your code: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyser, this can be used for duplication identification (you need a $99 license for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
== Summary ==&lt;br /&gt;
== See Also ==&lt;br /&gt;
== References ==&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:DSC01182.JPG&amp;diff=76271</id>
		<title>File:DSC01182.JPG</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:DSC01182.JPG&amp;diff=76271"/>
		<updated>2013-09-16T23:23:10Z</updated>

		<summary type="html">&lt;p&gt;Hsure: uploaded a new version of &amp;amp;quot;File:DSC01182.JPG&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:DSC01182.JPG&amp;diff=76254</id>
		<title>File:DSC01182.JPG</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:DSC01182.JPG&amp;diff=76254"/>
		<updated>2013-09-16T23:15:15Z</updated>

		<summary type="html">&lt;p&gt;Hsure: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=76251</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w23 ph</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w23_ph&amp;diff=76251"/>
		<updated>2013-09-16T23:12:52Z</updated>

		<summary type="html">&lt;p&gt;Hsure: /* Code Analysis Tools */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Ruby Coding Guidelines =&lt;br /&gt;
&lt;br /&gt;
Designed and developed in the mid-1990s by Yukihiro &amp;quot;Matz&amp;quot; Matsumoto in Japan, Ruby embodies syntax inspired by Perl with Smalltalk-like features and was also influenced by Eiffel and Lisp. Ruby Coding Guidelines include best practices followed generally for most of the object oriented programming languages as Ruby is entirely 'Object Oriented'. Also known as 'Ruby Coding conventions', these are a set of guidelines that recommend programming style, practices and methods for each aspect of a piece of program written in Ruby. &lt;br /&gt;
&lt;br /&gt;
Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Naming conventions, class and member design principles, maintainability, performance, documentation and layout are the important areas where these guidelines have to be followed. More important than the reasons for having a guideline is actually adhering to it consistently.  Having a coding guideline documented and available means nothing if developers are not using it consistently.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Importance of guidelines ==&lt;br /&gt;
== Types of Guidelines == &lt;br /&gt;
=== Naming Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Ruby uses the first character of the name to help it determine it’s intended use.&lt;br /&gt;
The standard Ruby file extension is .rb, although many people working on UNIX-like systems don't bother with it for stand-alone scripts. Whether or not one uses it for scripts is up to them, but they will need to use it for library files or they will not be found by the interpreter.&lt;br /&gt;
&lt;br /&gt;
*Local Variables&lt;br /&gt;
:Lowercase letter followed by other characters, naming convention states that it is better to use underscores rather than camelBack for multiple word names, e.g. mileage, variable_xyz&lt;br /&gt;
*Instance Variables&lt;br /&gt;
:Instance variables are defined using the single &amp;quot;at&amp;quot; sign (@) followed by a name. It is suggested that a lowercase letter should be used after the @, e.g. @colour &lt;br /&gt;
*Instance Methods&lt;br /&gt;
:Method names should start with a lowercase letter, and may be followed by digits, underscores, and letters. If possible, the name should be a verb e.g. paint, close_the_door&lt;br /&gt;
*Class Variables&lt;br /&gt;
:Class variable names start with a double &amp;quot;at&amp;quot; sign (@@) and may be followed by digits, underscores, and letters, e.g. @@colour&lt;br /&gt;
*Constant &lt;br /&gt;
:Constant names start with an uppercase letter followed by other characters. Constant objects are by convention named using all uppercase letters and underscores between words, e.g. THIS_IS_A_CONSTANT&lt;br /&gt;
*Class and Module &lt;br /&gt;
:Class names should be nouns. In the case of modules, it's harder to make a clear recommendation. The names of mix-ins (which are just modules) should, however, probably be adjectives, such as the standard Enumerable and Comparable modules. Class and module names starts with an uppercase letter, by convention they are named using MixedCase, e.g. module Encryption, class MixedCase&lt;br /&gt;
*Global Variables&lt;br /&gt;
:Starts with a dollar ($) sign followed by other characters, e.g. $global&lt;br /&gt;
&lt;br /&gt;
Considering customer order information as the data being used for an application, below naming guidelines give an idea of good class/table/file names.&lt;br /&gt;
*Model Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Table: orders&lt;br /&gt;
Class: Order&lt;br /&gt;
File: /app/models/order.rb&lt;br /&gt;
Primary Key: id&lt;br /&gt;
Foreign Key: customer_id&lt;br /&gt;
Link Tables: items_orders&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Controller Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Class: OrdersController&lt;br /&gt;
File: /app/controllers/orders_controller.rb&lt;br /&gt;
Layout: /app/layouts/orders.html.erb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*View Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Helper: /app/helpers/orders_helper.rb&lt;br /&gt;
Helper Module: OrdersHelper&lt;br /&gt;
Views: /app/views/orders/… (list.html.erb for example)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*Tests Naming Convention&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unit: /test/unit/order_test.rb&lt;br /&gt;
Functional: /test/functional/orders_controller_test.rb&lt;br /&gt;
Fixtures: /test/fixtures/orders.yml&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
To implement object-oriented programming by using Ruby, one needs to first learn how to create objects and classes in Ruby.&lt;br /&gt;
&lt;br /&gt;
A class in Ruby always starts with the keyword class followed by the name of the class. The name should always be in initial capitals. The class Customer can be displayed as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Customer&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A class is terminated by using the keyword end. All the data members in the class are between the class definition and the end keyword. &lt;br /&gt;
&lt;br /&gt;
One of the interesting things about Ruby is the way it blurs the distinction between design and implementation. Ideas that have to be expressed at the design level in other languages can be implemented directly in Ruby. To help in this process, Ruby has support for some design-level strategies.&lt;br /&gt;
&lt;br /&gt;
*'''The Visitor pattern''' is a way of traversing a collection without having to know the internal organization of that collection.&lt;br /&gt;
*'''Delegation''' is a way of composing classes more flexibly and dynamically than can be done using standard inheritance.&lt;br /&gt;
*The '''Singleton pattern''' is a way of ensuring that only one instantiation of a particular class exists at a time.&lt;br /&gt;
*The '''Observer pattern''' implements a protocol allowing one object to notify a set of interested objects when certain changes have occurred.&lt;br /&gt;
&lt;br /&gt;
Normally, all four of these strategies require explicit code each time they're implemented. With Ruby, they can be abstracted into a library and reused freely and transparently.&lt;br /&gt;
&lt;br /&gt;
=== Member Design Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
While defining class members, it is very important to keep in mind the access restrictions. Scope and life-time of class members are different for different restrictions, as illustrated below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public&lt;br /&gt;
totally accessible.&lt;br /&gt;
protected&lt;br /&gt;
accessible only by instances of class and direct descendants. Even through hasA relationships. (see below)&lt;br /&gt;
private&lt;br /&gt;
accessible only by instances of class (must be called nekkid no “self.” or anything else).&lt;br /&gt;
class A&lt;br /&gt;
  # Restriction used w/o arguments set the default access control.&lt;br /&gt;
  protected&lt;br /&gt;
&lt;br /&gt;
  def protected_method&lt;br /&gt;
    # nothing&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B &amp;lt; A&lt;br /&gt;
  def test_protected&lt;br /&gt;
    myA = A.new&lt;br /&gt;
    myA.protected_method&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # Used with arguments, sets the access of the named methods and constants.&lt;br /&gt;
  public :test_protected&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
b = B.new.test_protected&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Maintainability Guidelines ===&lt;br /&gt;
Maintainability guidelines are important to programmers for a number of reasons:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
*40%-80% of the lifetime cost of a piece of software goes to maintenance.  &amp;lt;ref&amp;gt;Robert L. Glass: Facts and Fallacies of Software Engineering; Addison Wesley, 2003. &amp;lt;/ref&amp;gt;&lt;br /&gt;
*Hardly any software is maintained for its whole life by the original author. &lt;br /&gt;
*Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. &lt;br /&gt;
*If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
The following guidelines are to be followed to improve the software maintainability&lt;br /&gt;
* Profile your code regularly&lt;br /&gt;
: If you profile your code regularly you’ll be able to tell if the latest change to the code will have an adverse effect on performance.  Integrate profiling into your testing process and make it automated to ensure that it’s not forgotten.  Like unit testing and BDD([http://en.wikipedia.org/wiki/Behavior-driven_development Behavior-driven development]) profiling goes a long way.&lt;br /&gt;
&lt;br /&gt;
=== Performance Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Computer software performance, particularly software application response time, is an aspect of software quality that is important in human–computer interactions. Performance of Ruby based applications can be regulated by following certain coding guidelines, such as below.&lt;br /&gt;
&lt;br /&gt;
*Avoid nesting loops more than three levels deep&lt;br /&gt;
: Nesting not only slows your code down but also can make maintenance of the codebase difficult if it goes too many levels deep.  Limiting nesting of loops and functions to three levels or less is a good rule of thumb to keep your code performant.&lt;br /&gt;
*Avoid unnecessary variable assignments&lt;br /&gt;
: New programmers, tend to assign variables more than necessary.  A great example is when someone defines a variable to store a return value and then returns that variable; just return the value directly.&lt;br /&gt;
*Reduce usage of disk I/O&lt;br /&gt;
: Disk I/O is one of the biggest bottlenecks remaining in computing.  Read/write operations to disk are extremely slow and it’s best to avoid using the disk whenever possible.  Many people are now using software such as memcached which allows data to be stored in memory and only periodically written to disk.  The speed improvement while using a memory caching system is tremendous.&lt;br /&gt;
*Use Ruby Enterprise Edition&lt;br /&gt;
: Ruby Enterprise edition provides up to [http://www.rubyenterpriseedition.com/faq.html#thirty_three_percent_mem_reduction 33% lower memory] usage.  Though, in order to take advantage of these performance gains you must be sure to program according to their guidelines.&lt;br /&gt;
*Avoid method calls as much as possible&lt;br /&gt;
: Method calls are very expensive operations and should be avoided when necessary.&lt;br /&gt;
*Use interpolated strings instead of concatenated strings&lt;br /&gt;
: Interpolated strings are faster than concatenated strings in almost all interpreted languages; Ruby is no exception.  Using the &amp;lt;&amp;lt; operator makes a method call which and method calls should be avoided when possible.&lt;br /&gt;
put “Hello there, #{name}!”&lt;br /&gt;
vs.&lt;br /&gt;
puts “Hello there, ” &amp;lt;&amp;lt; name = “!”&lt;br /&gt;
*Destructive operations are faster&lt;br /&gt;
: Ruby’s in-place methods that modify the actual value instead of working on a copy of it are much faster, but be careful as they sometimes behave strangely (i.e., for!)&lt;br /&gt;
*Avoid unnecessary calls to uniq on arrays&lt;br /&gt;
: In many cases methods are already calling uniq on an array and there’s no need for you to call it yet again.&lt;br /&gt;
*For loops are faster than .each&lt;br /&gt;
: When you use .each you encounter per-request execution; for loops avoid this expensive operation.&lt;br /&gt;
*Use x.blank? over x.nil? || x.empty?&lt;br /&gt;
: When using ActionPack there’s no need for x.nil? or x.empty?; x.blank? checks for both of these.&lt;br /&gt;
*Avoid calls to parse_date and strftime&lt;br /&gt;
: Both of these are very expensive operations.  Use regular expressions when parsing out date/time components.&lt;br /&gt;
*Don’t use unnecessary block parameters&lt;br /&gt;
: If you won’t be using the parameter in the block don’t specify it in the parameter list.  Go through your code and ensure that any parameters declared are used or removed.&lt;br /&gt;
*Know your gems&lt;br /&gt;
: Not all libraries are created with performance in mind.  Many gems are slapped together to solve a particular problem that author was having.  Before you introduce a new gem into your performance-oriented production codebase be sure to perform thorough benchmarking and testing against other gems that perform the same tasks.&lt;br /&gt;
*Improve your algorithms before you try to improve your code&lt;br /&gt;
: Algorithmic improvements are almost always going to have more of an impact on the performance of your code than tweaks to the way your code is written will.  Make sure your algorithm is designed to be efficient and has no extraneous methods or calls.  Also, test for most frequent cases first and exit loops as soon as possible.&lt;br /&gt;
*Test the most frequently occurring case first&lt;br /&gt;
: When using if statements or a case statement always test the cases in the order that they occur most frequently.  This allows less code to run before a decision is made.  It may not seem like much but over several hundred or thousand runs through the decision logic you’ll notice a definite performance gain.&lt;br /&gt;
*Optimize the way you access global constants&lt;br /&gt;
: Be sure to precede a global constant with it’s namespace and the double colon operator (Namespace::constant_name) to reduce the time needed to query the library.&lt;br /&gt;
*Use explicit returns&lt;br /&gt;
: Although Ruby will automatically return the result of the last completed operation if no return value is provided you should use explicit return values.  Explicit returns are faster, especially in older Ruby versions such as 1.8.x.&lt;br /&gt;
&lt;br /&gt;
=== Documentation Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
In languages files, RubyMine creates stubs of documentation comments on typing the opening tag and pressing Enter.&lt;br /&gt;
&lt;br /&gt;
Documentation comments can be created in accordance with RDoc and YARD syntax. Note that RDoc highlighting in documentation comments can be turned enabled or disabled in the Appearance page of the editor settings.&lt;br /&gt;
&lt;br /&gt;
When you create additional tags, RubyMine provides code completion that suggests the possible tag names.&lt;br /&gt;
&lt;br /&gt;
=== Layout Guidelines ===&lt;br /&gt;
----&lt;br /&gt;
Designing the layout of any application determines the readability factor for other developers.&lt;br /&gt;
Most followed order of code is as follows:&lt;br /&gt;
&lt;br /&gt;
header block with author's name, Perforce Id tag and a brief description of what the program or library is for.&amp;lt;br/&amp;gt;&lt;br /&gt;
require statements&amp;lt;br/&amp;gt;&lt;br /&gt;
include statements&amp;lt;br/&amp;gt;&lt;br /&gt;
class and module definitions&amp;lt;br/&amp;gt;&lt;br /&gt;
main program section&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Spreading Code Out and Lining it Up&lt;br /&gt;
:This is very important for readability. Basically the principle is to:&lt;br /&gt;
:*separate each component part by white space.&lt;br /&gt;
:*align everything in a meaningful way.&lt;br /&gt;
:As such one can easily scan up and down the code and see the patterns. This is very important not only for understanding the code, but also for looking for anomalies and as a tool for rationalising and consolidating the code.&lt;br /&gt;
:Code that has a lot of 'noise' - a lot of unnecessary variation and untidiness - is code that one can waste a lot of time working on. Well written and formatted code is code that is easy and quick to work with. It is code that allows one to easily 'see the wood from the trees'.&lt;br /&gt;
&lt;br /&gt;
== Code Analysis Tools ==&lt;br /&gt;
Ruby itself goes a long way towards helping one write clear code.&lt;br /&gt;
&lt;br /&gt;
*'''The Ruby debugger''' is a library loaded into Ruby at run-time. &lt;br /&gt;
This is done as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ruby -r debug [&lt;br /&gt;
            options&lt;br /&gt;
            ] [&lt;br /&gt;
            programfile&lt;br /&gt;
            ] [&lt;br /&gt;
            arguments&lt;br /&gt;
            ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The debugger can do all the usual sorts of things you would expect it to, such as set breakpoints, step into and over code, print out the call stack, etc.&lt;br /&gt;
&lt;br /&gt;
While tools for the mainstream languages such as Java and C++ have reached a certain maturity, tools for Ruby are still growing. And they might be needed more and more as Ruby's usage spreads from early adopters to the early majority, and SLOC (Source Lines Of Code) continues to rise. Automatic tools can be used to detect several types of problems including inconsistent style, long methods and repeated code.&lt;br /&gt;
&lt;br /&gt;
*[https://github.com/roodi/roodi/tree/master '''Roodi'''] (Ruby Object Oriented Design Inferometer) - this parses your Ruby code and warns you about design issues from the list you configured, ie: Class line count check, for loop check, parameter number check, cyclomatic checks and 10 other checks&lt;br /&gt;
*[https://github.com/troessner/reek '''Reek'''] - similar in concept to Roodi&lt;br /&gt;
*[https://github.com/devver/saikuro '''Saikuro'''] - designed to check cyclomatic complexity&lt;br /&gt;
*[https://github.com/seattlerb/flog '''Flog'''] - created by Ryan Davis, this computes a score of your code: the higher the score is, the worse your code is. ABC metrics (Assignments, Branches and Calls) are taken into account to compute the score&lt;br /&gt;
*[http://www.harukizaemon.com/simian/ '''Simian'''] - a similarity analyser, this can be used for duplication identification (you need a $99 license for commercial use)&lt;br /&gt;
*[https://github.com/seattlerb/flay '''Flay'''] - this is another free tool from Ryan Davis that finds structural similarities in code&lt;br /&gt;
&lt;br /&gt;
[[File:http://www.infoq.com/resource/news/2009/09/code-quality-metric-fu/en/resources/reek_flay_rake_e.png|thumb|left]]&lt;br /&gt;
&lt;br /&gt;
== Summary ==&lt;br /&gt;
== See Also ==&lt;br /&gt;
== References ==&lt;/div&gt;</summary>
		<author><name>Hsure</name></author>
	</entry>
</feed>