From 'Hello World' to AI-Powered Coding: My Journey & How ChatGPT Lights the Way

From 'Hello World' to AI-Powered Coding: My Journey & How ChatGPT Lights the Way

Introduction

I still remember the day when I wrote my first piece of code. I was in my 8th or 9th standard (around the year 2004) I think when this happened. I also think it was Ms. Shubashini’s class. It was not in C or Java, but in HTML. Much of this is pretty vague to me now, but I think it was in a notepad file where I typed in something like:

<html>
<head>
<title>Hello World</title>
</head>
</html>


We had to save the file as mysite.html and then had to open the file in a web browser. And when you did that, a nice looking “Hello World” popped up on the screen. (I tried to replicate this today, and it just didn't work. Have to figure out why). This was my first website. It was hosted on my computer and had almost nothing in it, but it was something that I created, and I was proud of it. Although I know a lot more about web development today, I am definitely still a novice with my HTML skills. I can go as far as to say that HTML is no longer required because webdesign has moved to much greater levels requiring a comprehensive understanding of CSS and Javascript.

After my little trysts with HTML, Java, C and Python, I think it is finally the language of R that I think I managed to learn with some degree of expertise. Today I do use R extensively for my analysis, teaching, and visualization. To get to this point, I had to read a lot of books, and work on a lot of trial and error. It is only recently that I stumbled on Chatgpt and Google Bard and I think if I had access to these amazing tools, my learning of programming would have been faster ( I would not go as far as to say better, but we’ll get there).

About 3 weeks ago, Dr. Manish Sarkel and I organized a workshop where we discussed how students could use ChatGPT to learn. A large chunk of that workshop was used to discuss how ChatGPT could be used to code and make certain difficult tasks rather easy. In the days since then, many students have spoken to me about how useful some of the tips were, so I felt it was a good idea for me to write about some of them. Hence this article. Throughout this article, I try and talk about the perspective of someone who uses R, but I’m pretty sure many of these learnings also apply to others.

Tip 1: ChatGPT can help with syntaxes

One of the most common challenges a novice and even intermediate programme faces is getting the syntax right. A single misplaced comma, an unclosed parenthesis, or an overlooked semicolon can bring your entire code to a halt. This is where ChatGPT shines. Instead of painstakingly scouring through lines of code or spending endless minutes on Google and Stack overflow, you can pose a query to ChatGPT and it will tell you exactly what you need.

Loops and complex GGPlots become supremely easy to handle. This helped me save time, but it also ensured that the correct syntax patterns were reinforced in my memory. I was also able to use ChatGPT to effectively utilize some of the less commonly used packages with much greater ease.

Tip 2: ChatGPT can write the code for you if you explain it well enough

When I was a novice, I knew what I wanted to do but I did not know how to get there. I think this is one place where ChatGPT shines. Even if one does not have access to the code interpreter (which is limited to Python only btw) there is an interesting work around that you should try out. In R, the head(dataframe) or str(dataframe) functions help you get the structure of the dataframe that you are interested in working with. All you have to do is to get the output of the command and give it to ChatGPT and then you can see the magic work. Now, let me demonstrate this for you.

Try this:

data<- data("CO2") #this basically loads the freely available CO2 dataset into your environment
str(CO2) #this gives you the structure of the dataframe.

The output has to look something like this:

> str(CO2)
Classes ‘nfnGroupedData’, ‘nfGroupedData’, ‘groupedData’ and 'data.frame':	84 obs. of  5 variables:
$ Plant    : Ord.factor w/ 12 levels "Qn1"<"Qn2"<"Qn3"<..: 1 1 1 1 1 1 1 2 2 2 ...
$ Type     : Factor w/ 2 levels "Quebec","Mississippi": 1 1 1 1 1 1 1 1 1 1 ...
$ Treatment: Factor w/ 2 levels "nonchilled","chilled": 1 1 1 1 1 1 1 1 1 1 ...
$ conc     : num  95 175 250 350 500 675 1000 95 175 250 ...
$ uptake   : num  16 30.4 34.8 37.2 35.3 39.2 39.7 13.6 27.3 37.1 ...
- attr(*, "formula")=Class 'formula'  language uptake ~ conc | Plant
.. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
- attr(*, "outer")=Class 'formula'  language ~Treatment * Type
.. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
- attr(*, "labels")=List of 2
..$ x: chr "Ambient carbon dioxide concentration"
..$ y: chr "CO2 uptake rate"
- attr(*, "units")=List of 2
..$ x: chr "(uL/L)"
..$ y: chr "(umol/m^2 s)"

Now, just try this in ChatGPT:

"This is the structure of my dataset:

"> str(CO2) Classes ‘nfnGroupedData’, ‘nfGroupedData’, ‘groupedData’ and 'data.frame': 84 obs. of 5 variables: $ Plant : Ord.factor w/ 12 levels "Qn1"<"Qn2"<"Qn3"<..: 1 1 1 1 1 1 1 2 2 2 ... $ Type : Factor w/ 2 levels "Quebec","Mississippi": 1 1 1 1 1 1 1 1 1 1 ... $ Treatment: Factor w/ 2 levels "nonchilled","chilled": 1 1 1 1 1 1 1 1 1 1 ... $ conc : num 95 175 250 350 500 675 1000 95 175 250 ... $ uptake : num 16 30.4 34.8 37.2 35.3 39.2 39.7 13.6 27.3 37.1 ... - attr(*, "formula")=Class 'formula' language uptake ~ conc | Plant .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv> - attr(*, "outer")=Class 'formula' language ~Treatment * Type .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv> - attr(*, "labels")=List of 2 ..$ x: chr "Ambient carbon dioxide concentration" ..$ y: chr "CO2 uptake rate" - attr(*, "units")=List of 2 ..$ x: chr "(uL/L)" ..$ y: chr "(umol/m^2 s)""

Can you help me draw a ridgeplot of 'conc' for each 'Plant'??."

Now ChatGPT knows the structure of your dataframe, and helps you by giving you the code that you need to copy paste into your R console and run.

In this case, it gives some instructires about the packages that have to be preinstalled, and then the code for the ridge plot.

Specifically, it tells me to run this:

ggplot(CO2, aes(y = Plant, x = conc, fill = Plant)) +
geom_density_ridges(scale = 3, rel_min_height = 0.01) +
theme_ridges() +
labs(title = "Ridge plot of conc across different Plants",x = "Concentration",y = "Plant")


Check out what happens when I run this:

I get this!!


Now, the possibilities with this are endless, and I cna keep going back and forth with ChatGPT about how I want to improve it.

For example, if I want to make this look like it was on the wall street journal, I could do that.


You want the economist - no problem


A wrapped version of the plot as opposed to the ridges in an economist theme. You got it!


Do not want an economist inspired theme, but the real economist these from the ggridges package:


I’m sure you can see the possibilities now.

In my view, ChatGPT and other AI tools bridge a very large gap that exists in the market. It helps people with limited understanding of coding to do more complicated tasks with ease. And that’s just great, if you are a teacher who has to help students to learn to code in certain topics.

Tip 3: Breaking down code to make it understandable

Don’t get me wrong. My intent is not to showcase the shortcuts in learning how to code. It is simply to demonstrate to novices that coding is not very difficult if you can specify what you want to do accurately.

Now, here is perhaps the best learning for me- breaking down complex code into more understandable chunks.

Let me show you - let’s say I want to scrape a website and get the information from there - I make use of the rvest package.

In this chunk of code, I pull out all the Nobel Laurete’s information from the wikipedia page

url <- "https://en.wikipedia.org/wiki/List_of_Nobel_laureates"
webpage <- read_html(url)
tables <- webpage %>% html_table(fill = TRUE)

Now I have the data with me.


Now, let’s say I want to understand what it happening.

I can simply ask chatgpt to help.

I simply ask it:

"Can you help me understand this code :

“url <- "https://en.wikipedia.org/wiki/List_of_Nobel_laureates"

webpage <- read_html(url)

tables <- webpage %>% html_table(fill = TRUE)”"

And then chatgpt tell me:


I’m sure you can see how this can help with more complex code such as the code from relatively unknown packages that we use for a particular methodological approach.

Some other tips

In addition to this ChatGPT can assist in a few more ways, let me quickly wrap that up:

  1. Making the Code Look Nicer by
  2. Providing Code Formatting Suggestions: When provided with a snippet of code, ChatGPT can review it and suggest a cleaner or more standard formatting. It can advise on proper indentation, spacing, and arrangement, ensuring the code adheres to common coding standards and practices.
  3. Offering Consistent Naming Conventions: ChatGPT can review variable and function names in your code, suggesting more descriptive or conventional naming choices. For example, if you've used vague variable names, it can recommend more meaningful alternatives that make the code's purpose clearer.
  4. Writing Comments for the Code by generating comments automatically. For basic to moderately complex code snippets, you can ask ChatGPT to generate comments directly. For instance, by providing a function, ChatGPT can help describe its inputs, outputs, and overall functionality. I’m sure you can appreciate how handy this can be when sharing your code with others.
  5. Improving or Optimizing Code:
  6. By examining your code, ChatGPT can suggest more efficient methods, functions, or algorithms that could optimize the performance of your code.
  7. ChatGPT can suggest alternative libraries or methods that are better suited or more efficient for specific tasks.
  8. Identifying Inefficiencies: If you're experiencing performance issues but aren't sure where they're coming from, presenting the problematic code to ChatGPT might help. It can often identify common bottlenecks or inefficiencies.

Conclusion

Okay, let's wrap up this really long article now. Coding can be a fickle beast, but it doesn't have to eat up our sanity. ChatGPT isn't just a nifty tool to whip out fancy-looking graphs or correct my syntax errors.  It's like that knowledgeable friend who’s always around to help us during times of need.

In essence, while coding languages evolve (and some of us remain stubbornly attached to our novice HTML skills), the heart of the matter is simple: it's all about solving problems and having a laugh along the way. Whether you're asking ChatGPT or just Googling why your "Hello World" website isn't quite world-ready, always remember the thrill of that first successful code. And, if you ever feel lost, just look back, chuckle, and remind yourself: "At least it's not 2004, and I don’t have to copy code from a black board to a text editor or write copious amounts of code as answers in exams on sheets