Learning Makes Developing Fun

Like many developers, I get into ruts where I'm not excited about what I'm working on. There are pieces of development that each of us doesn't enjoy for some reason. Maybe you hate QA.  Maybe you hate refactoring. Maybe it's writing JavaScript, PHP, CSS, or Go. It doesn't matter what it is for you.  These roadblocks of lack of excitement and interest need to be broken through if you are going to succeed, and one of the best ways to do that is to make things new and fun again through learning. 

Having Fun isn't Hard

When you've got a Library Card 

Arthur

Learning and using new techniques makes things fun again. Coding gets boring when we spend our time doing the same thing over and over again. When we aren't pushing ourselves forward, we are being left behind. It's important to overcome the challenge of doing something new so we continue moving forward.

Here are three things I've read over the last few weeks where I learned something. Maybe one of these will help you learn something new:

  • Linked Lists by Lara Schenck. This is an overview of a data structure that isn't native to any of the languages I spend the majority of my time in. Lara's series on computer science basics is a great primer for those of us without formal CS educations.
  • The Documentation for Styled Components. I was introduced to Styled Components when Laura Medalia started mentioning them on her Instagram Stories. I've started playing around with them to see if they are going to be right for some of the react things I've been doing.
  • A Twitter thread on the Social Model of Disability and Stephen Hawking has helped my thinking about how I speak about disabilities. Hat Tip to Ryelle for sharing it.

Null Propagation Operator proposal for JavaScript

At it’s January meeting, TC39 (essentially the standards body responsible for JavaScript) heard a proposal for some syntactical sugar that really excites me.  It’s called the Null Propagation operator and allows code that looks like

const firstName = (message
&& message.body
&& message.body.user
&& message.body.user.firstName) || 'default';

to become code that looks like

const firstName = message.body?.user?.firstname || 'default';

Essentially, it allows for eliminating lots of checks to ensure multilevel JavaScript objects are properly defined. This reminds me a bit of the null coalesce operator in PHP as  it allows for easier to read code. It needs to be balanced so these languages don’t turn into Perl filled with secret operators.

The proposal is now in stage 1 which means that TC39 “expects to devote time to examining the problem space, solutions and cross-cutting concerns”. It’s still early to get excited about this, but this is one of those pieces of syntactical sugar that make writing code easier.

“My Code is Self-Documenting” — Eric Holscher 

Self-documenting code is one of the biggest documentation myths in the software industry. This view generally conflates documentation with code comments. I’d like to make two arguments in this post:

Code comments have value

Documentation has more value than just explaining how software works

Source: “My Code is Self-Documenting” — Eric Holscher – Surfing in Kansas

New BASH Prompt

For the last few years, I’ve been using impromptu for setting my bash prompt. However, it felt in perpetual beta status and I wanted to try out something new, so today I installed bash-git-prompt and am giving it a try.

It was super easy to get started with it following the instructions.

  • Run brew update
  • Run brew install bash-git-prompt for the last stable release or brew install --HEAD bash-git-prompt for the latest version directly from the repository
  • Now you can source the file in your ~/.bash_profile as follows:
if [ -f "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh" ]; then
  __GIT_PROMPT_DIR=$(brew --prefix)/opt/bash-git-prompt/share
  source "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh"
fi

So far, it seems like something that should do the job. If not, I’ll look for a new solution. Let me know in the comments how you setup and manage your prompt.

Bash Functions I use for access logs

The command line is my IDE.  Vim is my editor and all the functions and programs in bash help me be a better developer.  As much time as I am writing code though, I also am often spending a lot of time looking through logs to see what is going on.  Over the last five to ten years I’ve collected a number of bash functions to help make working with access log files easier. Some are stolen from my old coworker Drew, others from various places online, and others I’ve cobbled together.

function fawk {
    first="awk '{print "
    last="}'"
    cmd="${first}\$${1}${last}"
    eval $cmd
}

This simple function allows me to pull one whitespace broken element out. Imagine an access file filled with lines like:

172.16.194.2 - - [13/Jan/2017:14:55:31 -0500] "GET /infidelity-husband-had-affair/ HTTP/1.1" 200 20685 "https://www.google.com/" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36" "1.1.1.1, 2.2.2.2, 127.0.0.1, 127.0.0.1"

I can run cat access.log | fawk 7 to pull out the urls. I can further pipe that to sort | uniq -c | sort -nr | head to pull out the most popular urls. I also have a function for visualizing these results.

function histogram {
 UNIT=$1
 if [ -z "$UNIT" ]; then
 UNIT="1";
 fi

 first="sort|uniq -c|awk '{printf(\"\n%s \", \$0); for (i =0; i<\$1; i=i+"
 last=") {printf(\"#\")};}'; echo \"\""
 cmd="${first}${UNIT}${last}"
 eval $cmd
}

For example, If I want to see all the response codes, of the last 500 responses I can do something like

tail -n 500 nginx/access.log | fawk 9 | histogram 10

 466 200 ###############################################
 8 301 #
 5 302 #
 1 304 #
 18 404 ##
 2 429 #

I often want to look at more than one access log at a time, but they are gzipped to save space after rotating. I have a function to cat or zcat all of them.

# cat or zcat all the access logs in a folder 
# Pass in folder to search in as the only param
# Likely want > into another file for further use
access_concat(){
	find $1 -name "acc*" -not -name "*.gz" -exec cat '{}' \;
	find $1 -name "acc*" -name "*.gz" -exec zcat '{}' \;
}

When it comes to working across many servers, I still rely on dancers shell in concert with these functions.  The occasional access log spelunking is much easier with these tools.

Bash punchcard

When are you using the command line? I use a python script and a change to my HISTTIMEFORMAT to keep track of when I am active on the command line.  Since I use vim, this means that every time I launch my text editor along with every time I run a command is recorded. This is my punch card since I got this computer in July, along with a punchcard of just commands that start with git and just commands that start with vim.

All commands
all git related commands
all vim commands

It’s interesting to see that I open Vim on Mondays much more than I commit code on Mondays. I think I need to spend some time figuring out how to make Mondays a day with more commits.

I first uploaded my bash punch card in 2012. I was much better at only working during the day then. I later did it in 2013 with both my personal and work computers.

An alternative splitting algorithm for publishiza

My friend John built plugin for writing tweet storms in WordPress. I’ve now used it to tweet storm about salary negotiations and WordPress committer stats in 2016. I like it, but I wasn’t super happy with how it split up the the post into individual tweets. It essentially just splits it up so that each tweet is whole words and doesn’t exceed 119 characters. It also adds an ellipsis at the end of every tweet. So I decided to build my own version that prioritizes whole sentences.

My tweet splitting algorithm first splits the post by sentences.  It then tries to build tweets and prioritizes keeping sentences intact.  My thinking is that this helps the tweets stand on their own a bit more.  If you want to use this, download and activate both publishiza and the gist below as plugins.

Stop macOS from asking for the ssh passcode every time

Upon the most recent update for macOS ( version 10.12.2), I was being prompted for my SSH passcode every time I would ssh into a server.  As someone who moves between servers often, this got annoying.

Thankfully, a new config option can fix this:

UseKeychain yes

Just add that to your .ssh/config and you don’t need to enter your private key passcode every time you ssh.  The old behavior will be back.  This can be enabled only for specific servers using the cascade inside.ssh/config or for all servers.

WordPress in a next generation PHP world

My elephant tattoo

HHVM has now released it’s second long term support release and PHP 7 is in the final stages of implementing changes. It’s an exciting time for PHP and to be a PHP developer which means it is also an exciting time to be a WordPress developer since it creates an opportunity for WordPress to once again embrace forwards compatibility.

While I was at PHPUK, one of the most common conversations I had was people being critical of WordPress for supporting PHP 5.2 as a minimum.  Many of those same people became less critical once they find out WordPress runs great on PHP 5.6 and that many people run it on HHVM.

For the last several weeks, WordPress has been running it’s unit tests on PHP7 nightly builds. They’ve been running on HHVM for months. Right now, the unit tests are not passing for either one and as far as I know, have never passed for either one.  This is a problem.

I’m planning on spending some time during the 4.3 development cycle focused on these next generation platforms. Rasmus has put together a php7 vagrant box and JJJ created an addon to Varying Vagrant Vagrants to enable HHVM there. WP engine also has it’s own WordPress HHVM vagrant box. I intend to use all three of these to help.

Davey Shafik has put together a great two part series on the changes coming in PHP 7.  The two changes that are most likely to cause issues for WordPress sites are the removal of all deprecated features and the deprecation of PHP4 style constructors. This is going to affect many widgets along with all sorts of other code.

It’s exciting to see PHP moving forward.  The competition between HHVM and PHP runtimes is making PHP faster and is only going to push the language forward. It’s a great time to be writing PHP.