Saturday, December 31, 2022

Looking Back and Looking Forward in CS Education 2023

Traditionally I write a year end look back on thee previous yest in CS education. (Last year at Looking Back on Computer Science Education in 2021) Honestly, that post would largely work for 2022 as well. I attended SIGCSE, CSTA, and the New England Regional CSTA conference. They were all great. There was good learning at all of them. But new stuff? Not a whole lot. A few new tools. Some new robots. Some new AI and cyber security curriculum. But really not a whole lot.

I think we’re in for some disruption in 2023 though. Tools like ChatGPT and GitHub CoPilot are probably only the first of tools that are going to shake things up in teaching programming. Are we even going to still teach programming in computer science? If not, what will computer science courses look like? If we are still teaching programming how will we do it? What will it be all about?

We’re still going to see a need for teaching about cybersecurity for sure. Artificial Intelligence is also going to be more important. We’re seriously going to have to think about how we teach about it. We have to include not just how it works but how it should be used. Ethics in computer science has never been more important.

The discussion about ChatGPT and what it means for education in general and CS education in particular is going to be ongoing. We have to reink how and what we teach. It’s going to be an interesting year. Have you been thinking about it? What are your thoughts so far?

Note: I highly recommend Mike Zamansky's blog post at Kicking off 2023

Monday, December 19, 2022

Thoughts on Optimization of Code

Donald E. Knuth famously said "Premature optimization is the root of all evil." The important word there is “premature.” Optimization is not a bad thing. It isn’t always required though.  The joke is that a programmer will spend hours coding a solution to a problem that can be solved in minutes manually. Is that a bad thing? It really depends on the circumstances. If one is getting paid by the hour one could easily argue that it is wrong. On the other hand if time is not really the issue and writing the code is fun – why not?

There have been times in my career when optimization has been very important. I helped spec a computer for high speed/bandwidth data collection. It was critical that the computer be able to take the data and place it in memory without losing data. We actually analyzed the speed of various machine language instructions for optimization purposes. That’s pretty exceptional of course.

I also remember writing a program of my own that took what seemed like a long time to process some data. I thought about trying to optimize it but as often as the program was to run it would not have been a good use of my time. Before I ran it again I got a new computer which made a bigger difference in execution time than I would probably have been able to get though “fixing” the code.

Old habits from the days of slower computers stick with me though and I tend to think a lot about code execution. Recently I had ChatGPT write some code for me (Coding with ChatGPT–Armstrong Numbers) I looked at the code very closely since I was trying to come to some conclusions about how good the code was. I noticed that the C# version it gave me was highly dependent on modulus and division which are somewhat time consuming compared to addition and multiplication. The program I write didn’t use any modulus or division but a lot of multiplication and addition. Both used the same number of raising to a power of 3 so it doesn’t play an important role in performance differences.

Having some time on my hands, I added some timing code to see if there was a difference. My program turns on to be much faster. Yea me. Of course both execution times are very small fractions of a second. Does it make sense to optimize this code? Probably not in this case. In fact, I would argue that the most understandable code may be a better goal for this project. That is a very subjective goal. Different people might come to honest but different conclusions.

Another goal might be scalability – the ease of adapting the program for other Narcissistic numbers. The ChatGPT probably wins there.

SO much depends on what we want to optimize:

  • Speed of execution
  • Ease of understanding
  • Ease of enhancement/modification
  • Possible other considerations(?)

Getting all three may not always be possible. It’s an interesting question though and one developers do need to ask.

Saturday, December 17, 2022

Coding with ChatGPT–Armstrong Numbers

Trying out ChatGPT seems to be all the rage these days in certain geek circles. I may even be late to the game but I tried a few things over the last couple of days.

I started with one of my favorite little projects. Armstrong numbers are a form of narcissistic  number which is a number that is the sum of its own digits each raised to the power of the number of digits. I first wrote a program to calculated these as an undergraduate when I wrote programs for it in about a half dozen different languages.

I fed in the following prompt using C#, Visual Basic, and Python as the programming language.

Write a [programming language] program to calculate three digit numbers were the sum of the cube of each digit equals the original number.

The results for C# and VB were the same algorithm written in the specific syntax. The Python code was slightly different. You’ll probably not be surprised that the code ChatGPT showed me is very similar, if not identical, to code examples you can find with Google or Bing.

The C#/VB plan depends highly on integer division and the modulus operator to break down the number into its digits.

for (int I = 100; I < 1000; I++)
{
     int sum = 0;
     int temp = I;

    while (temp > 0)
     {
         int digit = temp % 10;
         sum += digit * digit * digit;
         temp /= 10;
     }

    if (sum == I)
     {
         Console.WriteLine(I);
     }
}

The Python sample changes the integer into a string and used that to address the individual digits.


for num in range(100, 1000):
   # Convert the number to a string so we can access each digit
   num_str = str(num)
   # Initialize a sum to store the sum of the cubes of the digits
   sum_of_cubes = 0
   # Iterate through each digit in the number
   for digit in num_str:
     # Convert the digit back to an integer and add the cube of it to the sum
     sum_of_cubes += int(digit) ** 3
   # If the sum of the cubes is equal to the original number, print it
   if sum_of_cubes == num:
     print(num)

Note that the Python example nicely includes comments. A dead giveaway that a student didn’t write it. All samples come with simple explanations of how the code works. The explanations tend not to be very deep so oral questioning of a student would probably still show if the student understood the code or not.

It looks like, for some simple, student exercises can be solved by ChatGPT. It is not much different from doing any other online search for the most part. How worried should teachers be about ChatGPT especially? I’m not sure. Cheating goes on all the time and there is a lot of sample code already out there for many common assignments. It’s going to be a bigger problem for educators who have very large numbers of students who can’t be familiar with the code their students write. Or who can’t ask them questions to make sure they understand what they turn in.

I think I would ask ChatGPT to solve some of my assignments before I assign them just so I know what sort of answers I might see.

Side note: Neither of the ways ChatGPT suggested is the way I solve this program. There are more than one ways to skin this cat.

Sunday, December 04, 2022

When Computers Write Code

For most of my career I hive been hearing that some day computers will write all the code and human programmers will no longer be needed. Or at least, not as needed as today. Are we getting close to that time – finally? And if we are what does it mean for teaching computer science?

Recently, the CS education world has been discussing GitHub Copilot.

GitHub Copilot uses the OpenAI Codex to suggest code and entire functions in real-time, right from your editor.

While some of the discussion has been about the suit against Copilot (GitHub Copilot litigation) much of the discussion has centered around what it means as a tool for cheating by students. More recently there has been some visibility to the use of ChatGPT to write answers to programming questions.

For example, this year’s Advent of Code seems to have been “invaded” by ChatGPT climbing the leaderboard by answering the problems is seconds. (Adventures With ChatGPT: Advent of Code Edition | Tabs, Not Spaces) Of, perhaps, even more concern to teachers, ChatGPT seems to be somewhat satisfying as a solution to Advanced Placement Computer Science A questions. (ChatGPT passes the 2022 APCSA free response section)

I’d be very surprised if students are not already using these tools. This brings up several questions. One is - how do teachers keep this from happening? We probably can’t. So how do we detect when it does happen? Do we use these tools ourselves to see what sort of code is generated for our assignments? Seems like yet more work for people who don’t have enough time as it is.

Another question, which students are sure to ask, is what is the purpose of students writing code that artificial intelligence can write easier and faster? If you read the article above about putting the APCS A questions through ChatGPT you’ll see that the results are not prefect. So for the time being it looks like good programmers can still write better code than the AI. How long that will last is anyone’s guess. If history is any guide, it will not last long.

I remember when optimizing compilers started generating more efficient than the world’s best assembly language programmers could write. It was painful for some and a real boon for others. It didn’t completely do away with the need for assembly language programmers but it did reduce the need.

What do we tell students who ask “what’s the point of learning to code?” My thought is that we talk about the need for human oversight of AI generated code. We need to verify that it works as we want it to work and that means we need to understand code. We’re also going to need to fine tune generated code for some time to come. Understanding code will also help write good instructions for the AI that generates code. Again, understanding how code works is important for that.

Of course, there is a lot more to computer science than just writing code. Programming languages are the language of that study. Learning assembly language still helps people understand how computers and computing works. So will learning higher level languages.

The AIs will get better. Our conversations with students will get harder. Cheating is always going to be a challenge. We live in interesting times.

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.