Thursday, February 24, 2005

Way too much data

Let's say your aggregator gets 4000 items every day. Then, on average, you cannot spend more than 21.6 seconds per item.

It's impossible to read all that, much less sample it properly. Developing tools to make that mass manageable is a solution as useful as driving ten cars to work to shorten your commute time.

Occam's razor, anyone?

Monday, February 21, 2005

ComplexConditions

Are you sometimes irritated when you have to write conditional logic using and:/or:? I was there too, and I have two things to say.

  1. For the most part, classes and subclassing will eliminate the need for such methods,
  2. But if you cannot do that, then you should consider using ComplexConditions.

The basic idea is that when you say and:/or:, what you are really doing is connecting some pieces of code to, finally, ask something about the aggregated conditions as a whole. In other words:

    [conditionA], [conditionB], [conditionC]
      ifAllTrue: [such]
      ifAnyTrue: [such]
      otherwise: [such]

Also,

    anObject>>hasInterestingProperty

      ^[self hasKillerProperty] orAllOf:
        [self hasIrrelevantProperty],
        [self hasDisreputableProperty]

... and so on. The implementation of BlockClosure>>, is left to the reader. It can be done with an aggregate collection, or with a block.

Complicated boolean code shrinks with this arrangement. I have run into some cases where code goes from >20 lines and multiple statements down to 5 lines in one statement. The message ifAllDefined:ifAnyDefined:otherwise: is also very good at summarizing verbose nil checks.

So nice --- and the code reads so much better!

Update: look at what I found... somebody wrote something called ComplexConditions in C#! Unfortunately, it does not seem to be a port of ComplexConditions for Smalltalk.

Tuesday, February 15, 2005

Asymptotically/almost perfect

I do not know about other bloggers, but the first rendition of what I write usually leaves me satisfied but not excited. So I come back to it, read it, fix it some, and so on until I am happy. It happens with my posts all the time.

Finishing the hymnbook made me remember how much time my mom and I spent fixing typos and improving wording that could have been attributed to Yoda. Since I switched to LaTeX, I had to refactor the programming of every hymn to make it look better. I hope I haven't introduced any errors - my previous TeX edition had exactly three typos!

It's impossible to achieve timeless perfection, but it's so nice when you get close to it for a moment! It's actually good that you never get there, otherwise you would stop trying and that takes out the fun.

And keeping in mind the work on the hymnbook, I believe the same is true with refactored code. I learned Smalltalk about 9 years ago. Soon after I started, I heard all the new buzzwords about refactoring. The comment at the time was that refactoring was something you did after you hacked your way through, that it was hard, kind of a non-mandatory extra... a chore.

And I remember thinking: "it may be difficult, but if I practice enough it will be automatic and I will write desirable code by default".

I am so happy I forced myself to apply all the refactorings I learned about, because now they come out naturally. With practice, it takes way less time and effort than dealing with your code's shortcomings. And it becomes so much easier to do maintenance and to change stuff!

The cool thing is that by the time you think you have mastered a skill, you will have learned something new to practice.

Done!

The hymnbook, containing 280+ hymns in about 250 A5 paper pages, is done! LaTeX makes it look perfect, too!

Sunday, February 13, 2005

The video plugin conspiracy

I have been converting some of my videos to avi+divx lately. I really don't like to need a ton of codecs and other software that either becomes proprietary, or needs proprietary programs to be read, or locks you in with DRM... grrr... so here's how to do some interesting stuff on a Windows box.

What do you do if you want to convert a QuickTime .mov file into .avi so you don't need the rather large QuickTime player on your machine? First, get QuickTime Alternative from the web, and once you install the DirectShow codecs, you can use AviSynth to read the .mov file (using the DirectShowSource function) into VirtualDub, and there you go.

What do you do if you want to convert a Windows Media file into .avi so you don't have to use Windows Media Player? Sometimes AviSynth doesn't like it one bit when you put one of those videos in the script - it reports lengths of several years and so on. You can work on that with VirtualDub, but you can also load some Windows Media files with TMPGEnc, which lets you save the whole thing as an uncompressed .avi file, which then can be easily processed with VirtualDub.

What if you don't have the right codecs for that Windows Media file? Piece of cake: load the file in Windows Media Player and let it install all the necessary codecs. There you go. For less proprietary codecs, you should consider installing FFDShow.

Side note: try to stay away from codec packs since they become a mess to manage as the codecs become outdated or if you want to uninstall. Sometimes they pull good stuff out with them. If you install the right codec individually, things are much easier.

What if your player refuses to play a file and claims it doesn't even know what codec is needed? Or what if you know you have the codec, but the video still doesn't play right? If you have an .avi file, you can use GSpot to find out what's going on. If you need software, you can look up the 4 character code GSpot gives you (fourCC), and you can usually get codecs with that. Some other times though, it's a matter of a bad or incorrectly installed codec.

What if a bad codec gets in the way? For most codecs, it's pretty simple. Just go find the .dll, .ax or whatever file the codec is registered with, and do regsvr32 /u filename. Done, no more silly codec. And if you get such codecs or want to reinstall them, then regsvr32 filename works just fine.

Some videos, especially .mpg files that contain NTSC or PAL video, have interlaced frames. You can deinterlace them and adjust the field positions using AviSynth+VirtualDub so that now even/odd lines overlap properly, and now you have the same video but with twice the framerate. The difference is huge and stuff looks so much nicer, even with less "resolution" so to speak.

In addition, .mpg players tend to blend the odd/even fields together so that you do not see zig-zags in the video because of interlacing. Deinterlacing gets you twice the framerate and no more blurring. By the way, video cameras tend to interlace the video they capture... hint-hint...

In addition, there are some ways to take each field and "complete" it with information from the next field doing some smart interpolation so that it doesn't produce zig-zagging. This creates a full resolution 50-60fps video with all the temporal and spatial information available. However, this can sometimes create a file that modern computers cannot decompress in real time :(.

If you are into collecting videos, I hope this info will be useful for you. Except divx, I think all software I used to do the conversions is free. You can replace divx with its cousin xvid. They are more or less compatible with each other.

Sunday, February 06, 2005

Busy lately...

My mom made the trip from Argentina and has been here for some weeks now. It's very nice to be with her! We're working on some projects together - the hymnbook for the church she has been playing at for over 30 years should be complete in about a week.

She has also been telling me of music I really should listen to and things I really should see. For example, Chopin's 24 preludes. I like #15 very much. By the way, it was used on Akira Kurosawa's Dreams for the van Gogh dream. We watched that dream again as soon as prelude #15 was playing. Such a nice film, such nice music, such nice paintings...

... all distilled artistic expressions of extraordinary people.

It would have been so much easier for them to avoid the hard work it takes to be extraordinary. And it would have been so normal if they had been just average like everybody else.

I often wonder if any other random unworthy event would have caused their lives to go somewhere else. What amounts of chance events were necessary for these people to achieve what they did?

I am so glad that mankind has been, at least for a little bit, able to reflect on the Universe so artfully.