Archive for the ‘Coding With AI’ Category

The time has come for a post regarding using AI-powered tools to write code for us. I have been programming C++ since the early 90’s, ever since I was in middle school. Now, 30 years later, even with all the updates to the C++ language over the years, it is a very natural and intuitive language for me.

With the advent of AI and specifically large language models (LLMs), is all that C++ knowledge and practice and experience over the years now worth nothing? Well, not exactly. But the job of being a programmer is about to make a fundamental shift over the next few years, all because of tools such as Cursor.

What is Cursor

Cursor is a version of Visual Studio Code which is augmented by AI. A simple way to think about it is having ChatGPT right inside your development environment. But what makes it special is that it’s not just using an large language model, it is infact, an agentic AI. What does that mean? It means that the AI is empowered with the ability to execute shell commands on your behalf.

Alright. What’s this mean in a practical sense? It means the AI is able to compile and run your code, download libraries and files from git which you man need, it can help you set up your launch.json and tasks.json to get your project working, and so on.

Suppose you are having trouble getting your program to compile. Perhaps you are trying to incorporate a new library and have forgotten a #define, or some other dependency, or some trivial matter like this. You can ask Cursor why the program won’t compile, and it will try to compile the program, read the errors, make any changes necessary to the code or even the environment, try again, and again, and again, until it is successful. It takes the most frustrating parts of coding completely out of our hands!

An Example of the Power of Cursor

I will give you an example of what I mean. Often, my projects involve using a popular constraint solver known as Z3. This is an SMT solver, of which there are many. But instead of me going to google, and tracking down which version to download, or whether I need to compile a library from scratch, and all that usual stuff which takes forever, I simply ask Cursor to create a simple C++ program which uses Z3 to solve a very simple problem. X + Y == 3, X > 0, Y > 0, check if this is satisfiable (is the problem solvable?), and if so, print variable assignments for X and Y to make all my constraints true.

Within moments, Cursor was able to execute a web search on my behalf, resulting in a link to a pre-built release. After downloading it, unzipping it, it created my C++ code for me:

Example of Code Written By Cursor (not me!)

#include <iostream>
#include <z3++.h>

int main() {
    z3::context c;
    z3::expr x = c.int_const("x");
    z3::expr y = c.int_const("y");
    z3::solver s(c);

    s.add(x + y == 3);
    s.add(x > 0);
    s.add(y > 0);

    if (s.check() == z3::sat) {
        std::cout << "Satisfiable!\n";
        z3::model m = s.get_model();
        std::cout << "x = " << m.eval(x) << "\n";
        std::cout << "y = " << m.eval(y) << "\n";
    } else {
        std::cout << "Unsatisfiable!\n";
    }
    std::cout << "Finished!" << std::endl;
    return 0;
} 

At this point, the program still didn’t compile. So I asked Cursor to figure it out for me. In a few minutes, it was able to correctly modify my tasks.json file to include and linking paths for the header and library. Now the program could compile, but not run. After a quick conversation with Cursor, it was able to determine I needed to add the directory containing z3’s DLL file to my PATH environment variable. It quickly modified my launch.json file for me to adjust the environment.

Tab Completions

Cursor isn’t just ChatGPT integrated into an IDE and given the authority to execute shell commands on your behalf. It also offers tab completions. Pictures this. You’re writing a function, and let’s face it, very little of what we write is 100% new and original. Chances are overwhelming that we’re following some sort of structure, which the AI has been trained on countless examples of before. So as you type, Cursor will see what you’re typing, and make suggestions on how to complete whatever you’re doing. When a suggestion pops up, simply press TAB to accept it. These tab completions can dramatically speed up your programming speed.

What does this mean for us programmers?

Cursor (and other similar tools) are revolutionary for programming. They let us operate at a higher level than possible before. I find that cursor is excellent at performing the low level tasks which we’re all familiar with, but take our precious time. Does your program have to do trivial tasks like reading a CSV file and storing data for some specific columns? Just tell cursor what you need done, and it’ll do it. Even if you’re the fastest programmer on the planet, chances are Cursor can create equivalent code faster than you. So I’d recommend leaving all the low-level tasks to the AI prompt window and spend more time thinking about the bigger picture.

Is Cursor perfect? No. Sometimes it can make foolish mistakes. Sometimes the code it creates is repetitive. Before accepting a suggestion, it helps to tell it things like, ‘that’s great, but can you be more terse?’, and it’ll try to reorganize the code in a more terse, structured way. Or, if you just need a function done and you really don’t care if the code is a big messy or not to you’re liking, that’s fine too.

Moving Forward

Failure to use AI tools to augment your programming is effectively career suicide. Who is going to pay you the big bucks to write code the old fashioned way, when anyone right out of college armed with Cursor can create code faster? The only way to compete in the modern day work environment is to stay up to date with the latest AI tools.

Cursor isn’t the only name in town, but it is one of the best. The workflow I use is to develop in Cursor, but run and debug my program using Visual Studio Professional, since that is a much more mature program for debugging. But whatever your workflow, you should absolutely consider using AI tools to augment your programming, whether it be C++, Python, Perl, or literally any language.

A Final Example

Another great example of using Cursor can be found on this blog. I needed a program which could compile and run on Windows and Linux, and be able to read and write image files, such as JPG files. Instead of me spending half an hour of my time searching the web looking for the best library and whatnot, I simply asked Cursor to do everything for me. As a result, I have a program which could read any JPG file, modify it, and write that modified image back to the hard drive, all in less than 5 minutes. That is the amazing power of Cursor.