Quick search:

What is Once and only Once

Each and every declaration of behavior should appear only once

PhpPeanuts has borrowed "once and only once" from eXtreme Programming (XP). In XP it is a specific form of the more general principle of "the simpelest thing that could possibly work". Having the same code more then once in a system is described as making the system more complicated then necessary. XP favors frequent refactoring to eliminate duplication and keep the code in line with "once and only once" as a system is extended and modified.

PhpPeanuts sees itself as the outcome of applying these principles over multiple projects where similar (i.e. database-backed) applications are developed. Code that is similar between these projects is abstracted and substracted into the framework. Code that is specific for only one or a few applications is left in the application, or some intermediate layer.

Notice that phpPeanuts does not require code to be fully equal to be extracted. If a part of the code can be refactored to generic code that occurs only once, it's a candidate for refactoring too. Now others do that too, in fact many design patterns descibe ways to make code more generic so that it no longer needs to be repeated. It just seems that phpPeanuts is taking it slightly farther. For example with phpPeanuts you do not need to write (or generate) getter and setter methods in your model classes to get and set member variables. If you call the get or set method inherited from PntObject, getting and setting member variables is the default behavior for field properties. But if a getter or setter method does exist, it gets called instead, so encapsulation is not sacrificed.

PhpPeanuts also refrains from generating code because of "once and only once". Generated code is often not seen as subject to the "once and only once" principle. This may be true if the maintenance of all generated code is fully automated, but in practice that is often not the case. As soon as humans are saddled up with the maintenance of generated code, the same problems occur as with manually written code, and it will (or should) become subject of refactoring. PhpPeanuts prevents that by not generating the code, but just the behavior. Basically the code has already been refactored to be generic, so that it is not necessary to generate it. This may be a bit less transparent at first sight, but it results in a lot of code being eliminated from applications, effectively making them simpeler and easier to maintain and extend.


External links: