A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Coding wisdom

While working with Broadbean, I was reminded of some of these pearls for Perl, and learnt some others for the first time:

Database:
  • Use timestamp for when something happened: updated_time, created_time.
  • Use datetime for when something is scheduled to happen: reminder_time, renew_time - this is why datetime depends on the timezone; the point-in-time of the reminder depends on the daylight savings in that timezone at the point the reminder should fire.
Version control:
  • The commit message should explain WHY the change was needed, not WHAT the change does (because what the change does should already be apparent from the code and in-line comments).
Code:
  • If you have an if/unless conditional you can fit on one line, use a post-fix:
    • if ( $i == 3 ) { return $i }
      • is better written as
    • return $i if $i == 3;
      • The second form is better because it discourages nested logic (which is harder to read and maintain)
  • Generally prefer subroutines and "return" statements to control flow, instead of "if/then/else" statements. This way the code is factored into smaller chunks that are easier to understand, modify and test.
  • Always include a dry-run option in stand-alone scripts meant for production.
  • Every pull request must contain one and only one feature/behaviour that needed changing. This reduces cognitive drain on human reviewers who are required to scan everything and may therefore miss some details.

Perl module preferences

Preferences:
  • Never use Switch, it has some truly awful bugs.
  • Use Try::Tiny instead of TryCatch, it's less magical/scary.
  • Instead of JSON, use Cpanel::JSON::XS or JSON::XS
    • It's safer to explicitly name the package being used, because JSON picks one depending on what's already installed.

Mock time with Test::Time for Perl

Voila:

use Test::Time time => 1;

my $now = time;    # $now is equal to 1
sleep 300;         # returns immediately, displaying a note
my $then = time;   # $then equals to 301

Sending email with Perl

use Email::Stuffer; # everything else is a pain

How OG fixed his Mac AGAIN

`for i in ../opt/openssl/lib/lib*; do ln -vs $i .; done`

and also installing *pkg-config*

and also deleting a local directory from `~/`