4.4.1.9–4.4.1.11 - Decomposition, composition and automation (VB.NET)

4.4.1.9–4.4.1.11 - Decomposition, composition and automation (VB.NET)

Break a problem into useful tasks, combine procedures and data into larger abstractions, and explain how an abstract model becomes an automated solution. VB.NET source tasks are written-only; this route is incomplete: runnable support pending.

Procedural decomposition

Procedural decomposition breaks a problem into sub-problems that each accomplish an identifiable task. A sub-problem may itself be subdivided until its behaviour is clear enough to implement and check.

For a parcel quotation system, useful tasks are reading the parcel details, validating them, calculating a charge and presenting the quotation. “Lines 1–20” is not a useful task boundary: it describes the source layout rather than an identifiable purpose. Validation might be subdivided into checking positive weight and checking a supported delivery zone. Calculation might combine a weight charge with a zone surcharge.

Choose the boundary and interface together. A weight-charge routine could take weight and rate and return a monetary value. It should not unexpectedly request keyboard input or print the complete quotation. Explicit input and output contracts let the caller use it in different situations and let a developer check it independently. Decomposition need not produce one routine for every statement; excessive splitting can obscure a simple calculation.

The tasks should together cover the problem. If validation rejects a parcel, the overall procedure must decide what happens next; a collection of correct subroutines with no correct control flow between them is not yet a solution. Write down dependencies before implementation: calculation needs valid data, and presentation needs either a quotation or a stated error.

Composing procedures and data

Composition abstraction combines procedures into a compound procedure whose caller can use the larger operation without following every internal step. Decomposition identifies smaller tasks; composition connects them into a working larger task.

VB.NET
Module Program
    Function WeightCharge(ByVal weight As Decimal,
                          ByVal rate As Decimal) As Decimal
        Return weight * rate
    End Function

    Function ZoneCharge(ByVal remote As Boolean) As Decimal
        If remote Then
            Return 4D
        End If
        Return 0D
    End Function

    Function Quote(ByVal weight As Decimal, ByVal rate As Decimal,
                   ByVal remote As Boolean) As Decimal
        Return WeightCharge(weight, rate) + ZoneCharge(remote)
    End Function

    Sub Main()
        Console.WriteLine(Quote(2.5D, 3D, True))
    End Sub
End Module

The quote is 11.5: a weight charge of 7.5 plus 4 for a remote zone. D denotes a Decimal literal. The compound Quote exposes one operation while reusing two calculations. These routines assume validated non-negative inputs; hiding validation assumptions would make the abstraction misleading.

Data abstraction similarly combines data objects into compound data and exposes meaningful operations. A parcel record can combine an identifier, weight and zone flag. A tree combines nodes and the relationships between them. In this small native representation, each node contains its own label plus a list of child nodes:

VB.NET
Imports System.Collections.Generic

Public Class TaskNode
    Public Property Label As String
    Public Property Children As New List(Of TaskNode)
End Class

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account