Entries Tagged 'Nuggets' ↓
October 7th, 2008 — .Net, Code, Nuggets
Continuing on from my post about an alternative syntax for the non-disposable using statements, here’s a class I’ve been using lately. It serves as a wrapper around changing the colours in a console window. It’s not a difficult thing to do, it’s just a bit awkward because you have to maintain the original colour in a variable while you do your business.
Console.WriteLine("Start...")
var originalColour = Console.ForegroundColor;
Console.ForegroundColour = ConsoleColor.Red;
Console.WriteLine("WARNING!");
Console.ForegroundColour = originalColour;
It’s not too bad when you’re only doing it once, but when you start swapping colours all over the place, then it can become very noisy. So this is where my class comes into play. Using the same syntax I described in my previous post, I’ve wrapped up the colour changing in a helper method that takes an Action delegate. This allows you to write much more intention revealing code.
ConsoleColours.Foreground(ConsoleColor.Yellow, () =>
{
Console.WriteLine("Different text");
});
I find this much cleaner and the blocks gives my console code some much needed separation.
Here’s the full class code:
public class ConsoleColours
{
public static void Foreground(ConsoleColor colour, Action action)
{
var original = Console.ForegroundColor;
Console.ForegroundColor = colour;
action();
Console.ForegroundColor = original;
}
public static void Background(ConsoleColor colour, Action action)
{
var original = Console.BackgroundColor;
Console.BackgroundColor = colour;
action();
Console.BackgroundColor = original;
}
}
October 7th, 2008 — .Net, Code, Nuggets, Opinion
There’s been a bit of discussion of late about using statements, and how they’re more often being used for purposes other than just releasing resources. As always, there are those people who think it’s a flagrant abuse of a feature and shouldn’t be done, then there are those that like it. I’m in between. I do like what the using statement gives us, but I also think it is a bit of an abuse.
The “traditional” usage of the using statement can be found quite often in the land of files and streams. Take the following example, which opens a file and then closes it when it drops out of the using scope.
using (var stream = File.OpenRead("myFile.txt"))
{
// do something with the file
}
Examples of the alternative usage can be found all over the place, but Rhino Mocks is one that’s close to my heart. Here’s from the record/replay syntax, anything in the scope of the using is recorded, and once it drops out of scope it’s no longer in record mode.
using (mocks.Record())
{
Expect.Call(customer.Address)
.Return("123 Rester St");
}
Again, I do like what the using statement gives us outside of releasing resources (I’m not disputing it’s usefulness there). However, I think the using keyword itself adds noise and clouds intention.
With the adoption of 3.5, I’ve started using an alternative syntax instead of usings. Actions and anonymous methods to the rescue.
Scope(() =>
{
// do something within this scope
});
It’s a little bit more noisy in the compiler satisfying department, but because you have full control over naming, you can reveal intention more. No more unclear “using”.
So how does it work? Simple really, the method takes an Action delegate, which it the executes almost immediately. I say almost, because you can execute code before and after the execution. That gives you the benefits of the using statements wrapping ability.
public void Scope(Action action)
{
// do something before
action();
// do something after
}
Some more examples:
File.OpenRead("myFile.txt", file =>
{
// do something with the file
});
mocks.Record(() =>
{
Expect.Call(customer.Address)
.Return("123 Rester St");
});
I prefer this syntax over the using statement. Of course, it’s only valid for 3.5 projects.
May 15th, 2006 — JavaScript, Nuggets
Just a quick tip to enable the Debug menu in Safari, the main reason being to get the JavaScript Console (ala Firefox).
Simply close down your open instances of Safari and enter this into the Terminal:
defaults write com.apple.Safari IncludeDebugMenu 1
That’ll enable a new Debug menu next time you start Safari, simple as that. I was on the verge of loading up Firefox on the Mac
before I came across this, bad James!
Also, just in-case, running the above again with 0 will turn off the menu.
April 29th, 2006 — Nuggets, Ruby
The title pretty much covers it, but i’ll elaborate.
A few days ago I decided it was about time I had a tinker with Google Sitemaps, part of which involves uploading a temporary file so google can be sure the website you’re trying to claim is actually yours. Anyway, fun things aside, google gave me an error message (“We’ve detected that your 404 (file not found) error page returns a status of 200 (OK) in the header.”) when it tried to read the file.
The error message was caused by a feature of Ruby on Rails, which when in Development mode, invalid pages are still served by Rails and sent with a 200 header, but logged as having a Routing error of “Recognition failed”.
All information pointed to a simple fix of setting the rails environment (ENV['RAILS_ENV']) to production mode, which would disable the aforementioned feature. Unfortunately for me, my environment was already set to production! I double checked this, logged into the ruby console on my web-server and checked the variables, then even checked which log file was being written to; everything was pointing to it being in production mode.
After a lot of browsing of forums and chatting in IRC channels, I succumbed to randomly trying anything, which is when i noticed in my environment.rb file there was a line of code that I couldn’t for the life of me remember why it was in there, or what it was doing; removing this line of code worked a treat, everything snapped back to how it should be functioning.
For future reference, the line in question was:
require 'error_handler_basic' # defines AC::Base#rescue_action_in_public
So there you go, now I have a 404 page and google accepted my site map, so we can all live happily ever after.
April 26th, 2006 — Nuggets
I apparently got around to reading Coding Horror a bit too late, as I missed this gem. It’s an article about a small application used to bend XP’s IIS to your every whim, perfect for those of us suffering at the hands of Microsoft.
The basic gist of the application is that it removes the limits of XP’s IIS, namely the problem of only being allowed one website and 10 concurrent users. It takes a bit of getting used to, especially if you’re using Visual Studio; if you are, remember to reset IIS to it’s default website before opening a website created prior to using IISAdmin.
March 7th, 2006 — Nuggets
This was a task and a half! As I mentioned earlier I somehow managed to get my Trust Wireless Scroll Tablet working on a dual monitor system, mapped to only the primary monitor; so here’s my little guide on how I managed it.
As far as I am aware there isn’t an option in the trust software to allow you to map to a specific monitor, only to restrict the area on the tablet (which isn’t at all helpful). So what I’ve figured out is purely a work around, and it isn’t great either1. This is mainly for my reference, but someone else might find it useful, so here we go:
- Open up Display Properties and disable (“un-attach”) your secondary monitor.
- Open the Trust Control Panel from the icon in your system tray.
- Re-enable your second monitor.
- Click ok in your Trust Control Panel.
From there on out your tablet should only be mapped to the monitor that stayed enabled the whole time, that is until you re-open the Trust Control Panel. I didn’t say it was pretty!
What I can gather is that after disabling your monitor, when you open up the control panel it maps the overall screen size so it can apply that to the tablet, but when you click OK after re-enabling the monitor it doesn’t refresh with the screen change and so uses the single monitor values.
1 Also note that I have only tested this on my system.
February 7th, 2006 — JavaScript, Nuggets
Once again the guys at Mozilla have broken quite possibly the best plug-in for Firefox: the Venkman Javascript Debugger. This is becoming quite commonplace, with every update for firefox disabling the debugger until a fix is released.
Anyway, even though both the extensions manager and Venkmans official website both say there aren’t any updates available, Joe Walker from Getahead has created and update that contains some bug fixes from Bugzilla, but most importantly Venkman now works with Firefox 1.5.0.1!
Source: Venkman for Firefox 1.5
September 16th, 2005 — .Net, Code, Nuggets
I’m not a big fan of using the Microsoft.VisualBasic assembly, I avoid it at all costs basically.
As we all know, the visual basic assembly comes with a large collection of built-in functions one of which is the DateDiff function. This takes two dates and a comparison value (e.g. “d” for day) and then spits out the difference.
Here’s the same thing just minus the visual basic usage:
DateTime firstDate = DateTime.Now;
DateTime secondDate = new DateTime(2005, 8, 20);
TimeSpan difference = secondDate.Subtract(firstDate);
// days: difference.Days