A pleasant walk through computing

Comment for me? Send an email. I might even update the post!

What Is Cyclomatic Complexity In a Nutshell?

Imagine you're going to walk to a friend's house. If the house is a few doors down, you don't need to make any decisions. But if your friend is further away, you may come to an intersection. You choose a direction and come to another intersection, and so on. How many paths are there to your friend's house?

That's cyclomatic complexity in a nutshell.

The number of independent paths through a method.

Map1

Map2

Using FluentAssertions with xUnit Theory to Test for an Exception AND a Successful Return

I recently wanted to unit test a method that required significant setup, and where an invalid method argument would throw an exception while valid values returned easily testable results.

While I could have created a separate test, this really lent itself to using an xUnit Theory. The problem I faced was how to test for the exception but also test for a valid return. I didn't want to write duplicate code within the test itself, such as declaring the service twice.

Some research and experimentation led to the approach below. The trick is to declare a delegate function, then use FluentAssertions to either catch the invocation exception, or to invoke the function and return the value.

Here's a simple Class Library app to demonstrate the technique. I kind of love this because there's no wasted or duplicate code.

using System;
using FluentAssertions;
using Xunit;

namespace FluentAssertionsInvoking
{
    public class CustomerService_Should
    {
        [Theory]
        [InlineData(null)]
        [InlineData("Marcus Welby")]
        public void Return_a_customer_or_throw_an_exception(string name)
        {
            // arrange
            var expected = new Customer() { Name = name };
            var customerService = new CustomerService(expected);

            // act
            Func<CustomerService, Customer> action = service => service.GetCustomer();

            // assert
            // Exception condition
            if (name == null)
            {
                customerService.Invoking(action).Should().Throw<Exception>();
            }
            else
            // Valid condition
            {
                var result = action.Invoke(customerService);
                result.Should().BeEquivalentTo(expected);
            }
        }
    }

    public class Customer
    {
        public string Name { get; set; }
    }

    public class CustomerService
    {
        private Customer _customer;
        public CustomerService(Customer customer)
        {
            _customer = customer;
        }
        public Customer GetCustomer()
        {
            if (_customer.Name == null) { throw new Exception(); }
            return _customer;
        }
    }
}

References

Bonus

Here's the xunit.runner.json to show only method names in the Test Runner output. Remember to set the file to Copy to Output.

{
  "$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
  "methodDisplay": "method"
}

Remote Work Tiny Tips - Last One: Overcoming Obstacles

It's relatively easy to get the technology running to work from home. The hard part is the doing. Don't beat yourself up! Try this tiny tip on...

Overcoming Obstacles

What if there were a simple technique, backed by science, that you could use in five minutes to help you improve all kinds of behaviors?

There is.

Mental Contrasting With Implementation Intentions - But You Can Call It WOOP

Mental Contrasting is a visualization technique involving first thinking of a positive future outcome followed by thinking of obstacles.

Implementation Intention is a self-regulatory strategy in the form of an "if-then plan" that can lead to better goal attainment. For example, "If I see the cookies, I'm going to grab the banana." Repeatedly imagining the plan improves the brain's automatic response.

Dr Gabriele Oettingen is premier among those who've researched MCII intensively, and she created a method for applying that research. It's called WOOP.

WOOP

Takes five minutes in a quiet (uninterrupted), calm environment, and intensely imagine the following.

  1. Your WISH (detailed goal with metrics)
  2. The best OUTCOME (all the positive stuff and feelings)
  3. Potential OBSTACLES (the negative stuff)
  4. Your If-Then PLAN

Do this every day, several times a day.

Important: Do not switch the "O"s. You must imagine the Outcome before the Obstacles. The research shows that switching them nullifies any benefit.

Here's Dr Oettingen describing the four steps.
The Four Steps of WOOP

And here she is taking people through the technique. I just love her.
Let’s WOOP

What's Going On? What About the Power of Postive Thinking?

Norman Vincent Peale got it (somewhat) wrong. It's not enough to think positively. In fact, trying to only think positively will hinder reaching your goals.

"Studies are showing that when people limit their thinking to imagining positive outcomes, they tend not to put forth the effort to make those outcomes come about. As [Dr Oettingen] put it:"

Positive thinking fools our minds into perceiving that we’ve already attained our goal, slackening our readiness to pursue it.

Understood this way, negative feedback is no longer a threat, it helps with how to fulfill the wish. It isn't personal. WOOP creates associative links in the brain, linking the outcome with the obstacle and the means to overcome the obstacle.

  • Mental contrasting has the better long-term benefit to achieving and maintaining goals
  • For easily attained goals, implementation intention doesn't matter much, but for difficult goals, It increases success threefold.
  • Practicing WOOP can double effectiveness on the goal ten weeks out.

WOOP isn't someone's opinion. It's not a gimmick. It's real science.

Finally

How much is five or ten minutes worth to you? Is it worth improving your life? Your kids' lives?

Go ahead. Find out.

References