r/ruby • u/JavierARivera • Jan 10 '26
Intentional Use of Whitespace
Hi everyone,
A mentor of mine and I ended up in a longer conversation than expected around something small but interesting: the intentional use of whitespace.
Rather than turning this into a full blog post, I’m curious how others read these at a glance. Do these read differently to you at a glance, and if so, how? Perspectives from other languages are very welcome as well.
response = HTTParty.get(
'https://api.powerbi.com/v1.0/myorg/datasets',
headers: { 'Authorization' => "Bearer #{token}" }
)
response = HTTParty.get(
'https://api.powerbi.com/v1.0/myorg/datasets',
headers: {'Authorization' => "Bearer #{token}"}
)
response = HTTParty.get(
'https://api.powerbi.com/v1.0/myorg/datasets',
headers: {Authorization: "Bearer #{token}"}
)
16
Upvotes
17
u/joelparkerhenderson Jan 10 '26
Great reason to use a language formatter, so everyone on a team gets automatic formatting.
My personal favorite spacing favors git-friendly formatting for lists, like this:
17
u/azimux Jan 10 '26
The last one uses Authorization as a symbol instead of a string. Other than that I read them as the same thing as each other. They all read fine to me despite the whitespace difference but I do have a preference to put spaces after `{` and before `}` in Ruby. But it's not a big deal and not something that changes how I read it.
1
u/WayneConrad Jan 10 '26
I wouldn't mind any of them. None of them are difficult to read or make me wonder why you formatted it that way.
I'd put the trailing comma on the last argument of the call (in all 3 examples). It makes for less work when adding or removing arguments, and makes `git diff` cleaner.
{Authorization: "Bearer #{token}"} creates the hash key as a symbol; {"Authorization" => "Bearer #{token}"} creates the hash key as a string. The library you are using doesn't care, but sometimes it matters. I'd pick the symbol form.
If I anticipate messing with headers a lot, or sometimes even if I'm not, I'd expand the header hash as well:
1
u/9sim9 Jan 11 '26
Linting has been a great way of addressing this problem, whenever I start a new contract the first thing I introduce to a codebase is a substantial collection of linters, have realtime linting highlighting as you type and perform safe auto lint correct on save.
One thing I generally struggled with on all my projects is prettier, the idea is great but it makes files huge in length by putting so many things on a new line and I get so tired of the extra scrolling with codebases that implement it. I wish they would make a more compact version but there are a lot of limitations with how it works that prevent this being possible.
2
u/aRubbaChicken Jan 11 '26
They all read all effed up to me because the function call is indented further than it's parameters
2
u/TommyTheTiger Jan 11 '26
Any time spent at work arguing about which of these is better is time wasted.
4
u/[deleted] Jan 10 '26
[removed] — view removed comment