0

I see that the world is moving toward making code as expressive as possible. Some languages have even gone as far as allowing you to easily define your own DSL. However, I”m not sure how I can bring this into languages like PHP or pure Java or most other languages (I see this done to some extent in both Ruby and Python).

Imagine I have a Person entity. I’d want a function that tells me whether the user has signed up at least $days ago. Now, two questions:

  1. Should I put this function on the entity itself? As far as I know, entities are DAOs, and their role is just to provide low-level access to the underlying database. However, $person->hasSignedUpAtLeast(7) seems more expressive than something like PersonService::hasSignedUpAtLeast(7).
  2. How can I make the argument more clear, without the user having to look in his/her IDE and see the parameter name? As you have probably noticed, you can’t tell what 7 means. Does it mean 7 days? Months? Years? In Ruby, I could say person.hasSignedUpAtLeast(7.days.ago), but I don’t have such a luxury in PHP.

Please note that I’m looking for answers based on proven best-practices that are used in high-quality applications, not opinions or discussion. I believe there must be a single way of handling this kind of problems in which the function name must always be a verb, but the programmer needs to convey things like the unit of the argument (E.G. meters, days, etc) in the function name.

Parham Doustdar
  • 2,019
  • 3
  • 22
  • 41
  • `$person->daysSinceSignUp( 7 );`? Well, as it's largely an opinionated question, quite broad here. Although this obviously stems from function naming and PHP's very loose variable types; Type hinting in future versions will help here: `$person->hasSignedUpAtLeast( DateTime Obj )` – MackieeE Jan 27 '14 at 14:54
  • I have recently come to read some coding standards that make correct function names pretty clear. For example, many people seem to suggest that functions should always be verbs (E.G. signUp(), isSignedUp(), etc). daysSinceSignUp does not seem to fit into that category. – Parham Doustdar Jan 27 '14 at 14:57

0 Answers0