Multi-personality classes

I’ve been working on my spreadsheet library and I discovered something. There are classes where I needed them to be multiple end results at the beginning of their lives. Let me illustrate.

Suppose we have an Embryo class. However, the way we use it is that we need it to survive all the way to the end of our program, and we happen to need the Testicles and Ovaries properties depending on how our program uses it. For example:

Embryo emb = new Embryo();
if (boy)
{
    emb.PrivatePart.Testicles.MakeStuff();
}
else
{
    emb.PrivatePart.Ovaries.MakeStuff();
}

The thing is, we don’t know beforehand whether we have a boy or girl until runtime. And even if we know beforehand that we have a boy or girl, we need a generic placeholder class that can represent either a boy or girl until some later time. Using the .NET object class and doing boxing/unboxing seems unproductive (no pun intended).

So what’s the actual situation? Chart axis.

The primary horizontal axis of an Excel chart can be either a category axis, date axis or value axis. Category axes are used for most cases, where the chart data’s categories are text. But if they’re dates, then date axes are used. And if they’re scatter charts, then the axis is a value axis.

Well, the primary horizontal axis is usually those 3 types. Bar charts have them at the primary vertical axis. *sigh* Don’t even get me started…

So the primary horizontal axis has to be all 3 at the same time. Well, the way I expose the property/class requires it to be all 3 at the same time.

“Why don’t you expose them individually? That would solve the ambiguity problem.”

I could do this:

SLChart chart = new SLChart("B2", "G6");
chart.PrimaryCategoryAxis.Title.Text = "A category title";
chart.PrimaryDateAxis.Title.Text = "A date title";
chart.PrimaryValueAxis.Title.Text = "A value title";

And then based on the type of axis used, I’ll use the different axis class. The thing is, I want to expose only one property instead of 3. This means programmers using my library don’t have to differentiate which axis class/property to use.

If I expose only 1 property/class, then that underlying class has to be all 3 types of axis at the same time.

There’s probably a design pattern I don’t know about. If you know it, or have comments on how to approach this, I’d love to hear it in the comments.

Now if you’re in the business of producing (hahaha… pun… never mind) Excel spreadsheets, try my library!

A research/questionnaire project

Hey you! Yeah, you. If you’ve been reading this blog for a while now, thanks! If you’re new, welcome!

In any case, I’m doing a research/questionnaire project, and I’d like your help. You just have to answer these questions:

  • What are your biggest frustrations/problems right now?
  • What do you want to learn about right now?
  • What ebooks do you read (or would like to read) on the Amazon Kindle (or other ebook reading devices)?

Your answers don’t have to be specifically related to software development. Send your answers via email or use my contact form if you’re into that kinda thing. I’m not accepting comments because I don’t want everyone to see your answers. I’ll be compiling a summary and sending it to you if you help me answer, so commenting here won’t be useful.

My email address is
vincent (at) polymathprogrammer [dot] com

Lesson learnt on the Internet: Repetition. Send your answers to me via email or use my contact form. I’ll compile a summary and send it to you.

Much appreciation and gratitude to you. Have I mentioned I’ll be compiling a summary of other people’s answers and sending it to you? Thrice I’ve said it, and thus let it be done.

How to lessen fear of public vlogging

Public vlogging is like public speaking. Except it might be worse. At least with public speaking, you’re talking to people. With public vlogging, you’re talking to a camera. I’ll let you decide which is worse…

There was a time when talking into a mobile phone was weird because you don’t look like you’re talking to anyone. Now everyone and their grandmother is on a mobile phone. There might come a time when recording videos is commonplace, whether at home or in public.

Do we still have those guys with Bluetooth earpieces?

Next level of web development

The other day I met up with a friend who just finished giving a talk on HTML5. Well, not exactly HTML5 but more on the current mix of technologies that’s making up the current web development skills. The basic technologies involved are HTML5 (markup/data), Javascript (action) and CSS (presentation).

My friend used HTML5 in his title because there doesn’t seem to be a term for this new “level” of web development. And because he’s afraid his audience won’t know what he meant.

I remember writing Java applets (using the applet tag). I remember web sites without an ending paragraph p tag because the web browser was extremely tolerant. I remember web pages with font tags everywhere because CSS was practically non-existent back then. I remember displaying the current time using Javascript was an extremely cool thing to do.

Then web standards were introduced. HTML markup standards were encouraged. Javascript libraries started sprouting. And CSS came to the rescue, separating the presentation layer from the code layer.

As far as cross-browser issues go, adhering to current/latest HTML standards and using good Javascript libraries and using CSS meant that users are free to choose whichever browser they want. And the web site they’re visiting is expected to behave the same way and be rendered on the screen the same way (with maybe a few pixels off the mark as an error buffer, I guess) regardless of the web browser chosen.

And my friend is worried.

He’s worried that the current web developers are so used to the current set of technologies that basic programming problems are not even considered.

The big one is Internet access. He told me this group of (mostly young) web developers assume the Internet is as available as air. The idea that there might not be Internet access never crossed their minds, and so their web applications crash in the most spectacular manner when the user has no wifi.

And the worst part of it all was that these web developers think HTML5 is the current “web developing thing”. Sort of like “I’m a C# programmer” or “I’m a Python programmer”. They’re now developing in HTML5, without understanding that HTML5 is just the markup.

It’s the “as long as it works” mentality. I’m a practical man, so I agree with this mentality. I also do so with some understanding of the underlying technologies. This “HTML5, Javascript, CSS” combo seems like a black box, but it isn’t. Each part even advances independently of each other.

It’s like self-publishing on the Kindle without understanding the role of “traditional” editors and publishing houses. It’s like auto-tune of your song without understanding some music basics. It’s like relying on the auto-focusing of modern “prosumer” cameras without understanding basic camera equipment and terms.

I’m not saying they’re bad. I’m guilty of the last one. I use a camera that does everything for me when I click “Record” because I don’t want to fiddle with aperture and focal length and filters and such.

The problem is, if you don’t know (or care) what’s the underlying technology, you won’t know what to look for when things go wrong.

Question

Is there a term for this current level of HTML5, Javascript (I can only think of jQuery as a popular library) and CSS (CSS2/CSS3?)?

Compression through time

So the last time, I told you I came up with my own video format so I could load videos into my game engine. Today, I’m going to tell you what it is.

The concept isn’t revolutionary. Image compression is the art of expressing an image in as small a form as possible. There are 2 versions: lossy and lossless. Lossy compression means some data is lost, meaning the file size can be smaller, but overall you won’t notice the difference (much). The jpeg format uses this. Lossless compression doesn’t lose any data at all. The png format uses a lossless compression.

I don’t know much about video compression. However, a video is a series of images. So what I did was instead of focusing on image-by-image compression, I adopted time-based compression.

For each pixel position, I try to come up with an algorithm (or specifically a maths formula) that will represent all the pixels in that pixel position throughout the entire video.

Let’s say we use the top-left pixel of the video. And let’s say that pixel is pure white for half of the video, and black for the other half of the video. My aim is to represent this “pure white 1st half, pure black 2nd half” information in 1 formula.

With this in mind, let’s say I’ve got a maths formula somehow. Let’s say it’s a 640 by 480 pixel video (it was considered good resolution back then. I know we have HD now…). Let’s say there are 3 variables in that maths formula. This means, I have to store 3 * 640 * 480 variables in my movie format.

Yes, that’s the compression tactic.

Well, more specifically, I had to come up with 3 * 640 * 480 maths formulas that compresses itself as efficiently as possible. With some research, I found that the less the variables vary (hahahah…), the better the compression. This means the colour space I use is important. RGB values change too much.

And with some more research, I found the YUV colour space ideal for use. I think what I actually did was count the number of consecutive values and store that. The YUV space has a “lightness” component, which for most videos, doesn’t change very often.

This means in the ideal case, I store “N of X” for the “lightness” component, where N is the number of times the value X occurs for the “lightness” component of that pixel position. And in the ideal case, N would be approximately the frame rate multiplied by the number of seconds.

So for each pixel position, I store a series of counts of the Y component (could be 10, 50 or ideally just 1). Then I store a series of counts of the U component. And similarly for the V component.

Then I move on to the next pixel.

The idea is that the longer the video, the “better” the compression. It’s not that the compression tactic is better. It’s just that the tactic takes advantage of the fact that certain colour components don’t change much. If a pixel is always white, it doesn’t matter if the video is 1 second long or 1 hour long, we just store “30 white” and “3600 white” respectively (assuming 30 frames per second).

This works great for screencast videos because the screen typically doesn’t change much. It’s usually just the mouse moving around and clicking stuff and sometimes the screen changes. This means most of the screen stays the same. And if you’re like me, you’ve probably watched some screencast videos where nothing on the screen happens for minutes. It’s just the person talking.

Of course, I fudged the actual values a bit so they can compress better. Meaning it’s a lossy compression. But in the end, it was comparable to a moderately compressed QuickTime movie file, so I was ok with that.

But even if the compressed size is still larger than other movie file types, at least it’s not copyrighted. So I can use that. And if you want to try this compression method, go ahead and use it (you have my permission). Let me know how it works out for you.