Coding Techniques

1. Introduction

1.1. Goals of Coding

column
  • Translate the design of system into a computer language format

  • Reduce the cost of other activities (evolution)

  • Make the program more readable

  1. 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.

  2. To reduce the cost of later phases: The cost of testing and maintenance can be significantly reduced with efficient coding.

  3. 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.

— Oscar Wilde
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);
    }
}

2.5. Improving Assertions with Atlanmod Commons

class Example {
    // (...)
	private double calculate(int x, int y) {
		Preconditions.ensureThat(x).isNotZero();
		Preconditions.ensureThat(y).isLessThanOrEqualTo(10);
		// (...)
}

3. Defensive Programming

defensive
Technique for developing maintainable code
  • 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.

Listing 1. Guard Checking in Java
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 ...
}

3.4. Guards in Google Guava

public static void foo(String name, int start, int end) {

    // Guards
    Preconditions.checkNotNull(name, "Name must not be null");
    Preconditions.checkArgument(start < end, "Start (%s) must be smaller than end (%s)", start, end);

    // Do something here ...
}

3.5. Guards and Assertions

Different goals:
  • Assertions are for situations that should never happen

  • Guards are for expected errors

Use Guards for public and Assertions for private methods!