Level Up Your Coding Skills: The Power of Deliberate Practice for Developers
The Illusion of Efficiency
By observing my behaviour, I've realised that the knowledge and skills I believed to have mastered were often less solid than I thought. The efficiency I took pride in was akin to a colourful soap bubble that quickly bursts upon a gentle touch, leaving fragments scattered all around.
You Might Only Excel at Searching
In the era in which information is exploding, each person encounters and consumes a vast amount of data daily, swiftly digesting it only to forget. This misleading illusion of believing we possess knowledge as if we were emperors reviewing memoranda each morning propels us into a vicious cycle.
Even when we genuinely strive to solve problems and engage in proactive learning, we often find ourselves primarily becoming skilled at utilising search engines (after spending considerable time in a specific field, naturally, the accuracy of the keywords we use tends to surpass that of novices; however, that's the extent of it). Once we become masters of efficient searching, we develop an illusion that we have truly mastered the knowledge we acquire through searching.
An example: A simple shell script
I usually include images in my blog posts to make my points clear. These images are generally stored in a directory with a fixed format. For example, if it's May 2023, I would have a local directory named $BLOG_HOME/images/2023/05
. Since I use Markdown, when inserting an image, I have to input the complete image path, such as /images/2023/05/stack-overflow.png
.
However, I often forget whether "images" in the path should be singular (image
) or plural (images
), and I frequently mix up the image formats, which could be jpg, jpeg, gif, or png. These mistakes can result in the images needing to display correctly. Additionally, the original files in that directory can be quite large, so I usually need to resize them to 800 pixels wide.
To automate this process, I wrote a small shell script. When you input a file name, such as "stack-overflow.png," it resizes the file to 800 pixels wide, creating a new image file named "stack-overflow-resized.png." Furthermore, it copies the file path, formatted according to Markdown syntax, to the clipboard: /images/2016/05/stack-overflow-resized.png
. This way, I only need to paste it into the body of the article.
With this approach in mind, writing the script becomes relatively straightforward. I already know the command for resizing images:
$ convert -resize 800 stack-overflow.png stack-overflow-resized.png
However, adding the suffix -resized
to the file name, I need to separate the file name and the file extension. In Bash, how can I achieve this?
After a quick Google search, I got:
FULLFILE=$1
FILENAME=$(basename "$FULLFILE")
EXTENSION="${FILENAME##*.}"
FILENAME="${FILENAME%.*}"
convert -resize 800 $FULLFILE $FILENAME-resized.EXTENSION
The code could be done better, but it does the job. The next step is generating the complete path based on the current date. I know the date
command, and I'm aware that its format can be complex and different from the format
in JavaScript's Date object (in fact, there are almost as many date formats as there are date tools worldwide).
Let's Google that too:
$ date +"/images/%Y/%m/"
The final step of copying the path to the clipboard is also straightforward. I know how to use pbcopy
on a Mac machine: echo the string variable and pipe it to pbcopy
:
PREFIX=`date +"/images/%Y/%m/"`
echo "$PREFIX$FILENAME-resized.EXTENSION" | pbcopy
However, when I paste the content into Markdown, I notice that the script adds an extra line break. This behaviour is due to echo
automatically appending a newline character. Let's Google it again, and adding the -n
parameter solves this issue.
Alright, the complete script is ready:
#!/bin/bash
FULLFILE=$1
FILENAME=$(basename "$FULLFILE")
EXTENSION="${FILENAME##*.}"
FILENAME="${FILENAME%.*}"
convert -resize 800 $FULLFILE $FILENAME-resized.EXTENSION
PREFIX=`date +"/images/%Y/%m/"`
echo -n "$PREFIX$FILENAME-resized.EXTENSION" | pbcopy
Great, it's alright! The entire process took me just about fifteen minutes. From now on, inserting images into my blog will be much more convenient, which is fantastic!
What Is Wrong Here?
Did you see any problem here? Let’s zoom in and take a close look. I looked back at this script and realized that out of the seven lines of code, only one line was written by me without any searches!
Keep reading with a 7-day free trial
Subscribe to The Pragmatic Developer to keep reading this post and get 7 days of free access to the full post archives.