29. May 2014

VB - The Good Parts (for C# peeps)

GW Basic. Remember it? -- from WikiPedia: (http://en.wikipedia.org/wiki/File:GW-BASIC_3.23.png)

There’s a great book on JavaScript called JavaScript: The Good Parts that every frontend developer should own. It does a great job of highlighting both the bad and good facets of the language. In that vein, here are some handy tips about VB to help you, intrepid C# programmer, find success.

1) Ignore the logical operators And and Or. These operators do not short-circuit, so an expression like

If a IsNot Nothing and a.Foo > 3 Then Debug.WriteLine("booo")

will give you a NullReferenceException. Instead use AndAlso / OrElse.

If a IsNot Nothing AndAlso a.Foo > 3 Then Debug.WriteLine("yay")

2) IsNot adds a touch of readability. Compare:

If Not a Is Nothing Then ...

vs.

If a IsNot Nothing Then ...

It’s more like natural language, which is VB’s appeal after all.

3) Avoid IIf() for ternaries. Instead, use If(). The former is not a part of the core language so much as a built-in function, and all of its arguments are evaluated:

Dim x as Integer = IIf(a IsNot Nothing, a.Foo, 0) ' NullReferenceException because a.Foo is evaluated

The latter, added in VB 9, maps to the same ternary operator that ?: does in C#.

Dim x as Integer = If(a IsNot Nothing, a.Foo, 0) ' Proper short-circuiting = no problem!

4) Nothing is not null — it’s default(T).

Dim str as String = Nothing ' This is null.
Dim lng as Long = Nothing ' This is 0.
Dim dt as DateTime = Nothing ' This is DateTime.MinValue.
Dim dt as DateTime? = Nothing ' This is null again!

Personally, I’d only use Nothing as a synonym for null and avoid the other usages.

5) Remember, Is is for comparing object types, while = is for comparing value types.

Dim obj as Object
If obj = Nothing Then Debug.WriteLine("boo") ' Error: obj is an object
If obj Is Nothing Then Debug.WriteLine("yay") ' Will work
Dim dt as DateTime
if dt = Nothing Then Debug.WriteLine("oy") ' Also will work... but didn't I just say to not do this?
if dt = DateTime.MinValue Then Debug.WriteLine("you're learning!") ' Much better

Comments

10. April 2014

Async-Await Apologia

Work in progress -- by Alexander Baxevanis: https://www.flickr.com/photos/futureshape/4000128011/ (http://www.victorianweb.org/art/illustration/tenniel/lookingglass/1.4.html)

This update is long overdue. Previously, I wrote:

But as I learned today, once you do hit the await statement, you are still leaving the stack. And when the await returns, the place you come back to may not be identical to the place you left, depending on how it got there.

The fsw.Created handler is called on a different thread than the one you just came from. That’s a pretty well-known fact and is why the FileSystemWatcher has a SynchronizingObject property to help WinForms programmers navigate their way back to the UI thread.

Well, it turns out my analysis was completely wrong. Actually, my application was modifying files in a folder that belonged to an ASP.NET website. Modifying folders inside of ASP.NET can cause application restarts. Though I’ve moved on from this project, I now believe that the behavior I was seeing had to do with the timing of the ASP.NET restart, and had nothing whatsoever to do with crossing thread boundaries. Since async/await uses continuations, any differences I was seeing between where I came from and where I got to had to occur outside of the bounds of my program.

We live, we learn.

Comments

26. December 2013

The 2013 Holiday Card

My 2013 holiday card

I had a blast putting this together. Thanks to my friend Russell Williams for the photography and photoshopping!

Russell also put together this big-headed version of the same. It was a tough call but I decided to play it straight for the physical cards. Enjoy!

Look at the big heads!

Comments