One common mistake I see in Stimulus controllers is that people use the connect() callback to set up…
connect()
The latest iteration of generics is amazing. Here’s a short proof of concept for a Ruby-like enumerable construct.
enumerable
package main
import ( "fmt" "strings")
func newSliceEnum[T comparable](s []T)…
It’s a Risky Tool with No Benefit
Avoid calling hash[key]. Instead use hash.fetch(key) if the key is expected to always be present, or hash.fetch(key, nil) if it’s valid/expected for the key to be absent.
hash[key]
hash.fetch(key)
hash.fetch(key, nil)
In my experience, Ruby’s each_with_object (docs here) can almost always be replaced with a simpler more specific/semantic construct.
each_with_object
all_points = routes.each_with_object([]) do |route, result|…
Don’t take my word for it, let these people make the case.
https://www.youtube.com/watch?v=spW9ZlJpobM
https://blog.heroku.com/docker-images-with-buildpacks
https://www.youtube.com/watch?v=2Xorbt3TaP4
https://buildpacks.io/
Don’t use version constraints outside of lock files
How many ways can we pick a set of coins of allowed denomination to equal a specified amount?
import ( "fmt")
func numWays(amount int, coins []int) (result int) { if amount == 0 {…
When you’re a disciplined software engineer, particularly one working in the XP (Extreme Programming) school, it can be a bit challenging when people don’t readily see the importance of minute details. But I find that people tend to care once they’ve been burned. This is one of my experiences sharing…
I’ve come across countless articles debating the merits of object oriented programming (OOP) vs. functional programming (FP). I want to put a stop to this madness by simply noting that OOP and FP are only different in terms of minor semantics. For example, compare these two ways of writing the same code.
I recently set up some Go code to communicate with the Nielsen API using OAuth 2. In addition I wanted to use the VCR…
You’ll often hear Go developers claiming that it’s incorrect (not idiomatic) to panic in Go; instead we should return errors. After…
In October 2011, Gary Bernhardt did a two-part screencast titled “What Goes In Active Records” (1, 2). Starting with general principles, Gary states his goals for an ActiveRecord class’s design.
The things I want in this class are things that wrap the…
Imagine we have the following methods:
def book_content(indifferent) if indifferent book_json_blob_content_with_indifferent_access else book_json_blob_content endend
def book_json_blob_content_with_indifferent_access…
Buy it here.
Labels: rails, ruby, software
I recently wanted to use ActiveRecord’s association syntax on two models, called Proposal and Period. To add clarity, I wanted the Proposals to be called…
A common question teams run into while building a Rails app is “What code should go in the lib/ directory?”. The generated Rails README proclaims:
lib/
lib — Application specific libraries. Basically, any kind of custom code that doesn’t belong under controllers…
Interpolation automatically calls to_s, so the to_s calls in this sample text are unnecessary.
to_s
def s3_filename(record) name = record.name || 'Empty ' date = Date.today.to_s guid = UUIDTools::UUID.random_create.to_s…
Use declarative formats.
When the Ruby on Rails framework was first released, one of its biggest selling points was convention over configuration. Name your ActiveRecord model User, and it would use the users table in its generated SQL. This preempted the need for verbose…
Link to book.
The net cost of caching can be calculated only by comparing the benefit of increases in speed to the cost of creating and maintaining the cache. If you require this speed increase, any cost is cheap. If you don’t, every cost is too much.
Documentation in code. Programmers are taught to comment their code: good code has lots of comments. Unfortunately, they are never taught why code needs comments: bad code requires lots of comments. The DRY principle tells us to keep the low-level knowledge in the code, where it belongs, and reserve the…
I just finished up Extreme Programming Explained, by Kent Beck and Cynthia Andres. Here are some excerpts I found interesting.