编程基础:Representation invariants

Representation invariants

Many of the development techniques we discuss in this class, for example, incremental development, the use of good variable names, and unit-testing, are to help enable you to write correct code (and make it easier to enhance that code later). Another way to ensure correct code within a class is to make explicit any restrictions on what values are allowed to be in a private instance variable, and any restrictions on relationships between values in different instances variables in our object. Or put another way, making sure you know what must be true about our object representation when it is in a valid state. These are called representation invariants.
Representation invariants are statements that are true about the object as viewed by the implementor. Since for many classes, once a constructor has been called the other methods can be called in any order, you need to ensure that none of the constructors or mutators can leave the object in an invalid state. It will be easier to do that if you know what those assumptions are.

There are two assignment requirements for your SolitaireBoard class related to this issue listed here, and described in more detail right after that.

in a comment just above or below your private instance variable definitions for SolitaireBoard, list the representation invariants for the object.
write the private boolean method isValidSolitaireBoard() and call it from other places in your program as described below.

  1. The representation invariant comment for SolitaireBoard
    Write a list of all the conditions that the internals of a SolitaireBoard object must satisfy. That is, conditions that are always true about the data in a valid SolitaireBoard object. For example, one or more invariants would describe where the data is in a partially filled array (we did a similar example in lecture on 9/18). Another one (or more) would be related to the restriction that there are always CARD_TOTAL cards on the board.
  2. isValidSolitaireBoard() method
    This private method will test the representation invariant for the internals of a solitaire board. It will return true iff it is valid, i.e., the invariants are satisfied.

Call this function at the end of every public SolitaireBoard method, including the constructors, to make sure the method leaves the board in the correct state. This is one kind of sanity check: one part of a program double-checking that another part is doing the right thing (similar to printing expected results and actual results).

Rather than putting this test in an if statement, we’re going to put it in an assert statement. For example:

assert isValidSolitaireBoard();
Assert statements are described in Special topic 11.6 of the text.
Please make sure you are running your program with assertions enabled for every run of this program, since it’s in a development stage. See earlier section for how to do this. You won’t really know if they are getting checked unless you force one to fail.

The point of these assert statements is to notify you in no uncertain terms of possible bugs in your code. The program crashing will force you to fix those bugs. For example, if a board doesn’t have CARD_TOTAL (45) cards on it, then the simulation may never terminate, or if the array has “hole” in the middle, then other methods, such as configString may not work as advertised.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值