Design is Refactoring


If Writing is Rewriting, Then Designing is Refactoring

Factories

From Simple to Ridiculous

Recently I helped organize some Sandi Metz training at the University of Minnesota. It was great and I highly recommend you bring Sandi to your place of employment or that you attend one of her public courses.

During class we discussed factory methods and the different ways of implementing them. But we didn’t have enough time time to dive in to the options. I told my classmates that I would write something up, so here it is – a guided tour of factories from the simple to the ridiculous!

Read More

Simple Rust, Part Four

Iterators, References and Options

This is it, the home stretch. In the 3 previous articles we’ve learned a lot about Rust (like Strings, Arrays and Lifetimes), but we haven’t actually written the code that started us down this path – finding anagrams of words. Today we will write that code.

Read More

Simple Rust, Part Three

Vectors, Mutations, Macros and Lifetimes

Previously, we looked at how our function will use Arrays and whether or not it will own or borrow its inputs. Our code currently looks like this:

fn anagrams_for(s: &str, inputs: &[&str]) {
  //Amazing logic TBD
}

We’re still trying to get tests like these to pass:

fn test_detect_simple_anagram() {
  let inputs = ["tan", "stand", "at"];
  let outputs: Vec<&str> = vec!["tan"];
  assert_eq!(anagram::anagrams_for("ant", &inputs), outputs);
}

The test asserts that outputs is equal to what our anagrams_for function returns. But our function currently returns nothing. And while the ouptputs syntax has a few familiar aspects, there’s a lot of new code as well: Vec, vec! and those angle brackets in <&str>. What’s going on here?

Read More

Simple Rust, Part Two

Arrays, Memory and the &

Last time we wrote half of our anagrams_for function signature. We looked at the different ways we can declare a string, and how to decide which type our function would accept. Exciting!

We have one more parameter to handle, though. The tests we have from Exercism have us calling anagrams_for like this:

let inputs = ["tan", "stand", "at"];
  anagram::anagrams_for("ant", &inputs)

And our current anagrams_for accepts just the “ant” part of that.

fn anagrams_for(s: &str) {
  //Amazing logic TBD
}

We have two problems to solve:

  • What is inputs type?
  • What is that & that keeps showing up?

Turns out these two things have much in common, and they reveal an important part of Rust’s memory management story. More excitement awaits!

Read More

Rust Simple Enough for Me to Learn It

A couple of weeks ago I dove into Rust and documented my attempts to solve an Exercism problem. That was pretty fun! But I also felt like I cheated a bit, since I started off with working code copied from the Exercism repo.

This time we’re going to start without any working code, just the tests that Exercism provides. When learning a new language this can be super daunting! A new language puts us back at the ‘Novice’ level of the Dreyfus model, where we want to follow recipes and exact instructions. A blank text editor offers no instructions, just an implacable empty maw.

But Rust offers some help, thanks to its type system. And we can help ourselves, by decomposing our large problem into a series of small problems. Let’s hop in and see what we can learn!

Read More