Thursday, May 17, 2012

Another Little C# Thing I didn't know


Sometimes I don't think I know much about C# (or anything else for that matter).

I didn't know you could do this:
static void two(string x, int num=1)


in this, the parameter "num" is optional. I didn't know you could optional parameters in C#. I've been looking at C# code since C# has been around -- code reviews, my own code, samples on the internet -- but I can't recall ever seeing anyone use this.

Here's a code sample.
        static void Main(string[] args)
        {
            two("one", 2);
            two("two");
        }
 
        static void two(string x, int num=1)
        {
            Console.WriteLine(x + " " + num);
        }
 
 
You can also used named parameters. So this also works:
        two(num:7, x :"named");
(at least in .Net 2.0 and later).
I remember named and optional parameters in VB4,
but I was completely clueless that you could do it in C#.

Now, we're all a tiny bit less clueless.
--kevin

Wednesday, May 2, 2012


Quick diversion to Skyrim            

OK. Here’s a geek thing: I like to play computer games sometimes. Occasionally, I give into my guilty pleasure until it becomes an obsession. Naturally, I often keep my obsession hidden, like any good addict.

But I have to blog my recent experience with Skyrim, because I think it says a lot about the industry.

When my Skyrim disk arrived from Amazon, I eagerly popped the DVD into the drive and salivated during its loading. Only, you can’t just install a PC game anymore. The only way you can play (well, without a lot of back-door things) is to install Steam. For the non-gamers out there, Steam is an online service that verifies that I really own the copy of Skyrim I’m playing. Now forget the fact that Skyrim sold 3.5 million copies in the first 48 hours after its release and sold something around 10 million copies total, the companies that make this stuff now want to make sure that some poor 14 year old in Iowa isn’t playing illegally and costing them the 10 million and *first* copy they could have sold, so they burden the other 10 million of us with this top-heavy, internet-based nonsense.

(Example of the nonsense : I can’t play Skyrim if my internet connection goes down, even though I paid my $50 for the game and it’s not an online game. It’s kinda like OnStar preventing you from starting your car unless you first tell them your password. If you happened to park in an underground garage, too bad. You’ll have to be towed. Oh and Steam launches at start up every time I boot. There’s no option to turn that off without digging through a bunch of registry keys—which I did, by the way. Without doing that, every time you boot, you have to wait for the dialog to come up and prompt for your password, after it finishes verifying that the Steam service is available on the internet.)

I told myself last time I played a Steam game that I would never do it again. Seems I lied to myself. Well, to be fair, it wasn’t obvious that Skyrim used Steam. I think I’d heard it at one point, but forgot it. So, it is what it is.

Steam added quite a while to the install. It has to connect to the internet and sign you in with your account. Of course, I couldn’t remember my Steam password. I mean, it’s not like I use it all the time or have it tattooed on my arm or anything. So after a half an hour of installing and resetting and such, I was in.

Once I was in, Steam started to advertise to me – because, clearly, 10 million sales isn’t enough. Mostly, they advertise downloadable content, but occasionally, they push new games.
I get that I watch commercial TV and have to watch the ads because I don’t pay for it. And I get that I have to pay for HBO so that I don’t see the commercials. But when I pay for something *then* also have to watch the commercials, it fries my cupcakes.

OK. So I’m in and have seen all the ads. Now the game starts in earnest. The default settings on the game came up with lower-than-acceptable resolution, but I changed that. The game also came up in full screen mode, which I hate. I multi-task. I’m typically playing a game like this while I’m surfing the web, updating facebook, checking email and chatting with a friend. So I re-set the full screen to “windowed” mode – only to discover that the game was obviously written for an Xbox, since the window had no controls on it, and was just about impossible to drag. Oh, and it was on the wrong monitor. It always comes up on my primary, even though I set it for the secondary. Because, of course, Xboxes only have one monitor, I guess.

I figured out a trick to drag the window (had to place another app over it, so that just the outside of the frame was shown, then select the overlay-ed window and slide the mouse up until I just hit the exposed part of the frame. Then I could drag... no, really).

Well, a pain, but functional. So the game comes up and is just about *unplayable* with a mouse and keyboard. Oh, there’s instructions on how to do it, but the interface is completely unmanageable. Because it was made for an Xbox -- with its Xbox gamepad -- not a PC.

I remembered that I had an old Logitec game pad, so I shut down the game and searched for my controller. Got it, plugged it in and re-started the game. Only, Steam (God love them) re-set all my graphics settings and opened the game full-screen on the wrong monitor again, after trying to upsell me, of course. (*sigh*).

Well, OK, fine-- only the game doesn’t see my controller at all. I Google. And find out that the *only* control the game will support is the Xbox controller, because—wait for it -- it was written for the Xbox. I Google some more and find an emulator that will translate my Logictec controller into Xbox-ese, and fake out the game into thinking it’s an Xbox one.  So, I swallow the virus risk (well, I did scan the file), run the emulator and I’m good.

I restart Skyrim, sign into Steam and wait for Steam to respond. Steam re-sets my video again and opens full-screen on the wrong monitor with the low video settings. Then it tries to upsell me. But the emulator works.

So I’m gaming now. Although I spent so much time messing with it that I haven’t actually gotten into the game much. It’s playable now. But, so far, it’s not much fun. Hopefully, after $50 and a few hours of my time, that will change.

But all this speaks to the computer industry. The company that makes Skyrim (Valve, I think?) made something like half a billion dollars from the game. But it isn’t enough money anymore to avoid ads, to allow people to run offline or to actually support the target platform.

I see the same thing in other software I buy. The general quality sucks – I don’t mean functionality, I mean the platform support and such. Windows 8 looks like a kludge (more on that later). We’re told that we need a new interface because the old interface won’t support notepads – even though I don’t have a notepad… because the zillion dollars they make from PCs isn’t enough. The new version of Office (so I hear) will incorporate Office 365’s online support, even though I do a lot of editing offline. My new Dell gets angry when I don’t synch with Dell’s online cloud, even though I have no interest in putting anything on their online portal.

Seems to me, it’s all part of the same deal in computers to drive more review from the products. We’re being pushed into products that we’re told we want because it’s beneficial for the companies that make them. We’re having our personal data sold. We’re getting advertised to on our own computers.

Of course, we’re the ones who keep buying the stuff.
--kevin

Monday, April 23, 2012

Locks in C#

This is just something I didn’t know, so I thought I’d pass it along.

 When doing asynchronous coding in C#, it’s common to use the “lock” keyword, as in :

 lock(something)
 {
      // do stuff
}

One important thing that I didn’t know: the “lock” has no timeout. In some cases, this could be ok. But if the “something” that’s being done involves disk I/O or database access or anything that could potentially produce a lag, this could be bad. In some cases, it could even cause a deadlock.

Under the covers, “lock” just masks a call to Monitor.Enter and Monitor.Exit, so you can replace “lock” with these. Moreover, Monitor.TryEnter will allow you to set timouts, so you can use that. Even better, you can create a special object that implements IDisposable and do the “Monitor.Exit” in the Dispose() event. That way, any time the special object goes out of scope due to normal code flow or an exception, the Dispose() fires and you’re guaranteed to release the lock.

You can find a sample of this here:
http://www.koders.com/csharp/fid8EED099F752FABA10F6C8E661D1A6EF0736EB096.aspx

 Among other places. To use it, you just say:
 using (TimedLock.Lock(something, NumberOfMiliseconds))
 {
     // do stuff
}

So it works about the same way as “lock” only with the ability to pass a timeout. Very nice.

One other thing to remember is that locks are based on objects. If the “something” that you’re locking on is a simple type (say an int), .Net will box the int into a new object(). Unfortunately, the next thread that hits the lock sees that it’s an int and boxes it too, into it’s *own* object. The important thing is that thread1 and thread2 don’t share the object and both think they have a lock – which is really, really bad. So make sure the “something” is not a native type, but a real object. I sometimes use DateTime objects, since that also allows me to specify a datetime when the value would expire. But any subclass of object will do.


Till next time
--kevin

Friday, April 13, 2012

anonymous types & C#

My blog updates have been sparse lately. And by sparse, I mean non-existent. I mean rarer than a Google+ post.
There have been a lot of reasons for this – it’s been a rough winter and a busy spring so far.

But I’ve stacked up a lot of geek things to blog. I’ve got a little C# stuff, some SQL tricks, and a bit of general industry and gaming thoughts (for example, how Apple is now becoming more evil than Microsoft). For now, let me start with the C# stuff.

Anonymous types. My guess is you either
a) have no clue what they are,
b) think they’re the most evil thing since 8am meetings and are probably responsible for global warming or
c) love them so much you’re thinking about rewriting every line of code you’ve ever written (even things for companies you don’t work for anymore) to change all variables to anonymous types.

I’ve been a little surprised how little understanding people have of them. I blame Microsoft (as I often do). Why, oh why, did they select the “var” keyword when declaring anonymous types? I’ve had to explain to even senior technical people that they are *not* “variant” data types – like they kind you’d find in Javascript, for example. If you write
var x=””;
you *cannot* ever do
x=new Datetime();

the compiler will stop you.

Anonymous data types are bound at *compile* time, not run time. As I had to explain to someone just this week, there is no binary difference in the assembly between saying:
string x=””;
and
var x=””;
If you look at the MSIL, they are exactly the same.

So, as someone asked me this week: why use them?
Primarily, of course, they’re used for Linq. Because, at design time, the data set you are getting back from a Linq query isn’t known, you can’t very well use named data types. This is where it gets cool though.

If you think about what Linq does, it returns a set of data. That set is unknown at design time (as mentioned), but known at compile time through introspection. So, .Net uses reflection to type the data coming out of the Linq query and then creates class to contain it. The compiler does the work of creating the class definition and setting up a property (get()/set()) for each data element in the set.

The resulting class is “hidden” in that it’s not generated until compile time and in that it’s not named. Of course, names don’t really mean anything to the compiler anyway, so it doesn’t care, but that makes it complicated to actually refer to the specific class in code.
Enter the badly named “var”.

Here’s the cool thing. Since Linq is requiring the compiler to generate a class (really, it could *almost* use a struct, but it doesn’t for another reason which I’ll mention later), this means that the compiler now has the ability to generate class code. This means you can use that outside of Linq queries.

Why would I care? (I mean besides my own mental issues?)

Suppose you have a simple method which, say, reads the lines from a file and stores some info about them. For the sake of simplicity, lets say you want to know the original line number and the actual text from the input file—and you’ll be re-ordering the lines later, so you can’t just use a simple count, you have to persist the original.

You *could* create a class like this:
Class LinesAndCounts
{
int _lineNum =0;
int LineNumber
{
get
{
return _lineNum;
}
set
{
_lineNum=value;
}
}
…
…
…

And so forth.
It’s a lot of typing. And the class gets scoped to the assembly or the project or the namespace, not just the method. What if you’re just wanting to read a file, make some modifications to it, and re-write it, with the original line numbers. That shouldn’t require a class that gets scoped to the whole world, right?

What anonymous types let you do is this:
var x = new {LineNumber=0, LineText=”abc”};

The compiler will automagically generate code more or less the same as what I started to write above (before I got lazy). Frankly, I think that is so beautiful, it brings tears to my eyes (*sniff*,*sniff*). In one line of code, I’ve created a name-less class and assigned values that I can use later. Moreover, that class is scoped to the method/scope-block that created it. Once execution leaves the method, not only does “x” go out of scope, but the class itself gets de-scoped, and the garbage collector takes it back to the city dump.

So let me take a contrived, concrete example. Suppose I want to read all the lines of an input file. Let’s take a simple one:
its line one
its line two

And I want to print all these lines with their corresponding line numbers reversed. So the “its line two” would have line number 1, and “its line one” would have line number 2. And then I’m going to add a header line to throw this number scheme off.
OK, this is an overly simple example, and there are lots of efficient ways to do this using the file system objects and such. But let’s pretend that the operations are more complex than they really are.

I could create a class (call it MyClass) that would have 3 properties: the original line number, the text of the line, and the new line number. Then, I could add a method to this class that would transform the line numbers by taking the original line number and subtracting it from the total number of lines like

int Transform( int origLineNumber, totalLines)
{
return totalLines-origLineNumber;
}

I could create a List to hold the objects, and (for the sake of the demo) step through this original list to move the items to a new List with the re-numbered lines. Pretty inefficient, but demo-worthy and useful sometimes.

Doing that with anonymous types, though, I don’t need to create any of the classes. The code is here:


public class AnnonExample
{
public void messWithFile()
{
int lineNumber = 0;
var firstLine = new { LineNumber = lineNumber, LineText = "this is the new file", NewLineNumber=0 };
var lineList = (new[] { firstLine }).ToList();
lineNumber++;

foreach (string line in System.IO.File.ReadAllLines("fileStuff.txt"))
{
var theLine=new {LineNumber=lineNumber, LineText=line, NewLineNumber=0};
lineList.Add(theLine);
lineNumber++;
}

for (int i=0; i < lineList.Count; i++)
{
var l = lineList.ElementAt(i);
var p2 = Transform(l, (p) => new { LineNumber = p.LineNumber, LineText=p.LineText, NewLineNumber=((lineList.Count ) - (p.LineNumber)) });
Console.WriteLine(p2.LineNumber + ":" + p2.NewLineNumber + ":" + p2.LineText);
}


}
static T Transform(T element, Func transformFunc)
{
return transformFunc(element);
}
}

Note that the classes are all anonymous and loaded into lists and manipulated later. The “Transform” is accepting a lambda expression to do the dirty work, so it could be reused generically.

Under the covers, .Net is using the same anonymous class for each of the lines. It generates it once, then re-uses it. And it enforces type-checking on it – try to assign theLine=”x” at the bottom of the first loop and you’ll get a compile error. The generic list is the same way. You can’t fill it half-way with the lines above, then fill the rest with strings. It’ll bark.

My jury is still out on anonymous types. I can see their uses. But I do think they have the tendency to reduce readability if they’re over used. Still, they can be incredibly useful for the quickie class that you need just to hang onto for one iteration of a loop so that you can modify something, grab the results, then throw it away. In the example above, I could’ve done the same thing with parallel arrays, but I think this is much less messy. It isn’t always obvious how parallel arrays are connected. Here, it’s very straightforward.

In any case, it’s very cool stuff.

--kevin

Saturday, September 24, 2011

Security Hack

Just an interesting hack that people could use.
step1:
write a web page. On the page, put a bunch of links that you care about. Make them hidden, though.

step2:
Create a style sheet and set up a link visited font and color. Doesn't really matter which one, you just have to know it.
like this:
a:visited { color : red; }
a { color : orange; }

Step3:
In the web page, write some client-side script that loops through each of the hidden links and checks the color.
(you can use this: element.style.color) and compares it to the color of the visited link in step2. If the colors are the same,
that means the link has been visited.

Now, for every person who hits your site, you know whether a lot about their browser history.

If you sell cars, you can tell if the visitor has been looking for cars. Or..whatever interests you. If you're a site that hosts some free service, you can partner with companies who provide your ads to figure out which product(s) or services they visitor may be willing to buy.

It's a kind of slimy way to profile a site visitor. And kind of a slimy way to be profiled.
Next time, I'll maybe post a way to get even more data on the visitor.

Meanwhile, you can see a nice post on it here:
http://blog.mozilla.com/security/2010/03/31/plugging-the-css-history-leak/

Mozilla has some plugins to help with this. And that's a good thing.
--kevin

Monday, September 5, 2011

HP

Reviving the blog after taking the Summer off with this thought.
It's widely known that HP will stop making most hardware. (http://news.cnet.com/8301-1001_3-20094222-92/hp-halts-webos-business-spins-off-pc-unit/)

Although, they may un-retire their notepad now that they think they can actually make a profit, but they have announced that this may be short-lived.

Meanwhile they've been buying software companies. ( For example, Autonomy : http://www.bloomberg.com/news/2011-08-18/hp-said-to-be-near-10-billion-autonomy-takeover-spinoff-of-pc-business.html). In short, they're trying to become IBM. Spin off the PC business, like IBM did to Lenovo, focus on servers and corporate software and support. Make money from consulting.

From a business perspective that makes sense. In fact, it's real winner. But I have to think about HP's origins. In the 1960's it was a small company that built testing hardware for technical applications (oscilloscopes, etc.). One of their engineers decided to invent this thing called a "programmable calculator". The marketing department shot it down -- it could never make money. They cancelled the project. But Mr. Hewlett was an engineer at heart. When he saw it, he loved it and decided that he wanted one. What Mr. Hewlett wanted, Mr. Hewlett got. So, the marketing types rolled their eyes and went along with it, sure they couldn't ever sell more than 2.

Of course, 20 years later, HP was the leading manufacturer of programmable calculators and a multimillion dollar company. Bill Hewlett was right. He was a geek at heart and a bit ahead of his time.

That HP is stepping away from hardware implies to me that the geeks have lost. The accountants are twitting their pencils and deciding that there's more money in software and consulting than hardware. And they're right. I agree. But I'm sad.

I think it speaks of a shift in the industry that saddens me. The days when IT folks could just build cool stuff and figure out how to make money later are mostly gone. (well, Google may be the last one standing, but I think they've even shifted from an "innovative" approach to a "profit" approach about 70% of the time now).

Hopefully there will still be opportunities to make a lot of money doing something that's "just cool", rather than something that balances the cost-benefit sheets.

We'll see.
--kevin

Friday, April 29, 2011

amazon

Hypothetical situation. You're an IT executive. You're tasked to add a bunch of functionality to the internal systems. A sales rep comes in and shows some of your bosses how you can save a zillion $$ by moving the data to the cloud. You look over the numbers and find out that they’re basically right.

Now what do you do? You generally don’t get into the executive position if you’re not a bit self-focused. Here’s the problem. If you push your data to cloud, it’s reasonably safe, relatively stable and pretty cheap. But the people who maintain it don’t report to you. That means your org chart shrinks and you lose control over your budget and infrastructure. It means you can’t pull resources away from the most important work in order to focus on your pet project. In short, it means you lose power and position. Think of it. If all the functionality in the company moved into the cloud, you’d be out of a job. Or even if not, you can kiss your bonuses good bye. Who’s going to give you a raise from something your vendor did? Of course, if they fail, you’ll hear about it loudly.

So, this week Amazon’s cloud crashed. In truth, it just proves they’re human. I like to collect those stories so that when the bosses demand perfection, I can tell them that if they give me as much money as the folks at Amazon gets, I may have a shot at making it well, but even then, it won’t be perfect.

From a cloud perspective, the Amazon crash hurts. The mythical VP above doesn’t want to move anything to the cloud anyway. Now he has an excuse.

Will be interesting to see what the fallout is…
--kevin