Recommended Software Development Tools

From years of experience and use (and a few recommendations from other people), I recommend the following tools:

IDEs/Editors

  • Visual Studio Code – Windows/Linux/Mac. It might be from
  • Atom – Windows/Linux/Max
  • Clound9 – Browser-based IDE. The original version was better. Now owned by Amazon AWS and they’ve made a royal mess of the licensing, usage and documentation. Still good under the covers though.

GUI Git Managers

Command-line has its place but for the complicities of Git-based source control (and being a visual person), I prefer a good GUI for Git management.

File Compare and Merge

Web Proxy for Debugging

  • Telerik Fiddler – Windows (Beta for Max/Linux)
  • Charles Proxy – Windows/Mac/Linux
  • OWASP ZAP – Windows/Max/Linux (I’ve not used this, but OWASP does good work in security and have seen others recommend it)

General Development Helpers

  • JSON formatter – Great for inspecting large sets of JSON (and now XML and YAML)
  • RegEx Testers (enter a regex expression, test it, find out what it does):
  • Can I Use – The best resource to determine what CSS and JavaScript feature the major browsers support.

&$ prefix for PHP function argument

The &$ prefix of an argument name passed into a PHP function means the argument is passed “by reference” (as opposed to “by value).

For example:

function publish_contributor_post($arg1, &$arg2)
{
   ... 
}

In this example arg2 is passed “by reference”.

See also: https://stackoverflow.com/questions/20419613/php-string-what-does-this-mean

Concatenation in PHP produces ‘0’ (zero)

If you’re concatenating a string in PHP and the result is a zero (“0”), check you haven’t mixed up your languages and used a “+” instead of “.” as your concatenation operator.

As a regular developer in C# and JavaScript regular, I sometimes write “$result = $part1 + $part2″ whereas I should write “$result = $part1 . $part2″.

If you tell someone they’re coding wrong, then tell them ‘why’.

I’m writing unit tests in xUnit and have the following example asserts:

Assert.Equal(5, results.Count);
Assert.Equal(0, results.Where(r => r.MatchResult == ComparisonMatchResult.InvalidDonorAllele).ToList().Count);
Assert.Equal(1, results.Where(r => r.MatchResult == ComparisonMatchResult.Potential).ToList().Count);
Assert.Equal(4, results.Where(r => r.MatchResult == ComparisonMatchResult.Specific).ToList().Count);

However, xUnit gives me warning squiggles and messages like this:

xUnit - Do not use Assert.Equal() to check for collection size.

When I Google for the warning to find out more I come to https://xunit.github.io/xunit.analyzers/rules/xUnit2013.html.

The problem is that page – nor any other page I can find so far – tells me why I should not use check collection sizes when there are zero (0) or one (1) expected elements. Why are there specialised assertions for these 2 specific cases? Why am I penalised for writing code in what I feel is a consistent and, I believe, easier to read way?

The moral of the story for developers: if you’re going to tell someone they’re doing it wrong, please also tell them why they are wrong and why the alternative is better.

Debug Dump Nested Variables and Big Lists in Visual Studio

Problem

I’m working on a project where I need to debug query data sets in structures I’m not familiar. So I’m setting up records in the application for known testing scenarios, creating views directly against the database, and I running my code in debug mode to set breakpoints and inspect the object structures being populated and returned.

Working in debug mode like this is difficult (one scenario is I have a List<T> of 120 and need to drill down to a 3rd level object to inspect value) as you need to painfully hover over each variable to expand and inspect. Then once you move on you lose the values.

What I want to do is dump my object structures to the console so I can later inspect and analyse the set at leisure.

Solutions

Object Exporter

Referenced:

Custom Code: Serialize to JSON

Add a reference to Newtonsoft.JSON.dll to your project.

Then create the following function:

 private static void DumpToJson(object obj)
 {
 string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
 //Console.WriteLine(json);
 System.Diagnostics.Debug.WriteLine(json);
 }

And call if from your actual code. For example:

 DumpToJson("-- Complete combinedRecipientAlleles --");
 DumpToJson(combinedRecipientAlleles);

As you can see in the following screenshot, inspecting my List<T> object is painful if I want to review all the values in the list. However, you can also see I’ve already called the first of 2 sets of calls to DumpToJson() (the first call is just a text label to identify what I’m writing out) and you can see from the Output window I have JSON being dumped to the Debug window.

Visual Studio serialize to JSON to inspect variables

Now I just copy the text in the output window into whatever editor I want to review the complete object structure). Simple!

Other Resources

The solution I went with came from here: https://www.codeproject.com/Articles/1194980/How-to-Dump-Object-for-Debugging-Purposes-in-Cshar

 

Tips, Thoughts, and Gotchas Working with JavaScript Dates

Post Status: Draft/Under Development

This post is being written in conjunction with a piece of work dealing with JavaScript dates.

Draft last updated: 21 February 2018


Things to remember about JavaScript Dates:

  • JSON.stringify() in JavaScript will serialize a date to UTC.
  • Month numbers start at 0 (zero) in JavaScript, not 1 (one).
    • So, January is represented by 0, through to December being represented by 11.
    • You need to remember this when using the Date.getMonth() method;
    • And when creating a new Date() object with a constructor overload – e.g. var today = new Date(2018, 1, 21) to create a date representing 21 February 2018.
  • A JavaScript Date object is internally represented as the number of milliseconds since 1970-01-01 00:00:00 UTC;
  • But the object itself uses the local timezone of the system running it – e.g. In Melbourne, Australia it returns a date/time of UTC+1000 during Australian Easter Standard Time and UTC+1000 during Australian Easter Daylight Time (during daylight savings).
  • If you try to manually calculate the current UTC time and set a Date object to that date/time, then the value of date and time is the same as UTC, however, the Date object still returns the local system timezone.
  • The JavaScript Date.getTimezoneOffset() is non-intuitive and returns the opposite of the UTC timezone offset sign.
    • For example: If the local timezone is UTC+1100 then getTimezoneOffset() returns “-660” (that minus 660 minutes).
    • Why?
      • The definition of the method is: “The getTimezoneOffset() method returns the time zone difference, in minutes, from UTC to current locale (host system settings).”
      • In other words: it subtracts the local timezone offset (including the offset sign) from UTC.
        • Example: UTC+1100 timezone = UTC – (660 minutes) = 0 – 660 = -660 (negative 600).
        • Example: UTC-500 timezone = UTC – (-300 minutes) = 0 – (-300) = 300 (positive 300).
        • If you’re wondering how 0 – (-300) makes a positive, that’s fundamental maths: subtracting a negative value from another value adds those 2 values together.
        • The zero in the examples is the timezone offset of UTC.

With that in mind, consider this scenario:

  • We have a system that stores date/times in the database as UTC.
  • We have a web front-end that works with JavaScript date.
  • We want to pass a date from the front-end to a server-side API that records the date in the database.

I’ve seen the following:

  • A JavaScript Date variable representing the current local date and time is created:
    • var localNow = new Date();
  • We want to JavaScript Date variable representing a ‘StartDate’, and we want to assign the current UTC date/time to it:
    • var startDate = new GetUTCDate(localNow);
    • See below for the GetUTCDate() function.
  • We need to serialize startDate to JSON so send it over the wire to the API.

The problems with this are:

  • ‘startDate’ may have a value that represents the UTC equivalent of ‘localNow’ but the actual ‘startDate’ JavaScript object still retains the local timezone offset.
  • The JSON.stringify() method used to serialize a JavaScript object to JSON converts Date values to their UTC equivalent time (remember the internal representation of a Date object is an offset representing a UTC time);
    • So what ends up happening is the ‘startDate’ value represented in the JSON is actually the UTC time plus or minus the local timezone offset.
    • For example:
      • If the current local time is 21 February 2018 at 10:00am, then ‘the startDate’ JavaScript object has the value: “2018-02-21 10:00:00.00000 UTC+1100”
      • But the string/value for returned by JSON.stringify(startDate) is: “2018-02-20 23:00:00.00000 UTC” (11 hours earlier).

 

The following is written in a TypeScript:

// Takes a Date set to the local date/time, and return a Date representing the equivalent UTC date/time.
public static GetUTCDate(localDate: Date): Date {
  //UTC+1100 returns -660 (negative). Remove offset from local time to reach UTC. 
  //However, [local - (-offset)] = [local + offset], therefore equation is [local + (-offset)] which = [local - offset]
  //UTC-1100 returns 660 (positive). Add offset to local time to reach UTC.

  // NOTE: This still produces a Date object with local timezone offset
  let offsetMinutes = Math.abs(localDate.getTimezoneOffset());
  if (localDate.getTimezoneOffset() < 0) {
    //Ex: GMT+1100
    return new Date(localDate.getTime() - (offsetMinutes * 60000));
  }
  else 
  {
    //Ex: GMT-1100
    return new Date(localDate.getTime() + (offsetMinutes * 60000));
  }
}

Running Example

If you’re using Internet Explorer or there’s just a black window below, here’s the link to the code (and I suggest using a “modern” browser – Chrome and Edge work): https://stackblitz.com/edit/js-date-gotchas.

 

 

 

Other things to consider:

  • If you want to store just a date (no time component) in a database, you cannot perform any locale conversions if it is stored as UTC.
    • Why? Let’s assume you want to convert the UTC date to the date in Timezone UTC+1100 AEDT (Melbourne, Australia). There are actually 2 dates this can convert to. To understand, look at it from the point of view of the local time we want to convert to:
      • In Melbourne, for any local time from midnight to 10:59:59am the equivalent UTC time is the day before (e.g. if the local date is 21 February, then UTC date is 20 February);
      • However, for the remainder of the day – 11:00am to 23:59:50pm – the UTC date is the same as the local date (i.e. both dates are 21 February).
  • Think about how you want to handle dates that are timezone neutral. That is a date that remains the same regardless of where in the world you are. For example, a date of birth usually does not care about where in the world the person is born, nor the timezone.

 

Pro Tip: Insert records into a SQL database from an Excel spreadsheet

Here’s a process I’ve been using for years to load data into a SQL database from Excel (and possibly do some transformation in the process).
It may seem obvious to some people but I have known software developers who never considered it.

It’s simple:

  1. Write an INSERT SQL statement on each line of the sheet you need to import (do it for the first row, then use Excel’s “fill down” feature).
  2. Copy those SQL statements into your SQL database management tool of choice, and execute.

For example, if we have the following table row (green heading), I can add the column “Location Lookup SQL” column which contains a formula to create SQL string by concatenating values from other columns.

LOCATION CATEGORY RATING FROM TO Location Lookup SQL
NWS A Moderate 2000 7999 =”SELECT ID FROM [LOCATION] WHERE [Name] = ‘”&A2&”‘”

 

If you find copying the “Location Lookup SQL” pastes the formula and not the resultant SQL value into your database manager, then a simple workaround is to copy the whole “Location Lookup SQL” column (all rows), then paste into another column in the spreadsheet using the “Paste Special” option and past only the values.

A sample is always good to illustrate so here’s an adaptation of an Excel file I just worked on: Sample – Generate Insert SQL From Excel

Or, if you prefer a quick look at it in pictures:

Sample - Generate Insert SQL From Excel (Screenshot 1)
Sample – Generate Insert SQL From Excel (Screenshot 1)
Sample - Generate Insert SQL From Excel (Screenshot 2)
Sample – Generate Insert SQL From Excel (Screenshot 2)

 

Save tabs (open files) in Visual Studio

If you find yourself setting up code files in Visual Studio for a story, then having to mix them up, or close and lose them when switching to another story, try this extension: https://marketplace.visualstudio.com/items?itemName=eamodio.SaveAlltheTabs

Now you can save and restore sets of tabs easily. Nice!

(I’ve installed and tested it with Visual Studio 2017)

Cloud9 (C9) – Add a Workspace to Bitbucket

When you need to add a clean workspace in Cloud9 online IDE (c9.io) to a Bitbuck (or GitHub) repository after it is created (that is, when it is not already connected to source control).

Steps to make it happen:

  1. Create a new repo in Bitbucket.
  2. “git init” from command-line in the directory to act as the root of the new repo.
  3. Run “git remote add …” (e.g. “git remote add origin git@bitbucket.org:jsnelders/wp-test.git”)
  4. Run “git add” and “git commit -m” command to add files to Git staging, then commit (with a comment).
  5. Run “git push” to push the committed files to the origin

You can also:

  1. Add a .gitignore file.
  2. For WordPress, it may be worth adding “wp-config.php” to the .gitignore, depending on whether you store sensitive information (passwords, API keys) in the file.

See also: