Coding Malpractice

I just wrote the following line of code. And it’s no mistake: it functions perfectly and does exactly what I wanted it to do:

$count += 0;

This is surely poor programming practice, essentially implicitly recasting a variable as an integer. But it’s simple and it works flawlessly. (The context: I run an SQL query saying SELECT SUM(votes)..., which makes the tabulation of all the entries MySQL’s problem, not mine. The one ‘flaw’ is that the sum of no votes isn’t 0, but NULL. This becomes a very important distinction when you’re trying to display a number: “0 votes” isn’t the same as ” votes.”)

Since we all know that NULL + 0 = 0 (and, of course, integer + 0 = integer), adding 0 works flawlessly. Could I just convert it to an integer? Probably. But I haven’t done that stuff in a while, and I was far too lazy to pull up the documentation. And incrementing a variable by 0 is way more fun.

Saving the Auto Industry

My whole family drives Toyotas. We love America and all, but we want good, solid cars. The U.S. is, understandably, concerned about how much oil we’re using. So we’re trying for a requirement that, by 2020, all cars sold get 35mpg at a minimum. Of course, the car companies are complaining that this is going to be incredibly difficult to do.

Two comments:

  • This is utter BS. My mom gets 50 mpg with her Prius. Honda did it in 1987.
  • Why does the government need to get involved? The way I think it should be working is that we say, “$3 a gallon for gas is ridiculous! I want a car that gets better gas mileage!” We stop buying cars that get horrible gas mileage, and, consequentially, Detroit stops making cars that get horrible gas mileage because no one is buying them. It costs me $40 every time I fill up. I wince every single time.

I found this video online. I’m not going to lie: it’s dry, and 20 minutes long. I was kind of proud to follow him most of the time as he talks about internal rates of returns and demand pull and the like. He makes some extremely obscure references, and even now, I’m not sure what he was talking about with oil at $12 a barrel.

And yet, despite it being presented in a technical, academic manner to an audience that’s definitely not normal people, he makes some points that are really, really, really worth hearing. One of the simplest ones: efficient cars are going to be made, the question is who’s going to make them. And, at least right now, it’s not us. (And it really boggles my mind, frankly. Ford makes one hybrid: the Ford Escape Hybrid. 34mpg on an SUV is impressive (I get 20-22). But what the heck market are they appealing to? They manage to completely dilute the effects of a hybrid engine by putting it in an SUV.)

GM developed a “concept car” 16 years ago that, as I recall, got close to 100 miles a gallon. Where is it?

Besides oil, another huge problem we’re facing is a ridiculously huge trade deficit. If we could make cars good enough that we wouldn’t have to keep importing cars, we could certainly help.

He presents some amazing statistics, too. 87% of the energy from fuel used in cars is utterly wasted. Only 6% of the total energy actually moves the car. (And when you figure in that the car weights significantly more than the passengers and luggage, he says that less than 1% actually moves the passengers.)

He says the solution is to lighten the car. I cringed for a minute. Lighter cars, especially on today’s roads, are asking for disaster. You can go drive your 500 pound car, and I’m sorry if I kill you when you crash into my SUV.

But it turns out that this is somewhat wrong. He showed a picture of a McLaren SLR (a $400,000+ car) that was made out of carbon fiber. It’s very light. Some idiot T-boned the car. Their car was totaled. The McLaren driver had to buff out a scratch in the paint. He suggested that, if you were to smash the car head-first into a brick wall, about 25 pounds of carbon fiber is all it would take to absorb the impact and let you walk away unharmed.

He goes on to call heavy cars “hostile cars,” and really, he’s got an excellent point. We’re making heavy cars solely for safety with other cars. But we can increase fuel efficiency, maintain (or increase!) driver safety, and decrease risk to other motorists by simply changing materials.

Oh, and one final point he makes that I thought was interesting: we think of OPEC as a cartel that has tons of power. In actuality, our power of demand far outweighs their supplier power, and we have the power in the equation. Except that we can’t stop buying oil. Years ago we saw a lull in demand, and basically gave OPEC the bird. He suggests doing it again.

I didn’t expect to watch the whole video, which is 20 minutes long. But before I knew it I was done. And it’s pretty thought-provoking.

Serverama

I’ve been itching to start up a Boston-based dedicated server company. There’s surprisingly few options. As I dig around I’ve found a few that exist, but they’re practically unheard of. I want to start one up and do some good marketing. Name recognition is pretty big when it comes to dedicated servers. However, none of the people I’ve talked to so far (my mom) has been willing to give me the ~$100,000 start up capital I’d need.

I have slightly different plans for furnishing the racks I’d want in a colocation center, but there are plenty of good deals to be had on eBay, in case anyone’s in the server market.

  • 1U Dell, dual 3 GHz Xeons, 1 GB RAM, 73 GB SCSI (10K RPM). $485 + $25 shipping.
  • If you’d prefer disk space over disk speed, there are also 1U SuperMicro machines, a 2.8 GHz Xeon (dual-capable, but not so equipped), 2 GB RAM, and a pair of 250 GB disks. About $400 shipped.
  • Comparatively expensive, but $1500 buys dual 3.2 GHz Xeons, 4 GB RAM, and 3x 146 GB SCSI disks.
  • Not cheap at all, but dual quad-core processor, 16 GB of RAM, and 6x 146 GB disks is my type of machine! $5375 if you’ve got the cash burning a hole in your pocket… (It looks like these processors support VT virtualization, so you can run something like Xen on it and host multiple virtual machines, Windows or Linux, without problems.
  • Dual 3.2 GHz Xeons, 4GB RAM, and a pair of 750 GB disks? $1277+58 seems like a pretty good deal!

Subtly Bad Code

Alright, let’s have a little fun… I just added a new blog and went to include it on the main page, but my code failed citing the database throwing errors. It took me forever to find. I’m curious if others can find it.

I was further confused because the code worked fine until I added the new blog to the list of ones for it to use, and it was specifically built so that it wouldn’t matter how many blogs there were. It has a separate file that just lists blogs to include, and reads that file at runtime and builds a query to retrieve posts from all of them.

You need some background, first… All the posts are stored in a database, so each has its own table. I built this monster query, basically looking something like (Get most recent posts from blog 1) UNION (Get more recent posts from blog 2) UNION (…3…), and then tack an “ORDER BY…” onto the end. Credit for this idea goes to Andrew; I’d have never thought of it myself.

What the list includes is blog IDs in the database. They ranged from 2 to 9, skipping 8 (which isn’t used). After a bout of spam registrations, the numbers got run up, so when I included the new one, it was numbered 51.

The below code (in PHP) calls some custom-rolled functions, but I’ll just say up front that the error does not depend on understanding how they work. Similarly, the answer does not have to do with caching in any way, so don’t get too hung up on the amount of code devoted to working with the cache. (And finally, I’m building one huge variable called $query the whole time, and then return that variable… This isn’t a crucial thing to understand either, I just wanted to explain it since it’s somewhat of a bizarre practice. .= is the PHP variable concatenation method.)

// $count is the number of blogs to pull out
function genRPQuery($count) {
  // Retrieve it from Memcache
  $query = getCachedObject("bigquery-$count");
  // It'll return NULL if it doesn't exist, so we check for that...
  if($query) return $query;

    // Since we're here, we didn't return, and
    // thus didn't get it out of the cache

    // Next two lines read in the files. blogList()
    // returns a list of the blogs -- it's little more than a
    // file read with caching enabled.
    $blogs = explode(',', rtrim(blogList(),"n"));
    $fields = rtrim(cachedFile('./fields.inc',30), "n");

    foreach ($blogs as $i) {
      // We have a loop for each blog
      // For unfamiliar eyes, .= is PHP's means of variable concatenation
      // We're building a ridiculously-long query, each one a SELECT, encased in
      // parens, and we UNION them all together...
      $query .= "(SELECT $fields FROM wp_" . $i . "_posts WHERE post_status='publish' AND post_type='post' AND post_password='' ORDER BY post_date DESC LIMIT $count)n";
      // If we're not on the last one, insert a "UNION" in (see above)
      if($i

Remember, it worked fine when the list was blogs number "2,3,4,5,6,7,9" but the simple change to "2,3,4,5,6,7,9,51" causes it to blow up and try run a query with invalid syntax. This made no sense to me, since the code was built to not care about things like that. I eventually found it, and feel like an idiot.

I've posted a hint in the comments... It's in the interest of fairness because I turned on some debugging and got the information I share. But it also really narrows your attention to a couple of lines, so I don't want to include it in the main post.

Spam

My e-mail setup right now for n1zyy.com and ttwagner.com consists of just forwarding all e-mail to GMail. It works fine, and the spam filters there have been pretty much 100% effective. However, it bothers me that I’m forwarding dozens, if not hundreds, of e-mails just to have them ignored. Some basic spam filtering should really take place on my server.

I made a few basic configuration changes to Postfix, the MTA I run. In a nutshell, I tell it to require stricter compliance with e-mail RFCs: e-mails with HELO addresses that don’t exist (or just don’t make sense), and people sending multiple commands before the server replies to acknowledge them, for example, now results in mail delivery failing. The default configuration errs very much on the side of ‘safety’ in accepting mail, but the trick is to tighten it down enough that you’ll reject mail that’s egregious spam, but not reject anything that could be from a mailserver. And that’s where I’m at.

I also installed SpamAssassin. I’m currently using it in conjunction with procmail, and therefore wasn’t quite sure if it works. I set it up to make some changes to the headers, so that I can verify whether it’s working. But I ran into a problem I never thought I’d have: I’m not getting enough spam. I’m sitting here eagerly awaiting some to see what happens. And it’s just not coming.

Drive Carefully

This hilarious thread on bad drivers reminds me of a video that’s way funnier than it should… was shot of some cars sliding on fresh snow down a hill. It’s pretty common: someone brakes too hard or not soon enough, or steers to sharp, or whatever, and loses control. It’s scary. But watch that video–in particular, the first car.

How is that even possible?!

The Results are In…

While I make no secret of my political beliefs, I ordinarily try to shy away from outright endorsement of candidates. I used to think that politics was kind of like religion: something that’s a very important part of peoples’ lives, but something that’s bound to offend people if you talk about it much. Plus, I really don’t like to divulge too much in the way of significant personal information, and who I supported (or how I prayed) was no one’s business, I thought. But to me, my vote on January 8 is going to be one of the most important things I’ve ever done.

And I’m going to break with tradition and tell you who I’m voting for. I’m going to case my vote in the primary for Barack Obama. And I’d like to encourage you to do the same.

If you’d asked me a few years ago, I’d have told you that I was somewhat unhappy with politics. I was sad when Kerry lost the 2004 elections. But I’ve gone from “somewhat unhappy” to being truly afraid for the future of our country, and for being truly disgusted with some of the betrayals of the American people. I’ve been exploring jobs in other countries. Not because I want to send some sort of message that I don’t like politics, but because I don’t think I’m living in the America I was so proud to belong to. I hate alarmists, but, well, sound the alarm bells: America is in crisis.

Maybe about six years ago, I started getting disappointed every time I turned on the news. Something bad was always happening. And it’s happened every day for all these years. Most of the world hates us. Not all of it, but huge parts of it. While much of the world is already suspicious of our motives, we start a series of wars in Islamic countries and then go and issue proclamations expressing our support for Christianity. We seek to teach the “backwards” world about the benefits of freedom and democracy, by capturing them, transporting them to secret prisons, abusing them, and then torturing them in barbaric fashions. Does no one see the blatant hypocrisy? And when we point out that we already outlawed this, and that it flies in the face of everything that is America, we debate it. We’re at war against people who torture people and defy democracy, and we need to be able to torture people and defy democracy in order to do it, after all. The kings of democracy also have a whopping one state that permits same-sex marriage, something that more advanced countries have been permitting for years. And when we started calling it unconstitutional to deny people a basic civic right, we changed the Constitution in many states, and tried to change the Constitution. Forty-two states, and the Federal government, also passed laws explicitly prohibiting gay marriage.

When I graduate from college, I’ll have no health care until I get a job that offers it. But I count myself as lucky, because millions of people just don’t have health insurance at all. We tried to pass a bill ensuring that poor children could get health insurance, but we struck it down. Twice. We spend more than any nation on health care, and yet our system is among the worst. But people argue that there’s no problem, or that, if there is a problem, we shouldn’t pay for it.

Oh, and the American economy, kings of capitalism, is in the toilet. No offense to Canada, but it’s pretty depressing when the Canadian dollar surpasses the American dollar. We had an investment banker come in and talk to us a while back. He moved all of his money out of the country a few years ago.

I want to turn on the news and smile once. I want to stop looking at jobs in the Netherlands.

I want to be proud to be an American again.

I’ll admit that, at first, I didn’t want to support Obama. As much as he blew me away every time he spoke, I was really concerned about his lack of experience. Foreign policy is going to be huge, and he’s a one-term Senator. Why should he be the guy?

I think it’s the same reason that I’d be an amazing police chief, or an amazing president of my school. No, not hubris. Because he has a fresh experience. You spend too much time at something and you start to maintain status quo. Look at a lot of the businesses, especially small ones, that have been around for decades. What are they doing? Nothing new! Where are they going? Nowhere! They’ve built themselves a fabulous box in which to think, in which there’s really not a lot of room to maneuver. They found something that works and stuck to it. But this box–these blinders–prevent change. It’ll help you keep things going, but it won’t help you change the direction things are going on.

And if there’s anything we need, it’s change.

But don’t take my word for it. While I strongly support him, you owe it to do your own homework. Check him out. Read about his stance on the issues. And, living in New Hampshire, go see him speak. I always go in with high expectations and leave with them surpassed. And I’m firmly convinced that he’s the right man for the job. We’re not picking some arbitrary thing. We’re choosing our fate for the next four years. And we have an obligation–not just to ourselves, to our neighbors, and to the world, but to future generations–to make sure we elect the best person for the job.