Design by contract
I believe the statement is not used much in PHP. You can use it if "Design by contract".
Contract (DbC) or Contract Programming is an approach to developing software for computers. This prescribes that software developers must define formal, accurate, and verified interface specifications for software components that extend the usual definition of abstract data types with preconditions, postconditions, and invariants. These specifications are called βcontractsβ according to a conceptual metaphor with the terms and obligations of business contracts.
This tutorial explains this a bit.
TDD / Unit Test
Instead, I would advise you to unit test your code (TDD) after these 3 simple rules (should read the article).
- You are not allowed to write any production code if it should not fail unit test pass.
- You are no longer allowed to write unit test than it is enough to fail; and compilation failures fail.
- You are not allowed to write more production code than enough to pass a single unit test failure.
You should start by writing a unit test for the functionality that you intend to write. But by rule 2 you cannot write a lot of this unit test. As soon as the unit test code compiles or fails to complete the statement, you must stop and write the production code. But according to rule 3, you can only write production code that makes the test compile or transmit, and no more.
If you think about it, you will understand that you simply cannot write very code at all without compiling and executing something. Indeed, this is indeed the point.
To practice (discipline) you must use the excellent PHPUnit framework . You must read this Writing Tests for PHPUnit to get a feel for this discipline.
Alfred
source share