Mark Grealish

the life and code of a crazy cat man

No to everything

in me

I did an online quiz. Rabble rabble, power to the people.

My political spectrum

(Simple!) Sass/Scss margin size management

in code

Like, fuck it. After something like three years fucking with this problem, a realization has come upon me that the simplest solutions are the best solutions.

The core of my problem was this:

  1. I wanted to manage a set of margin and padding sizes from a central point.
  2. I want to base sizes all sizes off a central multiplier.
  3. I want everything to be the same type of unit.

My reason for these three requirements was to avoid the shit I came across in different codebases. Code smells such as a billion different little margins with no rhyme or reason are widespread, and these I hate with a passion.

I tried sets of variables, maps, lists, mixins and functions. I wrote elaborate mixins which only accepted certain parameters. I messed with third-party code. Variables take up too much space while returning too little-shit like margin: $m-full $m-full $-half appeared in my own code. What the fuck is $m-full?

Mixins and lists are just too fucking complex in combination, while too restrictive. Yes, it’s great there exists a master list of acceptable margin and padding sizes, but they are inflexible. A set of media query breakpoints is sensible; ten different approved margins isn’t.

Other mixins to parse sides are unwieldy as fuck-and I’m saying that as the author.

Below is my current (final) solution: The s() method takes a list of unitless numbers, maps them as multiples of $default-global-spacer, then spits out the mapped list. For example margin: m(1 3 0.5 1) with $default-global-spacer: 1.25rem becomes:

margin: 1.25rem 3.75rem 0.625rem 1.25rem

The multiplier values are themselves informative even when I don’t know $default-global-spacer, minimal logic and zero storage in lists are involved in this solution.

 * Multiply a margin against the default global spacer.
 * Any validations you want, you can insert here.
 * @access private
 * @param {string} $margin - Margin to fetch.
 * @param {number} $spacer - Global spacer or other default value.
 * @return {number} - Happy multiplied margin.

@function _m($multiplier: 1, $spacer: $default-global-spacer)
  @if $multiplier != 0 && unitless($multiplier)
    $multiplier: $spacer * $multiplier

  @return $multiplier

 * Multiply list of unitless sizes.
 * @access public
 * @param {string} $margin - Margin to fetch.
 * @param {number} $spacer - Global spacer or other default value.
 * @return {string} - Appropriate margin from map.

@function s($sizes: (), $spacer: $default-global-spacer)
  $multiplied: ()

  @each $size in $sizes
    $multiplied: append($multiplied, _m($size, $spacer))

  @return $multiplied

Some future improvements include:

  • Bounds check the list (-1 < list < 5) (likely, reasonable-margin and padding shorthands accept 1-4 items).
  • Check against a whitelist of permitted multipliers (unlikely, unwieldy).
  • Check against a whitelist of permitted units (unlikely, unwieldy).

Three Rock and Ticknock Forest

in ireland

I went up the mountain for a wander this evening, a perfect followup to tea in Malahide with my mum yesterday.

Dublin viewed from Ticknock Forest
The Little and Great Sugar Loafs viewed from Three Rock
The descent from Three Rock
A splash of green

In Malahide with my Mammy

in family

Mum came up for the day as a surprise visit!

Outside the Grand Hotel, Malahide

Circular shift Roman letters in CoffeeScript

in code

This Codewars problem annoyed the crap out of me.

shift = (letter, amount) ->
  mod = if /[A-Z]/.test letter then 65 else 97
  String.fromCharCode mod + (letter.charCodeAt(0) + amount) % mod % 26

The formula to constrain a number to a range is:

(range_start + offset_here) % range_start % range_size

Snapshots from the Wicklow Way

in ireland

Although by “Wicklow Way” I mostly mean “Cloghnagun” in this case.

Myself at the rear of Fairy Castle
The view from Cloghnagun
Looking toward the Great Sugar Loaf from Cloghnagun
The view from Cloghnagun
Looking toward Lough Bray Lower from Cloghnagun
Annacrivey Woods