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. Read more

Common JavaScript Gotchas – Part 1

Added to Code by on

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>

Added to Code by on

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. Read more

« Previous Page