Skip to content

Commit b64bbf1

Browse files
committed
Add Chapter 7 Notes
1 parent 63cf896 commit b64bbf1

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

README.md

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* [Chapter 4 - Comments](#chapter4)
88
* [Chapter 5 - Formatting](#chapter5)
99
* [Chapter 6 - Objects and Data Structures](#chapter6)
10+
* [Chapter 7 - Error Handling](#chapter7)
1011

1112

1213
<a name="chapter1">
@@ -874,25 +875,25 @@ public class Geometry {
874875
}
875876
else if (shape instanceof Circle) {
876877
Circle c = (Circle)shape;
877-
return PI * c.radius * c.radius;
878+
return PI * c.radius * c.radius;
878879
}
879-
throw new NoSuchShapeException();
880+
throw new NoSuchShapeException();
880881
}
881882
}
882-
```
883+
```
883884
884885
**Polymorphic Shape**
885886
```java
886-
public class Square implements Shape {
887+
public class Square implements Shape {
887888
private Point topLeft;
888889
private double side;
889-
890+
890891
public double area() {
891892
return side*side;
892893
}
893894
}
894895
895-
public class Rectangle implements Shape {
896+
public class Rectangle implements Shape {
896897
private Point topLeft;
897898
private double height;
898899
private double width;
@@ -902,7 +903,7 @@ public class Rectangle implements Shape {
902903
}
903904
}
904905
905-
public class Circle implements Shape {
906+
public class Circle implements Shape {
906907
private Point center;
907908
private double radius;
908909
public final double PI = 3.141592653589793;
@@ -913,7 +914,7 @@ public class Circle implements Shape {
913914
}
914915
```
915916
916-
Again, we see the complimentary nature of these two definitions; they are virtual opposites! This exposes the fundamental dichotomy between objects and data structures:
917+
Again, we see the complimentary nature of these two definitions; they are virtual opposites! This exposes the fundamental dichotomy between objects and data structures:
917918
918919
> Procedural code (code using data structures) makes it easy to add new functions without changing the existing data structures. OO code, on the other hand, makes it easy to add new classes without changing existing functions.
919920
@@ -925,16 +926,39 @@ Mature programmers know that the idea that everything is an object is a myth. So
925926
926927
### The Law of [Demeter](https://en.wikipedia.org/wiki/Law_of_Demeter)
927928
928-
There is a well-known heuristic called the Law of Demeter2 that says a module should not know about the innards of the objects it manipulates.
929+
There is a well-known heuristic called the Law of Demeter that says a module should not know about the innards of the objects it manipulates.
929930
930-
More precisely, the Law of Demeter says that a method f of a class C should only call the methods of these:
931-
- C
932-
- An object created by f
933-
- An object passed as an argument to f
934-
- An object held in an instance variable of C
931+
More precisely, the Law of Demeter says that a method `f` of a class `C` should only call the methods of these:
932+
- `C`
933+
- An object created by `f`
934+
- An object passed as an argument to `f`
935+
- An object held in an instance variable of `C`
935936
936937
The method should not invoke methods on objects that are returned by any of the allowed functions. In other words, talk to friends, not to strangers.
937938
938939
### Data Transfer Objects
939940
940941
The quintessential form of a data structure is a class with public variables and no functions. This is sometimes called a data transfer object, or DTO. DTOs are very useful structures, especially when communicating with databases or parsing messages from sockets, and so on. They often become the first in a series of translation stages that convert raw data in a database into objects in the application code.
942+
943+
<a name="chapter7">
944+
## Chapter 7 - Error Handling
945+
</a>
946+
947+
Many code bases are completely dominated by error handling. When I say dominated, I don't mean that error handling is all that they do. I mean that it is nearly impossible to see what the code does because of all of the scattered error handling. Error handling is important, but if it obscures logic, it's wrong.
948+
949+
### Use Exceptions Rather Than Return Codes
950+
Back in the distant past there were many languages that didn't have exceptions. In those languages the techniques for handling and reporting errors were limited. You either set an error flag or returned an error code that the caller could check
951+
952+
### Write Your Try-Catch-Finally Statement First
953+
In a way, try blocks are like transactions. Your catch has to leave your program in a consistent state, no matter what happens in the try. For this reason it is good practice to start with a try-catch-finally statement when you are writing code that could throw exceptions. This helps you define what the user of that code should expect, no matter what goes wrong with the code that is executed in the try.
954+
955+
### Provide Context with Exceptions
956+
Each exception that you throw should provide enough context to determine the source and location of an error.
957+
958+
Create informative error messages and pass them along with your exceptions. Mention the operation that failed and the type of failure. If you are logging in your application, pass along enough information to be able to log the error in your catch.
959+
960+
### Don't Return Null
961+
If you are tempted to return null from a method, consider throwing an exception or returning a SPECIAL CASE object instead. If you are calling a null-returning method from a third-party API, consider wrapping that method with a method that either throws an exception or returns a special case object.
962+
963+
### Don't Pass Null
964+
Returning null from methods is bad, but passing null into methods is worse. Unless you are working with an API which expects you to pass null, you should avoid passing null in your code whenever possible.

0 commit comments

Comments
 (0)