Josh Thompson     about     archive     tags

Primitive Obsession & Exceptional Values

I’ve been working through Avdi Grimes’ Mastering the Object Oriented Mindset course.

One of the topics was using “whole values”, instead of being “primative obsessed”. The example Avdi gave was clear as day.

He used a course with a duration attribute to show the problem.

course.duration 
=> 3

3 what? weeks? days? months?

Of course, you could write a method like:

course.duration_in_weeks
=> 3

But now you’ll have trouble rendering this all over the place. You’d have conditionals every time you wanted to render courses in weeks (if it makes sense), or in months (if appropriate), or of course, days.

So, the solution is to use “Whole values”. This means an attribute should be a complete unit, in and of itself, and should need no further refining to be usable.

So, you should be able to do something like this:

course.duration
=> Months[3]
shorter_course.duration
=> Weeks[5]
>> Read more

`ls` command to show directory contents

I like to use the tree command on my local machine when trying to peek into the structure and contents of a given directory.

tree -L 2

will [L]ist recursively everything [2] levels deep from your current directory. The output is nicely formatted like this:

> tree -L 2
.
├── cargo
│   ├── ARCHITECTURE.md
│   ├── CONTRIBUTING.md
│   ├── Cargo.lock
│   ├── Cargo.toml
│   ├── LICENSE-APACHE
│   ├── LICENSE-MIT
│   ├── LICENSE-THIRD-PARTY
│   ├── README.md
│   ├── appveyor.yml
│   ├── rustfmt.toml
│   ├── src
│   ├── target
│   └── tests
├── fastly
│   └── fastly-test-blog
├── get-pip.py
├── learning_aws
├── learning_elixir
│   ├── hellow
│   ├── sample01.exs
│   └── sling_clone
├── learning_over_the_wire
│   ├── bandit
│   └── leviathan
├── learning_react
│   └── react_tutorial
├── learning_ruby_rails
│   ├── InstaClone
│   ├── blocks_practice.rb
│   ├── chris_pine_ruby_lessons
│   ├── eloquent_ruby
│   ├── email_sender

If you’ve SSHed into a linux box, however, and you’re trying to look around a bit, you won’t have tree available to you. How can you list out the contents of directories?

Easy. The good ‘ol ls command:

>> Read more

Rails Migration: When you can't add a uniqueness constraint because you already have duplicates

I get to occasionally contribute to the Wombat Security dev blog. I wrote the following for development.wombatsecurity.com.


For work, I picked up a bug where a CSV export was creating duplicate rows when it shouldn’t have been.

We generate the CSV export with a big ol’ SQL statement in some Elixir workers, and got the bug reproduced and found the problem was with how we did some joins on other columns.

We had something in the statement like this:

LEFT OUTER JOIN `reports` ON `reports`.`id` = `people`.`report_id`
LEFT JOIN `addresses` ON `addresses`.`people_id` = `people`.`id`
LEFT JOIN `favorite_colors` ON `favorite_colors`.`people_id` = `people.id`

In this highly contrived example, I found out that we’re expecting a single row in the favorite_colors table for each person, but we were getting multiple favorite_color rows.

Every time we had a duplicate row on that joined table, the LEFT JOIN created two rows in the export, even though there should have been one.

>> Read more

Sidekiq and Background Jobs for Beginners

I’ve recently had to learn more about background jobs (using Sidekiq, specifically) for some bugs I was working on.

I learned a lot. Much of it was extremely basic. Anyone who knows much at all about Sidekiq will say “oh, duh, of course that’s true”, but at the time, it wasn’t obvious to me.

The reason I needed such basic overviews is because prior to my current job, I’d had just a few hours of exposure to background jobs, and understood little of those hours. And I got dropped into a project that has dozens of jobs, handling hundreds of thousands of actions a day.

As is my style, when I don’t understand something, I like to go to the very basics.

Most of the interesting stuff is way down at the bottom, on watching Redis do it’s thing

>> Read more

Mocks & Stubs & Exceptions in Ruby

Some of my recent work has been around improving error handling and logging.

We had some tasks that, if they failed to execute correctly, were supposed to raise exceptions, log themselves, and re-queue, but they were not.

The class in which I was working managed in large part API calls to external services, services that our team has no control over. Sometimes the services work great, sometimes they don’t. The tests for this class made heavy use of mocks, stubs, and the VCR gem.

This post was originally going to be about error handling, but then I realized I was getting a bit crossed up just by the mocks and stubs, so I took a quick detour into the topic, in order to build out a mental model of what was going on.

As I often do, I spun up a bare-bones implementation of all of the required pieces, to see how everything was playing together. One guide I found was excellent, but the sample app was a bit over-kill. I didn’t want a whole new rails app - I just wanted a class and test file.

note: This isn’t a tutorial, per se, but it’s just two files and you can easily copy-paste the code into an editor and run the tests. I’ll link to specific commits in a github repo throughout. Clone it down, check out the commit, poke around.

This project took me through:

  • stubbing
  • mocking
  • raising exceptions
  • rescuing exceptions
  • testing all of the above
>> Read more