Warnings GaloreMy dearest VB.NET programmers (and C# programmers up to a point), I once again have to inform you of one of my pet peeves. This is pretty much a follow-up to one of my previous posts, but more than two years after, I still work in projects from various online sources as well as professionally that are littered with the warnings about undefined variables:

warning BC42104: Variable 'X' is used before it has been assigned a value. A null reference exception could result at runtime.

At runtime, these warnings usually translate into the dreaded NullReferenceException:
NullReferenceException was unhandled, Object Reference not set to an instance of an object.

So, what causes this and how can can we fix this? Let’s take a look at a piece of code that demonstrates something incredibly common, especially in VB circles:

Dim MyString As String

If ThisAndThat Then
    MyString = "Some Value"
End If

...

SomeFunction(MyString)

If the boolean ThisAndThat evaluates to False, MyString will never be defined, and a NullReferenceException will arise when the SomeFunction function tries to do anything with MyString. Remember that The System.String class does not have a default constructor, so no constructor is implicitly called. MyString is NOT String.Empty, but NULL.

To fix this, all you need is an Else statement in your code and some default value that you’d like to assign to MyString:

Dim MyString As String

If ThisAndThat Then
    MyString = "Some Value"
Else
    MyString = String.Empty
End If

...

SomeFunction(MyString)

in fact, the best way to handle default values is to define the variable upon declaration:

Dim MyString As String = String.Empty

If ThisAndThat Then
    MyString = "Some Value"
End If

...

SomeFunction(MyString)

Or you could even eliminate the If statement entirely by using the IIf function:

Dim MyString As String = IIf(ThisAndThat, "Some Value", String.Empty).ToString()

...

SomeFunction(MyString)

And even though I’m using the class System.String as an example, this should be applicable to any variable of any data type. It’s a good practice and will eliminate many of those pesky runtime errors that can be difficult to trace.

The next tutorial covering some basic data types and in introduction into variables using the C programming language is now available. Make sure to watch the 1080p version if your connection allows it:

C Video Tutorial 01: Hello, World!

Due to time restrictions, all of the promised future C tutorials will be in video format. Here’s the video reworking of my previous written “Hello, World!” tutorial (make sure to watch it in 1080p if you can):

I hope you learned something, and if you have any comments or questions, please let me know by posting a comment below.

C Bitwise Operations Cheat Sheet

Operations Table

Operation Sample C Operator
AND 1001 AND 0101 = 0001 & (ampersand)
OR 1001 OR 0101 = 1101 | (pipe)
NOT NOT 1001 = 0110 ~ (tilde)
XOR 1001 XOR 1011 = 0010 ^ (caret)
LEFT-SHIFT LEFT-SHIFT 0001 BY 1 = 0010
LEFT-SHIFT 0001 BY 3 = 1000
<<
RIGHT-SHIFT RIGHT-SHIFT 1000 BY 1 = 0100
RIGHT-SHIFT 1000 BY 3 = 0001
>>

C Assignment Operators

Operation Syntax Short For
AND X &= Y; X = X & Y;
OR X |= Y; X = X | Y;
XOR X ^= Y; X = X ^ Y;
LEFT-SHIFT X <<= Y; X = X << Y;
RIGHT-SHIFT X >>= Y; X = X >> Y;

Setting & Checking Bit-Flags:

#include <stdlib.h>
#define FLAG_ONE 0x0001		/* 0001 */
#define FLAG_TWO 0x0002		/* 0010 */
#define FLAG_THREE 0x0004	/* 0100 */

int main(void)
{
	unsigned int Flags = 0x0000; /* Initialize to zero */
	Flags |= FLAG_ONE; /* Flags now contains 0001 */
	Flags |= FLAG_THREE; /* Flags now contains 0101 */

	/* will return EXIT_SUCCESS since we never set FLAG_TWO: */
	return (Flags & FLAG_TWO) ? EXIT_FAILURE : EXIT_SUCCESS;
}

Counting Set Bits:

#include <stdlib.h>
#include <stdio.h>
#define BITS_PER_INT ( sizeof(unsigned int) * 8 )

int main(void)
{
	unsigned int Flags = 0x0115; /* Dec: 277, Bin: 100010101 */
	unsigned int Mask = 0x0001;
	unsigned int Count = 0;
	unsigned int i = 0;

	for (i = 0; i < BITS_PER_INT; ++i, Mask <<= 1)
		if (Flags & Mask)
			++Count;

	// The following line should print: Flags contains 4 set bits.
	printf("Flags contains %d set bits.\n", Count);
	return EXIT_SUCCESS;
}

Five Quick Check-In Guidelines

If you work in a source controlled environment, these are the minimal guidelines that you’ll have to stick to for making your teammates’ lives a bit easier.

  1. Don’t Check-In Broken Code: More than often, you’ll pull down the latest version of a project from your repository only to find that the code won’t build. Please, don’t check-in code to the main project that doesn’t compile, even if you intend to pick up the slack the next day. However:
  2. Don’t Go Home Without a Check-In: Even though you shouldn’t check-in your broken code to the main branch, many source control systems have a way to branch a user’s changes and merge them back later. If you’re using Team Foundation Server, take a look at Shelve Sets.
  3. Use Check-In Comments: Always leave a descriptive comment of the work you’ve done when your check-in. Your teammates weren’t hired to analyze undocumented code, so that 500-line change across multiple files better have a comment.
  4. Check-In Association: If your source control system allows this type of integration, associate your check-in with a work item, bug, or help-desk ticket. If it does not, at least make a reference to the work item’s identifier in the comment section.
  5. Use Check-In Policies: Use check-in policies to ensure that all of the above things aren’t impeding your build if your source control system supports them. These policies make your life easier and disallow sneaky developers from checking-in their broken spaghetti code.

Share any tips you may have!

C Tutorial 01: Hello, World!

If you ask a programmer which programming language you should learn as your first, they’ll often prescribe you their personal favorite, often not considering if the language is supported on multiple platforms, easy to learn, exposing underlying system mechanics, and fully featured.

This post is the first of a series of tutorials intended to teach you the C programming language, an excellent first language because of the following reasons:

  • Supported on all major operating systems
  • Relatively straight-forward syntax
  • Mature and stable feature set
  • Used in many articles, books, and other publications
  • Easy transition into higher-level languages


Read the rest of this entry »

CONFIRMED: Windows is going ARM

Microsoft confirmed today at the 2011 CES that the next generation of the Windows operating system will indeed run on ARM processors, following wild rumors and speculation this past week.

Windows on ARM processors means that the operating system is now capable of running on a plethora of mobile devices, thus opening up an entirely new market segment for Microsoft. Is this the end of Windows Phone? Will Windows 8 be the new Microsoft mobile OS?

EDIT (01/06): Not much more info was released in last night’s keynote speech by Steve Ballmer. However, the implications of having the full-fledged Windows operating system on a mobile device such as a phone are tremendous. Android and iOS will have to pick up some speed to compete with the OS that has been in the making since 1985 and has excellent hardware and software support e.g.: multi-threading, scheduling, peripheral support, .NET Framework, WPF, Win32 API, etc., etc.

Thoughts on Sandy Bridge’s HD 3000 IGP

Intel LogoSandy Bridge is Intel’s new microarchitecture, slated for release in January of 2011. Sandy Bridge processors will effectively phase-out the current lineup of processors, based on the LGA 1366 socket, with processors fitting a brand new socket, LGA 2011.

While I understand the need for technology to move forward, and am an avid user of Intel CPUs myself, I can’t help but feel underwhelmed by the reviews, especially on the IGP (integrated graphics processor) front, the part that concerns me greatly.

The tech that Intel chose to use on the die is the HD 3000, and it’s little brother the HD 2000. Let’s hope it’s nothing like the GMA (Graphics Media Accelerator) X3000 and family, the scourge of the entire IGP world. Please, Intel, please.

Read the rest of this entry »

« Previous Entries Next Entries »