Friday, July 31, 2009

Recession

There's one thing about the recession (and the economy in general) that really bugs me: Debt. Not personal debt so much, but debt owed by governmental bodies.

People complain about how we are becoming a nation of spenders, without saving any money. Where do you think this habit came from? Watching our government! The federal government as well as every single state (as far as I know) owes money to somebody, and is paying interest on those loans.

But, you say, how else would things get done?

Like they get done in the households of people who know how to save! If I want to buy a computer, I save money until I can afford it, and then I purchase it. The same thing goes for jewelry and virtually everything else less than the price of a car.

Loans exist to satisfy the impatience of people. I don't want to wait for 4 years saving money to purchase a new car, when I can purchase it today and pay for it over the next 6 years (notice the time difference there). This is especially true for houses; I don't want to save my money for 20 years to purchase a house, when I can buy it now, move in, and enjoy it while paying for the next 30 years.

But why are governments impatient? Sure that road needs to get paved today, but why not put money aside so that when it needs repaving in 20 years, we don't need to take out a new loan for it?

My problem with governmental debt really boils down to this: It is a black hole. If you borrow money to do something today, then you will need to not only pay back more than what you borrowed, you will also need to borrow more to do that same thing next year (or in five years, or ten years, ...).

So why don't governments save money? Why not tax a little more than you need, invest, and make money, rather than tax less, take out a loan, and lose money (which you will need to replace by taxing more in the future)?

Because elected officials want to lower taxes now, and do not care about the future. I would rather that my taxes went up by 5% today, the government stopped taking out loans, paid off their debt, and was able to drop taxes by 10% in ten years, than have my taxes increase by 1% each year out to the foreseeable future.

So here's my advice to government officials: Once we get out of the current recession, STOP TAKING LOANS! Pay off all of the debt, save some money, and lower taxes as the interest payments start going the OTHER way. It'll be a good model for your citizens, too.

Thursday, June 18, 2009

Bad Burgers

Recently, my company had its annual picnic. It's probably about like any other company picnic, really: A couple guys bring their grills, a couple others bring food, and the vast majority of people donate $5 so they don't need to think about it and can just eat.

Well, this year, only two grills showed up. And this is using the term 'grill' pretty loosely. Each was a tiny little propane-powered vertical vertical heater. Now, I don't have a problem with propane. My grill is propane-fired. But in this case, I realized that other people do have a problem with propane: One of the tanks was empty.

With charcoal, you can tell how much you have. You can't run out without noticing. But with propane... Well, the tank stays the same shape and size no matter how full it is. So if you don't really notice that it doesn't slosh when you pick it up, you might still think it's full.

So back to the story: There's one small grill roaring away, and one that has a tiny little flickering flame in one corner. The two chefs decide to use the grill that has fuel for cooking, and the grill with the pathetic little candle-flame for keeping food warm until it is claimed.

Then the CEO shows up and claims the dead grill.

Don't get me wrong, I really like our CEO. He's an amazingly nice, honest, and straight-to-the-point guy. Plus, he's the new replacement for the bad-tempered owner of the company who used to be the CEO. Think Steve Jobs from Pirates of Silicon Valley, but not so successful and without the great ideas to back up the temper.

So what does this mean? Well, at first it means that he stands there in his apron and laughs about the tiny flame. He flips the burgers that are on the grill a few times, and all is well. Then those burgers get claimed. And he throws a fresh, barely-thawed patty on.

What do you think would happen at this point? If I was cooking, the lid would go down for at least five minutes, then I'd open it and see if any cooking happened at all. Does the CEO do that? Nope. He watches the burger like a hawk for about thirty seconds, and then he mashes it with the spatula, hoping for the sizzle that you get when you squeeze a mostly-done burger on a hot grill. Instead, the patty spreads out a bit and just sits there, silently.

So he waits a bit longer, and mashes it again! It's still raw, there's no juice available to squeeze! The patty takes the abuse silently, spreads out a bit more, and starts to sink though the grate.

You'd think that at this point he'd stop mashing it and let it be. A couple of guys in line for food even mention this, pointing out how it is starting to fall through the grate. In response, he laughs and mashes it yet again!

The poor patty has completely lost its chance of ever being edible at this point, and there's nothing else on this dying grill, so the three guys in line (myself included) move to the line at the other grill, hoping to never see such burger torture ever again.

But unfortunately, the two "chefs" at this grill don't know a thing about cooking either. There is one done burger and three almost-done burgers, along with about six raw patties, two kielbasy, and some chicken breasts. One spatula-jockey just stares at everything, looking confused, while the other mashes the three mostly- or completely-done burgers mercilessly, and occasionally flips one over.

I don't know how many of you out there know how to cook a burger, but unless you're at a fast-food joint where the goal is to serve a tasteless puck of meat, you do not, under any circumstances, mash the burger. Not during cooking, not during formation, NEVER! It may make a cool sound when you squeeze all of the juices and flavor out of it on a hot grill, but that's exactly what you're doing: You're turning what had the potential to be a flavorful, juicy burger into a bland, tough, puck of meat.

So finally the guy in front of me points to the burger that's obviously done and conveys this concept of doneness to the guy manning the grill. The burger is immediately served to him, and he walks away. I continue to stare at the next burger, which by this point is completely done, as the anti-chef flips it over and over, as if expecting it to yell out, "I'm DONE!" when it is ready.

So I say, "That one's done," get my burger, and walk away.

Next year I'm bringing my grill, with the stipulation that I will be the chef on it. And I will spatula-block anybody who tries to squeeze my burgers!

Sunday, May 3, 2009

Basic Electronics

For People Who Don't Get It

From Someone Who Barely Does

Electronics are difficult to understand, especially for a beginner. One of the most difficult parts to understand is the difference between voltage and current, so I'm going to do my best to describe basic electronics using a simple analogy that I've found works well. I barely understand how electronics work, so hopefully my explanations will be clearer than those by professionals, who can make things complicated just by trying to be perfect.

So on to the analogy that I use: water. To start out, I will explain voltage, current, and resistance using the water analogy.

Water

Imagine a squeeze bottle full of water (contact lens solution is a great example, but really any squeeze bottle will work). As you squeeze, you are applying pressure, which forces more water through the nozzle. The pressure that you are applying is like voltage; increasing the pressure is the same as applying more voltage, perhaps by adding more batteries to a circuit. The nozzle is a like resistor, keeping you from emptying the entire bottle at once. Finally, the flow of water coming out is like current.

The squeeze bottle also gives you a simple way of thinking about the relationship between pressure, flow, and nozzle size (voltage, current, and resistance, respectively). As you increase pressure, with a constant nozzle size, you increase the flow through it. Similarly, with an electronic circuit, if you add more voltage (perhaps by using more or larger batteries), but the resistance remains the same, then the current through the circuit will increase. Current is what can destroy electronics, usually by generating heat as it passes, which is why you should never apply extra voltage to a circuit: It will force more current through the constant resistance of the circuit, burning out the components in it.

That wasn't so tough, was it? So let's move on to a few other electronic components that you may have wondered about:

Capacitors

Imagine that you have a rubber bladder blocking off a water pipe. It can stretch, but only so far before it simply stays put (assuming that you don't apply so much pressure that it blows out). This could be useful if you have an uneven source of water pressure; place one of these near the source (but not in the direct line of flow, just off to the side) and it will absorb some of the pressure whet it is high, and then apply some pressure (until it slackens) when the pressure source is too low.

Similarly in electronics, a capacitor helps to even out the sine-wave shape of AC (Alternating Current, like a wall outlet) into something flat. This is especially useful in combination with diodes (covered next), which can help convert AC into DC (Direct Current, what most electronic circuits run on).

Diodes

A diode is like a valve. A valve only lets water pass in one direction, but not in the other. If you've never seen one, it may look like a hole in a metal sheet. A rubber gasket sits on one side, and beyond the gasket is a circle of metal that is free to move against or away from the metal plate. As water tries to flow from the side away from the metal circle, the circle moves away from the plate and allows the water to flow through the opening around it. If water tries to flow the other way, though, it pushes the circle against the gasket on the plate and prevents any flow from happening.

Diodes allow electricity to only flow in one direction, blocking it when it tries to flow the other way. They are the main component of a circuit called a 'Rectifier,' which converts AC into DC. What you do is take four diodes, and you arrange them into a diamond shape, all pointing upwards. Your AC source connects to the two sides of the diamond (at the intersection of the diodes pointing up from the bottom with the diodes pointing up to the top), and your DC circuit connects at the bottom and top of the diamond, at the intersections where the diodes both point away or towards the intersection. Instead of the sine-wave of AC, you'll have what would look like the absolute value of a sine-wave. You can flatten this into almost a straight line using capacitors.

Inductors

An inductor is like a propeller sitting in a water pipe. As the water flows, it resists for a little while as it builds up speed, but once it is running at the same speed as the water, there is no resistance. Then, when you turn off the pressure, it continues to try to turn, pushing more water until it has stopped.

In an electronic circuit, an inductor resists changes in the flow of electricity, just like that propeller.

Transistors

These are the most complex pieces of an electronic circuit, and not only because they connect to three wires instead of the usual two. But I'll try to describe an analogous system using water.

Imagine a straight pipe, allowing normal water flow. Now, add to this a spring-loaded valve. But, instead of the direction of the flow affecting whether the valve is open or shut (or somewhere in between), there is a separate pipe. This separate pipe has only a pressure plate that pushes on the valve, but it does not actually lead anywhere. As the water pressure increases on the pressure plate, it pushes the valve open more; as the water pressure decreases, the spring mechanism pulls the valve shut.

A transistor works like this. The level of voltage applied to the Base terminal allows or restricts flow through the other two terminals.

To complicate matters, however, there are two main types of transistors: NPN and PNP. NPN works like you might expect, with a higher applied voltage on the base allowing more current to flow through the other terminals. PNP works the opposite way: Lowering the voltage on the base allows more current to flow.

Standard Uses

So what's all this stuff good for, anyway? Here's a basic list of applications:

  • Capacitors:
    • Smoothing out a rough flow
    • Storing energy to be released later (like a fast battery)
  • Diodes:
    • Rectifiers -- making AC into DC
    • Protection -- if you worry that someone using your circuit may plug it in backwards, this could keep more sensitive components from blowing up
  • Inductors:
    • Not really sure... I'm guessing that these could also be used to smooth out a rough flow, since they resist change
  • Transistors:
    • Amplifiers -- Apply the signal you want to amplify to the base, and apply a power source to the input terminal (the collector, if you are using conventional current (positive flows towards negative)). The output would be controlled by the signal on the base.
    • Digital switching -- the flow through the collector/emitter is very sensitive to the voltage applied to the base, so you can basically turn the flow on or off. This is how digital logic in computers and other electronics works.

I hope this was informative. If I got anything wrong, please leave a comment, but remember, I'm not trying to be perfectly correct here; that's what has made most basic electronics textbooks too difficult to understand. I'm trying to be accurate enough that people can understand, and perhaps have a better idea of what is going on if they try to learn more later.

Main source of inspiration: "Electricity" Misconceptions Spread By Textbooks, by William J. Beaty.

Tuesday, April 21, 2009

Termios

So it's apparently been quite a while since my last update... So why the long break? Well... mostly because I forgot to write. Oops.

Here's a quick summary of what's happened since my last update:

  • Took a weeklong vacation to Amsterdam, Holland, with my wife. A very fun adventure, although there were some difficulties coming home. A couple couple of birthday gifts were in the carry-on instead of the checked bag, and were confiscated because they weren't allowed on the plane.
  • Finally finished stripping wallpaper and painting another room in the house. Only a bit more wallpaper left to strip, and a few more rooms to paint, and it'll be time to move on to more expensive projects.
  • My wife has decreed that we are getting a dog. Next week. She had an attack of Florence Nightingale Syndrome at the veterinary office where she works. An American Bulldog in really bad shape came in after being rescued by the local animal shelter. She decided that when he gets better, we're taking him. I met him a couple weeks later, and still am not as excited as her, but I have been wanting a dog, so hopefully everything goes well.
  • And finally, to the topic of this post: Termios.

Occasionally over the last few months, I have tried to figure out how programs like Vim and libraries like Curses and Readline are able to read and act upon individual characters from the terminal without waiting for the user to hit Enter. I tried internet searches, and even browsing the source of the NCurses library, but didn't have any luck.

Then, last night, curiosity struck again, so I made some more internet searches, and I finally hit upon the answer: Termios. If you open the man page for 'termios,' it will describe a large number of options, many of which deal with serial port settings. (In fact, I dealt with it a couple of years ago while writing a JNI serial port interface, but forgot about it shortly afterward.)

If, however, you skip the serial port settings, you will find an option under the 'c_lflag' constants named ICANON. The description is brief: "Enable canonical mode (described below)."

If you are intrigued enough to read below, you will discover that canonical mode is what determines whether characters are available immediately or queued until an end-of-line is encountered. This is exactly the option I was looking for!

So I had finally found my answer. Of course, at this point, I had completely forgotten what I really wanted to do with it when I first started searching. But I still had to play with it, so I wrote this program. It will read input in groups of up to 15 characters, outputting the groups of character codes. (Some keypresses generate multiple character codes, such as the arrow keys or the Delete key.) Press Escape to exit (that character code, 27, will be printed as well).

Wednesday, February 11, 2009

First Brew Retrospective

For those following this blog, you know that I brewed my first batch of beer a couple months ago. The process was lots of fun, and it was very interesting to see how everything works together to produce beer.

So how did it come out? Bitter. Too bitter. I actually don't particularly like it. (Luckily, my father in law loves it, which is encouraging.) But why is it bitter?

The Water

Initially, I blamed the water. I was using unfiltered tap water, and I know that our tap water is heavy in iron. Plus, from everything I'd read, tap water may have residual chlorine in it that can lead to off flavors and hurt the yeast.

But no, that can't be it... The water, despite being iron-rich, still tastes fine. And, the thing that nobody mentions about chlorinated water is that boiling it drives off the chlorine. The only time that residual chlorine matters is if you only do a partial boil and top off with tap water. (Which you should never do, since tap water may have microbes in it that can infect your beer; you should boil your top-off water or use bottled water.)

DMS (Dimethyl Sulfides)

Well, maybe it was DMS. DMS is produced by boiling the wort (raw beer, before adding the yeast), and is carried away by steam when you've got a good rolling boil. I didn't have a good rolling boil, so maybe there was some DMS still in solution.

But no, that couldn't be it either. DMS produces a flavor more like cooked vegetables, not bitterness.

Hot Sparge

Sparge water! It must have been the sparge water! You shouldn't sparge your grains (rinse them to extract more flavor) with water above 170*F. I used 170* sparge water, perhaps my thermometer is off a bit and it was even hotter! That could rinse tannins into the wort!

Hmm... no, that couldn't be it... Tannins lend astringency and dryness, but not this bitterness I was getting. Besides, a couple of degrees wouldn't have killed the whole batch.

Frustration

So what could it be? I was pretty frustrated at this point; what other mistake could I have made?

Now for a side-track to see how I figured it out. Two weeks ago, I was brewing another beer (this time a Brown Ale using the Cumbrian Double Brown Ale kit from Northern Brewer). While reading over the instructions, I noticed a bit describing the changes to the recipe that you should make if you are doing a full boil. (A full boil means that you are boiling the entire volume of wort that you are going to ferment, as opposed to a partial boil, where you only boil a portion of highly-concentrated wort and water it down to achieve the proper volume just before adding the yeast.)

Hops!

One of these changes was to reduce your bittering hops by 25-30%. Bitterness is more efficiently extracted from hops in a larger volume of water!

The Scotch Ale that I made was also a partial boil recipe where I had done a full boil... So I should have used 25% less bittering hops.

But wait... Isn't there something else that affects hop extraction? Oh yes! Late extract addition also increases bittering efficiency! (Late extract addition means that the malt extract is added close to the end of the boil; it is generally thought to improve the beer by not caramelizing the malt extract from a long boil, and also by better utilizing the hops. The other, more common method, is early extract addition.) I used the late extract addition method, so I got more bitterness out of my hops than I really needed. So I should have reduced my bittering hops by another 25%!

So, why was my Scotch Ale so bitter? I used almost twice as many hops as I needed!

"But wait!" you cry out, "25% + 25% = 50%! That is half!" Ahh yes, young padawan, that is true. However, I would have wanted to reduce my initial hop addition by 25%, and then reduce the remainder by 25%. So, with 1oz Chinook hops, that's 1oz * 75% = .75oz after adjusting for boil volume, and then .75oz * 75% = .5625oz, or just slightly more than half of what the recipe called for.

Oh, and there's one more source of bitterness: yeast. If you don't let your bottle-conditioned homebrew sit upright in the fridge for at least 48 hours, there are is so much yeast in solution that it will take on a bitter, moldy character which is really not very good. The last one I drank sat in the fridge for four days, and was quite good (except for the unnaturally high hop bitterness).

So, in the words of Charlie Papazian, "Relax. Don't worry. Have a homebrew." And don't overdo the hops.

Monday, January 26, 2009

Log4j

Recently I started a new project at work. It is a server-side program suite for managing a large number of devices out in the world, with a database back-end and all sorts of goodies. I decided that it would be a good idea to use a standard logging package, instead of my standard method.

Not that my standard method is bad, it just isn't very flexible. There is a program-wide DEBUG flag set at startup which controls all debugging output of any sort, from any source. Since I'd be working with a much larger system than I'm used to, I thought it'd be nice to use a package with a few more features than 'debugging output is on' and 'debugging output is off.'

So Log4j seemed like the best choice. It's open-source (from the Apache foundation) and provides all of the neat little features that might be useful:

  1. It can selectively turn on/off logging depending on the source of the call.
  2. It can direct logging output to any of a number of different destinations.
  3. It has importance levels, so you can filter out the debugging messages from stable code but continue to see the errors/warnings from the same object.

Sounds pretty good, huh? It certainly did to me. But if that's the case, why am I writing this, you might ask? If you're a loyal reader, you may have noticed that I don't have many positive posts...

Log4j has virtually no documentation. Well... no, let me revise that: Log4j has virtually no free documentation.

There's a short introduction describing what it can do, how fast it is, ... BUT it doesn't tell you how to do it.

There's a FAQ which tells you what it can do, how fast it is, ... but again, not how to do it. It just gives you more details on the same subjects as the introduction.

There's even a Wiki! But no help there, it's aimed at developers of Log4j, not developers of programs using Log4j.

So where can I learn how to use this feature-rich, fast logging platform for Java? Well, you can buy a $20 PDF book that describes everything!

But surely there's information available online, through Google or something, right? No. Everybody just seems to know how to use it, but nobody is saying how they know. Did everybody read the source code? I don't know... I certainly don't want to learn how to use a library by reading its source, that's what documentation is for!

Oh, there are some examples on the Log4j page, and a few more spread around the web, but guess what? They're the same examples! Nobody thought that it would be a good idea to, perhaps, come up with their own to describe the other features that aren't documented on the home page. They just copied the same examples and assumed they explained everything.

So here are a few little pieces to help the lost developer trying to configure Log4j that doesn't want to buy the book or read through the source:

Other useful things you can do are use a rolling file appender (class is 'org.apache.log4j.RollingFileAppender'), which is given a maximum file size. It will output all messages to that file until it hits the max size, and then rename it and create a new one to write to. Here are the properties you should know about with it:

  • log4j.appender.RFA.File=filename.log
  • log4j.appender.RFA.MaxFileSize=1M
  • log4j.appender.RFA.MaxBackupIndex=3

This will append to filename.log until it reaches 1 megabyte, then it will rename it to filename.log.1 (renaming filename.log.1 to filename.log.2 and so on as necessary), keeping files up to filename.log.3, but no more.

You can specify multiple appenders for the root logger like this:

 log4j.rootCategory=ALL, Console, RFA

One more useful trick is to send logger output to specific places. You can do that like this:

 log4j.logger.loggerName=ALL, A2
 log4j.additivity.loggerName=false

That will cause all logger output from that specific logger to go to A2, but not to the root logger's appender.

And finally, to get Log4j working, you need to add a little bit of code to the initialization of your program. I'd suggest doing this in a static block in your main class. You just need to call

 PropertyConfigurator.configureAndWatch(
     "log4j.properties", 5000)

That will cause the properties file that you just wrote using all of the tips I gave to be loaded at startup, and it will be checked for updates every 5 seconds. If it changes, the new settings will go into effect. That allows you to selectively enable logging as you notice unexpected behavior, but not be overwhelmed by log messages all the time.

So what's my conclusion? I know, I jumped from complaining about Log4j to showing how it works and what you can do with it. I actually kind of like the system. It is convenient to work with (except when you need to configure something), it monitors the configuration file so I can enable logging after the program starts, and I can supress messages for parts of the program I've already finished debugging.

So I'd say that you should give it a try, and hopefully someone will help you out when you can't find the right option for your current situation. And if nothing else works, read the source. It's painful, but that's how I figured out the additivity options.