Sunday, May 7, 2023

AI: The Future of Programming?

Like just about everyone else, I've been exploring ChatGPT to figure out if it is a curse or a blessing. All of the experts seem to agree it's both. At universities like mine, there has been quite a bit of gnashing of teeth over the worry that students will use it to cheat. This is a real concern, though of course cheating didn't start with ChatGPT. Still, it is a bit unnerving to type a simple request into ChatGPT, such as "Write a 3 paragraph essay on what were the major causes of the American Civil War" and then watch the AI engine produce a very convincing essay neatly matching your content and length requirements. 

Of course, worries about the impact of new technologies on education and society are not new. My first encounters with educators' worries about technology were with the advent of the calculator and then spellchecking. I also recall reading some historical literature claiming that Gutenberg's printing press took away our memories. Somehow, we have grown use to these cognitive aids and don't question their value anymore. Besides, I think education's attempts to either thwart or stall the use of a new technology is best characterized as a "cat and mouse" game. In the end, the mouse always wins. So, rather than look to how to stop the technology at education's door, I think a better approach is find ways to harness it to support students and other educational aims.

Education has also long been ambivalent about what Donald Norman referred to as "knowledge in the head" versus "knowledge in the world." That is, we seem to think that the purpose of education is to require students to commit to long-term memory everything they would ever need to know or do. A better approach is to look at ways to form partnerships between what one knows and what one can get from interactions with the world. Anyone who ever put their car keys on top of the grocery list to make sure they didn't forget to take it as they walked out the door in the morning understands the value of knowledge in the world. One can go to bed relaxed about not needing to remember to take the list in the morning because when you are running late the next day and reach for your car keys, the list will be there to remind you. 

LiveCode and AI

Alright, enough philosophy, let's get down to business. The purpose of this post is to share how the AI of ChatGPT has helped me to do some programming. Amazingly, ChatGPT knows all about LiveCode. A couple of weeks ago I tried it out by requesting a bunch of examples, such as "Write a program that sorts a list of the 50 states in alphabetical order using the LiveCode programming language." ChatGPT wrote out some very nice code for me to then insert into buttons and cards. However, I already knew how to do that task. I tried some trickier requests, such as "Write a stopwatch program that counts the seconds and allows the user to pause the counting at any time using the LiveCode programming language." ChatGPT again had no trouble doing so, and it even gave me a nice explanation of the various handlers it created in the code (see the appendix at the bottom of this post for the ChatGPT output). Of course, I had to know how to create the buttons and cards and also know how to put the code provided by ChatGPT into them.

So, wouldn't it be nice if there was an AI program that could be embedded into LiveCode that not only could write the code, but also create all of the various LiveCode objects with the code already comfortably nestled inside? Well, that's exactly what the LiveCode company is wanting to do by eventually offering a new programming platform called Xavvi. I recommend watching the video by Kevin Miller as he explains why and how LiveCode needs to move in this direction. He repeated a stark prediction that in about 10 years there will be two types of companies - those that have embraced AI and those that are no longer in business. It's unclear if the LiveCode company will be able to muster the resources to pull this off, but I hope they succeed. (They are running another fundraising campaign to support this move.) 

My Encounters this Weekend with ChatGPT

This past weekend, I had my own epiphany that gives support to Kevin's opinion of the need to move in the direction of using AI to support app development. Interestingly, it doesn't involve LiveCode, but PHP. I needed to do some updating of my web site that supports my Q sort project, now in it's eighth year. I built a Q sort app for the project with LiveCode, but the supporting web site uses PHP and mySQL to store instructor credentials, Q sort definitions, Q sort activities, and Q sort data. However, I've never been very conversant with PHP. When I began my foray into creating database-driven web sites, I used the built-in PHP server behaviors of Adobe Dreamweaver. (Please don't laugh. It was really wonderful.) I was able to build and maintain full CRUD (Create, Retrieve, Update, and Delete) web sites very quickly with very little need to know PHP code. Of course, I had to sometimes tweak the code or learn how to extend the code, but I could easily find some help and examples online. But I never learned PHP well enough to code in it myself from scratch and I was fine with that. My goal was to be productive and I didn't have the time to learn how to code in PHP from scratch. Unfortunately, Adobe took away all of those behaviors from Dreamweaver a few years ago, so I've been faced with coding PHP on my own. Trouble is, I only poke around in PHP every few months or even years. So, each time I've largely forgotten most of what I had learned previously. And I really needed to get those updates for my Q sort web site done now. Alas, what to do?

Well, I decided to see if ChatGPT could help, and help it did. I was able to deploy several new web pages that successfully connected to the mySQL database with code written entirely by ChatGPT. The hard part was figuring how how to appropriately prompt ChatGPT to do it. That is, what instructions did one need to give it? Here is one example of a simple request I gave ChatGPT that resulted in perfect code for a completely new web page that retrieved data from the database and displayed it in a table:

Write the HTML code for a web page with PHP from a mySQL database DBLIVECODE using mySQL from the table qsort_data with the database username NAMENAME and the password is PASSWORDPASSWORD . Put the database in a html table. 

Here's the code that ChatGPT generated, which I was able to easily copy and paste into a web document (with the private information clearly altered in all caps):

 <!DOCTYPE html>  
 <html>  
 <head>  
      <title>MySQL Table to HTML Table</title>  
 </head>  
 <body>  
      <?php  
           // Database connection information  
           $host = 'localhost'; // or IP address of the server  
           $dbname = 'DBLIVECODE'; // name of the database  
           $username = NAMENAME; // database username  
           $password = 'PASSWORDPASSWORD'; // database password  
           // Create a new MySQLi object  
           $conn = new mysqli($host, $username, $password, $dbname);  
           // Check for errors  
           if ($conn->connect_error) {  
                die("Connection failed: " . $conn->connect_error);  
           }  
           // Define the query to retrieve data from the table  
           $sql = 'SELECT * FROM qsort_data';  
           // Execute the query  
           $result = $conn->query($sql);  
           // Check for errors  
           if (!$result) {  
                die("Query failed: " . $conn->error);  
           }  
           // Display the data in an HTML table  
           echo '<table>';  
           echo '<tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr>';  
           while ($row = $result->fetch_assoc()) {  
                echo '<tr>';  
                echo '<td>' . $row['q_topic'] . '</td>';  
                echo '<td>' . $row['q_code_activity'] . '</td>';  
                echo '<td>' . $row['q_data'] . '</td>';  
                echo '</tr>';  
           }  
           echo '</table>';  
           // Close the database connection  
           $conn->close();  
      ?>  
 </body>  
 </html>  

Again, the only hard part was figuring out what exactly to tell ChatGPT to do. Obviously, some knowledge of databases, PHP, and HTML was required on my part in order to get ChatGPT to give me what I needed. But, only a little trial and error was necessary to get ChatGPT to deliver the goods. Over the weekend, I made other requests to ChatGPT, such as as for code to update specific database tables and records. 

I'll treat AI like I treat GPS

ChatGPT and I are now "partners" in web development. However, I don't want to just blindly follow what it gives me. I want to learn as I go with the hope of putting some of the knowledge gained in my head and not just in the world. I think this relationship will be similar to how I use GPS apps on my phone. I always want to know where I'm going and will never just follow a GPS blindly. I like to map out my route to get the big picture of of my trip. But when I'm in a big city (like I was in Chicago a few weeks ago), it is wonderful to have the GPS guide me moment-to-moment in taking key exits and getting in the correct lanes as I anticipate making the next turn. I just hope that ChatGPT doesn't lead me into a blind alley.

Appendix

My request: 

Write a stopwatch program that counts the seconds and allows the user to pause the counting at any time using the LiveCode programming language.

ChatGPT's response: 

Here's an example LiveCode code for a stopwatch program that counts the seconds and allows the user to pause the counting at any time:

1:  global gStartTime, gPausedTime  
2:  on startStopwatch  
3:   # record the start time  
4:   put the seconds into gStartTime  
5:   # reset the paused time  
6:   put empty into gPausedTime  
7:   # start the timer  
8:   send "updateStopwatch" to me in 1 second  
9:  end startStopwatch  
10:  on pauseStopwatch  
11:   # record the current time as the paused time  
12:   put the seconds - gStartTime into gPausedTime  
13:   # stop the timer  
14:   cancel "updateStopwatch"  
15:  end pauseStopwatch  
16:  on resumeStopwatch  
17:   # adjust the start time to account for the paused time  
18:   put the seconds - gPausedTime into gStartTime  
19:   # start the timer again  
20:   send "updateStopwatch" to me in 1 second  
21:  end resumeStopwatch  
22:  on resetStopwatch  
23:   # reset the start time and paused time to zero  
24:   put 0 into gStartTime  
25:   put 0 into gPausedTime  
26:   # update the stopwatch display  
27:   updateStopwatchDisplay  
28:  end resetStopwatch  
29:  on updateStopwatch  
30:   # calculate the elapsed time  
31:   put the seconds - gStartTime + gPausedTime into elapsedTime  
32:   # update the stopwatch display  
33:   updateStopwatchDisplay  
34:   # continue the timer  
35:   send "updateStopwatch" to me in 1 second  
36:  end updateStopwatch  
37:  on updateStopwatchDisplay  
38:   # format the elapsed time as a string  
39:   put elapsedTime into timeString  
40:   put format("%02d:%02d:%02d", timeString div 3600, timeString mod 3600 div 60, timeString mod 60) into timeString  
41:   # update the stopwatch display  
42:   set the label of field "stopwatchDisplay" to timeString  
43:  end updateStopwatchDisplay  

This code defines several handlers for controlling the stopwatch. The startStopwatch handler starts the timer by recording the current time in a global variable called gStartTime, resetting the gPausedTime variable to empty, and then scheduling the updateStopwatch handler to run every second using the send command.

The pauseStopwatch handler stops the timer by recording the current elapsed time as the gPausedTime variable, and cancelling the scheduled updateStopwatch handler using the cancel command.

The resumeStopwatch handler resumes the timer by adjusting the gStartTime variable to account for the paused time, and then scheduling the updateStopwatch handler to run every second again using the send command.

The resetStopwatch handler resets the stopwatch by setting the gStartTime and gPausedTime variables to zero, and then updating the stopwatch display using the updateStopwatchDisplay handler.

The updateStopwatch handler calculates the elapsed time since the stopwatch was started, updates the elapsedTime variable, updates the stopwatch display using the updateStopwatchDisplay handler, and then schedules itself to run again in one second using the send command.

The updateStopwatchDisplay handler formats the elapsed time as a string in the format hh:mm:ss, and then updates the label of the stopwatchDisplay field to display the formatted time.

To use this stopwatch program, you can create a new stack in LiveCode and add a field called "stopwatchDisplay" to display the elapsed time. You can then add buttons for starting, pausing, resuming, and resetting the stopwatch, and assign each button to the appropriate handler.

No comments:

Post a Comment