13

I came across this programming technique while doing pair programming in an interview and couldn't find the name of it on google.

The idea is that you first write the expression which uses the variables and then you write the code that computes the variables later on.

To use some example code here:

private bool ValidPolicyNumber(string policyNumber) 
{
    var hasExpectedPrefix = policyNumber.Substring(0,5) == "POLIC";
    var followedBy7Digits = Regex.IsMatch(policyNumber.Substring(6,7), "^[0-9]{7}$");
    var hasLengthOf12 = policyNumber.Length == 12;

    return hasExpectedPrefix && followedBy7Digits && hasLengthOf12;
}

When writing this function using the technique I mentioned, you would first write the last line return hasExpectedPrefix && followedBy7Digits && hasLengthOf12; and then write the 3 lines that precede it.

The closest technique that I could find is "wishful thinking" and that's from SICP but that relates to calling functions that you are going to implement later rather than using variables that you are going to initialize later.

  • 8
    I believe this is just a form of top-down design. – Vincent Savard Apr 6 '17 at 19:13
  • 1
    I don't know a specific name for it, but I've seen this quite often often when a complex set of conditions needs to be checked. This technique makes it much easier to read and understand complex conditions. – FrustratedWithFormsDesigner Apr 6 '17 at 19:20
  • I've done this. You use code to sketch out an idea without sweating the details at first. Afterwards the IDE complains that something doesn't exist so you go make it exist. It helps when you have a test that will fail until you get around to finishing. – candied_orange Apr 6 '17 at 19:25
  • If this was preceded by a unit test then it would be Test Driven Development. When using TDD I know my expected result and I work backward from that expected behaviour. – Martin Spamer Apr 6 '17 at 20:00
  • I would call it "writing complex conditions sanely". – Jimmy Breck-McKye Apr 6 '17 at 21:45
13

It's just a variation on functional composition.

Witness:

private bool ValidPolicyNumber(string policyNumber) 
{
    return hasExpectedPrefix(policyNumber) 
        && followedBy7Digits(policyNumber) 
        && hasLengthOf12(policyNumber);
}

private bool hasExpectedPrefix(string policyNumber)
{
    return policyNumber.Substring(0,5) == "POLIC";
}

private bool followedBy7Digits
{
    return Regex.IsMatch(policyNumber.Substring(6,7), "^[0-9]{7}$");
}

private bool hasLengthOf12
{
    return policyNumber.Length == 12;
}

The only real difference is that your version combines the calculated results of the smaller functions into a single functional scope, which is what you want unless the smaller functional expressions are intended to be re-used elsewhere.

9

This could just be a suggestion from Clean Code (the book), to be used when Functional Decomposition (as Robert explained) is not applicable due to the functions resulting not being reusable on their own.

However, if you want to get technical about it, the book Implementation Patterns, by Kent Beck, names this technique as Explaining Local Variables (emphasis mine):

Local Variable

Local variables are only accessible from their point of declaration to the end of their scope. Following the principle that information should spread as little as possible, declare local variables just before they are used and in the innermost possible scope.

There are a handful of common roles for local variables:

  • Collector: a variable that collects information for later use. Often the contents of collectors are returned as the value of a function. When a collector will be returned, name it result or results.

  • Count: a special collector that collects the count of some other objects.

  • Explaining: if you have a complicated expression, assigning bits of the expression to local variables can help readers navigate the complexity:

    int top= ...;

    int left= ...;

    int height= ...;

    int bottom= ...;

    return new Rectangle(top, left, height, width);

    While not computationally necessary, the explaining local variables help what would otherwise be a long, complicated expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.