Tag Archives: math

In this universe we obey the law of commutativity

This kind of thing has happened to be a few times now, so I thought I’d share the fun.

In one of our pieces of software we have a process that looks like this:

void MyThread()
{
    while (true)
    {
        DoFunctionA();
        DoFunctionB();
        SleepFor10Seconds();
    }
    
}

While FunctionA and FunctionB are conceptually similar, they interact with completely different systems.

We had a problem with FunctionA the other day–it was taking 120 seconds to do its thing instead of the normal 10 (or less) because a remote server was down. This caused problems for FunctionB because it wasn’t running as often as it should have so things were getting backed up in the system. Oops.

Now, the solution is to split these two functions into two independent threads so they don’t interfere with each other, and I’ve been meaning to do this for a while, so that’s what I proposed.

Response back: “That’s a good idea, but before we do something complicated like that, can we just put FunctionB first?

Um, no.

The time we want to minimize is the time between running FunctionB, which is TimeSleep + TimeA. Putting FunctionB first makes it TimeA + TimeSleep. Last I checked, those were actually equivalent.


Check out my latest book, the essential, in-depth guide to performance for all .NET developers:

Writing High-Performance.NET Code, 2nd Edition by Ben Watson. Available for pre-order:

Fighting Brain Rot

Alex Shalman has a great post at zenhabits about how to avoid letting your brain decay into apathy and atrophy. It’s a great call to action, to find ways of self-improvement. I think the behaviors listed here dovetail very nicely with the attributes of highly effective programmers.

By continuing to do as we always have, the quality of results will be the same as always. Only when we step out of our comfort zones, and push ourselves to improve, will we gain useful new experiences, knowledge, and ideas.

I think the methods of expanding the mind are highly applicable to software developers. I just have a little bit of commentary on each one.

11. Reading – I think I would have put this at #1. It’s the easiest way of cramming information into your skull. It’s the most efficient method of information transfer, and that’s our bread-and-butter as programmers, so we should become expert at it.

10. Writing – We write code for a living, not necessarily prose, but communication is key to so many areas in life, that learning how to write effectively is critical to most careers. For myself, I definitely find it easier to express myself in writing than in-person. Doing this well becomes a critical ability.

9. Puzzles – Developing a large software project is in many ways like an enormous software project. It’s so large, though, that we can’t comprehend it all at the same time. But practicing other types of puzzles can train our brains to look for patterns and to develop new, creative ways of thinking. My favorite offline puzzle is the New York Times Crossword, but I enjoy the occasional sudoku.

8. Mathematics – a good understanding of boolean logic, prepositional calculus, discrete mathematics, asymptotic notation, etc. are great things for developers to have. A general understanding of algebra, calculus, trigonometry, and statistics also comes in handy more-than-occasionally. Another valuable idea that comes out of mathematical understanding is the idea of precision in thought and rigorousness in testing or understanding your software–think loop invariants.

7. Painting – I am definitely not an artist by any means, but the underlying principle of some kind of artistic self-expression is important. The creative side of your brain must be regularly exercised. For me, this is in the form of building Legos.

6. Cooking – I initially found this to be a peculiar choice, but it makes more sense when I ponder it. Cooking is at once creative and precise. Not only does it use all the senses, but it requires you to think on your feet and be very, very organized and detail oriented, especially when you start cooking for more people. Planning and execution both become huge issues.

5. Music – I wholeheartedly agree, and I’ll even go out on a limb and say that you need to listen to lots of genres of music, especially classical. Why classical? Because it exhibits more musical complexity than all others. It doesn’t minimize various musical aspects (variation, melody, harmony, tempo, timbre, i.e.) for the sake of a single one (i.e., rhythm).

4.Poetry – I used to write fiction and poetry in high school and earlier, but it’s been quite a while. I do remember it being quite the exercise to compose sonnets–it forces you to be extremely creative with grammar, syntax, meaning, vocabulary, and more.

3. Meditate – This is an art I need to learn more about. I find I do this automatically in some situations where I’m not otherwise preoccupied (the shower), and I can solve a question I’ve had. I find that NOT doing something is as important as doing something in many cases. When I’m faced with an especially thorny problem at work, it really helps to just write down my thoughts about it and let it sit for a few days while I think about it in my off moments. Most of the time, I can come back and have a better solution than if I had started right away.

2. Learn a language

A language that doesn’t affect the way you think about programming is not worth knowing. – Alan Perlis

The original article obviously means foreign spoken languages, which I definitely agree with. I speak Italian, and the insights it’s given into my own native English are quite valuable. If you’re a careful student, knowing two languages definitely forces you to think about the meaning of words and constructs. It’s much harder to take things for granted.

I think the same is true of programming languages–knowing more than one helps your mind think about a problem in different ways. Once you understand functional programming, for example, you will never look at programming the same way again.

1. Question Everything – This is analogous to love of learning in my Effective Programmers essay. It’s not being a jerk and denigrating everybody else’s ideas. It’s asking yourself continual “Why” questions in order to understand the issue.

Technorati Tags: ,,,,


Check out my latest book, the essential, in-depth guide to performance for all .NET developers:

Writing High-Performance.NET Code, 2nd Edition by Ben Watson. Available for pre-order:

My Wife’s Logic (or Women’s Logic Explained?)

For all of you who learned boolean algebra in your CS courses in college, I am sorry to be the bearer of bad news: your education was incomplete. The list of boolean tautologies and truth tables that you may have memorized or learned over time was wrong, with some startling and glaring errors.

To rectify this, I present some new truth.

First, an example in real life, which really happened. For context, Leticia is my wife, has beautiful, olive skin, with dark brown eyes, and hair with various colors of brown, and the little girl in discussion was as white as can be.

Leticia: Look at that little girl–she’s so beautiful! Do you think we’ll ever have a girl who looks like her?

Me: No.

L: So you think our daughter will be ugly!

M: Uhhh……no. I don’t think she’ll be white.

Transforming this little conversation into boolean logic:

A: This little girl is beautiful

B: Our future daughter will look like this girl

C: Our future daughter will be beautiful

So my wife says that AB–>C, and that if I say !B, then !B–>!C. I always knew that, but what I didn’t know is that the other options I thought existed aren’t actually valid! (i.e., that !B–>C is also true, or in other words, that C can be true regardless of the value of B) Who knew! So I present below corrected truth tables. Wikipedia, take note.

Standard Truth Table for Implication

Improved Truth Table for Implication

X Y X–>Y
F F T
F T T
T F F
T T T
X Y X?Y
F F T
F T F
T F F
T T T

So you see that the correct form of implication when dealing with this logic is the same as equality.

Now you know, beware.


Check out my latest book, the essential, in-depth guide to performance for all .NET developers:

Writing High-Performance.NET Code, 2nd Edition by Ben Watson. Available for pre-order:

3-Valued Logic (Boolean + Unknown)

There is a really interesting post by Hugo Kornelis on what happens when you have to introduce the value Unknown to boolean algebra. Unfortunately, it also reminds me of What Is Truth? on WorseThanFailure. 🙂


Check out my latest book, the essential, in-depth guide to performance for all .NET developers:

Writing High-Performance.NET Code, 2nd Edition by Ben Watson. Available for pre-order:

Math Magic

I just started reading Scott Flansburg’s Math Magic book. It’s all about more efficient and intelligent ways of doing math. It’s an idea I’ve long wanted to try–to increase my speed and ability to crunch numbers in my head. The hard part is practice–how do you make yourself good at this without a lot of practice? And how fun is it to practice adding numbers in your head? It’s easy to think of some periodic situations: shopping and budget balancing, for example, but after that I’m not sure.

I think I’ll write a small PocketPC app that can give me random problems to solve in my head, as well as time me and note improvements. Cool…any excuse to write a program!


Check out my latest book, the essential, in-depth guide to performance for all .NET developers:

Writing High-Performance.NET Code, 2nd Edition by Ben Watson. Available for pre-order:

Math can be freaky…

To preface, I am not a mathematician. I can hold my own in most college-level math (I’ve taken 3 years of algebra, trig, geometry, 3 years of calculus, and 1 year linear algebra: where is all that knowledge now?), but I’m still easily impressed.

I’ve been working through an algorithm textbook (more on that later), which mentions a number of very interesting mathematical equalities:

I.
13 = 1
23 = 3 + 5
33 = 7 + 9 + 11
43 = 13 +15 + 17
etc.

II.
13 + 23 + … + n3 = (1 + 2 + … + n)2

III.
12 = 1
22 = 1 + 3
32 = 1 + 3 + 5
42 = 1 + 3 + 5 + 7
etc.

To be honest, when I first read these it blew my mind. There is a beautiful correspondence demonstrated in these ancient forumlas that is nearly breathtaking when you first learn of them.


Check out my latest book, the essential, in-depth guide to performance for all .NET developers:

Writing High-Performance.NET Code, 2nd Edition by Ben Watson. Available for pre-order: