Skip to content

Instantly share code, notes, and snippets.

@rymawby
Created September 14, 2015 14:42
Show Gist options
  • Save rymawby/e89092d5da58427979ca to your computer and use it in GitHub Desktop.
Save rymawby/e89092d5da58427979ca to your computer and use it in GitHub Desktop.
Improve this with "Tell, don't ask".
function getSalary() : Number
var employee:Employee = new Employee()
var salary:Number = employee.salary
if(employee.hasBonus) {
salary = employee.salary * 1.1
}
return salary
}
@cwright017
Copy link

Kinda feel as though this function would never exist as is. You would, surely, have the getSalary function as a method of the Employee class?

But..

Class Employee
var salary:Number
var bonus:Number
var hasBonus:Boolean

function getSalary() : Number {
if (hasBonus) {
return salary * bonus
}
return salary
}
End

thus you can then do:

var Ryan:Employee = new Employee()
Ryan.getSalary()

returning the value of the salary without having to break the encapsulation of the employee state - if setBonus has been called then you will get a salary with a bonus, else you wont.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment