Used Groceries?

Sometimes using the same display template in web applications can lead to amusing/gross errors:

image 

Not sure that used Shells & White Cheddar would be as tasty.

 

(Amazon is running a good deal thru the end of March 2009 on these and other Annie’s products right now if you’re interested, here. We really like their Shells and Cheese – better than that blue box you might be familiar with.)

image

Arcade Billiards, the Source Code

Although my Mix09 entry didn’t place/win, as promised I’m posting the source code, as-is. It’s not the final completely condensed version that I actually submitted (as that version had carriage returns/line feeds, tabs, etc. all removed).

Download Source

As a consolation prize, I did receive a limited edition T-shirt just for entering (which admittedly, didn’t make the effort worth while – but, I had fun putting it all together nonetheless).

I’ve programmed in many nasty, strict environments over the years, but it felt really dirty to do this in c#:

DateTime _t, _aL, _tS;
B[] _b, _e;
Random _r = new Random();
double TW = 12, TH = 12, SY = 50, MW, MH, SA, G = .03, CSA, SSA;
Point A;
int CW = 20, CH = 40, TB = 21, _s, GM = 0, _c, BR;
Brush _o;
IsolatedStorageSettings _a;
delegate void PAD(Canvas c);

Yeah. Seriously, those were the variable names that I used to keep the file smaller.

I shrunk the standard Application class down to it’s bare minimum and used Lamda expressions everywhere:

public partial class App : Application
{
    public App()
    {
        Startup += ((s, e) => RootVisual = new Page());
        UnhandledException += ((s, e) =>
        {
            if(!System.Diagnostics.Debugger.IsAttached)
            {
                e.Handled = true;
            }
        });
        InitializeComponent();
    }
}

and …

Loaded += ((s, e) =>
{
    _a = IsolatedStorageSettings.ApplicationSettings;
    btnP.Click += ((o, a) =>
    {
        cMs.Visibility = Visibility.Collapsed;
        RG();
        GO();
    });

Last but not least, this was probably the silliest thing I did to stop needing to use the Canvas class everywhere directly (it was still necessary in some places):

class C : Canvas
{
}

You’ve got it. I derived from Canvas so I could just refer to the Canvas class as the letter “C”! Thankfully, it wasn’t sealed.

The code does the necessary math to compute an Isometric view of the game table so that the balls on the table are properly placed.

Fully condensed down, I think I ended up with 240 bytes to spare.

 

(Are you going to Mix09? Sounds like some great stuff will be announced!)

Search Engine Optimization Dirty Tricks….

Dear web site owner,

My distrust of your web site and product increases ten-fold when you add dozens or hundreds of search words or phrases to your web pages in order to potentially lure unsuspecting Internet searchers.

image

In the example above, Word reports that there are

image

487 extra words on your page (none of which are standard copyright text) and 3,472 characters total.

If you have a good web site or product, they will come. I promise. Get rid of the slimy techniques to draw traffic. You may have drawn me there, but I almost always move on as soon as I see that type of nonsense. You must have something to hide, or your product is inferior. You’ve just lost a repeat visitor and/or customer.

Signed,

Concerned about your giant footer.

Silverlight Stopwatch class in C#

I needed a simple method for doing some timings in Silverlight. Silverlight lacks the high performance query counter that is available natively in Windows (and available in .NET 3.5 for example), but it does have a TickCount. My code (mostly) mirrors the existing .NET Stopwatch class using the TickCount property of the Environment class.

Just create an instance of the class and call Start to start the timer, and then get the value of one of the  ElapsedNNN properties to get a live reading without stopping the timer or the Stop function to stop the timer completely.

Update April 4, 2010:

Even though there were some doubters in the comments suggesting I didn’t test the original version that was here – I had … so I’m confused as to why it had worked for me, yet not in current versions of Silverlight. In any case, I’ve updated the code and have included a sample of how to use it. Thanks to those who provided suggested fixes.

using System;
using System.Diagnostics;

namespace WiredPrairie.Silverlight
{
    /// <summary>
    /// Stopwatch is used to measure the general performance of Silverlight functionality. Silverlight
    /// does not currently provide a high resolution timer as is available in many operating systems,
    /// so the resolution of this timer is limited to milliseconds. This class is best used to measure
    /// the relative performance of functions over many iterations.
    /// </summary>
    public sealed class Stopwatch
    {
        private long _startTick;
        private long _elapsed;
        private bool _isRunning;

        /// <summary>
        /// Creates a new instance of the class and starts the watch immediately.
        /// </summary>
        /// <returns>An instance of Stopwatch, running.</returns>
        public static Stopwatch StartNew()
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            return sw;
        }

        /// <summary>
        /// Creates an instance of the Stopwatch class.
        /// </summary>
        public Stopwatch() { }

        /// <summary>
        /// Completely resets and deactivates the timer.
        /// </summary>
        public void Reset()
        {
            _elapsed = 0;
            _isRunning = false;
            _startTick = 0;
        }

        /// <summary>
        /// Begins the timer.
        /// </summary>
        public void Start()
        {
            if (!_isRunning)
            {
                _startTick = GetCurrentTicks();
                _isRunning = true;
            }
        }

        /// <summary>
        /// Stops the current timer.
        /// </summary>
        public void Stop()
        {
            if (_isRunning)
            {
                _elapsed += GetCurrentTicks() - _startTick;
                _isRunning = false;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the instance is currently recording.
        /// </summary>
        public bool IsRunning
        {
            get { return _isRunning; }
        }

        /// <summary>
        /// Gets the Elapsed time as a Timespan.
        /// </summary>
        public TimeSpan Elapsed
        {
            get { return TimeSpan.FromMilliseconds(ElapsedMilliseconds); }
        }

        /// <summary>
        /// Gets the Elapsed time as the total number of milliseconds.
        /// </summary>
        public long ElapsedMilliseconds
        {
            get { return GetCurrentElapsedTicks() / TimeSpan.TicksPerMillisecond; }
        }

        /// <summary>
        /// Gets the Elapsed time as the total number of ticks (which is faked
        /// as Silverlight doesn't have a way to get at the actual "Ticks")
        /// </summary>
        public long ElapsedTicks
        {
            get { return GetCurrentElapsedTicks(); }
        }

        private long GetCurrentElapsedTicks()
        {
            return (long) (this._elapsed + (IsRunning ? (GetCurrentTicks() - _startTick) : 0));
        }

        private long GetCurrentTicks()
        {
            // TickCount: Gets the number of milliseconds elapsed since the system started.
            return Environment.TickCount * TimeSpan.TicksPerMillisecond;
        }
    }
}


Sample usage:

Stopwatch sw1 = Stopwatch.StartNew();
Thread.Sleep(20);
sw1.Stop();
Debug.WriteLine("Slept for 5 milliseconds ... actually: {0}", sw1.ElapsedMilliseconds);