Get your leap years right
Thirty days hath September
April, June and November
All the rest hath thirty one
Except February alone

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!
Choosing colours
Colours of the rainbow
Graze from rage to royal
Beauty on eyes they bestow
Passions inside us they boil

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?
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

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.
The need for better, younger programmers
Lately I’m seeing a theme gradually forming. There are commercials featuring young people voicing their support in maintaining an environmentally friendly lifestyle. I also see commercials targeting young people, judging by the SMS inspired characters such as the carat ^, slashes /, and at signs @ to form pictures. Then there’s Microsoft’s free software for students. And finally, there’s the winning bid by Singapore to host the Youth Olympics 2010.
The focus on young people is growing.
My math teacher (or was it chemistry) once remarked that it’s a shame that she’s teaching a concept the way it was, at that age of learning we’re in. Because when we progress to the next education level, we would find out it was wrong, and that we’d have to learn a more correct version.
It’s like fractions in math. Say
2/3 = 4*a
I was to “bring the 3 over to the other side”, so I get
2 = 3*4*a
Later on, I learned that it’s actually multiplying both sides of the equation by 3, which gave the easier to explain conclusion of “bringing the 3 over”.
Education at a young age is important. Ideas arise, opinions form, and suddenly they’re set in stone (or at least very difficult to change). Once you’re at a certain age, learning new concepts might involve unlearning old ones. For some people, learning a new concept is not the problem. It’s the letting go of old concepts that’s painful, because that means they might be wrong before.
The world needs computers and software to make a lot of things work. New problems need to be solved, and they appear faster and more complex. Programming experience doesn’t count for much anymore. The ability to think, is.
I share Joel’s faint distaste for Java being taught in schools. Alright, fine, I hate Java. I don’t know what pricks me about it. When I first learned it, which was the version 1.1.8, it was cool. But not fun, like C. And Java kept jumping up in version numbers. But there’s only one version of C (at least it didn’t keep adding new “cool” commands).
I remember many an afternoon where I helped my fellow students figure out what went wrong in a segmentation fault. It’s the worse output error one could ever get, because there’s nothing to give you any clue whatsoever about why the program failed. It was a test of logical skills, intelligent omission and creative printf additions.
But if young programmers don’t get these kinds of training, what will happen to our future programs? They are the best at coming up with innovative and creative solutions, because they don’t have past baggage. Yet if they get a lousy programming education, they won’t be able to come up with those solutions in the first place.
Dream In Code does a great job at code guidance. A student (or any person with a programming question) can ask for help, but code must be provided. Gentle guidance is gladly given. Sample solutions are staunchly stemmed. The questioner is encouraged to think.
The betterment of our future doesn’t just lie with us, the so called established and experienced programmers. Our younger generations of programmers matter too, probably more so.
Essays, washrooms and variables
I seem to be writing a lot more these days. As in longer in length. Such as the article on functional specs, the unhealthy focus on flaws, or the fear of authentication.
When I was younger, I hated writing stuff. I’m fairly fluent in English. I just hated the composition part. My mind was better at grasping at concepts, and using them in spurts. Making me write essays with a minimum length or number of words was torture. Geography and history were the bane of my studious years…
Mathematics suited me. Couple the conciseness of mathematical expression with elegant programming code, and here I am, a programmer. I’m actually surprised that I can write so much. I will vary the length of my articles more. Sometimes, short articles work just as well to bring my point across.
Anyway, I found someone who shares my opinion about talking in the toilet. See point number 2.
Then there’s the advice on keeping variable naming simple for purposes of competitions or tests. Because
the lifespan of the code is 30 minutes, not years or decades
Contrast that with variable naming for clarity, for easy maintenance, and for the sanity of your fellow programmers.

