March 2018
Intermediate to advanced
208 pages
4h 52m
English
| | enum SmallDistanceUnit { |
| | |
| | CENTIMETER, |
| | INCH; |
| | |
| | double getConversionRate(SmallDistanceUnit unit) { |
| | if (this == unit) { |
| » | return 1; // identity conversion rate |
| | } |
| | |
| | if (this == CENTIMETER && unit == INCH) { |
| » | return 0.393701; // one centimeter in inch |
| | } else { |
| » | return 2.54; // one inch in centimeters |
| | } |
| | } |
| | } |
Comments are there to explain the code. But it’s even better if the code speaks for itself!
In this example, you see a unit conversion. It’s very similar to what you’ve seen in Group with New Lines, just for small instead of large distances. The method getConversionRate() returns a conversion rate number. For each number, there’s a comment that explains what the number ...