Spolsky on Microsoft

August 10, 2022

I was just reading Joel Spolsky’s classic 2004 essay How Microsoft Lost the API War and much of it still rings true today, almost 20 years later. Replace WinForms with MAUI and ASP.NET with Blazor, and this essay is as good for 2022, as it was in 2004!

No developer with a day job has time to keep up with all the new development tools coming out of Redmond, if only because there are too many dang employees at Microsoft making development tools!

This was the exact reason I was tired of working with the Microsoft stack and started looking for alternatives, finally finding my solace in Ruby and Rails.

Read More

How to Check if a Variable is Defined in Ruby

August 10, 2022   • ruby

Ruby provides a handy defined?(expression) keyword that tests if the expression refers to anything recognizable. The expression can be an object, a variable that’s initialized, method name, etc. If Ruby can’t resolve the expression, it returns nil. Otherwise, it returns a string describing the expression.

Here are some examples of using defined? with different types of expressions. Note that a variable set to nil is still initialized and recognized by ruby.

RSpec.describe 'Defined' do
  it 'tests if the local variable is defined' do
    name = 'Akshay'

    expect(defined? name).to eq('local-variable')
    expect(defined? b).to eq(nil)
    expect(defined? nil).to eq('nil')
    expect(defined? String).to eq('constant')
    expect(defined? 1).to eq('expression')

  it 'ensures that a variable set to nil is still recognized' do
    name = nil
    expect(defined? name).to eq('local-variable')
Read More

Refactoring UI

July 22, 2022

If you’re a programmer who wants to get better at design, I highly recommend reading Adam Wathan’s new book Refactoring UI.

Adam Wathan is the creator of Tailwind, a CSS framework I really love. I started reading this book by earlier this year, and have re-read it multiple times since. It’s packed full of user-interface design wisdom, and if you are a designer, it’s a must-read.

Read More

Testing Rack Applications using Rack::Test

July 19, 2022   • no-rails-app

This is the fourth post in the series on building a web application without using Rails. In the last post, we saw how to dynamically render the views templates. This post is all about testing.

As we add more features to our Rack-based web application, it’s getting difficult and tedious to manually test our application in the browser. It’s important to make sure that as we add new features or refactor the code, we don’t break the application. In this article, we will set up tests using the Rack::Test library along with RSpec.

Read More

Reverse Method Refactoring

July 12, 2022   • smalltalk-patterns

Here’s a handy refactoring technique I learned today while reading Kent Beck’s Smalltalk Best Practice Patterns.

But first, what’s the problem? Sometimes, you have an unreadable method because you are calling different methods on different objects. You cannot extract a method because it’s already small (but unreadable) and does one thing. For example,

class Point
  def print(stream)
    stream.print(', ')

Though the example is not complicated, it takes a while to understand what’s going on and who’s calling whom.

We can solve the problem by making sure that all messages go through a single object. Kent provides the following approach for this refactoring:

Read More

Building a Web Application Without Rails : Building Dynamic Views

July 9, 2022   • no-rails-app

This is the third post in the series on building a web application without using Rails. In the last post, we saw how to serve static files from our rack-based application. In this post, we will dynamically render the view template using the erb gem.

At the end of this post, our application should be able to render the data passed in the query parameters.

Dynamic Page Demo

In a typical Rails application, the following actions take place when you request a dynamic web page:

  1. The controller interacts with the model to build the data that needs to be displayed.
  2. The controller passes the generated data to the view template via instance variables.
  3. The view template uses the data to render the final view HTML to the user.

In other words, the controller layer gets what it needs from the model layer and hands it off to the view layer.

The way Rails implements controller-to-view data handoffs is through instance variables. Typically, a controller action initializes one or more instance variables used by the view.

Let’s implement a similar structure for our application.

Read More

Tailwind + BrowserSync Sandbox

July 8, 2022   • tailwind

If you are tired of changing a Tailwind class, doing alt + tab, and reloading the whole app to see the change your made, check out my Tailwind + Browsersync project template.

Treat this as your local sandbox to quickly build small React or Turbo Frame components. The browser reloads automatically after you make a change and save. Just like Tailwind playground.

Once you’re satisfied with a design, copy + paste it in your main project. I found this workflow very productive, compared to making a small change, and reloading my big rails app. Hope you do, too.

Tailwind + Browsersync

What about Tailwind playground? it is great, but I do like to work in my favorite IDE with my favorite fonts and themes.

Read More

Can You Spot the Error?

July 2, 2022   • ruby

Do you see the error in this simple Ruby snippet?

class Person
  attr_accessor :name

  def assign_name
    name = 'akshay'

  def greet
    puts 'my name is ' + name

Keep reading if you’re curious.

Read More

Up and Running with Minitest

June 29, 2022   • ruby testing

I love minitest. Minitest is simple. There’re a few ways to write tests, and they are so intuitive. I learned it last year, when I got into Ruby, and since then I haven’t really felt the need to dive into the docs that frequently. It just works.

That said, I do find myself doing the minitest setup for my side-projects and quick-off learning projects often. Maybe you’re the same. So here’s a quick guide to rapidly get up and running with minitest in less than 2 minutes.

Read More

Faster Specs using Spring

June 28, 2022   • rails testing

If you’re working on a giant rails app that takes a long time to boot whenever you run the tests, I highly recommend giving spring-commands-rspec gem a try.

Spring is a Rails application preloader. It keeps the application running in the background, so you don’t need to load it every time you run a test, rake task, or even a migration.

Our application used to take almost a minute to load the files before running the tests. With Spring, it takes less than 5 seconds to run. Faster tests are great for developer productivity. Earlier, I used to wait for changes to accumulate before running a test. Now I can run the tests frequently after making small changes.

90% speed boost? I’ll take it.

Read More
Page: 1 of 19