You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+38-14Lines changed: 38 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,7 @@
7
7
*[Chapter 4 - Comments](#chapter4)
8
8
*[Chapter 5 - Formatting](#chapter5)
9
9
*[Chapter 6 - Objects and Data Structures](#chapter6)
10
+
*[Chapter 7 - Error Handling](#chapter7)
10
11
11
12
12
13
<aname="chapter1">
@@ -874,25 +875,25 @@ public class Geometry {
874
875
}
875
876
else if (shape instanceof Circle) {
876
877
Circle c = (Circle)shape;
877
-
return PI * c.radius * c.radius;
878
+
return PI * c.radius * c.radius;
878
879
}
879
-
throw new NoSuchShapeException();
880
+
throw new NoSuchShapeException();
880
881
}
881
882
}
882
-
```
883
+
```
883
884
884
885
**Polymorphic Shape**
885
886
```java
886
-
public class Square implements Shape {
887
+
public class Square implements Shape {
887
888
private Point topLeft;
888
889
private double side;
889
-
890
+
890
891
public double area() {
891
892
return side*side;
892
893
}
893
894
}
894
895
895
-
public class Rectangle implements Shape {
896
+
public class Rectangle implements Shape {
896
897
private Point topLeft;
897
898
private double height;
898
899
private double width;
@@ -902,7 +903,7 @@ public class Rectangle implements Shape {
902
903
}
903
904
}
904
905
905
-
public class Circle implements Shape {
906
+
public class Circle implements Shape {
906
907
private Point center;
907
908
private double radius;
908
909
public final double PI = 3.141592653589793;
@@ -913,7 +914,7 @@ public class Circle implements Shape {
913
914
}
914
915
```
915
916
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:
917
918
918
919
> 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.
919
920
@@ -925,16 +926,39 @@ Mature programmers know that the idea that everything is an object is a myth. So
925
926
926
927
### The Law of [Demeter](https://en.wikipedia.org/wiki/Law_of_Demeter)
927
928
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.
929
930
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`
935
936
936
937
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.
937
938
938
939
### Data Transfer Objects
939
940
940
941
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
+
### WriteYourTry-Catch-FinallyStatementFirst
953
+
In a way, try blocks are like transactions. Yourcatch has to leave your program in a consistent state, no matter what happens in the try. Forthis 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
+
### ProvideContext 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 PassNull
964
+
Returningnull 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