Coding Techniques
1. Introduction
1.1. Goals of Coding
-
Translate the design of system into a computer language format
-
Reduce the cost of other activities (evolution)
-
Make the program more readable
-
To translate the design of system into a computer language format: The coding is the process of transforming the design of a system into a computer language format, which can be executed by a computer and that perform tasks as specified by the design of operation during the design phase.
-
To reduce the cost of later phases: The cost of testing and maintenance can be significantly reduced with efficient coding.
-
Making the program more readable: Program should be easy to read and understand. It increases code understanding having readability and understandability as a clear objective of the coding activity can itself help in producing more maintainable software.
2. Assertive Programming
2.1. Assertive Programming
There is a luxury in self-reproach. When we blame ourselves we feel no one else has a right to blame us.
The Picture of Dorian Gray
2.2. Assertive Programming
-
Technique that follows the principle of fail fast and fail visibly
-
It reduces the error propagation because of side-effects.
-
It forbids(kind of sanity-check) the system to enter an inconsistent state because of user-data or by subsequent code changes.
2.3. Assertions
-
Assertions are predicates that check conditions that should never occur, and blow up catastrophically if they do.
-
Goals:
-
Ensure the state of variables.
-
Improve code readability.
-
Improve error localization.
-
-
Can be disabled.
| Assertions prevent the impossible! |
2.4. Java Assertion Example
public abstract class Application {
private Logger logger = Logger.global;
private List<Service> services = new ArrayList<Service>();
private Registry registry;
private MessagingService messaging;
public Application(Factory factory) {
registry = factory.createRegistry();
messaging = factory.createMessaging();
assert registry != null : "Registry service was not created correctly";
assert messaging != null : "Messaging service was not created correctly";
this.addService(registry);
this.addService(messaging);
}
}
3. Defensive Programming
-
ensures the code to behave in a correct manner, despite incorrect input.
-
guarantees that a method can be executed only when some requirements are met.
3.1. Guard Checking
-
Most spread technique form of Defensive Programming.
-
Guarantee that a method can be executed only when some requirements are met.
public static void foo(String name, int start, int end) {
// Guards
if (null == name) {
throw new NullPointerException("Name must not be null");
}
if (start >= end) {
throw new IllegalArgumentException("Start (" + start + ") must be " + "smaller than end (" + end + ")");
}
// Do something here ...
}
3.2. Improving Guards with Atlanmod Commons
public static void foo(String name, int start, int end) {
// Guards
Guards.checkNotNull(name, "Name must not be null");
Guards.checkArgument(start < end, "Start (%s) must be smaller than end (%s)", start, end);
// Method body ...
}
3.3. Guards in Apache Commons
public static void foo(String name, int start, int end) {
// Guards
Validate.notNull(name, "Name must not be null");
Validate.isTrue(start < end, "Start (%s) must be smaller than end (%s)", start, end);
// Do something here ...
}