API's that Suck

February 27, 2011

CIL Parameter Passing: By-value, By-ref, and Typed Reference

Filed under: Uncategorized — Grauenwolf @ 1:33 am

The common language runtime supports three kinds of parameter passing.

By-value

By-value parameters work as one would expect. Primitive types are simply copied onto the stack, as are value types, object references, managed pointers, and unmanaged pointers.

CIL Type: typename

By-ref

By-ref parameters of course references to other values. Any of the types that can be passed by value can also be passed by reference. However there certain restrictions. In order to get a reference to a value, that value must have a home. A home can be any of the following:

  • An incoming argument
  • local variable of a method
  • An instance field of an object or value type
  • A static field of a class, interface, or module
  • An array element

At first glance the list appears to be comprehensive, but something is missing. Intermediate values, those which are on the stack but are neither a local nor a argument, do not have a home and thus cannot be referenced.

CIL Type: typename&

Typed Reference

A typed reference parameter is a reference to a home matched with the argument’s type information. Normally it isn’t necessary to explicitly pass the argument’s type information as one of the following is usually true:

  • The argument is a value type that matches the parameter type
  • The argument is a reference type and thus the type information is encapsulated.
  • The argument is a boxed value type, again with the type information encapsulated.

It is only when the argument is an unboxed value type that doesn’t necessarily match the parameter type that a Typed Reference is needed. This will never occur in a early-bound language such as the original version of C#, but it can in late-bound language such as VB with option strict turned off.

does and not

one that combines a by-ref parameter with extra type information. The reason this is necessary is that it is possible for an argument to otherwise lose it’s type. This occurs when…

  1. The parameter is a value-type. Value-types do not internally store their type unless boxed.
  2. The parameter is passed without boxing.
  3. The argument’s type doesn’t necessarily match

CIL Type: typedref
CLR Type:
System.TypedReference

Advertisements

February 13, 2011

Meditations on Testing

Filed under: Uncategorized — Grauenwolf @ 1:20 am

There is never enough time, so first test that which you are uncertain about.

If it is easy to test, it is easy to write. If it is easy to write, it didn’t need to be tested.

Test the way the program will be used, not the way it should be used.

A wise student tests his code. A wise master tests everyone else’s.

A test is not a debugger. Nor is a debugger a test. Learn them both.

The student saw asks his master, “Why do our tests take so long to run?”
The master replies, “The services are slow.”
The student then asks, “Why don’t you mock the services so we can run the tests faster?”
The master deletes all of the test code and says, “The tests are now fast.”

All combinations of user input should be tested. Configuration files are user input.

To not test what the compiler can prove.

Play with the clock.

If a child can break it in an hour, and adult can in 5 minutes.

Create a free website or blog at WordPress.com.