Saturday 2 March 2013

When methods and functions do too much

It's like asking a robot to get you some milk and it comes back to you with flavoured milk. You might want flavoured milk, but you might not! Your methods should only do as much as they are contracted to do. No more.

Here's another example: you create an Android AlertDialog with the AlertDialog.Builder class. Now every time you click one of the buttons of your AlertDialog, the AlertDialog is automically dismissed, as well as notifying you that a button was clicked. But you don't want the AlertDialog to be dismissed, you only want to be notified that a button was clicked!

Sometimes doing more is less helpful.

No comments: