This week in #dev (Aug 4, 2023)

Welcome to another edition of This Week in #dev, a series of posts where we bring some of the most interesting Slack conversations to the public.

Learning DeMorgan Laws

Matheus Richard recommended using DeMorgan Laws to simplify boolean operations or spot wrong refactoring. He also shared a RailsConf talk by Joël Quenneville about this very topic: The Math Every Programmer Needs.

Mozilla’s Guide to Web Security

Steve Polito shared that Mozilla maintains a guide on web security which provides information on how to protect websites from security threats. It covers topics such as authentication, encryption, and secure coding. The guide has a web security cheat sheet that is particularly useful!

Glob Expansion On Rake Tasks

Summer ☀️ shared a client’s tip to use glob expansion on rake tasks. Here’s an example:

rake db:{drop,create,migrate,test:prepare}
# This is the same as:
rake db:drop db:create db:migrate db:test:prepare

Optimizing Heavy Queries on Rails with accessed_fields

Neil Carvalho suggests using the accessed_fields method to help optimize heavy queries on Rails when using select. This method returns all the database fields that were read by an ActiveRecord model instance.

Creating or Finding with Rails 6+

Neil also shared Rails 6+‘s create_or_find_by method, which attempts to create a record in the database and, if it fails, finds the record instead.

The Benefits of Using --force-with-lease

Jeanine Soterwood advised a client to use git’s --force-with-lease instead of --force when pushing to a git branch, as it will not allow you to force push if another developer has added other commits to the branch in the meantime. She also mentioned that she uses --force-with-lease even when working on a feature branch by herself to make it a habit and avoid accidentally force-pushing over someone else’s work when working together on a feature branch.

Thanks

This edition was brought to you by Jeanine Soterwood, Matheus Richard, Neil Carvalho, Steve Polito, and Summer ☀️. Thanks to all contributors! 🎉