7 kyu

Refactored Greeting

5,754 of 11,687jhoffner

Description:

The following code could use a bit of object-oriented artistry. While it's a simple method and works just fine as it is, in a larger system it's best to organize methods into classes/objects. (Or, at least, something similar depending on your language)

Refactor the following code so that it belongs to a Person class/object. Each Person instance will have a greet method. The Person instance should be instantiated with a name so that it no longer has to be passed into each greet method call.

Here is how the final refactored code would be used:

var joe = new Person('Joe');
joe.greet('Kate'); // should return 'Hello Kate, my name is Joe'
joe.name           // should == 'Joe'
joe = new Person('Joe')
joe.greet('Kate') # should return 'Hello Kate, my name is Joe'
joe.name          # should == 'Joe'
joe = Person.new('Joe')
joe.greet('Kate') # should return 'Hello Kate, my name is Joe'
joe.name          # should == 'Joe'
let joe = Person::new("Joe");
joe.greet("Kate"); // should return "Hello Kate, my name is Joe"
joe.name;          // should == "Joe"
joe = Person('Joe')
joe.greet('Kate') # should return 'Hello Kate, my name is Joe'
joe.name          # should == 'Joe'
val joe = Person("Joe")
joe.greet("Kate") // should return "Hello Kate, my name is Joe"
joe.name          // should == "Joe"
Object-oriented Programming
Refactoring

Stats:

CreatedFeb 17, 2013
PublishedApr 13, 2013
Warriors Trained17036
Total Skips1106
Total Code Submissions39201
Total Times Completed11687
JavaScript Completions5754
CoffeeScript Completions473
Ruby Completions2458
Python Completions3310
Rust Completions127
Scala Completions6
Total Stars110
% of votes with a positive feedback rating91% of 1017
Total "Very Satisfied" Votes863
Total "Somewhat Satisfied" Votes130
Total "Not Satisfied" Votes24
Ad
Contributors
  • jhoffner Avatar
  • anter69 Avatar
  • Madjosz Avatar
  • NunoOliveira Avatar
  • booniepepper Avatar
  • Captain_Howdy Avatar
  • Just4FunCoder Avatar
  • KayleighWasTaken Avatar
  • saudiGuy Avatar
Ad