The Global Leader in PC & Android System Health Solutions

Year: 2008 (Page 45 of 52)

Enums in C++ Suck

Like most strongly typed languages, C++ has a way to group a set of constants together as their own type called enums. Enums are extremely useful in a wide variety of circumstances. However, enums in C++ have a lot of problems, and, in fact, they’re really a mess. I’m certainly not the only person to complain about this, either.

Enums don’t fit in with the rest of the language. They feel like something that was tacked onto the language to me. This is purely an aesthetic issue, and the fact that they’re useful in a wide variety of circumstances probably negates this.

More practically, you can’t control the conversion of the enum to and from integers. For example, you can use the less than operator to compare an enum and an integer without using a cast. This can result in accidental conversions that don’t make sense.

Perhaps the worst problem is the scope of the constants defined by the enum. They are enclosed in the same scope as the enum itself. I’ve seen a lot of code where people prepend an abbreviation of the enum’s type to each of the enum’s constants to avoid this problem. Adding the type to the name of a constant is always a good sign that something bad is happening.

In addition, you can’t decide ahead of time what the size of your enum’s type is. C++ normally tries to give the programmer as much control as possible. In the case of enums, this allows the compiler to store your enum in whatever type it wants to. Frequently, this doesn’t matter, but when it does matter, you’ll end up copying the value into an integer type that’s less expressive than than the enum.

After the break, I’ll explain what other languages are doing about it, what the next iteration of the C++ standard will do about it, and what you can do about it now. Continue reading

A Theory of Scheduling Low Priority Work

PC-Doctor delivers an enormous number of different products to different customers. Each customer gets a different product, and they get frequent updates to that product as well. Delivering these products requires complex synchronization between dozens of engineers. We’ve gotten great at scheduling the most important work. Our clients love us for that.

However, the low priority projects get released significantly less reliably. Until recently, I’d assumed that this problem was unique to PC-Doctor. Based on some extremely sketchy evidence from another company, I’m going to release my Theory Of Scheduling Low priOrity Work (TOSLOW).
Continue reading

Making Regexes Readable

Regular expressions are extremely powerful. They have a tendency, however, to grow and turn into unreadable messes. What have people done to try to tame them?

Perl is often on the forefront of regex technology. It allows multiline regexes with ignored whitespace and comments. That’s nice, and it’s a great step in the right direction. If your regex grows much more than that example, then you’ll still have a mess.

Click through to see another solution.
Continue reading

Anonymous Methods in C# are Your Friend

A previous post warned of some of the dangers of using Anonymous methods in C#. Yes, it’s true you can get yourself into serious trouble with these C# treats. But as you play with them some more, you can actually see that they are much more fun and less scary than you might first think.

One of the first things you need to realize when anonymous methods are created is that they capture / hijack variables that are used in that anonymous method from the scope that they were originally in. So yes, modifying the variable from a particular scope is going to change its value for everyone else within that same scope as well, regardless of which block it is executed from. This is very different from the Java way of doing things (as I understand) where a variable that is accessed from an inner class must be declared as final, read-only, uninteresting. That’s not to say that you can’t do the same thing in Java, but one dimensional arrays are such a pain. I digress. Continue reading

Nginx vs. Apache2 – in Rails Running Death Match

Recently I proposed running Nginx rather than Apache as the frontend web server for a soon to deployed Rails app that shall remain nameless. The response I received was that Nginx was unknown and likely introduced security risks. My response was, “maybe, but Nginx is faster and uses less server resources”. To which I was ordered… “prove it”.

And so I did, by setting up my Rails app on a local server with all things being the same but switching the frontend servers (Nginx, Apache) and running a set of performance tests. The results were interesting and I was able to create some pretty graphs of the results.

Continue reading

« Older posts Newer posts »