generating a unique range of numbers

Often at work I find the need to generate a set of n unique integers in a specified range. In order to do this as efficiently and as easily as possible, I created a small C# class, which a colleague thought may be of general interest. Hence, I’m posting it here. I’m sure someone else has come up with a similar (or better) way to do this in the past, but I’m sharing my way regardless ūüėÄ .

The code is shown below (and you download the C# class here):

 class UniqueSetGenerator
      private int[] store_;
      private int size_;
      private Random random_;
      public UniqueSetGenerator(int size, int start)
          size_ = size;
          store_ = new int[size];
          random_ = new Random();
      private void PopulateArray(int start)
          for (int i = 0; i < size_; i++)
              store_[i] = start++;
      private int Delete(int pos)
          int val = store_[pos];
          store_[pos] = store_[--size_];
          return val;
      public int GetRandomNumber()
          if (size_ <= 0)
             return -1;
          return Delete(random_.Next(size_));

I think it’s pretty easy to see how this works, so I will not go into it in much detail discussing it, but the sequence of figures below shows the basic operations.

First populate the array with the values.

First populate the array with the values.

Use the Random class to obtain our first random number - in this case 6. Now copy the value at position size_ in the array to position 6, and decrement size_. Note not we don't actually delete anything from the array.

Use the Random class to obtain our first random number - in this case 6. Now copy the value at position size_ in the array to position 6, and decrement size_. Note: not we don't actually delete anything from the array.

We then use the Random class to generate another random number in our reduced range - in this case 3. We then copy the value at position size_ (i.e. 9) to position 3, and then decrement the size_ count as before. This process continues until size_ is reduced to 0.

We then use the Random class to generate another random number in our reduced range - in this case 3. We then copy the value at position size_ (i.e. 9) to position 3, and then decrement the size_ count as before. This process continues until size_ is reduced to 0.

So when would you require something like this? Well, say you need to generate unique random IDs with values 1 to 10, then you can use the class as follows:

    UniqueSetGenerator uniqueSet = new UniqueSetGenerator(10, 1);
    for(int i = 0; i < 9; i++)
        int id = uniqueSet.GetRandomNumber();
        // Now do something with the id....

For the moment I have only included a C# version but I will update this post with a Java version soon. Hope some of you find this useful.

is twitter the next myspace?

I use twitter and think it’s pretty cool way of throwing out a link or having some conversation bites with friends, but the marketing/social media whore side of it is getting out of control. The same thing happened with myspace.

There are a few articles I have read (sorry I can’t remember the exact articles) that state that twitter does not suffer the same woes as myspace etc ¬†– as ultimately you choose the people you want to follow. However, there are loads of people who follow you on twitter for no apparent reason, which to my mind is the same problem I experienced with myspace (it doesn’t really affect you much per say, it’s just seems a little lame).

Not that I mind people following me, I would like to encourage it, but only if you are interested in what I have to say. Otherwise what is the point?

Lately, I have been wondering what the response would be if I posted a link that directed you to a page that said “¬£100 will be given to each of my followers that see this page”. I suspect I would only be a couple of hundred pounds down, if I was down at all. Those who are following 5000 people must set up TweetDeck to ignore most of them and read only a chosen few. WHAT IS THE POINT IN THAT?

Well here’s the point (if you can call it a point). They were hoping to get you to follow them in return. I refuse though. That said, I do actually check the profiles of each and every person that adds me as a friend – apart from those with usernames like manboylovesxxxx. If their tweets are something that are relevant to my life, i.e. developer/designer chat, music chat, or sport then I will follow them. Other than that, what is the point in me following them?

Some recent articles ([1],[2]) have illustrated the measured conscious switch of twitter from a comunications medium between friends to a marketing tool. Surely the idea of using it in this manner is really just tantamount to spam? The idea is that you follow as many people as possible with the hope (knowledge) that a subset of those who you follow will follow you back. In fact you can even use Mr Tweet to observe users who adhere to this pattern. Surely what these users are doing is the same as those who send spam emails for Viagra and penis extensions do Рthey know that if they send out enough emails some idiot will reply. Is this a good thing to be encouraging?

One thing that has surprised me so far is the lack of using twitter to meet chicks! I haven’t seen much of this, and given that there is no better way to get a guy to use something than to encourage him with the thought of getting laid, this is a real surprise. I mean everything is there to make this happen. Who knows, maybe it won’t be long before the first ever twitter marriage. And mark my words, it will happen, with photos on TwitPic and a running commentary of the ceremony¬†using an iPhone. Maybe we need twitter-like sites that are domain specific to save us all from this black hole!

[1] 8 Useful Tips To Become Successful With Twitter

[2] The Twitter Survival Guide 

it’s easy to live in perfect hell

Just the other night I was watching a video of a live music performance on YouTube of my favourite band of all time Manic Street Preachers. While watching I observed how young they looked in the video. It then struck me how I remember the era when the video was recorded as if it were yesterday. The only problem is that it was nearly 13 years ago!

I’m pretty sure that I had many more ambitions and hopes back then – I would have been around 18-19 at the time. Certainly over the years my hopes and ambitions have changed. For better or for worse? I’m not sure.

There are things that I knew I really wanted. One of them was to attended university, my parents never forced me into this, I simply wanted to go (I was the first person in my family to do so). During my 4 years at uni I became certain that I wanted to do a PhD. However, by the end of my 4th year, the only real¬†post-graduate opportunity was at the University of Bath, and I wasn’t really interested in moving. As a result, I ended up taking a job at OKI. The week after accepting the job, I was contacted by someone at a local children’s hospital about an opportunity to undertake research for a PhD in 3D modelling of the eye for the early detection of certain diseases in children. I turned it down. I thought to accept it would be unfair on my new employer.

After working at OKI for a year I moved on to work with at Cisco Systems. Eventually after 3 years at Cisco (the last year being pretty miserable, but I was kind of holding out for a possible redundancy payment) I got made redundant. As it happens, just before I got made redundant, I had applied for a PhD position in the area of algorithms, and was fortunate enough to have been accepted (I ended up obtaining a really great supervisor and was glad I held off). So not only did I get a pay off as part of my redundancy, I also had been accepted for my PhD prior to being made officially redundant. Sweet. Persistence pays off.

So, why the life story I hear you all say. Well, it was basically to illustrate the point that if you want something, you really need to go and get it. During my time at OKI and Cisco I had become stale – you just settle in and accept what is coming to you. It was rare when I tried to force what I wanted in those jobs – and in actual fact when you did, you were usually met with resistance. When you meet this resistance to ideas it is probably the time to move on. In large companies you have too many people protecting their own jobs.

Now, what exactly is the relationship between what I’m saying and the music video? Well if the band in the music video has aged that much, then I have to – despite the fact that it doesn’t quite feel like I have! And I think the older you get the less likely you are to make bold moves in your life. But you shouldn’t let this happen. You should always have goals, if you don’t believe me, believe Seth Godin.

I often find myself writing/talking about poor management and poor working conditions. However, it’s not just me, with the increase in the number of blogs and social network sites,¬†people are finding it easier than ever to express their opinion. It’s not too difficult to find people saying: “I need two monitors and a Herman Millar¬†chair”, and “My manager doesn’t have a clue how to do his job”. The thing is, many will be like me, that is, they have never been a manager and never owned a company. Both myself and others have a total barefaced cheek to criticize something we have no experience of, but it also shouldn’t stop us.

So, this brings me round to my point. If we are going to complain about, our managers, our co-workers, or the company we work for, then maybe we should get off our arses and do something. Stop sitting about telling people how things should be done or moaning that nobody listens. The cost of doing something for yourself these days in minimal, so if we all really believe that we can do things better then do something about it. This is what moves society and technology forward.

Not everyone is going to read this and think “Yeah let’s do it”. You will have the folk who say “It’ll never work”. You just have to ensure that you don’t surround yourself with these people – I’m not saying ignore advice here, as you may genuinely be exploring something that is never going to work, but ignore advice from those who say a lot but do very little. It will be the same folk that say “He was just lucky with that business”, or “I could have done that”. ¬†Their procrastine¬†nature will only dampen your enthusiasm.

Surely there can’t be a person who is happy to live in mediocrity? The only glowing benefit of Capitalism (and¬†it’s the one that allows it to beat all other systems into the ground) is that it promotes the bettering of oneself and in turn society in general. It’s easy to mistake Capitalism for greed, but they are two different things. ¬†So if you find yourself hit by the credit crunch and unable to find work, moaning about your job, or moaning about how everyone else appears to be making all the money, stop it and try to do something positive. I’m not the only one saying this. The legendary Paul Graham wrote and essay about it and many people who own their own software companies are saying sales are as strong as ever.

Just remember “Libraries gave us power, and then work came and made us free”.

nonsense job adverts

I’m not currently looking for employment, but last week I was browsing the job adverts on a certain web-based recruitment site. One of the first adverts that I looked at had a description that went something like this:

We are looking for a web developer whose skills include the following: Java, Perl, Ruby, Python, HTML, XHTML, CSS, JavaScript, and Flex.

Exactly what language is their product written in? Is it all of them? I mean for god’s sake what a ridiculous, unreasonable and unobtainable set of requirements to be looking for. This was not an isolated incident though, there were a few adverts that contained this kind of nonsense in them – they were usually looking for someone with 2-3 years experience into the bargain. So, who applies for these jobs?

It may be someone who thinks they know each of these things inside out. But, forgive me if I’m wrong, I’m willing to bet that no such person exists – especially in their target audience. Any candidate who says they are experts in all of these things would have me suspicious right away.

Next, we have the folks that think: “Screw it, I only have a few of those skills, but I’m going to go for it anyway”. For me, the alarm bells would be ringing about the type of company that creates such a poor job spec. However, the person applying may be desperate. Now, given that that are unlikely to obtain many people applying for the job with the complete array of required¬†skills, and presuming they actually want to fill the position, they are going to have to accept someone who is less qualified. ¬†So, is possessing only two of the required skills enough to get you an interview? Or is there one skill in particular, say Java, that they are really looking for? If this is the case then JUST SAY THAT ON THE JOB SPEC YOU MORONS. It’s not that difficult, here, let’s give it a go:

Knowledge of Java is essential and experience of dynamic languages and web technologies will be looked upon favourably.

The companies core product must be written in some specific language, does it not make sense to promote the need for this skill? I cannot imagine any organisation creating a product with this myriad of technology. It’s just absurd.

So what are these companies achieving with job specs containing such utter nonsense? I don’t know. If my previous observations are valid, then it’s stupid candidates that think they know everything or people who are desperate. Is that a good thing? I think not.

I have never applied for a job through an agency, or website like this, but I know many who have – often you have no idea who the employer is before you enquire and I hate that (for fear of it being a bank). It is my personal opinion that it’s better to look for a job on, say, a companies website rather than through an agency – it takes a bit of work to identify companies in your area, but it’s worth it in the end. If a company can’t be bothered updating¬†their website to include their latest job advert, they they probably ain’t worth working for; it’s either laziness or bureaucracy preventing this them putting it up, either way, they are probably best avoided.

Enough of the company bashing though. There is one other thing that I would like to talk about in more detail, and that is the candidates. In the past year or so I have heard a few “interesting” stories about people who have showed up for interviews at various companies. I would like to share them with you.

The first is about some dude who turned up for an interview at a rather large web-based company that sells, well just about everything these days, in a pair of denims and a running shoes. I mean what in the face of this Earth would make you think that dressing in this manner was how you go about getting a job. I’m all for dressing casual, and I wouldn’t work for a company that made me wear a shirt and tie to sit at a desk writing code, but it’s an interview for the love of god, dress appropriately.

In another incident a young fellow turned up for an interview and when asked why he wanted the job he said that his Mum had forced him to go to the interview. I was astounded when I heard this – I did piss myself laughing though. Maybe he just didn’t fancy the job and was too nice to say this. I must admit, though, I have done something similar: basically, I was asked if I would go and “talk” to someone who was looking for a developer, and as it turned out, I didn’t want the job, but the guys were so nice, I found it difficult to tell them I wasn’t interested (the work they were doing was simply “not my thing”). I ended up stringing it out for several interviews, I even answered my mobile phone in the interview (I had a legitimate reason mind you), and they still invited me back. This nicely brings me onto my last, and most recent, story.

Only a few weeks ago a friend told me about a guy who had turned up for an interview and when they went down to greet him he was on his mobile. Not only did he not hang up, he put his hand up to their faces and said “I will be with you soon” and continued on with his phone conversation. As far as I’m aware this conversation was of no particular importance, and as he had applied for the job (in my case I hadn’t), so you have to assume that he wanted it. Hardly the way to go about getting it.

Anyway, I’m always interested in hearing about the stupidity of candidates and employers, so feel free to leave your stories in the comments.

the so SOLID principles

Year after year programmers find themselves faced with a new set of acronyms and principles that dictate the ideal way to develop software. After 10 years in this game it gets rather tedious and tiresome. The problem is you can’t let your guard slip with this stuff, because if you fail to listen, you may fail to find a job.

I have been meaning to write about this since the whole Joel Spolsky vs Uncle Bob debate [1][2]. As it happens,¬†Jeff Atwood beat me to the punch with his article “The Ferengi Programmer“. However, there are a few points I would like to address with regards to the whole acronym waving brigade.

First, let’s look at the SOLID principles. Do most good developers not use a relaxed variant of these things anyway, and have done so for the last 10 years? It seems like we are creating a set of rules for the dumb-ass programmers to follow because they can’t figure out for themselves what is good and what is bad. The problem with this approach is that the average dumb-ass programmer will not even know or attempt to understand the principles anyway. They will just keep typing.

Where does this leave the good programmers? Well they may find themselves trotting along to a job interview and one of the questions they are asked is: “Do you use the SOLID principles when writing code?”. ¬†This person then responds: “What are the SOLID principles?”. Often the interviewer will then disregard the candidate and in doing so may be throwing away the chance to work with a great talent. OK, you may say the interviewer is a dumb-ass for disregarding someone for this reason alone, but then again, I bet you can think of several that would. I’m certain there are people I know who are not aware of the SOLID principles, but who I think are awesome developers.

My problem with all this is that we shouldn’t automatically disregard someone because they can’t state the SOLID principles or that they are not using TDD (oh yes another acroymn), BDD (and another), and¬†DDD (and ANOTHER). God, are there people sitting at their workstation trying to figure out how to squeezing every-single-one of these things into their development cycle. I would bet you quite a bit of money that there is. These folk must get NOTHING done because they are spending so much time trying to do the “right” thing. The consequence of this is that they fail to write code that actually implements ANY functionality. It seems like it is no longer enough to be able to write good software. You also have to know all the latest buzz words. This is sad.

However, despite my reservations, I find myself thinking why shouldn’t we allow “bad” developers to become “good” developers? Is it rather patronising to say otherwise?¬†

What we all as developers should watch out for is the developer that accepts all these rules blindly, and applies them to EVERY situation. I’m all for writing code the correct way, but we also must be careful how we judge fellow developers.

developing software nobody wants

Developing software that nobody wants is a classic mistake.  Joel Spolsky once said that if he had listened to what his customers wanted, and created a product with only their suggestions, they would not have came up with as many nice features (I paraphrase from an episode of the stackoverflow podcast). However, maybe his logic is slightly skewed. Why? Well he is developing a product for software developers (FogBugz). Therefore, his target market is essentially the same people who are writing the software. This makes developers a great source for feature suggestions.

However, consider on the other hand the average software developer. They are not creating a product that is used by other software developers. Instead, customers can range from a highly technical audience (say Matlab), to a secretary with little computing knowledge. Clearly if we are creating a product aimed at the latter market, a software developer is the last person you should be asking for feature advice.

Unfortunately, misaligned product requirements actually happen in the wild. I have been involved a project that forced a user into making around 10 clicks to achieve a particular goal. Each step was valid, and allowed the user fine grained control over a feature. However, the user didn’t need this level of control, and they simply selected the same options every time. There was great difficulty convincing most of the developers that this kind of fine grained control was not required. Thus:¬†developing software without thinking about how a customer is going to use the software should be high on the list of UNFORGIVABLE sins.

Some problems, like the one described above, are hard to grasp for us programmers. As programmers we are taught to generalise, and I think we find it difficult to turn this off in our heads. This can be seen in many applications that drown us in XML configuration. Product managers, and CEOs have less room for excuses.

So how do we ensure that we are not developing software that nobody wants? It just seems obvious to me that you have to ask the people that will be buying your software. This seems so simple right? Can you even believe that it doesn’t happen? Is it beyond the realms of possibilities that somebody, somewhere, is developing a piece of software for a market without asking that market the features that they want? You’d better believe it. It happens all the time.

is it ok to not ask questions?

First, excuse the inherent contradiction in the title! Now, with the recent rise of as a programmers favourite dynamic and responsive encyclopedia, I find myself questioning my own behaviour.

Why? Well, I find that I almost NEVER ask any problem-based programming related questions to my peers.

Don’t misunderstand me here, the amount of stuff I don’t know is literally unbounded, it’s just that I will always work away at a problem until I have found the solution myself. I’m pretty sure that I didn’t always do this – I can vividly remember asking a friend with whom I worked with years ago many many programming related questions. Thinking back to those times though, it almost felt like I was cheating asking so many questions.

However, in the last 5 years, much of the development work I have undertaken has been as a contractor. In this setting I literally had no-one to ask. So what did I do? Well I just spent the time researching the problem, figuring out details, and then tried to use what I had learned to solve the problem – where the range of problems encountered ranged from the trivial to utterly complex.

This habit of not asking questions has continued with me. Even on a recent project I encountered several issues that literally had me stumped. ¬†Yet with a lot of effort and hard work, I got something that worked in the end. ¬†This, maybe wrongly, leads me to believe that those who ask a lot of questions are just not willing to put this kind of effort in – I suppose it’s that or they are just not capable! Or maybe they just prefer to economise their effort! Myself, I just think it seemed much easier to ask, and so that’s what I did. But that really just boils down to laziness. Right?

What can we tell from those who ask a lot of questions and those who ask none?

Despite my previous remarks, I think it’s easy to see why asking questions is a good thing: it saves time, builds communities, and most importantly it allows you to benefit from other people’s experience. That said, I feel that a developer who has the ability to free think, and “discover” a solution is extremely valuable – you may not always be in a position to find an answer to your burning question, especially if it is highly tied to the business logic of your application. I’m sure many of those people who ask the most trivial questions would benefit from the process of finding their answer, rather than being given it on a plate at the likes of stackoverflow. Is the success of stackoverflow the first sign of the dumbing down of programmers or is it the start of collective knowledge transfer that can only benefit our whole field?

The link between free thinkers and those who do not ask questions is not an if and only if relationship – anyone with any experience of tutoring students will know this. Many people who do not ask questions actually don’t have a clue (and maybe never will). ¬†You see this all the time with certain students; they never say they have a problem and they don’t ask questions. Then you see their exam results and think WTF.

This leaves me with a problem though: I don’t want to say that all free thinkers don’t ask questions, and I know those who do not ask questions are not all free thinkers. How do you figure out what is what? Mmmm, maybe I will just leave that as a question ūüėÄ

are you in it for the long run?

As developers we are often faced with the issue of time vs. quality. Let’s face it though, we could spend forever getting it all just right, from documentation right through to unit testing. However, there has got to be a point when you let it go and see how it flies.

This is particularly true when you are faced with a real-world optimization problem, e.g. scheduling, time tabling, process optimization.  Often with this type of problem it is very difficult to obtain an optimal solution. However, I find the more time you spend on the problem the better you understand it, which normally leads to an improved model.  In turn, the improved model leads to a better solution, or an equivalent solution found in a shorter time.

This begs the question when do you give up and say it’s finished?

To be honest, I’m not sure what the answer is to that. As a developer I want to keep on going, hoping to pull the rabbit out the bag, but as an employer, I just want the thing done good enough to have a competitive edge.

The same goes for unit testing – especially at a startup. In this setting you are NOTHING until you get your product out the door, and until you do, you are hoping and praying that no one beats you to the punch. Some people say that quality shines through, but how bright it shines I’m not sure. ¬†Take myspace as the perfect example. It has no redeeming features –¬†it just got there before everyone else. Does anyone know a band not on myspace though?

However, Google proved that the best product can establish its market regardless of its starting position – I now find it difficult to believe that I thought Altavista was good. It’s important to remember though that for every Google there is a myspace (or the ipod, for that fact) where the first is not the best, but people have invested too much time in their choice to change (or in the case of the ipod, maybe it’s fashion outshining functionality).

So what IS the answer to when is the software ready to go? Maybe the simple answer is “whenever someone is willing to buy it”!