May, 2006

Posts
  • Eric Gunnerson's Compendium

    C# Trivia Test - Part 11

    • 28 Comments

    Here we go, with the last installment of the series...

    Language Details

    1) How is decimal different from other C# types?

    2) What kind of constructor is not legal on structs? Why?

    3) What’s the difference between “out” and “[out]”?

    4) When you write ulong, what does the runtime see?

    C# and the Runtime

    5) What interfaces does foreach use in C# 1.1? What about 2.0?

    6) What is the name of the attribute that controls the usage of an attribute?

    C# and other Languages

    7) C# and C++ programmers are at a party (okay, it's a really a user-group meeting). The mood is too nice. What topic do you bring up?

    Real Triva

    8) What is chapter 11 of the C# Language spec?

    9) For a while, C# had a mascot. What was his name? Extra points for his full name.

    10) What is my most useless computer-related skill? Hint: It's not my rudimentary knowledge of 6502 assembler.

    Power Users

    11) Why does the compiler put "nop" (ie no op) instructions in my IL?

    12) I have an extraordinary power. I can diagnose problems with P/Invoke statement psychicly. You're having a problem, you come to me, and I say, "Change the <x> in your definition to <y>". And I'm right, about 83% of the time.

    What is <x> and what is <y>?

    Ancient History

    13) Which one of these was a C# codename before disclosure:
     
    a) Awesome!
    b) C+++-
    c) C-Sure
    d) Safe-C
    e) C how quick I can write code now!
    f) The C clearly system

    14) C# was first talked about widely at a PDC. What year was it? Where was it held, and how hot was it?
     
    a) 98 degrees
    b) Hotter than a jalapeno in a heat wave
    c) Hot enough to make your palms sweat at 7:00 AM
    d) Hot enough to lose 20 pounds in 20 days, ask me how!

  • Eric Gunnerson's Compendium

    Seven Deadly Sins of Programming - #6

    • 22 Comments

    Seven Deadly Sins of Programming - #6

    Way back in my early career, I worked on a graphics program written in FORTRAN that was used to plot engineering data.

    It ran on Tektronix 4014 terminals.

    Our application - known as E.G.G. (Engineering Graphics Generator, one of a whole series of not-very-creative punny names used by the group) - used a menu-based approach. The user would place the cursor (moved by the thumbwheels on the right) over a menu option, hit the spacebar, and then the code would figure out what to do.

    The internal architecture of the app was really pretty good. There were, if I recall correctly, 39 different screens, and each item on the screen had an id associated with it. The code would find the right item on the page, come up with a numerical code (menu number * 100 + item number), find that in an array of function pointers, and then call the appropriate function.

    It worked fine, and allowed us to have a batch mode as well.

    But after I started playing around with it a bit, I noticed that the command dispatch was a bit slow. After you selected an item on a screen, it took at least a couple of seconds for the screen to erase and redraw (this was running on shared Vaxen, so you didn't get much CPU time, and what you got was fairly slow).

    I did some digging, and found that the original developer (no longer with the group) had written the item lookup code using a Fibonaccian search.  This is a search that is similar to binary search, but doesn't require complex operations such as divide by two or shift right (that alone should tell you the vintage of this search).

    Such a search *may* have made sense on earlier systems, but on Vaxes, both the divide by two or shift right are in the hardware. So, we were using a search method optimized for constraints that we didn't have that nobody in the group understood, in an attempt to be faster than binary search.

    But it was a *clever* algorithm.

    At this time, we had about 1200 items in our dispatch table. The first thing I did was build sub-index that stored the starting index of the first item in each menu, which meant the search space for a typical menu item went from 1200 items to about 30 items. The second thing I did was switch to interpolation search, which, given the nearly perfect distribution of items numbers within a menu (most menus had item numbers from 1-n, with very few holes), gave an almost perfect search. And it was code that most people in the group could understand.

    So, the dispatch time went from a few seconds to about half a second, and I wrote up a "cost savings" in which I multiplied the number of users we had by the amount of time they used the application by the number of menu picks per hour, and figured out that I had saved the company around $30K per year. For which I got a $100 gift certificate.

    So, what's the point of that story?

    Well, it makes me look good, which is the primary goal. It also illustrates that cleverness is an over-rated attribute of developers. How many times have you seen:

    for (int i = 0, j =bounds; i < upper; i++, j += JUMP_SIZE)
    {
        ...
    }

    That's somebody who is trying to be clever. They should have written:

    int j = bounds;
    for (int i = 0; i < upper; i++)
    {
        ...
        j += JUMP_SIZE
    }

    Which takes us on a long and tortuous trip to

    Sin #6 - Inappropriately clever code

    What examples of this have you seen?

  • Eric Gunnerson's Compendium

    Review - Operation Mindcrime II (Queensryche)

    • 17 Comments

    I own a fair number of Queensryche (yes, I'm missing the umlaut over the y (a sure sign of 1980s metal pretentiousness)) CDs, but the band has never been a favorite of mine, mostly because of the varying quality of their albums. Though they do get a few points for being from Bellevue...

    But Operation Mindcrime is an album that I've listened to hundreds of times. Looking through my musical choices, I tend to like groups that 1) are complex musically, 2) have vocals that are challenging to sing, and 3) rock.

    This album has all of that in spades, especially #2, with the Geoff Tate's vocals taxing my voice even when I was much younger. There are few guys around who have that kind of range and power. The music shows both a layering and minimalism, with Tate's vocals playing lead, and the other instruments filling in the breaks between the vocals.

    So, that's all good. And it's layered around a pretty good story of corruption, drug use, crime, etc. The main character gets involved, bad things happen, and he takes the fall.

    So, now it's 18 years later, and the group decided to do a sequel to the original. And how did they do?

    I should preface my answer by saying that I don't expect bands to stay the same musically over the years. So I expect something different.

    If you read the Amazon reviews, they are mostly polarized. Some people love it, some people hate it.

    I listened to it a few times, took a week off, and just listened to it again. There are a few decent songs on the album, but overall it falls short.

    The first problem is the story. There doesn't seem to be the same edge as there was in the original - some of it is okay, but we get lines like, "I've always been afraid, except in the moments that I loved you". The character starts out pissed off, and then it just sort of peters out at the end. But what's really missing is the underlying subtext of the original ("I used to trust the media to tell me the truth... who do you trust when everyone's a crook?") What happened to the revolution? The sequel is just about some guy who doesn't seem to believe in anything any more, so it's hard to care.

    The second problem is the production. The original has a ton of ambient sound, music, and spoken word - the same technique used so well in The Wall - and that serves to tie the various songs together and to do exposition about the story. That's mostly missing from the sequel.

    And finally, the music. Well, to be fair, there are some decent songs here. But the mix isn't very good - there's a lot of "wall of sound" approach - and while in the past Tate would have been able to lay vocals over the top of that, now his voice often gets lost in the mix. So, you get a muddy sound, and it's hard to pick out the lyrics. I also get the feeling that the songs in the album were mixed as individual songs rather than an overall mix - there's no overall feel to the songs.

    The best songs? "I'm American" is pretty good. "The Hands" lifts a riff from the original, and is also okay.

    But, please, spare me tracks like "The Chase", which features Ronnie James Dio in a duet with Tate that could easily be a number from a musical (not that there's anything wrong with that), with the two of them singing over the top of each other.

    Verdict: It's okay. If you have the original, you might want to buy the sequel. If you don't have the original, go and buy that one instead.

  • Eric Gunnerson's Compendium

    C# Trivia Test Answers - Part 11

    • 16 Comments

    C# Trivia Test - Part 11

    (My initial set of responses vanished into the bit bucket due to the wonder that is on-line blog editing, so you'll have to do with the following, absent most of the humor and wittiness that was present in the first version)

    Here we go, with the last installment of the series...

    Language Details

    1) How is decimal different from other C# types?

    Decimal is from Mars, int is from Venus. Or perhaps it's the other way around.

    Try the following experiment:

    a) Write a short C# program that adds two integers.

    b) Look at the generated IL.

    c) Do the same thing with two decimals.

    The difference is that decimal is implemented as a user-defined type, while types like int have specific opcodes devoted to them.

    2) What kind of constructor is not legal on structs? Why?

    A default constructor. Why? Because in some interop scenarios, the runtime needs to create structs, but it has no way of running any managed code.

    At least that's what I heard...

    3) What’s the difference between “out” and “[out]”?

    One has brackets around it, the other doesn't.

    I'm surprised nobody got that one.

    If you're looking for another difference, "out" is all about definite assignment, and lets you skip initializing a variable that is passed by reference, while "[out]" is a directive telling the marshaler what to do.

    4) When you write ulong, what does the runtime see?

    Throatwarbler Mangrove.

    Or, perhaps it's System.UInt64. Definitely one of those.

    (extra credit: Discuss, without any websearches, the relation of the above reference to naval ambitions.)

    C# and the Runtime

    5) What interfaces does foreach use in C# 1.1? What about 2.0?

    IEnumerable, IEnumerator. IEnumerable<T>, IEnumerator<T>.

    Extra credit: Why is the generic version of this so much cooler?

    6) What is the name of the attribute that controls the usage of an attribute?

    AttributeUsageAttribute

    C# and other Languages

    7) C# and C++ programmers are at a party (okay, it's a really a user-group meeting). The mood is too nice. What topic do you bring up?

    Well, I would bring up deterministic finalization, otherwise know as the "why can't you run my destructor when my variable goes out of scope?" question.

    Real Trivia

    8) What is chapter 11 of the C# Language spec?

    There are two answers to this question. One is "structs", the other is something I'm too lazy to look up right now.

    (extra credit: explain why there are two answers.)

    9) For a while, C# had a mascot. What was his name? Extra points for his full name.

    His name is Andy. No credible evidence has been found to document his first name, but some sources list it as "Angry".

    10) What is my most useless computer-related skill? Hint: It's not my rudimentary knowledge of 6502 assembler.

    Back in my formative years, I got up extra early in the morning to back up my university's data center. Months of careful practice led to me mastering the art of mounting a backup tape with one hand - and this wasn't just the "put it on the machine with one hand", it was the full "pop off the outer tape ring, put it on the spindle and hit the load button faster than you can say "Bob's your uncle"".

    And then, with more months of practice, I progressed to to a new level, when I was able to do that with both hands simultaneously.

    It wasn't very useful 2x years ago, so it's certainly not useful now...

    Power Users

    11) Why does the compiler put "nop" (ie no op) instructions in my IL?

    On some systems, you can encounter synchronization issues if one process runs significantly faster than another. The compiler therefore contains an analysis engine that looks at your code, and if it is especially fast, inserts some nop instructions to slow the speed to down to a safe level.

    So, consider this a compliment by the compiler about the code that you wrote.

    Or, perhaps it has something to do with giving the debugger a place to put a breakpoint. I forget.

    12) I have an extraordinary power. I can diagnose problems with P/Invoke statement psychicly. You're having a problem, you come to me, and I say, "Change the <x> in your definition to <y>". And I'm right, about 83% of the time.

    What is <x> and what is <y>?

    <x> = "messed-up word"
    <y> = "the right word"

    Or,

    <x> = "long"
    <y> = "int"

    In C++, "long" means 4 bytes. In C#, it means 8 bytes. Using long when you mean int makes the runtime angry.

    Ancient History

    13) Which one of these was a C# codename before disclosure:
     
    a) Awesome!
    b) C+++-
    c) C-Sure
    d) Safe-C
    e) C how quick I can write code now!
    f) The C clearly system

    "d" is the correct answer, and is one of two names used on C#.

    I'm sworn to never reveal the other codename. I've thought many times about writing about it any way, because it would make a great story. I've even started to write a post.

    But in the end, I decided that doing that wouldn't be cool.

    14) C# was first talked about widely at a PDC. What year was it? Where was it held, and how hot was it?
     
    a) 98 degrees
    b) Hotter than a jalapeno in a heat wave
    c) Hot enough to make your palms sweat at 7:00 AM
    d) Hot enough to lose 20 pounds in 20 days, ask me how!

    Reliable sources suggest that the year was 2000, and that it was held in a hotel steam bath. Others suggest that it was held in Orlando, and further others maintain that there is no practical difference between the two, except that in the hotel steam bath you may ask the attendent to fetch you a drink.

    As for how hot it was, all four answers are correct, though the first one is a bit of a underestimation.

    Why Orlando in July? Well, unless you know exactly when your software will be done, it's hard to book early enough to get a nicer venue.

  • Eric Gunnerson's Compendium

    Anybody working on an interesting hobby project?

    • 15 Comments
    Anybody working on an interesting hobby project?
     
    I finally got around to firing up my Atmel STK500 development board, after the untimely death of a wireless router gave me the perfect power supply to use. That will get used on some LED-based holiday lights in a project that I haven't fully scoped yet.
     
    My current project, however, is a hill-mapping application, in my never-ending quest to map the gradients of the hills that I cycle on. Topo data isn't good enough to tell you what you really want to know, so a couple of weeks ago I sent in a order for an ISU Inclinometer, which will give me the direct measurement of angle through a serial interface.
     
    I then need a data-logging solution. I have an old PocketPC sitting around that would work for the level information, but to do the whole solution I need a way to get one more bit of data into the pocketpc, so I can measure distance with a magnetic wheel sensor. I may be able to hack something in using the IR port.
     
    So, what are you doing?
  • Eric Gunnerson's Compendium

    Seven Deadly Sin of Programing - #7 - Excessive coupling

    • 13 Comments

    (these will be in reverse order...)

    #7 - Excessive Coupling

    We start out with a simple one.

    Coupling is obviously a necessary evil. The Console class, for example, wouldn't be able to send text through Win32 without being coupled to Win32.

    Given that, less coupling is nearly always better that more coupling. A few cases that I see:

    1. Using switch statements rather than polymorphism or interfaces.
    2. Classes that do too much, rather than being focused on a single goal.

    What do you think? Do you have more examples?

  • Eric Gunnerson's Compendium

    The Seven Deadly Sins of Programmers

    • 9 Comments

    A while back, I made an offhand comment about something being one of the seven deadly sins of programmers (/programming/developers/software engineering/coding/...).

    At the time, I really didn't have 7 things in mind, but after a little thought, I came up with what I think is a good list.

    But before I write the first entry, I'd like you to spend 5 minutes and write down your list of deadly programmer sins. Though 7 is the canonical number of sins, your list can have any number of items. After I've gone through my list, I'll do another post where you can share your list.

    [Update: Sorry I wasn't clearer. Write your list down, and then post it when I'm finished. Or post it now on your own blog if you'd like]

  • Eric Gunnerson's Compendium

    Yelling at car drivers...

    • 8 Comments

    While bicycles don't have horns, I do have some decent lungs, and every once and a while I'll yell *HEY* loudly at a car driver to get their attention before they do something stupid. It works often enough to make it worthwhile, and even when it doesn't, it makes me feel better.

    On Saturday, a van driver totally didn't see me, started to pull out, and I yelled as loudly as I could. In this case, however, it didn't have the desired effect on the driver, as I was sitting in the passenger seat of our 328. It did, however, have an effect on my wife, who was able to take appropriate evasive action.

  • Eric Gunnerson's Compendium

    Retire Shuttle Now

    • 8 Comments

    Dave Brody advocates retiring the shuttle now - or at least after servicing Hubble one more time.

    I've got to agree. A few things are clear to me:

    1. Shuttle is old, and getting older.
    2. Shuttle cannot fly the number of flights that make ISS a going concern (presuming, for the sake of argument, that ISS is worth continuing). Without a crew return vehicle and/or quick shuttle access, you can't put enough people on ISS to make it worthwhile.
    3. Every flight on the current shuttle manifest is an ISS mission.

    Shuttle costs $4.8 billion a year. I think the question to ask is:

    What could a streamlined project achieve in 5 years with a 20 billion dollar budget? Would the result be cheaper to operate than shuttle?

    Given the right approach and design philosophy, I think the answer is a definite yes. Or, to put it another way, what would you get if you gave Burt Rutan $1 billion to develop a vehicle?

    If you ask the other question:

    What is the chance that NASA can develop a follow-on to shuttle while still flying shuttle?

    The answer is "slim to none"

    Note that if you do develop a nice follow-on to shuttle - perhaps a vehicle that you can use to, I don't know, perhaps "shuttle" people from the earth to orbit - you can use expendables to launch ISS modules, and then your new vehicle to get people up there for assembly.

    Will it happen? Seems pretty unlikely to me. The politics of the situation do not favor the right thing happening, and NASA has always been driven by politics.

    (Via Phil Plait's excellent Bad Astronomy)

     

  • Eric Gunnerson's Compendium

    Random sometimes, not random other times

    • 5 Comments

    From a email:

    private void button1_Click(object sender, EventArgs e)
    {
       Customer c = new Customer();
       c.Randomize();

       Customer b = new Customer();
       b.Randomize();

       MessageBox.Show(string.Format(
            "object c random number = {0}, object b random number = {1}",
            c.RandomNumber, b.RandomNumber));
    }

    public class Customer
    {
       private int random = 0;

       public void Randomize()
       {
          Random r = new Random();
          random = r.Next();
       }

       public int RandomNumber
       {
          get { return random; }
       }
    }

    If I run the above code without debugging it always returns the same random number, but if I step through the code I get different random numbers.

    ******

    This is a variant of a question that comes up fairly often.

    When you create an instance of the Random class, the pseudo-random number generator needs to be seeded with an initial value. Some generators always seed with zero, which means you always get the same sequence of numbers. The .NET Random class seeds using the time, which means you get different results from run to run without having to set the seed yourself.

    It also means, however, that if you create two Random instances one right after another, the seed value (I think it uses GetTickCount()) hasn't had time to change, and you get the same sequence. But if you run it in the debugger, enough time passes between the creation of the two instances that you get different seeds and different numbers.

    I think the best solution here is to move the Random instance to a static field:

    static Random r = new Random();

    And then just use that instance of Random from all the Customer instances.

  • Eric Gunnerson's Compendium

    Sacrifices

    • 4 Comments

    Sacrifices are part of parenting. And as a long-time parent, I've made a fair number of them. Sitting up with a sick child. Coaching soccer games. Going to plays and recitals. That sort of thing.

    You don't have to like it. You just have to do it, and not let your real opinion about the activity show. It's part of being a good parent.

    My daughter will be driving in a few years, and I know that will bring its own sacrifices. Insurance costs. Fuel costs. Time spent worrying about her safety.

    There's not much that I can do about the first two, but there is something I can do about the last one. A way to make a pre-payment on the sacrifice, as it were. Like going to the dentist - you don't want to, but you know that you should.

    So yesterday, I rearranged my schedule so I could have the afternoon free, picked up my daughter, and took her Karting.

    There's a fair amount of research that shows that inexperience is one of the leading causes of car accidents among teenagers. They just aren't very good drivers, especially if they get into situations where they need to make quick decisions.

    I don't want that to happen to my daughter.

    So if I have to repeatedly strap myself into a low-slung high-performance electric kart and fly around a small indoor track at speeds of up to 30MPH, or even try to learn how to drift smoothly through the corners without spinning out, if that's what it takes for my daughter, I'll do it. Without complaint. Because that's the kind of father that I am.

    I'm just hoping I don't have to follow that up by doing this.

  • Eric Gunnerson's Compendium

    My favorite driver's ed moment

    • 4 Comments

    One of the comments on my last post reminds me of a story.

    Back when I took driver's education  - which would have been the fall of 1979 - one of the modules of the class was spending time in the "simulator".

    I used quotes around term "simulator", because what it really was was pretend driving. Less like playing a first-person driving game - not even like playing Pole Position - but more like playing with one of these:

    But less interesting...

    Here's how it worked.

    You sat in a room with about 12 other students. In front of you you had simulated controls - there was a speedometer, a steering wheel, turn signals, brake, and accelerator. At the front of the room there was a movie screen, on which you would watch a movie taken out the front of a car, and while watching, you would pretend to drive.

    The sole feedback system consisted of a red light on your console. If the system - assuming it's not to grandiose to call $2.00 of electronic components a system - thought that you were outside the acceptable parameters, it would light up the red light, and you would come to the attention to the drowsy shop teacher who had to get up at 4:30 to run this thing. I think it also logged your non-compliance, probably using cuneiform.

    Each of the consoles had a speedometer, which ran off a motor which got its input from the accelerator and throttle. That helped everybody develop the all-important skill of keeping their fake speed correct.

    Rudimentary motor control skills were enough to be compliant in most cases - presuming you could figure out what was coming up in the old, scratch movie. One day we were doing some freeway driving - which meant I was "traveling" at the highly patriotic double nickel - and we got into an interchange to go onto another freeway.

    Or at least that's what I thought it was. But it turned out that it was a right-turning exit that ended in a stoplight. By the time I realized that, I only had about a second before the car was coming to a stop. I spun the wheel, started braking, and as the car in the movie stopped, I looked down, and noticed that my speedometer was dropping through 40 MPH. It took at least another second before it dropped to zero.  I took a deep breath, and waited for the approach of Mr. Schmeer.

    And the red light stayed off.

    Turns out that the system didn't check for your speed - only for your inputs. I was on the brakes as I came up to the light, and that was good enough for it. Similarly, if you were turning to the right, it didn't differentiate how far you turned. You just needed to turn.

    Simulator class was a lot more fun after that.

  • Eric Gunnerson's Compendium

    Using GC Efficiently

    • 2 Comments

    Spending lots of time on C++ means I haven't been paying as much attention to managed code as I did in the past, and I missed an excellent series of articles by Maoni about the GC. They are:

    Using GC Efficiently – Part 1

    Using GC Efficiently – Part 2

    Using GC Efficiently – Part 3

    Using GC Efficiently – Part 4

    Go read Maoni's blog for lots of other useful information (Large object heap, etc.)

  • Eric Gunnerson's Compendium

    In case you were worried, I'm okay...

    • 0 Comments

    Zombies attack Seattle hospital

     

  • Eric Gunnerson's Compendium

    New LINQ bits now available...

    • 0 Comments

    There's a new download of the LINQ bits available.

    I haven't had a chance to play around with it, but I hope to in the near future.

  • Eric Gunnerson's Compendium

    Make your own motivational poster

    • 0 Comments

    If you're a fan of Despair, Inc., now you can make your own.

    Motivator

Page 1 of 1 (16 items)