Contracts With special reference to RabbitHunt Multi-person projects • Except for coursework, it is rare for a single person to write an entire program • Each programmer has to know what the other programmers are doing • In addition, each programmer has to let the others know what he/she is doing, BUT • In order to maintain control of code, it is necessary to hide details of the implementation Contracts • A contract specifies the rights and responsibilities of each party to the contract • In programming, a contract spells out what other programmers are allowed to depend on in my code • When I write a contract, I have these responsibilities: – I must provide enough information so that others can make use of my code (my classes and methods) – I must not arbitrarily change what I offer • I also have these rights: – I can change the code however I like, so long as it continues to meet the terms of the contract – I can provide additional functionality javadoc • In Java, the javadoc comments (or just “doc comments”) typically provide the contract • In BlueJ, you can read these more easily by choosing Interface from the pull-down menu in the upper-right corner • If you have not read “The contract” portion of the RabbitHunt assignment, you should do so • Rule 89, “Program by contract,” is also relevant The RabbitHunt contract • What is most important about the RabbitHunt contract is the information I do not provide: – I provide a number of constants, such as Model.N, Model.MIN_DIRECTION, and Model.FOX, but I do not specify the numerical values of those constants – I did not specify the size of the field, but in the improved version I provide the additional constants NUMBER_OF_ROWS and NUMBER_OF_COLUMNS • If you read the assignment, you already know that we may grade your program with different values for these constants – This is relevant to your grade! Magic numbers • A magic number is a constant (such as 20) that appears, sometimes without explanation, in the code • Magic numbers are poor style • Named constants, such as NUMBER_OF_ROWS, have two advantages: – They provide some documentation for the meaning of the constant – If you decide to change the constant, you need only do so in one place rather than many • Zero and one are typically not considered to be magic numbers The End