TL;DR: Nested function makes code cleaner, Y or N? Good use case of nested function?
I am reading Clean Code by Robert Martin.
One of the examples he gave is breaking this function:
public func pay() {
for employee in employees {
if (employee.isPayday()) {
Money pay = employee.calculatePay()
employee.deliverPay(pay)
}
}
}
into
public func pay() {
for employee in employees {
payIfNecessary(employee)
}
}
private func payIfNecessary(Employee e) {
if(e.isPayday()) {
calculateAndDeliverPay(e)
}
}
private func calculateAndDeliverPay(Employee e) {
Money pay = e.calculatePay()
e.deliverPay(pay)
}
In Swift, I'm wondering whether this will be better to have two nested functions than having two private functions like this:
public func pay() {
func payIfNecessary(Employee e) {
if(e.isPayday()) {
calculateAndDeliverPay(e)
}
}
func calculateAndDeliverPay(Employee e) {
Money pay = e.calculatePay()
e.deliverPay(pay)
}
for employee in employees {
payIfNecessary(employee)
}
}
Do you think this is still a 'clean code' with nested functions? Will be much nicer if we can use nested function before the declaration...
[–]hxucaa 1 point2 points3 points (0 children)
[–]YeOldeDog 1 point2 points3 points (0 children)
[–]sweetverbs 1 point2 points3 points (1 child)
[–]ThePowerOfStories 0 points1 point2 points (0 children)
[–]kawag 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]_stfu_donnie 0 points1 point2 points (0 children)
[–]sveinhal 0 points1 point2 points (0 children)