Posts

Stupid code fragments, part two

Probabilities are hard. As an example, there's a known puzzle: a family has two children; if one of them is a girl, what is the probability that the other one is also a girl? The answer, un-intuitively, is not 1/2 but 1/3. There are various explanations but – as with the Monty Python puzzle years ago – I wanted to write code to check it out, so I wrote the following using LinqPad: void Main() { var rnd = new Random(); // Generate a random set of families with two children; true means girl, false means boy var all = Enumerable.Range(1, 10000).Select(_ => new Pair(rnd.Next(2) == 0, rnd.Next(2) == 0)).ToList(); // Extract only the families with at least one girl var oneGirl = all.Where(it => it.First || it.Second).ToList(); // Out of those families, how many have two girls? The result should be 1/3rd var otherGirl = oneGirl.Where(it => it.First && it.Second).ToList(); Console.Wri...

Stupid code fragments, part one

I just discovered a surprisingly simple (and obvious in hindsight) algorithm for calculating the week index of a given date. For example, April 15th is in the 3rd week (or the 3rd Tuesday of the month). I was going to do the usual thing and just Google for it but then I realized that the solution is extremely simple: private static int GetWeekPosition(DateTime date) { // the position of the given date is how many times I can subtract 7 days (go back one week) and still be in the same month // in other words, it's the integer part of (day / 7) return date.Day / 7; } (I am returning a base-zero result, but you can of course add 1 if you need it.) I realize this is not the answer to the Universe or anything but I thought it's interesting.

Science

I've long used the expression "real science, so called because it can only be found in books and movies". From a talk at Pycon 2014 : The ideals reality of science: The pursuit of verifiable answers highly cited papers for your c.v. The validation of our results by reproduction convincing referees who did not see your code or data An altruistic, collective enterprise A race to outrun your colleagues in front of the giant bear of grant funding H.T to Daniel Lemire .

Telerik components

I am working on some projects where I am forced to use the Telerik components. They are, without a doubt, the most overpriced junk I've ever had to work with. It pains me that people pay a lot of money for them just because they look pretty. (I am trying to remember ONE issue where the Telerik components were involved and I haven't had to fight them. It's just not coming to mind.)

Avoid boolean parameters

Here's an example of a method in a sim. This started out as a simple method: all days are the same in our simulation, so the method just prints out some activities: void SimulateDay() { Console.WriteLine("Wake up."); Console.WriteLine("Eat breakfast."); Console.WriteLine("Watch TV."); Console.WriteLine("Eat lunch."); Console.WriteLine("Watch TV."); Console.WriteLine("Eat dinner."); Console.WriteLine("Go to sleep."); } Ok, so not much of a life, but this article is supposed to be about programming. :) A new requirement comes up: Sunday should be different. Well, almost; it's actually the same as the other days with a single difference: instead of watching TV, the sim goes to church on Sunday mornings. No big deal, we can augment the method with a boolean parameter: void SimulateDay(int isSunday) { Console.WriteLine("Wake up."); Console.WriteLine(...

Going to college

There's a saying in my country: going to college is not about learning X; it's about learning how to learn, how to study, how to find out things about X by yourself. Which means, in a liberal translation: going to college is for people who don't know how to use Google.

Stop doing this

Pet peeve: I hate micro-management when it comes to code. Yes, it's faster in the short-term but it's a pain when the code has to be changed. (If you don't know that all code has to be changed, you haven't programmed long enough.) In this particular case I have encountered code similar to: var sb = new StringBuilder(); foreach (var p in list) { sb.Append(p.Name); sb.Append(" "); sb.Append(p.Quantity); sb.Append("*"); sb.Append(p.Price); sb.Append("="); sb.Append(p.Value); sb.Append(","); } if (list.Any()) { sb.Remove(sb.Length - 1, 1); } return sb.ToString(); (It's even worse when it's a for loop instead of foreach , but let's not go there.) The problem with this code is that it's extremely easy to get lost in the details. "Oh, I should put spaces around the operations." "Oh, I should add a space after the comma, but then it has to be remov...