Meaningful method names are great!
I came across (and added a bit to) a great snippet of code today in the Workspace codebase while fixing a bug on QA. The code represents just how you can use OO to encapsulate complex logic into meaningful behaviors of entities.
We have a business rule that says the following:
"The current user may save a membership in a workspace may be saved if all of the following are true:
1. The membership's user is not already a member in the space. 2. The current user is the owner of the space OR the space is marked public and the current is a member of the space's group."
Thanks to some good method names, the code is easier to understand than the English!
Here it is, in code:
if ( !membership.space.containsMember( membership.user )
&& (
membership.space.isOwnedBy( currentUser )
|| ( membership.space.isPublic && currentUser.isAMemberOf( membership.space.account ) )
)
)
Thanks to whichever of my coworkers originally wrote this - it was easy to modify to suit a new need!
