Category: Programming

Use Static Factory Methods Instead of Constructors in TypeScript

The traditional way for a class in TypeScript to allow a user of the class to obtain an instance of it is to provide a public constructor. There is another useful technique that should be a part of every programmer’s toolkit.

Good Toolkit

A class can provide a public static factory method. Now don’t confuse this with the stuffy Factory Method pattern from that Design Patterns books. It’s not the same at all and has no direct equivalent in that book or other design pattern books. Also, using a static factory method instead of a public constructor has both advantages and disadvantages that you need to consider.

Read more…

TypeScript try/catch/finally and Custom Errors

Several years ago, the entire booking and check-in system for a major airline in the United States ceased to function for more than an hour during the morning rush on a weekday. This resulted in flight delays for the entire day all across the country. In the end, the cause was found to be an unhanded error that resulted in a flight lookup service to become completely unresponsive.

Handling errors in TypeScript and JavaScript is one of the fundamental things a developer should be experienced in. It’s as important as the rest of the code, and should never be overlooked or underestimated. This is a guide to help newer developers understand how to handle errors, throwing errors, and using try/catch/finally according to industry standards, and also how not to handle them.
Read more…

Using Short Imports in TypeScript

When importing modules (e.g. classes, interfaces, etc) in TypeScript, the most common method is to allow the IDE to create a relative import statement. Here is an example:

However, there is an easy way to completely avoid these long import statements. All it requires is some simple and minor modifications to your tsconfig.json file.
Read more…

Guide to Extract Method Refactoring in PHP

The purpose of the extract method refactoring is to turn a section of code into a new method. The method should be given a name that accurately describes the purpose of the extracted section of code. Extracting methods allows the code to be self documenting and also allows the code to be reused elsewhere thereby reducing duplication.

Read more…

Guide to Extract Method Refactoring in TypeScript

The purpose of the extract method refactoring is to turn a section of code into a new method. The method should be given a name that accurately describes the purpose of the extracted section of code. Extracting methods allows the code to be self documenting and also allows the code to be reused elsewhere thereby reducing duplication.

Read more…

Simple Beginner’s Guide to JavaScript Closure

Closure in JavaScript can be confusing for newcomers. However, it doesn’t have to be that way. Here is a quick rundown of what you need to know.

First, let’s define closure using a definition that is easy to understand.

Read more…

Immutable Objects in PHP

When I first learned to program, I made many objects that were mutable. I made lots of getters and lots of setters. I could create objects using a constructor and mutate and morph the heck out of that object in all kinds of ways. Unfortunately, this led to many problems. My code was harder to test, it was harder to reason about, and my classes became chock full of checks to ensure that it was in a consistent state anytime anything changed. Of course, my classes had to be backed by unit tests and this often resulted in a combinatorial explosion. This how I look when I try to imagine all of the possibilities and how to test them.

Read more…

Is a coherent full stack (client and server) testing strategy possible?

Let’s assume we are doing TDD (and maybe BDD too) and we don’t want to write software without a failing test.

Back in the day, whether you were writing PHP, Java, or Asp.Net, testing your server side application was easy. You wrote unit tests for your models, controllers, data objects, etc. Then, you ran them every few minutes as you wrote new code and features. Everything was simple and all of your tests existed in the same language and execution environment as the business logic (i.e. the server).

Read more…

Why is dynamic typing so popular?

I have worked with a couple of statically typed languages and a few dynamically typed languages. I have noticed that when using statically typed languages I get all kinds of IDE support. That IDE support makes me really productive. It also really helps me to move stuff around without breaking things too badly (i.e. refactoring). Tests also help with ensuring that I didn’t break things. Compile times are usually sub-second due to partial compilation. Auto complete allows me reduce typing down to a two or three keystrokes per identifier (e.g. class name, variable name). Documentation and type info for the method or variable often pops-up automatically. This allows me to stay focused in the IDE.

Read more…

A Simple Set of Time Utilities for PHP

php5.4-300x300Haven’t you ever wished you had an easy to use chronograph, set of constants representing the days of the week, or perhaps months of the year? Maybe you have forgotten the exact format to use when writing datetime’s to a MySQL database (i.e. ‘Y-m-d H:i:s’). I know I have.

Read more…