Learn to Code: Chapter 0 – Wax on, Wax off

Warning! Programming might not make you rich. On a completely different topic, here’s Paul Allen’s private yacht, “Octopus”. (He’s a programmer).

So you want to learn to code? Well this is the right place. I assume no prior knowledge, except knowing how to turn on a computer, open a text editor such as Notepad, and have some idea of the operation of a keyboard. It may not be immediately obvious WHY I’m covering some of the topics in this article, but rest assured that, like Daniel-san painting fences or waxing Myagi’s car, it is all about preparing your state of mind.

Actually, while we’re on the subject of films, before you begin, make sure you watch The Social Network and The Pirates of Silicon Valley. They’re essential viewing for wannabe programmers!

 

Basic Mathematics

WAIT! Stop! Don’t be afraid, we’re only going to touch the absolute basics. Primary school stuff. Nothing else. That’s all you need!

Really? I hear you ask. Yes!! You don’t need to be a maths whiz to be a great programmer. Leave all that business to the computer!!

If you’ve ever written this:

1 + 1

…you’ve written a “program”. A program is simply a set of “instructions” for the computer to perform. Even if it’s just 1 + 1! Sure, most programs are a lot more complicated than adding 1 to 1, but at its most basic, that’s it. Easy!

Computer programming (also known as “coding”) is usually done to replace human effort with automation. The first step in any coding project is usually to break the real world problem down to its smallest parts. That way you can better understand what it really is that you’re turning into code.  Continue Reading “Learn to Code: Chapter 0 – Wax on, Wax off”

Automatic Task List in Visual Studio

Uhh… a little help?

The Task List in Visual Studio is apparently very well hidden. But if you’ve ever put a “TODO” comment in some code in Visual Studio, you’ve already started to use it! This is a feature of Visual Studio that has been around since at least VS 2005 and yet I talk to so many developers who have no knowledge if its existence!

 

Enabling the Task List in Visual Studio

Go to View, and click on “Task List”, it is usually about half way down the list. Continue Reading “Automatic Task List in Visual Studio”

Common JavaScript Gotchas – Part 1

This is a gotcha. But don’t worry, Noel Edmunds isn’t anywhere nearby.

Pop quiz, hotshot, the image shows a common JavaScript ‘gotcha’ – or inadvertent mistake through incomplete knowledge of something. What exactly is happening here?

You create a simple function called “getPerson”, and it returns you a JSON object with a single property on it, “name”. However! When you come to use the “name” property, it is undefined.

How come?

 

The Amazing Solution!

I prepared this gotcha after a lunchtime discussion with some colleagues turned to the “Python” programming language (yes, we’re geeks, get over it).

One chap made a derogatory comment of Python, something like, “what kind of a programming language uses white-space as flow control?!”

The irony is, ubiquitous JavaScript is happy to use whitespace as flow control too…

JavaScript Uses Whitespace as Flow Control? What on earth are you on about, Richard? You lunatic.

It’s true! The problem with the code above is where the curly brace ‘{‘ is placed after the return statement.

 

JavaScript Gotcha!

Wrong:

<!DOCTYPE html>
<html>
	<head>
		<title>JavaScript Gotchas 1</title>
	</head>
        <body>
		<script type="text/javascript">

              		var getPerson = function () {
				return 
				{
				    name: "Bob"
				};
			}

                        alert(getPerson().name);
                </script>
	</body>

</html>

 

Right:

<!DOCTYPE html>
<html>
	<head>
		<title>JavaScript Gotchas 1</title>
	</head>
        <body>
		<script type="text/javascript">

              		var getPerson = function () {
				return {
				    name: "Bob"
				};
			}

                        alert(getPerson().name);
                </script>
	</body>

</html>

 

As JavaScript doesn’t require you to define the return type of a function (like C# does, for example), you can just simply call “return” at any point and it will end the execution of the function. This is the important part: Semi-colons in JavaScript are sometimes optional! And calling return is one of those cases!

From the JavaScript parser’s point of view, you’ve simply called “return” and then put a line break (return key on your keyboard). The parser assumes you want to return then and there and ends execution. It DOESN’T go looking around on the following lines to see if you’ve specified something to actually return!

So in-fact, the above call to “getPerson()” returns you “undefined”, and “undefined” doesn’t have anything on it called ‘name’.

Bottom line: JavaScript uses whitespace as flow control!

 

Mitigation

You can stop this from happening by always ensuring your curly braces go on the same line as the return statement (as above in the “Right” code example). In fact, in JavaScript you should always try to write your code with the curly brace on the same line as the statement that prefixes it. It’s the “One True Brace Style” after all. (Although I prefer Eric Allman indentation for everything that isn’t JavaScript).

 

    if (expression) {
        // do something
    }

 

    for(var i = 0; i < myArray.length; i++){
        // Enumerate myArray
    }

 

Etc. Etc. Etc.

In the next “Common JavaScript Gotchas” article, I will be delving into the murky depths of Type Coercion! (I bet you’re salivating at the thought.)

 

 

Func<T> vs. Action<T>

Methods…

Although I’ve been using Func<T> and Action<T> for years, I’m embarrassed to say I hadn’t really thought about the differences between them – or more importantly, how they are in fact two sides of the same coin.

Most of the time their use has been a natural reflex action to a coding problem, and the consideration of the other method hasn’t even entered my mind. It’s time I also start using their proper names, which also belies their use:

 

Action<T>

and

Func<TResult>

 

Today’s Problem

I needed a way of enumerating a directory of files and executing a bit of arbitrary code against each one. The code that was to be executed needed the contents of the file. Continue Reading “Func<T> vs. Action<T>”