Pattern catalog

Patterns are common ways of doing things, like recipes. People have patterns for solving equations, making web pages, wrapping gifts, all sorts of things. Part of learning a skill is learning the patterns that help you be more effective.

Here are patterns on this website. This list can help you find the patterns you need to do a task.

Name Tags Situation Pattern summary, needs, provides Where referenced
Accumulator Loop

You have a data set, and you want to total up or count the values.

Loop over a data table. For each record, add a field to a total, count, or whatevs.

Needs: A data set.

Provides: A variable with a total or count.

Data tables, Processing a Range
Accumulator (Range) Range, For loop

You have records in a worksheet, and you want to find out something about the entire record set, like the average of a field.

Use a For loop to run through a range, adding (or counting) as you go.

Needs: A @Range@ object, representing records in the worksheet.

Provides: A variable with a characteristic of the record set, like a field's total, or a record count.

Processing a Range
Analyzing subsets Loop

You want to separate out data subsets, like old goats, or goats who can code.

Make accumulator variables for each subset, and an If for processing them.

Needs: * A data set, in a CSV file, or a @Range@.<br /> * Rules for defining subsets, like goats who are younger than 35, goats between 35 and 55, and goats older than 55.

Provides: Different accumulator variables (totals and counts) for each subset.

More than two groups
Create a Range Range

You have a set of records in a worksheet that you want to process.

Set rSomething = Range(top left cell, bottom right cell)

Needs: A worksheet with records in it.

Provides: An object that you can loop across.

Worksheet tables: Ranges
Find smallest/largest values Arrays, Range, For loop

You have some records in a Range, or a set of arrays.

Loop across some records. Put an If in the loop, comparing the current value with the smallest/largest so far.

Needs: You want the smallest and/or largest value(s) for a field in the records.

Provides: Smallest/largest values.

Min and max, Full CSV example
Flag Validation, If

You have a bunch of code. You want to see whether something happened in the code. It could happen in more than one place in the code. Validation is a common example, where user input could be invalid in a number of different ways.

A variable that summarizes the results of a bunch of code.

Needs: A variable you can use as a flag.

Provides: A value in the flag, that you can test for.

Basic validation: strings, Better validation, Validating during Range processing, Full CSV example
Guard pattern If

Your program could do something stupid, like try to divide by zero.

Prevent your code from doing something crazy.

Needs: Some data to test.

Provides: Usually an error message.

Guard pattern, Range: validate then process
Hungarian variable names Variables

You need to name some variable names.

"Sales tax rate" is sSalesTaxRate. Price is just sPrice.

Needs:

Provides:

Avoiding bugs
Input from a cell Input

You want to get one data value from the user.

Get input from a cell.

Needs: A cell in a worksheet.

Provides: A variable with a value.

Patterns
Input from a dialog Input

You want to get one data value from the user.

Use a dialog to get some input.

Needs: A human to type a value into a dialog, and/or read a value from a dialog.

Provides: A variable with a value.

Patterns