JS – Avoid duplicating addEventListener()

I see this mistake way more often than I should. Someone adds an event listener with an anonymous function callback to an element (like a button) then puts it in a a function that may get called multiple times. While this very common setup may seem harmless, it can create some serious issues.

The problem is this setup will add multiple listeners to the element, a new one each time the function is called, meaning three calls will add three listeners, then when the event is triggered the callback will be run three times. This JSFiddle demo clearly shows the scenario in action.

Continue reading “JS – Avoid duplicating addEventListener()”

Advertisements

WordPress – Load enqueued JavaScript in async or defer

Lately I have been working with WordPress, creating a complete website (even original theme) with it for my college capstone. Generally, things have been fair but there have been some things I have wanted to write a rant-y blog post about (but will not because I value my readers too much 🙂 ). One of those things is loading JavaScript into a site.

According to the documentation, wp_enqueue_script is “the recommended method of linking JavaScript to a WordPress generated page.” So as a good developer I have used this for loading my JavaScript. Aside from the rather crazy parameter order and defaults, I was quickly irked by the inability to load scripts asynchronously. After much searching though the WordPress docs and various programming sites, I came across the following article which uses a simple hash to denote async scripts, use the clean_url hook to filter all external URLs for JavaScript files (although file extensions are only hints) and add the async HTML attribute. However, the function was a bit too naive and limited for my taste (what if I wanted to defer a script?), so I wrote my own function using the basic idea of that snippet, which is the topic of this article. 🙂

Continue reading “WordPress – Load enqueued JavaScript in async or defer”

PHP – Calculate your age

Back in 2014 I wrote a JavaScript tutorial on calculating your age. Today, we are going to be exploring the same thing but in PHP!

Calculating the difference between two dates is much easier in PHP thanks to the DateTime family of classes introduced to the standard library in PHP 5.2.0. They provide built-in methods for timezones, date comparison, and date difference, just to name a few things. Because they abstract all the hard work for us (like good APIs ought to do), we will be putting them to use in this tutorial. If you are one of those poor souls who have to use an even older version of PHP (and I am so, so sorry about that. I hope you are paid well.), it would not be hard to port the JavaScript tutorial and replace the UTC calls with calls to date() (I recommend using “j”, “n”, and “Y”). You might also require running date_default_timezone_set("UTC"); before that depending on your server configuration.

On to the code! 😀

Continue reading “PHP – Calculate your age”

CSS – Overqualified or more specific selectors?

How many times have you seen CSS like this?

img.alignleft {
  float: left;
  display: inline;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.25);
}

img.alignright {
  float: right;
  display: inline;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.25);
}

If you were to run this through CSSLint, one of the warnings it throws would be “Disallow overqualified elements”. To quote this CSSLint documentation for this warning:

Continue reading “CSS – Overqualified or more specific selectors?”

Writing Good Comments

This post is an excerpt from a beginner-level Python book I am working on. Though it focuses on Python, the core message is language-agnostic. Additionally, I have generalized some of the more Python-specific sections.

Comments are little notes you write alongside your code. They are used to document your code, stating why it works that particular way or what it accomplishes. Comments are also used to detail what type of input the code expects and its type of output.

Continue reading “Writing Good Comments”