Monday, September 14, 2009

Remove duplicate pictures with VisiPics

http://download.cnet.com/VisiPics/3000-2193_4-10424234.html

Friday, September 11, 2009

Source Safe woes

One would believe that Visual Source Safe has been long since retired, but no.

I am in one of those rare occasions where I have to deal with it.

One thing that struck me after yesterday’s mishap, is: With Source Safe we are set up to fail. This has nothing with poor or broken functionality, but with a totally broken usability.

Source Safe is a source control versioning system, built around the following concepts:

  1. A developer checks out his files. As long as they are checked out, no one can change them.
  2. All files are versioned, and has a history.

So why did they built in the following wtfs?

  • When you check in a file, you are asked if this is what you want!? You can dismiss this dialog, but what did they think? That I really want to delete my precious work? So what, if I check in the wrong code. That’s what the versioning part is there for.
  • When there are conflicts, you are presented a dialog asking if you want to overwrite yada, yada with the usual Yes / No / Cancel / Ok / Help options. Sad thing is, Source Safe does not ask you for the safe thing to do. The question you are presented is not if you want to check in your conflicts anyway, but if you want to overwrite your local files.

With these behaviors built in, why have the versioning at all?

I committed the two following sins: I never checked in during the day, so I had too much to lose. And when presented with the conflict dialog, I should have gone completely paranoid and zip all my work files.

Wednesday, September 09, 2009

Plugin: Insert Code for Windows Live Writer

Update: Try the Source Code Formatter by Amer Gerzic. The one downloaded from the Windows Live Gallery inserts <br> tags and screw up the code when posted to blogger.

The most important thing nobody told you about model binders

A bold statement, but I stand by it.

I have done numerous searches on how to implement custom model binders in ASP.NET MVC, and all of them are variations of:

public override object GetValue(ControllerContext ctx, string modelName, Type modelType, ModelStateDictionary state)
{
   Customer customer = new Customer();
   customer.FirstName = ctx.HttpContext.Request["FirstName"];
   customer.LastName = ctx.HttpContext.Request["LastName"];
   // ... other properties ...
   return customer;
}


But that’s counter productive, isn’t it? ASP.NET MVC is all about conventions and flexibility. The last thing I want from my model binders are to hardcode expected property names, which would fail anyway. If my type is ReportIdentificator, I certainly don’t want to give all my properties and parameters the very same name. I want my freedom to call them foo and Bar, if that’s what I need.



So I finally went to the source, so to speak. The ASP.NET MVC source code is available, and inside of the DefaultModelBinder, I found the following gem:



    if (!performedFallback) {
        ValueProviderResult vpResult;
        bindingContext.ValueProvider.TryGetValue(bindingContext.ModelName, out vpResult);
        if (vpResult != null) {
            return BindSimpleModel(controllerContext, bindingContext, vpResult);
        }
    }


The getaway here is bindingContext.ModelName. That property is the key to get the proper ValueProviderResult, which contains the posted values for the model. With that information, I am finally able to create the model binder I want.



The usual binder registration:



    ModelBinders.Binders.Add(typeof (ReportIdentifier), new ReportIdentifierBinder());


And my binder:



    public class ReportIdentifierBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var model = (ReportSectionIdentifier)base.BindModel(controllerContext, bindingContext);
            ValueProviderResult serialized;
            if (model == null && bindingContext.ValueProvider.TryGetValue(bindingContext.ModelName, out serialized))
            {
                var values = serialized.AttemptedValue.Split("-".ToCharArray());
                model = new ReportSectionIdentifier
                            {
                                Organization = int.Parse(values[0]),
                                Period = int.Parse(values[1]),
                                Report = int.Parse(values[2]),
                            };
            }
            return model;
        }
    }


I let the base try to create the model first, in those cases where the usual conventions are followed. My implementation deserialize the model from a value on the form “1-2-3”. That code enables me to support drop down lists bound to a report name and its identifier, which posts a value on the form “1-2-3”, or use the identifier in action links, which appends the property values to the querystring according to the conventions.

Monday, July 27, 2009

The specified module could not be found (vscomptr.inl)

An explanation and solution is found here.

You do something in Visual Studio 2008 that causes a “Get Latest” or “Check Out”, and are greeted with the following message:

Unexpected error encountered. It is recommended that you restart the application as soon as possible.

Error: The specified module could not be found.

File: vsee\internal\inc\vscomptr.inl

Line number: 618

Reinstalling OleDB may fix it. Open command prompt, and:

  1. cd "c:\Program Files\Common Files\System\Ole DB"
  2. regsvr32 oledb32.dll

Tuesday, July 21, 2009

The project type is not supported in Visual Studio 2008

You download some source code demonstrating ASP.NET MVC, a library or an application built on this MVC framework, but when you open the .sln-file, you are greeted with the following error:

The project file ‘your-project.csproj’ cannot be opened.

The project type is not supported by this installation.

It’s wonderful, isn’t it, that this message tells us next to nothing? Which project type is unsupported? There’s no way to tell, as the Property Dialog for an unloaded project doesn’t tell us much.

The reason, it seems, is that the unsupported project is probably built against a different MVC version.

And the solution is to hack the project file:

  1. Right click and select ‘Edit …’.
  2. Replace the whole tag with the following:
    <ProjectTypeGuids>{603c0e0b-db56-11dc-be95-000d561079b0};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>

Or, as in my case, download and install ASP.NET MVC :-(

Thursday, July 09, 2009

Programming IQ test

While the test is purely for fun, I have to comment on my score, which is no better than 75%. I got my bragging rights, though :-)

Many of the questions I got right are just as unimportant than the ones I got wrong.

The worst offender for suckers like me who takes this nonsense very seriously, are:

Question 6: Your local supermarket is all sold out of energy drinks, Jolt Cola, and Mountain Dew. Which beverage will keep you going, packing the most caffeine and sugar into a 12-ounce can?

Correct Answer: Sunkist Orange Soda

Your Answer: Dr. Pepper

Sunkist Orange contains just as much caffeine as runner-up Dr. Pepper but surpasses it in sugar content.

I mean, are programmers only located in the North of America? I live in Norway, which may very well compare to living under a rock. It certainly looks that way on certain government decisions. But that’s a Law of Nature, I guess.

But I digress. WTF is Sunkist Orange Soda? And haven’t people heard of coffee? Need more caffeine? So up the dose! Need sugar? No you don’t, but anyway, what’s stopping us from adding as much sugar as the coffee mug can absorb?

I think I am proud of failing #6.

Then there is, and this is the stupidest, as it contains no correct answer. I failed this one on purpose, because I had to answer something:

Question 12: Which of the following is the best way to write reusable code that is easier to maintain?

Correct Answer: Insert comments throughout your source code files

Your Answer: Use more global variables

Documenting your code is the best way to ensure that another programmer can understand it. All the other choices are recipes for disaster.

People who know me professionally, know I am a test infected kind of guy. Along with Test Driven Development comes Merciless Refactoring, and that is the correct answer. That is, both of them. Code comments are one of the hardest thing to get right, simply because they have to address the code at an abstract level. That is the only way to write comments which will stand the test of time. Most comments simply repeats what the code does, not why. Our variable-, method-, and class-names should do that.

I opted to comment on #16, simply because I hate to be wrong:

Question 16: A client has asked you to write some basic accounting software in C. What data type is best for representing figures in dollars and cents?

Correct Answer: Integer

Your Answer: Double

Integers can be used to implement fixed-precision math. Floating-point numbers, whether single or double precision, aren't accurate enough to keep track of finances.

I flunked this one. Badly. However, the question itself is ill-phrased. The available answers only refer to the type “Integer”, while it should be a set of integers. This is my opinion, though. I can see how one integer can solve the problem: Just look at any financial number and pretend there is always two fractional digits, or four if one need this kind of precision. In financials, we do. Then we pretend that the first two, or four, digits always represents the cents. But then we have greatly reduced the highest value possible to address. Better hope we never have to address Zimbabvian dollars with that software package.

No, the correct answer must be some composite of multiple integers: One for the cents, and one or more for the dollars.

Would I be a better programmer if I knew Kernighan’s best known achievement? Maybe. Or maybe not:

Question 17: For what achievement is Brian Kernighan best known?

Correct Answer: He was co-creator of AWK, a programming language for text processing

Your Answer: He was co-creator of the C programming language

Kernighan is known as the "K" in the K&R C language specification, but he didn't create the language; he just helped Dennis Ritchie document it.

In the same category: I didn’t know what “Turing complete” meant until #18. Who cares? And I don’t know about standard SQL, does that even exist as an implementation? All I know, is that loops are possible in T-SQL. And for PostScript? I thought that was a markup language, like HTML and XML:

Question 18: A programming language is said to be "Turing complete" if it can be used to implement any conceivable algorithm. Which is NOT a Turing-complete language in its standard form?

Correct Answer: SQL

Your Answer: PostScript

Standard SQL can't do loops, although you can get Turing completeness with proprietary extensions to the language from certain vendors.

That said, the questions I got right, by pure chance or otherwise, there are a few that doesn’t tell anything about programming skills:

  • Question 1: What is the relationship between Java and JavaScript?
  • Question 2: Hungarian Notation is a variable-naming convention used by some programmers. How did it get its name?
  • Question 3: Just-in-time (JIT) compilation improves the performance of languages that compile into bytecode. Which language featured the first JIT compiler?
  • Question 4: If I told you a key characteristic of my programming language of choice was that it generated threaded code, which language would I most likely be talking about?
  • Question 5: Once very popular and widely used, Pascal spawned a number of derivative languages. Which is NOT a successor to Pascal?
  • Question 7: What is the best way to preserve type safety in assembly language?
  • Question 8: Which of the following is NOT a central tenet of extreme programming?
  • Question 9: Why are race conditions a problem in modern software development?
  • Question 10: Why do some consider Ruby to be more "purely" object-oriented than other, more popular OOP languages such as Java and C++?
  • Question 15: Is P equal to NP?
  • Question 19: Which group has had the most impact on modern object-oriented programming practices?
  • Question 20: Which of the following is NOT a data structure used in modern programming practice?

There must be something wrong with me: Out of 20 questions, these are the only three I find useful:

  • Question 11: Failure to validate user input is one of the most common sources of software security vulnerabilities. When is it safe to accept user input without validation?
  • Question 13: Of the following, who is NOT the inventor of a programming language in current use?
  • Question 14: To what concept does "the mythical man-month" refer?

Take the test at InfoWorld.