Wednesday, November 4, 2009

Structs and Stack Allocation

C# reference types get allocated on the heap just like Java. Unlike Java, however, you have the option of using the struct value type like all other value types (int, bool), on the stack.

Here's more detail on memory usage/location:

Sample Stack allocated struct:
public struct myReadResult {
public int BytesRead;
public bool Success;
}
...
public void ReadComplete(String theLine) {
ReadResult br; //Notice, no assignment or new keyword
br.BytesRead = theLine.Length;
br.Success = true;
return br;
}

Anti-pattern: Exceptions for Program Execution Flow

TIP: Exceptions are relatively expensive. It's generally wise to use them in exceptional conditions ONLY.
These details hold true for both Java and C#:

Tuesday, October 20, 2009

C# Pass By Reference

C# like Java passes parameters by value for value types and reference for reference types. However, you can override pass-by-value in C# for value types using the ref and out keywords. The difference between the two is that out does not require the parameter to have been initialized before being passed to your method.

Sample:

public void assignToRefForValueType(ref int refValType, out int outValType) {
refValType = 2;
outValType = 4;
}

public void testAssignToRefForValueType() {
int valueType = 0;
int outValueType;
assignToRefForValueType(valueType, outValueType);

//Prints 2, 4
System.Console.WriteLine("New values for ref and out: {0}, {1}",
valueType, outValueType);
}

Tuesday, October 6, 2009

Case Statement Superiority

C# allows you to switch on enums and strings in addition to the types supported by Java (char, int)!

Unlike Java, you have to explicitly specify fall-through if you have any logic in your statement.

Sample Java:
switch(myInt)
{
case 1:
callSomething();
case 2:
//this will still be executed for case one
callSomethingElse();
}

Equivalent C# Code:
switch(myInt)
{
case 1:
callSomething();
goto case 2;
case 2:
callSomethingElse();
}

Thursday, September 10, 2009

Debugging in Visual Studio

You are going to spend 30-40% of your development time debugging code. 

Here are some power-user tips to maximize your effectiveness during this time: 

Wednesday, August 26, 2009

UML: Sequence, Activity or State?

Do you find yourself torn between modeling types for your component? These rule-of-thumb's from Martin Fowler's book "UML Distilled" may come in handy!!

"You should use sequence diagrams when you want to look at the behavior of several objects within a single use case. Sequence diagrams are good at showing collaborations among the objects; they are not so good at precise definition of the behavior."

"If you want to look at the behavior of a single object across many use cases, use a state diagram."

"If you want to look at behavior across many use cases or many threads, consider an activity diagram"

Tuesday, August 25, 2009

Simple Semantic Context Brought to You By Typedef

You vote: Confusing as heck or extremely useful?

The typedef specifier in C++ allows you to add that little bit of context that may be missing from your variable's type.

The following statement tells us that our property is not just a collection of Strings, but a collection of Strings that represent first names. 

typedef vector<bstr> FirstNames;
FirstNames mtvFirstNames;

The variable name implies the additional type context to me, but hey... I'm a Java developer.
Is there some uber niche where this comes in really handy?

Thursday, July 30, 2009

COM Programming

Supporting ActiveX controls or COM objects?
Clueless like me?

Get started on the right foot with MSDN's Component Development Guide: 
http://msdn.microsoft.com/en-us/library/ms809977.aspx

Wednesday, July 29, 2009

Strings in the Wild

There's so much more to Strings in Visual C++ than just character arrays!

Check out this article on different implementations and where they fit in your project: 

Hello World

Welcome to my Tech Tidbits blog!

Vision:

This blog will be my weekly diary of things I've encountered in the industry. Topics will range from nitty-gritty details of the programming languages and tools I use, all the way up to architectural philosophy and organizational behavior. 

Goals:
  • Record the tips&tricks I come across (to remind myself later)
  • Solicit feedback on industry standards and personal preferences
  • Track my thoughts and musings on industry trends, tools and work preferences