Periodically, I check the JSLint update history on GitHub to see what’s going on, and what significant updates have been included.

So, imagine my surprise when I saw a simple one-line comment on a June 12, 2011 edit:

eqeq

I did a quick diff on the source code to see this following line added:

 //     eqeq       true, if == should be allowed

This means that you can now configure JSLint to ignore uses of ‘==’, where previously it would have insisted you should use ‘===’

At the time of writing it hadn’t been updated on the JSLint front page, however testing locally with the following yielded no ‘==’ related warnings:

    /*jslint eqeq: true*/
    var x = 0;
    if (x == 1) {
        alert("test");
    }

I always recommend that use the ‘===’ in this instance anyway, but I am aware of the differing opinions surrounding it. Ultimately, you can make your own decision (and it is ‘on’ by default so you have to opt-out).

, ,

Periodically, I see the following sort of email being broadcast across the department:

Can you please log out of [Some Machine]. I need to restart [SomeService]

Once sent, some kind individual will log out and send an email confirming this. The original correspondent will then log onto the machine and perform the necessary actions before logging out, emailing the original user, who will then log back in.

Hassle, right?

Happily there is a simple shortcut from the service control manager on your local PC.

1. Simply open this up from the control panel (or typing ‘services.msc’ into the ‘run’ dialog available from the start menu).

When it has loaded, if you look at both panes you will see the text ‘Services(local)’.

2. Click on the ‘Services(local)’ item in the list in the left pane and then click ‘Action’ from the main menu (the options in this menu change depending on what you have selected) then click ‘Connect to another computer’.

3. Type in the name of the computer you would like to connect to.

4. Restart the services as you would have done:

This facility isn’t just limited to the service control manager. The following tools/services also allow you to connect remotely in some way:

  • Component Services
  • Computer Management
  • Event Viewer
  • Registry Editor

So next time you want to log onto a box, why not consider whether or not you really need to, and instead use the method above?

, , ,

I discovered this little trick today.

If you’re wanting to count the number of characters present in a given SQL Server varchar variable, or common, use the following convention

--assuming a search character contained in the variable @SeekChar

--from a variable
SELECT LEN(@StringVariableName) - LEN(REPLACE(@StringVariableName,@SeekChar,''))

--from a table field
SELECT LEN(SomeTableCol) - LEN(REPLACE(SomeTableCol, @SeekChar, ''))
FROM SomeTable

--The Oracle  PL/SQL Variant (for the hell of it).
--All we have to do is replace the LEN function with LENGTH!
SELECT LENTH(SomeTableCol)-LENGTH(REPLACE(SomeTableCol, @SeekChar, ''))
FROM SomeTable

Quick and easy and no annoying looping constructs.

, ,
Problems????

Image by essecento via Flickr

You might be familiar with the following scenario:

You have a SQL Server database server hosting more than one database, but you just so happen to be accessing one in particular from your development environment (Query Analyser, Management Studio, etc).

So, you run a little query to automatically connect you to this database when you connect to the server. You might try something like:

Exec sp_defaultdb @loginame='DOMAIN\UsrNam', @defdb='SomeDatabase';

So, now when you log on, your database is ready and waiting to be used without having to manually select it. I have found this scenario common in development environments, when you are typically running lots of ad-hoc SQL queries, or running in code (stored procedures, views, triggers, etc) against the database.

However if you are working on different databases on the same server, defaulting to a particular one can cause you problems if you need to run in code against a different database. On occasions I have applied stored procedures to the default database, when I meant to do it against another one on the server!

Another consequence is when a development database is refreshed or rebuilt from some external source (from a live environment, for example). Although your default database may exist by name, this is not enough for SQL Server, which fails to log you on and gives the error message:

Cannot open user default database.

You will also get this if you have chosen to rename the database.

So what to do in this instance?

Well, we can defer to the trusty ISQL command line utility, as shown in the following screenshot:

Simply follow these steps:

  1. Open a DOS  command Prompt
  2. Open up an ISQL Session. I used the following command:
    • -S SQLServer_Name – Specify the name of the SQL Server to which you are connecting.
    • -E – Stipulate to connect as a trusted connection (Windows Authentication)
    • -d master – The database name to which you are going to connect. Here I specified ‘master’
  3. isql -S SQLServer_Name -E -d master

    The three options user above are as follows:

    Of course, you can connect however you want. For more options (also shown in the screenshot above), type in isql /? for a full list, or refer to the site on the above link.

  4. Now run a version of the above script, setting the default database to master
  5. Exec sp_defaultdb @loginame='DOMAIN\UsrNam', @defdb='master';
  6. To apply this, enter ‘GO’ on the next line.

This will ensure your SQL Server defaults you to the ‘master’ database in all subsequent connections (unless you change them again). I would counsel this as general good practice, and it comes recommended by a friendly DBA I happen to know.

, , ,

In JavaScript all functions and variables are often ‘hoisted’ to global scope, effectively making them global variables. This is potentially bad, as the following demonstrates:

if (true) {
    var x = 0;
}

alert(x); //alerts "0"

Here, even though ‘x’ is defined in a block, it is given global scope, and hence accessible outside the block. (For a demo, see http://jsfiddle.net/vRmLL/)

If we define our variable within a function, however, the scope is retained within the function. So the following will not work:

function SomeTest() {
    var y = 0;
}

alert(y); //undefined – a scoped variable - just what we want!

So, by using functions we can introduce scoping into JavaScript:

(function() {
    var z = 0;
})();

alert(z); //undefined – just what we want!

The last () executes everything in the body of the anonymous function. This pattern is called a closure.

This is a very simplistic overview of them. A more comprehensive one can be found in Morris Johns’ article JavaScript Closures for Dummies.

, , ,

I’ve been playing around with delegates and lambda expressions, and thought I would note down what I had learned (this is a programming notebook, after all).

    class Program
    {
        delegate int IntFunc(int a, int b);

        static void Main(string[] args)
        {
            IntFunc add = new IntFunc(delegate(int a, int b) { return a + b; });
            Console.WriteLine(add(1, 2));
        }
    }

I am particularly interested in how we can improve the following line with lambda expressions and a little syntactic sugar:

IntFunc add = new IntFunc(delegate(int a, int b) { return a + b; });

So, let’s start by turning it into a lambda-expression:

IntFunc add = new IntFunc((a, b) => { return a + b; });

Here, we say “parameters a and b of our deletegate ‘go to’ (=>) our block of functionality, which is defined in the {} braces.

But we don’t actually need the curly braces, or the return for that matter, as the lambda expression will infer this. So, we can reduce the expression down to the following:

IntFunc add = new IntFunc((a, b) => a + b);

However, looking at the original statement, we realise that the new IntFunc isn’t necessary either. It could have read:

IntFunc add = delegate(int a, int b) { return a + b; };

So, our lambda expression reduces down even further:

IntFunc add = (a, b) => a + b;

Marvellous! It also explains to me quite nicely how lambda expressions syntax derives from the equivalent delegate syntax.

But, we’ve not finished here. Take our delegate type:

delegate int IntFunc(int a, int b);

C# already has an in-built type func<> to save us from such declarations. We can dispense with the delegate line completely when we recognise that we have two inputs of type int and one output of type int.

Our final class looks like this

    class Program
    {
        static void Main(string[] args)
        {
            Func add = (a,b) => a + b;
            Console.WriteLine(add(1, 2));
        }
    }

Lovely!

, , ,

Have you ever wondered where C#, BASIC (yes, it’s an acronym), or C++ came from? What about Ruby, Python or PHP? And whatever happened to Algol, Ada or Pascal. And where exactly is Delphi at these days?

Well, you can see a timeline of these and forty or so other languages here at levenez.com.

Fascinating!

, ,

An incorrect placement of the ‘break’ statement within a loop will yield this error message. As shown in the following example:

function MyFunc(){
    var x = 0;
    for (x=0; x<10; x++){
        //do something
        break;
    }
}

Here, we will never reach the second iteration of the loop.The ‘break’ statement will kick us out at the end of the first time around. Break statements used in this way should always be wrapped in a conditional statement.

At best this is redundant code that was never cleared up properly, and, at worst, this is some sort of bug that is yet to be identified.

A Guide To JSLint Messages

This article is one of a series on the error and warning messages produced by JSLint.

, , ,