Friday, December 02, 2022

Adventures in Taking Code From the Internet

Facebook memories remined me that ten years ago I was thinking about writing a program that would ring bells (nautical time) on the hour and half hour. I didn't write it back then. Probably because I was busy with my day job. It got me thinking about writing one now just for the fun of it.

Now I have written a fancy clock program before. It looks something like this.

That was written in Visual Basic and I really wanted to use C#. Besides that, that program is a bit busy for something as simple as I wanted to write. The Timer and DateTime classes in the .NET Framework make writing a simple clock application very easy. I thought I would take a look on the internet to see what code samples I could find. There are plenty of them. I found several interesting looking samples that also drew analog clock faces.

The first one I found (C# Analog Clock Program (thecrazyprogrammer.com)) looked simple enough so I created a project and copied the code into it. It was broken. Looking back at the comments on the original post I found a number of comments saying the codebase broken. None of them had answers.

That seems to be pretty common. No doubt that things worked fine for the original coder but something was lost in the posting. I suspect that most people who come across this sort of thing are beginners which would explain the questions. Having a bit more experience I quickly fixed the errors I had and got the program to work. It didn’t look quite like I wanted but again, having enough knowledge to understand the code without a lot of comments, I made some adjustments and got it looking the way I waned.  I suspect that many beginners would either life with it or try a whole lot of things until they stumbled on the right combination. Or maybe broke the program beyond fixing.

That highlights one of the big issues with beginners taking code from the internet. Without some real knowledge even minor issues will keep success away from the student.

I found a second project that was put on the internet some years after the first one I found. Interestingly enough, it appeared to be a refactoring and modest improvement over the first one I found. The code was nearly identical. No credit was given to a previous coder. In fact it was so close that I copied a snippet and pasted into the earlier project. With a tiny edit (a name change) it worked perfictly. This is what I wound up with.

The Code Project web site has a variety of analog clock code samples. I took at look at one - Analog clock control in C# – CodeProject that was pretty good. and worked more or less right off the bat. Except that is was written in a much older version of Visual Studio than I was using. (2003 compared to 2022). An upgrade was required with Visual Studio handled pretty well. That is not always the case if coders used depreciated or removed features or changed names. Yet another issue with taking code from the internet.

Well, now I have a couple of code samples to play with. Most of all I am more convinced that actually getting code from the internet to work can be more complicated than many would think it is.

Now to think about ringing bells. Which reminds me of one last story,

Back when I was in college during the mini-computer era we had a lab full of ASR-33s. They had actual bells that were hit with a little hammer. One student wrote a program that ran in the background of the computer and "grabbed" control of each terminal as it became available. Then it would start ringing all the bells at once. It so happens that when it ran the only one in the lab was the computer department secretary.

The department chair held a meeting with all the TAs and it never happened again.

No comments: