Showing posts with label Other. Show all posts
Showing posts with label Other. Show all posts

Wednesday, November 24, 2010

How to hire a skilled programmer?

There are wide and long discussions on how you can test programmers who apply for a job with you/in your company. Most of developers have some experience on that (because they apply for jobs themselves), few of them are actually on “the other side” and have to somehow pick the best from the best. The most skilled from skilled.

I am not going to elaborate on what is better, to freely talk to candidates, to order them to write code online, write code offline or to let them answer a dozen of specific questions to score points for correct answers. All these have their pros and cons and, based on my own experience and from what others say, it all depends on the specific profile, the size of the company, the kind of the job, the amount of candidates etc.

What works best for me (since I am responsible for the technical part of the recruitment procedure) is to ask candidates to write a simple but complete application focused on a precise specification. It could be a simple SQL query generator for POCO classes or a core of an IoC container. It does not really matter whether or not a candidate recognizes the notions from the specification, for example people implement perfect IoC containers based solely on my specs with no prior knowledge of what an IoC container is. They are allowed to work offline for few days (although for someone experienced the implementation should not take longer than few hours) and then we meet to discuss the details of the implementation. This is where I ask every candidate to write something fast, on demand, using only a pen and a sheet of paper. This could be “a method to sum an array of ints” or “a method to reverse a string”. People usually do some subtle mistakes so I test how they adopt to “I am a test engine and your implementation seems to throw NullReferenceException which is not expected, please correct this”.

After both these tests I know that:

a) the candidate can read, understand and follow a written specification even if he/she does not recognize some notions

b) the candidate can work under a pressure and knows the programming language well enough to implement a simple algorithm without a compiler at hand (from which I conclude that the real knowledge of the language, with the compiler at hand, is much deeper)

For some time, however, I am thinking of making some changes to the way the process is organized. Instead of a pen, sheet of paper and a consise, short specs of a tiny method, I am thinking of a virtual web-based environment, containing a text editor and a compiler so that the candidate could follow a more complicated specification, write, compile and test the code he/she submits as the proposed solution.

There are two great examples of how this could look like:

1. Codility. It’s a web-based environment where candidates are presented with a concise and precise specs of an algorithmic task and they can invoke a compiler (which sits on the server) to actually compile and test the solution. The engine performs some tests on the server-side so it not only detects that the implementation misses the specification (I wonder if Codility tests are hard-coded so that it’s possible to write a code which only solves the test cases by hardcoding the answers for test cases?) but also detects the complexity of the solution (so for example if a linear solution is expected, you get a lower score for a solution with quadratic complexity).

2. Pex for Fun. It’s actually pretty smart. Pex is an engine which follows all the control flow paths in given code and generates a complete set of test cases. Based on that, Pex for Fun has a “secret” implementation of a simple method (like “take x and return x * x”) but you are not allowed to see the implementation. Instead, you have to provide your own implementation for which the engine generates a set of test cases and feeds the secret implementation with it (and vice versa). You are then presented with a list of test cases both your and secret implementations and if there are some differences in the output, you have to change your implementation and follow the validation procedure until your implementation is semantically equivalent to the secret implementation (all test cases produce the same output for your and secret implementations). Beside being extremely fun, this one not only checks if someone writes a correct code but also – to what extent he/she can adopt to changing conditions (the answers of the Pex comparing engine change each time you ask it for comparision, as you change your implementation).

While implementing a custom solution which somehow follows the idea from Codility seems easy, I wonder if presenting “Pex for Fun”-style task for a candidate would be as fun for a candidate as it surely would be for me watching people who try to solve such puzzles :)

Thursday, October 14, 2010

Is .NET Type-Safe?

A type error is erroneous or undesirable program behaviour caused by a discrepancy between differing data types. […] The behaviors classified as type errors by a given programming language are usually those that result from attempts to perform operations on values that are not of the appropriate data type.

That’s Wikipedia.

I always thought that .NET is type-safe. I always thought that the only legitimate way to access object data is to use it’s direct interface or an external dedicated interface (reflection).

I was wrong.

You see, in C/C++ there’s a notion of “union” where different data can share the same memory location. It seems that to preserve the compatibility with the legacy code, the same feature has been added to .NET but what shocks me is that it not only applies to value types but also to reference types.

Consider following example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
 
namespace ConsoleApplication50
{
    class Program
    {
        static void Main( string[] args )
        {
            Foo f = new Foo();
 
            f.A = new A() { ab = 123, ai = 12345678 };
 
            Console.WriteLine( "{0} {1} {2} {3} {4}", f.B.b1, f.B.b2, f.B.b3, f.B.b4, f.B.bi );
            Console.ReadLine();
        }
    }
 
    [StructLayout( LayoutKind.Explicit )]
    public class Foo
    {
        [FieldOffset( 0 )]
        public A A;
        [FieldOffset( 0 )]
        public B B;
    }
 
    public class A
    {
        public int  ai;
        public byte ab;
    }
    public class B
    {
        public B( string Param ) { }
 
        public byte bi;
        public byte b1;
        public byte b2;
        public byte b3;
        public byte b4;
    }
}

Note that Foo is defined so that A and B overlap. But both A and B are reference types, however they point to the same address in memory. The result is somewhat surprising – although only A is explicitely initialized, B is accessed without any issues and because A and B overlap, the internal content of A is read through consecutive members of B.

I must say that I am really and deeply confused.

The ECMA, Partition II, 10.7 says

Offset values shall be non-negative. It is possible to overlap fields in this way, though offsets occupied by an
object reference shall not overlap with offsets occupied by a built-in value type or a part of another object
reference. While one object reference can completely overlap another, this is unverifiable.

If the code is unverifiable how can it run with no issues?

I always thought that the code which cannot be statically verified to be type-safe (and thus considered unsafe) can be executed only if SecurityPermission( SecurityAction.RequestMinimum, SkipVerification=true ) attribute is present in the assembly (this is how you let the code using pointers be executed by the runtime environment – while it’s usafe, it’s your responsibility to let it execute).

This is not the case, however, in the above example. The code runs perfectly under .NET 2.0 and .NET 4.0. What’s also confusing, there are two outputs from two different versions of PEVerify.exe:

Microsoft (R) .NET Framework PE Verifier.  Version  3.5.30729.1Copyright (c) Microsoft Corporation.  All rights reserved.All Classes and Methods in ConsoleApplication50.exe Verified.

and

Microsoft (R) .NET Framework PE Verifier.  Version  4.0.30319.1Copyright (c) Microsoft Corporation.  All rights reserved.[IL]: Error: [C:\Users\wzychla\Documents\Poligony\C#3.0\ConsoleApplication50\ConsoleApplication50\bin\Debug\ConsoleApplication50.exe : ConsoleApplication50.Program::Main] Type load failed.[token  0x02000003] Type load failed.2 Error(s) Verifying ConsoleApplication50.exe

Would you at least expect the latter to be used by .NET 4.0 Runtime? Well, it’s not, as the code runs under .NET 4.0. The former is invoked from

c:\Program Files\Microsoft SDKs\Windows\v7.0A\bin

the latter from

c:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools

The case is getting interesting! It seems that even .NET Framework engineers seem unsure whether or not such code is verifiable or not (or maybe the notion of “type-safety” changes as the time passes). Why the runtime does not prevent this code from executing without proper SecurityPermission even though the latest PEVerify claims it’s unsafe?

Nevertheless, I belive that the example from the above reveals something not quite intended. To me the .NET Framework is not type-safe anymore. Most people consider that type-safety means that the code accesses only the memory locations it is somehow authorized to access (directly or using dedicated API). However, consider this (I just modified the Main method from the above example):

static void Main( string[] args )
{
     Foo f = new Foo();
 
     f.A = new A() { ab = 123, ai = 12345678 };
 
     Console.WriteLine( f.A.ai );
 
     f.B.b3 = 44;
 
     Console.WriteLine( f.A.ai );
 
     Console.ReadLine();
}

How am I allowed to modify the internal contents of an object (A in this case) by modifying the contents of another object? Do you still belive that the runtime cares for the consistency of your objects? Consider this:

public class A
{
    public A( int i ) { this.i = i; }
 
    private int i;
 
    public void WriteI()
    {
        Console.WriteLine( i );
    }
}

This is your class. It does not expose any public data, it can only print the internal value. I can easily compromise your object by wrapping it together with another object having different structure:

[StructLayout( LayoutKind.Explicit )]
public class HackShell
{
    [FieldOffset( 0 )]
    public A A;
    [FieldOffset( 0 )]
    public HackA B;
}
 
public class HackA
{
    public byte i1;
    public byte i2;
    public byte i3;
    public byte i4;
}

and now the integrity of your object does not hold anymore:

static void Main( string[] args )
 {
     A a = new A( 5 );
 
     // do you believe in the integrity of your object?
 
     HackShell shell = new HackShell();
     shell.A = a;
 
     // well, here you are:
 
     shell.B.i3 = 17;
 
     a.WriteI();
 
     Console.ReadLine();
 }

Unbelievable.

Altough the internal data of an object can be modified using reflection, the reflection cannot be used to alter the integrity of an object. And while pointers can be used to alter the integrity of objects, using them means that your code is not verifiable and so it will not be executed without explicit security permissions.

The conclusion is as follows: it seems that it’s valid to write a code that is completely type-unsafe and verifiable in the same time.

Wednesday, October 14, 2009

A Master Programmer

You can say that you are just a good programmer when you write a computer program for a year.

You can say that you are an experienced programmer when you first write a computer program for a year, then you release it to your clients and then you fix bugs and develop the program for next five years.

You can say that you are a true master programmer when you first write a computer program for a year, then you release it to your clients, then you fix bugs and develop the program for next five years, then you screw something so badly that the version you release makes some huge, ultimate damage to the clients' important data and then you have to release yet another version which has to somehow magically undo the damage you have made with the previous version.

Are you a master programmer yet?

Wednesday, July 1, 2009

MVP Award 2009

I am glad to announce that today I've been awarded with the Microsoft Most Valuable Professional award for the 5th year in a row. I promise to keep up the pace in the upcoming year.

Wednesday, February 18, 2009

Beware when you upgrade ...

It's a real pity when you realize that you are truly disappointed by a new version of a software you use.

When Windows Media Player 11 came out, I've upgraded from WMP 10 immediately. To me, a new version always means new functions, enhanced compatibility, stability and security.

I got no problems with WMP 11 until I've bought two great soundtracks by Jeremy Soule from DirectSong. These soundtracks are protected by the DRM - you are required to obtain a valid license from the soundtrack provider to be able to play it with the Windows Media Player.

To be able to backup your downloads, DirectSong says that:

Our current licensing is set up to allow unlimited Soundtrack CDs to be burned per machine download.

Great, this does not work in WMP 11 - you learn from the WMP that the license does not allow you to burn, synchronize or even play the music (which does not stop the music from actually playing!).

After few nervous hours spent trying to find out what's the problem, I've learned from someone's blog that you have to downgrade to WMP 10 to get this to work.

I don't remeber beeing so disappointed by a new version of the Microsoft's software for a long time. A new version which breaks the backward compatibility is not something which I could accept.

Tuesday, December 16, 2008

Application Architecture Guide 2.0 from Patterns & Practices

The Microsoft's Patterns and Practices group continue their great work on the Application Architecture Guide. You should definitely check out the newly released Beta 2 version.

http://www.codeplex.com/AppArchGuide

Thursday, September 18, 2008

ASPNET user account folder and files

Did you ever thought about the folder and files created by Windows XP for the ASPNET user account?

"The ASPNET user account is an internal account and no one is able to log using this identity"

This is the most common answer for my question. And yes, the answer is true. At least it touches the most important part. But there's more.

You see, since Windows XP invokes ASP.NET processes using ASPNET account, it also automatically creates a user folder with typical structure (Application Data, Cookies, My Documents etc.) for this account.

And here comes the strange part: the folder structure for the ASPNET user account is created under "Documents and Settings" but instead of username as a folder name, the machinename/username is used.

Suppose now that your machine's name is XYZ and you use a user account named XYZ. Guess what - you'll see ASPNET user local folder inside your own user folder.

Something tells me that this is not quite right. I should not be able to peek into other user's files but since the folder sits inside my own local folder, I am able to browse it with no restrictions.

I would say that this is rather insecure, no matter if it can be easily misused or not.

Wednesday, August 27, 2008

Remote desktop to another OS and back again

As you surely know, XP is a single-user OS which means that there can be only a single user logged-in at one time. If the administrator logs in from a remote machine using the remote desktop tool, the current user is logged off and a message shows up saying "this machine is currently in use [...]"

If you have a spare 3 minutes, try to reproduce following steps:

  1. Log into a non-virtual Windows XP
  2. Use Virtual PC to boot a virtual Windows XP and log into it
  3. Use remote desktop in the virtual Windows XP to log back to the non-virtual Windows XP (which hosts the virtual one)

What happens to your non-virtual Windows session after you log into it from the virtual machine? Will the "this machine is currently in use" message show up?

Well, if you do it using two XPs, the host screen goes black and the machine stops responding. The non-virtual XP is controlled "from the inside".

What's great is that the non-virtual machine still runs - you can access all it's services, the application server, the database server and others run perfectly. You just cannot control the machine anymore using the keyboard.

I belive that there are few other interesting options to continue this experiment.

For example, can you regain the control of the OS by accessing it with the remote desktop from yet another machine? What happens when the Windows Server 2003 is used instead Windows XP?

Tuesday, July 1, 2008

Google considers its own query as spyware

Since I am stuck in an ASP.NET issue, as always I try to use Google. Specifically, I would like to somehow detect in server code whether the application is deployed on IIS or on the integrated development web server.

So I type:

asp.net detect development server

in the Google search text field:

However, only first 11 pages can actually be examined. When you click on the link to page number 12 (or any greater number) you'll learn that the link comes from a virus or a spyware software (screenshot taken on another machine since the description is localized on my development machine):

I guess it's one of the simplest viruses/spyware ever written. If you are a numerologist you'll probably know what's so specific in number 12.

Wednesday, November 21, 2007

The Definition of an Antipattern

A friend of mine has studied antipatterns extensively and asked me "What would be the best way to identify an antipattern in my code"?

To increase his confusion, I said "Almost everything from your code you've just shown to me is considered an antipattern".

Then I've added "Relax, this is not true. It's just you falling into paranoia of antipatterns".