John Lindal’s Blog
The Long Road to Hell
November 29, 2009 on 8:15 pm | In Programming | No CommentsA friend just introduced me to Variadic Templates in C++0x. When will they admit that they just plain started from the wrong place? Their example of how to print a comma-separated list of arbitrary values, which is impossible in C++ without variadic templates, is so trivial in an untyped language that nobody would bother to discuss it.
Has it really never occurred to the C++ crowd that they should start with a loosely typed language and then add in an option for compile-time type checking? Objective C actually does this. A function parameter can be id, which means it accepts anything, or it can be a type, which means that it accepts that class or any subclass, or it can be a Protocol, which means that it accepts any class which implements the required methods. (Protocols also feature prominently in the new language, Go.)
The only flaws I can see in Objective C are (1) id only accepts objects, not primitives, and (2) constructors return id, so there is no type checking when an object is created. Java fixes the former this via autoboxing, but Java doesn’t have the concept of a Protocol and reflection is very painful.
One could argue that another flaw in Objective C is the lack of private functions. However, this can be solved the same was as in JavaScript: static functions, which are accessible only to other functions declared in the same source file.
Eee PC Back Online
November 12, 2009 on 9:16 pm | In Computers | No CommentsI recently made the grave mistake of upgrading my Eeebuntu installation to the latest Ubuntu, after which support for my external monitor vanished. Ouch! I finally have it working again, after starting from a clean install off the Eeebuntu CD. This time around, it went a bit faster, since I knew that networking would not work without a kernel upgrade, so I downloaded it before the re-install. Thank goodness I put /home on a separate partition!
Vacation in Buffalo
October 28, 2009 on 3:53 pm | In Travel | No CommentsThe US Airways planes seem old, but their pilots really know how to land. On our flight from LAX to Charlotte, the landing was so smooth that I didn’t even realize we were on the ground until they reversed the engines to slow down. Later, when we landed in Buffalo, there was turbulence at ground level, but the pilot still set us down smoothly. The service on both flights was also great. Just remember to bring your own food
In Buffalo, we visited SUNY Buffalo, my wife’s alma mater. We also ate Buffalo wings at Duffs, a local place that knows how to do it right. The fall colors were beautiful.
Next, we drove to Niagara Falls. I wasn’t impressed by the Behind the Falls
tour, but the view from our room in the Marriott was fantastic. The dinner at the Mandarin, a chinese buffet a few miles off the strip, was even better. If you want a good breakfast, eat at Falls Manor. They also have a motel, if you don’t want to pay for a hotel on the strip. In fact, the service at the Marriott in Niagara was terrible. This was quite surprising since the service at the Marriott in Buffalo was terrific.
Our final destination was a visit to a couple in Ontario (CA) whom my wife knew from long before she met me. It was great to finally meet them! The husband is a retired pastor. He encouraged me to continue digging into Evolutionary Theism.
We drove back to Buffalo through a persistent drizzle. It was a nice reminder that I only want to visit
Fortune Cookies
October 18, 2009 on 6:43 pm | In Miscellaneous | No CommentsI’ve finally found a fortune cookie with a prediction that I don’t want to come true:
Your most memorable dream will come true.
My most memorable dreams turn into stories, and while I enjoy writing them, I certainly wouldn’t want them to happen!
Encapsulation & Polymorphism
October 17, 2009 on 11:03 am | In Programming | No CommentsMost programmers nowadays are educated with Java. As Joel discusses, this eliminates the need to teach many difficult topics such as pointers. Two topics that Joel does not dig into are encapsulation and polymorphism.
Encapsulation is the fundamental concept of Object Oriented Programming (OOP). In C++ and Java, this translates to objects encapsulating tightly coupled data and functions. However, since all messages between objects are function calls, it is very easy for tight coupling to creep in. This is why some people campaign against getters and setters: overuse can lead to tightly coupled data and functions residing in separate objects.
In addition, C++ and Java force concepts to be mixed together because polymorphism is implemented primarily via inheritance. Polymorphism boils down to different behavior in response to the same message. Coupling this with inheritance, which is merely one way to re-use code, requires programmers to either get stuck in a straight jacket of single inheritance or go hog wild with mix-in interfaces. The latter is better, but it can still get very confusing.
To see encapsulation and polymorphism clearly, it helps to consider other implementations.
The purest forms of encapsulation occur when the boundaries are strongest: interacting processes on separate machines, e.g., web services or XHR/JSONRequest. In this context, interaction requires network traffic, and efficient systems strive to minimize this. Interestingly, this does not lead to minimal interfaces such as what you find in C libraries where each function does one small, simple operation. Instead, each interaction accomplishes as much as possible to minimize the network traffic. Getters and setters are non-existent, except in special cases when that is all that is required.
The purest forms of polymorphism occur in weakly typed languages. Objective C allows any message to be sent to any object. If the object does not implement the message, it can either delegate it to another object or throw a run-time error. The same is true for JavaScript, Perl, Python, etc., though language support for delegation varies.
How can we apply this to C++ or Java? Since encapsulation is weakly enforced by the language, it helps to encapsulate functionality, i.e., modules, instead of individual classes. Modules are much less likely to need getters or setters. Instead, they normally expose higher level actions. Messaging frameworks similiar to Objective C can been built in strongly typed languages, e.g., signals-and-slots or JBroadcaster, but it typically requires manually writing or automatically generating dispatching and receiving code.
Along with questions about pointers and functional programming, digging into a person’s understanding of encapsulation and polymorphism makes for a good technical interview.
C++
October 17, 2009 on 10:05 am | In Programming | No CommentsLots of smart people hate C++, but I cannot get myself to agree. It’s true that C++ is vastly more complex than C, but this complexity can be managed. When managed properly, the additional features of C++ are very powerful extensions to C. Because one must be careful, many people seem to feel that Java is much easier, but that is really a people problem. Bad programmers can write spaghetti in any language.
Let’s examine the alternatives to C++. Java is the closest one. I believe it has everything C++ has. It also has garbage collection. This beats manual memory management hands down. Garbage collection enables usable exceptions. Java generics are definitely easier to use than C++ templates, but I feel that this is mostly due to the system linker still being in the stone age — though there is virtue in simplicity. Java also has reflection, which is way beyond the primitive RTTI support in C++: dynamic_cast and type_info. But the reflection API is bulky and very tricky to use with generics. In a strongly typed language, it is best to avoid the need for run-time type information and rely instead on polymorphism. Also, Java comes with a huge run time, the JVM, and this does not run lean, though the need to package it with your program has thankfully disappeared on many platforms.
Another class of alternatives is weakly typed languages such as JavaScript, Objective C, Perl, and Python. These are wonderful for smaller programs. Their expressiveness is much higher than C++, so the source code is much shorter. Since they are weakly typed, polymorphism does not rely on inheritance. You can invoke any method on any object. The cost of this flexibility is that everything is a run-time error. There is no way to know whether or not a call will succeed. In the worst case, it depends on the state of the program when the call is made, so you must have integration tests (not units tests) that exercise every conceivable code path. As with C++, this complexity can and must be managed, but unlike the static complexity of the C++ language, this complexity is dynamic, so it grows exponentially with the size of the program.
Given the alternatives, I think that C++ still has a place, just as C does. When C++ does make sense, it is always worth asking whether Java is a better choice, but I do not think the answer is always yes.
Update (10/18/2009): After reading this post and remembering some of the features
of C++ that I suppressed after reading Meyers’ Effective C++, I have to admit there is much to hate about C++. The only explanation for why it doesn’t bother me day-to-day is that I am using a very carefully selected subset of the language. I’m certainly using more than 20%, but all of what I use, I use in moderation.
YUI 3
October 13, 2009 on 8:10 am | In Programming | No CommentsI finally got a chance to study the API for YUI 3, and it is beautiful. I can’t wait for them to port the widgets from YUI 2!
10/GUI
October 12, 2009 on 8:24 pm | In Computers | No Comments10/GUI is an amazing re-imagining of how we could interact with computers. They have designed a really clever way in which we can do basic desktop interactions with all ten fingers. After you watch the video, I would be surprised if you didn’t wish your current laptop touchpad were much bigger! Of course, for this to really go anywhere, application developers will need to figure out how to take full advantage of this, too. I suspect that will take some time…
Faith
October 12, 2009 on 8:59 am | In Deep Thoughts, Faith | No CommentsI’ve been struggling with my belief in God for the last several weeks, ever since finding Douglas Adams’ speech at Digital Biota 2. Basically, he claimed that God is a fiction that makes it easier for us to get on with our lives. If you have ever witnessed the dramatic personality changes caused by psychoactive drugs, e.g., antidepressants like Prozac, you will know how much of what a person considers to be me
is actually governed by brain chemistry, not something supernatural like a soul.
Adams also brought up the issue that after we evolved to survive on this planet, we fell into the trap of thinking that the environment was designed to fit us, instead of the other way around. This was reinforced when I discovered this blog post about the purposelessness of evolution. This isn’t news to me. Intelligent Design is nonsense. Nature is neither efficient nor kind — though I can’t call it cruel either, since that would anthropomorphize it by implying that it had a concept of morality. It’s safest to say that Nature is blindly hostile. Life is an endless struggle against entropy.
If the existence of carnivores isn’t sufficient, consider the arms race in a species of beetles recently reported in Science News: a male essentially uses a spiked club to fertilize females and tear them up so they cannot mate again. This gives his genes a higher probability of continuing to the next generation. Under these conditions, females with extra tough armor have a survival advantage: they are less likely to die from coitus. When females get tougher, then only extra spiky males have an advantage. This never-ending escalation is clearly horribly painful and very inefficient!
This is tough for me, because as a scientist, I can’t simply accept the Bible on faith. Anybody can write a book and build a religion around it — just look at Scientology. So can I believe in God when there is such overwhelming evidence for the Big Bang and blind evolution? (Claiming that God is the creator of everything just pushes the question back a step. Where did He come from?) Can I believe in a loving God when nature is hostile? Can I believe in the existence of God when brain chemistry is the simplest explanation for religious experiences?
While driving home from work a couple of days ago, it struck me that even if God doesn’t exist, the fundamental advice in the New Testament is still valid. (Our pastor’s sermon on Sunday solidified this.) The human experience clearly indicates that we face a paradox: what we instinctively want is not what is really good for us.
- Self-reliance isn’t psychologically healthy. We need deep, stable relationships.
- Money, power, fame are hollow. They are just as addictive and destructive as heroin. Instead of fulfilling you, they leave you hungry for more. We need food, shelter, and recognition in moderation.
- Sex for gratification is just as hollow as money, power, and fame, and it spreads diseases. Faithfulness within a healthy marriage deepens the bond by building trust.
- Revenge may succeed, but it is guaranteed to destroy us. We need to forgive others, for our own sanity. We also need to be forgiven, or at least forgive ourselves, since guilt is crippling, too.
The Bible’s shortest summary of this is two rules (Luke 10:27):
- Love the Lord your God with all your heart and with all your soul and with all your strength and with all your mind.
Translation: Don’t worship money, power, fame, sex, self-reliance, revenge, or any of your other instincts. They are all traps. Instead, focus on love, mercy, justice, etc. - Love your neighbor as yourself.
Translation: Treat others as you wish to be treated. Don’t murder, steal, lie, abuse, insult, etc. You don’t like it, and neither does anybody else. Instead, help others when they are in trouble, just as you would like to be helped when you have problems.
The longer summary is the Ten Commandments. Using the Talmudic method of counting, the first four are consequences of #1 above, and the rest are consequences of #2.
This, of course, doesn’t prove that God exists. It could be that some really smart psychologists wrote the Bible to tell us what we ought to do — essentially what Adams claimed — and that the Good News about forgiveness of sin was invented because (1) it’s obvious how bad we are at doing what we ought to do and (2) as noted above, we need forgiveness to avoid being crippled by guilt. But if He does exist, then it’s clear that He’s for us, not against us.
So I can’t prove that God exists. But neither can I prove that God does not exist. Thus, if I spend all my time wondering whether or not He exists, I’m wasting my life grappling with an axiom. This is analogous to parallel lines in geometry. If you decide they never cross, you get Euclidean geometry. If you prefer that they can cross, you get some variant of non-Euclidean geometry.
If I choose to believe in a loving God, how does this fit with blind evolution and hostile nature? Evidence suggesting the existence of free will is mounting. Conway and Koshen recently proved the Free Will Theorem, which states that if people are free to make choices, then elementary particles also make choices that do not depend on anything in the past. Again, it is impossible to prove, but Conway strongly believes that we do have free will, which would imply that the universe is fundamentally unconstrained. In this situation, a loving God couldn’t do better than help us go in the right direction. Otherwise, we would end up as enslaved puppets.
Of course, free will might not exist, in which case we are puppets. Though it may sound paradoxical, I choose not to believe this.
Moreover, I choose to believe that God does exist. Why? As discussed above, the advice attributed to Him in the New Testament is sound, and He offers to forgive and lift the burden of guilt for those who honestly want to change. I want Him to exist. The alternative is terrifying.
Working Out
October 10, 2009 on 12:05 pm | In House | No CommentsAfter four years, I have finally moved my Linex workout tower inside. When we first moved in, the back living room was too chaotic, so I assembled the tower in the back yard and covered it with a tarp. Last weekend, the second tarp was finally worn out by sun exposure. My wife suggested selling the tower, since I wasn’t using it, so I hosed it down to clean off the dust and ash from the recent Station fire in the local mountains. Then, during the week, she decided it could fit in the back living room — now that all our books are finally all on the bookshelves instead of in boxes all over the floor — so I wiped it down this morning, took it apart, moved it inside, and put it back together again.
My lower back blew out in the Spring, and once I recovered, I started working out to reduce the chances of a recurrence. Now I can add pullups, dips, and lower-back-supported leg lifts to my regimen!
Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds.
Valid XHTML and CSS. ^Top^