How to Think with AI

How to Think with AI

I am writing to journal a typical journey of a very simple task of working with AI and its coding capabilities. The user's perspective is what matters most. and the prompts provided to AI may not be sufficient unless the user clearly understands their requirements. Let me take you through a step-by-step approach and work with AI to create a program to solve a simple problem. The idea is not to give a fish but to teach how to think and fish. Many people teach how to fish, but the thinking part is missing because it is personal. So, this is lengthy by purpose and design. I'm sorry if you are not a good reader. Use ChatGPT to summarize this. #AI #coding #journal #programming


To explain the process, I took up a simple task that removes double asterisks from text and formats the text between them in bold. I noticed many tools use HTML markup and the bold text gets copied over as double asterisks. So, as a journaling experiment, I tried to create a small program with AI to change this in a macro for MS Word. Obviously, this could have been done very easily manually, but again, I wanted a simple case for the article. #aicoding #experiment #macros #vba #vbscript


Like any AI, I started the coding journey by simply stating my wish and asking the AI to create the code. The first output was very nice-looking, but it did not work. It was bad at execution - "Help me create a program (macro) to identify text between double asterisks in a Word document and change them to bold formatting". #codingjourney #execution #macro

Sub BoldTextBetweenAsterisks()

    Dim doc As Document

    Dim rng As Range

    Dim startIdx As Long

    Dim endIdx As Long

    

    Set doc = ActiveDocument

    Set rng = doc.Range

    

    Do

        startIdx = InStr(rng.Text, "**")

        If startIdx > 0 Then

            rng.Start = rng.Start + startIdx - 1

            rng.MoveStartUntil Cset:="*", Count:=wdForward

            endIdx = InStr(rng.Text, "**")

            If endIdx > 0 Then

                rng.End = rng.End + endIdx - 1

                rng.Text = Mid(rng.Text, 3, Len(rng.Text) - 4)

                rng.Bold = True

            End If

        End If

        rng.Collapse wdCollapseEnd

    Loop Until startIdx = 0 Or endIdx = 0

End Sub
        

The above code deleted the first instance of the double asterisks and made the whole document bold. Chuckles. The subsequent iterations were even worse. They lengthened the code and kept getting uglier and uglier—so much so that I doubted if I even knew English, if English not being my first language was the issue, or if I read Agatha Christie too much when growing up. #codingissues #debugging


So I pivoted. I decided to break the logic into multiple steps and converse with AI to build the code one logic at a time. The first step - clear all formatting, unbold the text, and revert the color to automatic, using a font size 11. #AIlogic #AI #conversation #programminglogic


I then asked the AI to find the positions of double asterisks by finding the beginning and end character positions of the double asterisks in the document and indexing them in an array. So, note that the array was my brainchild. A table of index numbers for the double asterisk's start and end positions in the array. I still did not ask the bot to start coding the bolding part, as I wanted to confirm the positions of the asterisk and if my genius idea of the array was good. I could not see the output because, embarrassingly, I did not even know where the code writes its output. Macro outputs are written in a console that I can access through Ctrl+G. #confirmationbias


Now that I know where the output is written, the AI misunderstood the prompt and made the code output for each double asterisk's start and end positions. So now I have all the positions of the double asterisk in an array, but not as pairs. After failing miserably previously in writing prompts and my confidence in the English language, I did not dare to ask the AI to correct the code. Chuckles. Instead, I asked AI to create a new array using the previous information and create pairs of asterisks. These pairs will now be read as the starting location and end locations of the text, which need to be bolded. Thus preserving my life's work and progressing toward the desired outcome. Evil laugh. #codingmistakes #mistakes


This time, I knew where to look for the locations of the new array, ahem, in Ctrl+G. I was happy; I happily asked AI to write this bolding code. Again, funnily, the formatting happened for a few characters in the first sentence of the document. Chuckles. So, I went into the CONSOLE, and everything looked okay. Then I realized AI, again, misunderstood my prompts. Under some black magic, it was understood that the array positions for the starting and end positions of the asterisks were to be entered manually. So, the program created arbitrary start and end positions and bolded a few characters in the document's first line. Chuckles.


So I had to go on an elaborate rant of literally screaming at the AI like my young cousin, telling it that the whole point of creating two arrays was to automatically get the positions of the asterisks in the program and use them. To my surprise, the emotional rant worked. It not only fixed the automated determination of the start and end positions of the asterisks, but when I asked it to create the bolding code, it didn't even need to be told that the asterisks were two characters beginning and two characters ending, so determining the length of the text that you need to bold properly.


After all was said and done, I wanted to delete the double asterisks. Last and such a simple task, right? Nooooo. To my utmost surprise, this part seemed even harder. More debugging. First, it gave me an elaborate loop to deal with this. I quickly revolted; no, this is just like finding and replacing. Find double asterisks and delete (replace). It agreed that it was more efficient, and it wrote the code. Funnily, the code would only delete the last occurring asterisk pair. Chuckles. The issue was just like how finding and replacing would work; the position of the cursor was at the end after bolding the texts. So I asked it to take the cursor upwards, and it kept adding the code to the bottom of everything. How will the find and replacement program work if you add moving the cursor to the bottom when everything is done? So, I had to explicitly tell it to move the cursor to the top after the bolding code was done.


My experience emphasizes the importance of persistence and clarity when working with AI. It shows how logical thinking and problem-solving can overcome misunderstandings and errors. Devin is not a thing to be worried about but double your efficiency, proficiency, and capabilities. Now, you are not only your software engineer, but you can now do the role of 1+1=11. That's the power of Devin and you combined. I hate how the company pushed it as a software engineer killer. It is still about to do the work that all AI tools were doing; it just put them together. It's still not there yet. #persistence #clarity #logicalthinking #devinai

Sub FormatTextBetweenAsterisks()

    Dim doc As Document

    Dim rng As Range

    Dim asteriskArray() As Variant

    Dim pairArray() As Variant

    Dim asteriskIndex As Integer

    Dim pairIndex As Integer

    Dim startPos As Long, endPos As Long

    

    ' Define the document and range

    Set doc = ActiveDocument

    Set rng = doc.Content

    

    ' Unbold the entire page

    rng.Font.Bold = False

    

    ' Set font color to automatic

    rng.Font.Color = wdColorAutomatic

    

    ' Set font size to 11

    rng.Font.Size = 11

    

    ' Initialize the asterisk array

    ReDim asteriskArray(0 To 2, 0 To 0)

    asteriskIndex = 0

    

    ' Find the positions of double asterisks and store them in the array

    Do While rng.Find.Execute(FindText:="\*\*", MatchWildcards:=True)

        startPos = rng.Start

        endPos = rng.End

        ReDim Preserve asteriskArray(0 To 2, 0 To asteriskIndex)

        asteriskArray(0, asteriskIndex) = asteriskIndex + 1 ' Index

        asteriskArray(1, asteriskIndex) = startPos ' Start position

        asteriskArray(2, asteriskIndex) = endPos ' End position

        asteriskIndex = asteriskIndex + 1

        rng.Collapse Direction:=wdCollapseEnd

    Loop

    

    ' Create pairs of start and end positions from the asterisk array

    ReDim pairArray(0 To 2, 0 To asteriskIndex \ 2 - 1)

    pairIndex = 0

    For i = 0 To UBound(asteriskArray, 2) Step 2

        If i + 1 <= UBound(asteriskArray, 2) Then ' Check if there's a pair

            pairArray(0, pairIndex) = asteriskArray(1, i) ' Start position

            pairArray(1, pairIndex) = asteriskArray(2, i + 1) ' End position

            pairIndex = pairIndex + 1

        End If

    Next i

    

    ' Bold the text between each pair of positions

    For i = 0 To UBound(pairArray, 2)

        startPos = pairArray(0, i)

        endPos = pairArray(1, i)

        rng.SetRange Start:=startPos, End:=endPos

        rng.Font.Bold = True

    Next i

    

    ' Move the cursor to the beginning of the document

    rng.Start = 0

    rng.Collapse Direction:=wdCollapseStart

    

    ' Delete all instances of double asterisks

    rng.Find.ClearFormatting

    rng.Find.Execute FindText:="\*\*", ReplaceWith:="", Replace:=wdReplaceAll

End Sub        

#AI #programming #problemSolving #Devin

Fascinating coding adventure! Could you share your top tip for tackling complex programs? ?? Varun B.

Vikas Tiwari

Co-founder & CEO ?? Making Videos that Sell SaaS ?? Explain Big Ideas & Increase Conversion Rate!

11 个月

What a journey! Your resilience and logical thinking shine through.

Phil Tinembart

I connect your personal brand with your SEO | Helped companies rank on AI search engines | I share content marketing frameworks that work

11 个月

What an insightful coding journey with AI, Devin! Loved reading about your persistence and problem-solving skills. ?? Varun B.

要查看或添加评论,请登录

Varun Biswas的更多文章

社区洞察

其他会员也浏览了