Skip to content

Commit 029d3f8

Browse files
committed
Merge branch 'main' into sean-parent/contracts-cleanup-2
2 parents a0368e3 + 0d00da7 commit 029d3f8

4 files changed

Lines changed: 1174 additions & 9 deletions

File tree

better-code/src/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22

33
- [Introduction](./chapter-1-introduction.md)
44
- [Contracts](./chapter-2-contracts.md)
5+
- [Errors](./chapter-3-errors.md)
6+
- [Types](./chapter-4-types.md)

better-code/src/chapter-2-contracts.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@ It's an invariant of your program that a manager ID can't just be
642642
random; it has to identify an employee that's in the database—that's
643643
part of what it means for the program to be in a good state, and all
644644
through the program you have code to ensure that invariant is upheld.
645+
645646
#### Encapsulating invariants
646647

647648
It would be a good idea to identify and document that whole-program
@@ -730,7 +731,7 @@ only slightly modified.
730731

731732
```swift
732733
/// A resizable random-access `Collection` of `T`s.
733-
struct DynamicArray<T> {
734+
struct MyArray<T> {
734735

735736
/// Removes and returns the last element.
736737
public mutating func popLast() -> T { ... }
@@ -760,7 +761,7 @@ The first one
760761

761762
```swift
762763
/// A resizable random-access `Collection` of `T`s.
763-
struct DynamicArray<T>
764+
struct MyArray<T>
764765
```
765766

766767
gives us the context we need to understand the methods: we're looking
@@ -817,9 +818,9 @@ the array has an element. OK, so what about postconditions?
817818

818819
The postconditions are the effects of the method plus any returned
819820
result. If the preconditions are met, but the postconditions are not,
820-
and the function does not report an error, we'd say the method has a
821-
bug. The bug could be in the documentation of course, *which is a
822-
part of the method*.
821+
and the function does not report a runtime error, we'd say the method
822+
has a bug. The bug could be in the documentation of course, *which is
823+
a part of the method*.
823824

824825
```swift
825826
/// Removes and returns the last element.
@@ -905,7 +906,8 @@ it need not take more than a few seconds:
905906

906907
### A More Complicated Example
907908

908-
Let's take a look at a traditional sorting algorithm.
909+
Let's take a look at a traditional sorting algorithm on a fictitous
910+
collection type:
909911

910912
```swift
911913
extension DynamicArray {
@@ -916,7 +918,7 @@ extension DynamicArray {
916918
/// over the elements of `self`.
917919
/// - Complexity: at most N log N calls to `areInIncreasingOrder`, where N is
918920
/// the number of elements.
919-
mutating func sort<T>(areInIncreasingOrder: (T, T)->Bool) { ... }
921+
mutating func sort(areInIncreasingOrder: (Element, Element)->Bool) { ... }
920922
}
921923

922924
var a = [7, 9, 2, 7]
@@ -1049,7 +1051,7 @@ tricky, we can drop the example, and we're left with this:
10491051
/// over the elements of `self`.
10501052
/// - Complexity: at most N log N comparisons, where N is the number
10511053
/// of elements.
1052-
mutating func sort<T>(areInOrder: (T, T)->Bool) { ... }
1054+
mutating func sort(areInOrder: (Element, Element)->Bool) { ... }
10531055
```
10541056

10551057
But we can go further and use a much simpler and more natural summary:
@@ -1093,7 +1095,7 @@ declaration:
10931095
///
10941096
/// - Complexity: at most N log N comparisons, where N is the number
10951097
/// of elements.
1096-
mutating func sort<T>(areInOrder: (T, T)->Bool) { ... }
1098+
mutating func sort(areInOrder: (Element, Element)->Bool) { ... }
10971099
```
10981100

10991101
There is one factor we haven't considered in making these changes:

0 commit comments

Comments
 (0)