Did You Hear Something? Observer Pattern vs. Event Listeners

The Observer Pattern is like a DMV wait line. Event Listeners are more like a fire truck with its sirens blaring. Let me explain, and give you an example of each — the former in Ruby, and the latter in JavaScript.

Continue reading “Did You Hear Something? Observer Pattern vs. Event Listeners”

I See You: Intro to Observer Pattern

Observer pattern is a behavioral design pattern that attempts to resolve the problem of multiple objects depending on the state of a single object. Here’s an overview, and a somewhat simplified example.

Continue reading “I See You: Intro to Observer Pattern”

Something out of Nothing: Null Object Pattern

What is nothing? nil? null? undefined? A vast void of emptiness that fills your soul with dread? Oh sorry, that’s just my stomach.

We often think of nothing as… well, nothing. It’s when something doesn’t exist and therefore cannot be interacted with. So in our code, we try account for having nothing. No User? No problem.

Continue reading “Something out of Nothing: Null Object Pattern”

Ruby’s Safe Navigation Operator &. and is it a Code Smell?

What is &.?

Ruby devs are probably all too familiar with seeing this error:

NoMethodError (undefined method `foo' for nil:NilClass)

Most of the time, it’s probably due to a typo, but every now and then we end up having to do something like:

defined?(bar) && bar.foo
# returns nil if bar is nil

If you’re on Rails, or are using ActiveSupport, you can use present? or try():

Continue reading “Ruby’s Safe Navigation Operator &. and is it a Code Smell?”