29 February, 2008 | Written by Vincent 1 Comment

Get your leap years right

Thirty days hath September
April, June and November
All the rest hath thirty one
Except February alone

Snowboarder up high by Jason Lugo @ iStockphoto

You just divided the year by 4 right? If the year is divisible by 4, then it’s a leap year, isn’t it? Yes it is, if you still use 2 digit years, and that’s if 00 represents 2000 and not 1900.

[The Gregorian calendar is the basis used for the following discussion.]

Back in the pre-Y2K years, many programs were written with 2 digit representations of years. Actually, I have no idea when the first computer was built. I’m not talking about abacus, or big light bulbs, or gas chambers. There were gas chambers right? *smile* I remember hearing about humongous contraptions that required entire rooms to house them. They’re not what I’m referring to. I’m talking about the computer as we know it now, that’s about a few decades old, back in the 90′s.

Anyway, the time period when those programs were written was between 1900 and 2000. So any 2 digit year, such as 1940 represented by 40, divisible by 4 was a leap year. 2000 came and went. There were still many programs written with the 2 digit year representation. This presented a problem.

Does 04 represent 1904, or 2004? To minimise changes from a 2 digit to a 4 digit representation, a workaround was used. Any 2 digit year below a certain number was designated to be in the 21st century, otherwise it’s in the 20th century.

For example, 15 would represent 2015, whereas 63 referred to 1963. The Oracle database uses 50 as the magic number. So 2 digit years range from 1950 to 2049.

I’ve maintained legacy code which uses 80, so it could handle 1980 to 2079. Probably with the assumption that anything before 1980 was really really old and thus was no longer useful data.

The magic number was introduced to allow the divide-by-4 leap year calculation to continue working.

I implore you. Please use the proper calculation formula for leap years. And please use the 4 digit year representation.

if ((year modulo 4 is 0) and (year modulo 100 is not 0)) or (year modulo 400 is 0)
then leap
else no_leap

I’ve had occasion to write a Javascript function involving counting days in months. Which involved February. Which inevitably involved calculating leap years. So we have,

(year modulo 4 is 0) and (year modulo 100 is not 0)

which translates to

(iCurrentYear%4==0) && (iCurrentYear%100!=0)

And

year modulo 400 is 0

becomes

iCurrentYear%400==0

It’s not that hard, is it? So the final if condition becomes

if ( ((iCurrentYear%4==0) && (iCurrentYear%100!=0)) || (iCurrentYear%400==0) )

In English: Any year that’s divisible by 4 but is not a century year (I made that up) is a leap year. So 1896, 1904 are leap years but 1900 is not.

What about 2000? That’s a leap year, and it’s a century year. Aahhh… Century years are not leap years, unless they are divisible by 400. So 1600, 2000, 2400 are leap years.

“But that formula won’t kick in till 2100!” you exclaim. “I can still use the divide-by-4 method!”

You and I will probably not be around in 2100. The application will probably not even live past 2010, let alone 2100.

The point is, the change is minor, yet allows your application to live a lot longer. So why don’t you change? Your unwillingness to change (or improve)? Your laziness? What is holding you back?

P.S. The Wikipedia article on leap years indicate that in 8000 years, the above formula might fail. 8000 years is plenty time.

An alien might time-travel to 2008 and use an application I wrote. Then the alien travels back to his time, year 2099 and finds that my application fails once 2100 crosses over. I’d hate to have an angry extraterrestrial breathing down my neck…

And happy birthday to leaplings!

27 February, 2008 | Written by Vincent 3 Comments

Choosing colours

Colours of the rainbow
Graze from rage to royal
Beauty on eyes they bestow
Passions inside us they boil

What colour? by Christopher O Driscoll @ iStockphoto

We don’t have a graphic designer in the office. So it’s up to the individual developer to come up with graphics, styles and design. Well, you can imagine the look of the applications created. My colleagues and I try to assuage the situation by making sure our own applications look decent.

Without much training or good image editing software available, there’s only so much we could do. Swathes of colour, cool looking fonts and some simple images. Luckily I have Paint.NET to tide over some pixel operations.

Anyway, I have to choose colours fairly often. The colour of the text, that of the background, and that border lining. CSS does an amazing simplication for web applications. It can’t tell me what colours to use though.

I wrote about my agony over web safe colours before. This is like a continuation of sorts. So for colour selection, there’s the colour wheel and rectangular blocks of colour arranged in a grid according to red, green and blue values.

The thing with swathes of colour is that, they are continuous. Trying to decide which of two close points on the colour wheel give a better “feeling” makes my head hurt. So I have this handy colour chart to help with at least coming up with a starting colour. Please have a look before reading on.

Alright, so the colours defined by the KnownColor enumeration in .NET framework don’t form any kind of standard. You’re missing the point. I use them as a starting point, sometimes even as the colour I want. Because I arranged the chart by sorting with colour names alphabetically, the colour chart displays colours with similar hues usually away from each other.

This gives my eyes distinct patches of colour to look at. I can find that exact hue I want because of the added variable of distance. With the other surrounding colours, the ideal hue sometimes jump out at me.

The problem isn’t with designing. It’s about indecision. Whether you’re designing the look of a web page or designing the code structure is secondary. Your inability to decide, stalls the project.

I’m better with writing code. I’m learning to improve my web design skills. So I wrote a program to generate a colour chart, which helps me make faster design decisions. What did you do?

25 February, 2008 | Written by Vincent Comments Off

Solve the given problem first

Solve the given problem first
Ignore other thought tatters
Like water filling need of thirst
It’s the only thing that matters

Pondering the problem by Paul Kline @ iStockphoto

You’ve been given the software requirements. The problem is defined. The tasks are laid out. Yet half-way through coding, you start deviating. You think up cool new stuff to “enhance” the application. You’re distracted by the wonderful “features” you can add to make the application better. You’re not getting the project moving, and as such, you’ve become a bottleneck.

This isn’t about solving the actual problem, where the underlying problem is obscured by a superficial but seemingly real problem. It’s about coding against whatever is already discussed, decided and defined. It’s about doing what’s needed before doing what will be nice to have.

That rotozoom effect will be so cool!

Yes it will. Right after you finish coding the basic camera movements first. And that texture loading function. And that resource file management class.

I remember trying out as a hobbyist game programmer. I’ve played lots of role playing games. I know about sprites and 2D maps. I know about polygons and isometric maps. I understand orthogonal views and culling planes. I learned about colour mappings and Phong and Gouraud shading.

Coding the basic game code structure is incredibly boring and tedious. I’ve written “Hello World” equivalents for OpenGL and DirectX rendering, to test the basic code template. I’ve written functions to generate simple geometry objects such as spheres, cubes and pyramids. I’ve written custom import facilities to take in a 256 by 256 pixel bitmap as the game font, and render the individual letters correctly (try thinking about fixed and variable width fonts).

I was a student then, and didn’t want to shell out money to buy third party game frameworks. Besides, it’s fun to learn from the start. I just didn’t realise it involved so much work.

So I understand that sometimes, it’s easy to get sidetracked and go do something more interesting. Now older and wiser, I’ve gained the resolve and discipline to finish the basic stuff first. The fancy stuff can come after that.

I wanna do sorting as well

There was an incident where I did a quick impromptu tutoring on C for John (as I’ll call him). John was a freshman, and just started C programming as one of his courses. I was asked to help him with one of his assignments.

The assignment was a standard question where a list of students with their test scores were given (or were to be input). They were then to be given a grade, calculated by the answer program. Say 70-100 would be an A, 60-70 would be a B and so on.

The actual requirements in the assignment were simple. I helped John understand some of the compiler errors, and gave hints on how to go about coding some of the tasks. But he already had plans…

The lecturer set it such that there would be extra credit for, well, extra stuff (probably because the assignment was easy to begin with). What kind of extra stuff? “Well, you go figure it out!” the lecturer said (paraphrased). He did however dropped hints like user manuals, extra input information about students, and sorting.

John was obviously still in the process of understanding “missing semicolon” errors, and why scanf needs an ampersand before the variable name (if not scanning character strings/arrays). I was there at his house for a visit. It’s almost by coincidence that I was there. He obviously wanted me to help out as much as possible, preferably the whole assignment and the extra credit part.

So he asked me how to do sorting, for extra credits. He had difficulty understanding how to slot student scores into grade levels, which involved if-elses and “smaller than, greater than” comparisons. I’m not sure how much he could take in about sorting.

I told him to solve the basic assignment first. He wasn’t even done with all the required tasks. I advised him that all the extra credit means little if the actual assignment wasn’t even completed. I doubt his lecturer would award him full marks for that kind of work.

Beautify later

I’ve often had to resist the urge to add in extra display help, or create a better looking graphic. It could wait. I’ve already planned out the AJAX required for that snazzy display text. I’ve already decided which design elements needed polishing. But I needed to get the application working correctly first, right then.

It would be more user-friendly for the user. It would look more impressive to the customer. But it had to work first. What’s the point of it looking pretty if it doesn’t work?

Bake the cake first. You can add icing later.

Next Page →