avatarRakia Ben Sassi

Summary

A comparison of human and AI code efficiency in a real-world case study involving the extraction of fields from a text using specific delimiters.

Abstract

This context explores a coding challenge where a human programmer and ChatGPT, an AI language model, are tasked with creating a TypeScript method to extract fields from a text using specific delimiters. The solutions are analyzed for their efficiency, maintainability, and clarity. The human solution, while more complex and time-consuming, successfully handles various edge cases and delimiter arrangements. ChatGPT's solutions, while straightforward and effective in specific scenarios, fail to return the expected outcome in many scenarios.

Opinions

  • The human approach, with its recursive depth and meticulous checks, is seen as a masterful orchestration of logic, precisely attuned to the symphony of complex scenarios.
  • ChatGPT crafts solutions that are straightforward and effective within specific scenarios, but struggle with more rugged and intricate landscapes of code.
  • The human intellect shows a deep understanding of the subtle intricacies that AI has yet to fully grasp.
  • The art of programming is not a mere contest of algorithms, but a rich tapestry woven with ingenuity, foresight, and adaptability.
  • The complexity of the human implementation, initially seen as a downside, is actually necessary to handle more intricate scenarios.
  • The usage of extractFieldValuesFromRemainingText() in the human method improves the modularity and maintainability.
  • The human solution's ability to handle various edge cases accurately indicates a high level of logical precision, which is not covered by the AI solutions.

Web Development

Human vs AI Code Showdown: Who Dissects Messages Better?

Would AI’s efficiency in programming stack up against human prowess (a real-world case study)?

Human vs AI Code Showdown: Who Dissects Messages Better? (image by author)

Join 30K+ Students and my complete Web Performance Udemy video course with a discount with this link (available for a limited time).

The video version is available here.

Artificial Intelligence is no longer a science fiction, but an everyday reality shaping our lives. At the heart of this revolution lies a burning question:

How does AI’s efficiency in programming stack up against human prowess?

Enter the arena of a seemingly simple challenge: extracting fields from a text using specific delimiters. Sounds simple, right?

Think again. When ChatGPT goes head-to-head with a seasoned human programmer, what unfolds is not just a mere coding showdown.

It’s an electrifying battle of wits and strategy, revealing the starkly different ways AI and human minds dissect and conquer problems:

  • One relies on vast data and algorithms and processes information with a speed that’s almost otherworldly.
  • The other relies on experience, intuition, and that elusive spark of creativity.

From the efficiency of the code to its maintainability and clarity, this contest isn’t just about who gets it right. It’s a dive into the dynamic and rapidly evolving world of programming in the AI era.

Will the logical precision of AI outshine human intelligence? Or does the human touch still hold its own against the machine?

The stakes are high, and the implications are profound.

Join us as we delve into the fascinating intersection where human ingenuity collides with the force of artificial intelligence.

The Battleground

Let’s roll up our sleeves and dive into the nitty-gritty of the challenge.

Our battleground is implementing a TypeScript method, a seemingly straightforward task that quickly unravels into a complex puzzle. Here’s the scenario:

The method takes two parameters — a text and a list of delimiters. Its mission is to navigate through the text, using the delimiters as guides, and extract an array of fields hidden within.

Let’s consider some examples:

  • When the delimiters are ['/', '/', '/'] and the text is /folders/files/101/doc/client-req-12.text, the method should extract ['folders', 'files', '101/doc/client-req-12.text'].
  • In a different scenario, with delimiters as ['T', '.333'] and the text as 02–02–2022T11:11:11.333, the method should skillfully extract ['02-02-2022', '11:11:11'].
  • If a delimiter is missing in the text, the method must throw an error.

This task, while elementary at first glance, demands more than just coding skills. It requires strategic thinking, adaptability, and a deep understanding of language nuances.

Who will emerge victorious in this clash of coding? Let’s delve into their solutions and find out.

The Solutions

ChatGPT

ChatGPT took less than 19 seconds to generate this solution:

Time required by ChatGPT to implement extractFieldValues() (screenshot by author)

The Human Programmer

The human programmer took about 29 minutes to implement this version of extractFieldValues() method:

Time required by the human programmer to implement extractFieldValues() (screenshot by author)

Upon initial examination, the contrast between the two solutions is striking: The human-crafted code stands out with its lengthier and more intricate structure, naturally demanding a greater investment of time compared to ChatGPT’s succinct approach.

This initial observation begs the question:

Does the length and complexity of the human solution translate to greater effectiveness?

Or does ChatGPT’s concise coding style conceal a surprising depth of functionality?

Now, the moment of truth arrives. Let’s run our test scenarios, the ultimate proving ground to uncover the truth.

Setting the Scene to Test the Solutions

For that sake, I’ve prepared an Angular component that contains a form with two input fields:

  • The first form field allows me to enter the text I want to dissect.
  • In the second form field, I will specify placeholders for the fields I want to extract. In other words, the dissect pattern.
  • I will pass the text and the dissect pattern to the generateExtractedFields() and generateExtractedFieldsAI() methods, which will parse the dissect pattern and determine the delimiters.
  • Then they will call extractFieldValues() and extractFieldValuesAI() methods with the text and the list of delimiters as arguments.
  • For better visualization of the result, I’m calling addBgColorsToFields(text, extractedFields), which will add a unique background color for each extracted field.
  • At the bottom of the view, we will see the list of extracted fields with their associated colors and names. On the left, we have the result of the human programmer’s method. And on the right, we have the result of ChatGPT’s method.

Running 20 Test Scenarios

Test Scenarios to Dissec a Message (picture by author)

The human method extracted the fields successfully while ChatGPT failed the test.

ChatGPT Second Chance

In my quest for a solution, I once again turned to ChatGPT, and prompted it with this text:

When the delimiters are different like [‘T’, ‘.333’] and text = ‘02–02–2022T11:11:11.333, the result of extractFieldValues() was wrong. Can you fix it?

ChatGPT second prompt (screenshot by author)

ChatGPT responded with a refined algorithm, crafted to adeptly navigate the maze of varying delimiters. The new implementation began by dissecting the text with the initial delimiter and then continued splitting the resulting substrings with the subsequent delimiters.

However, this solution, vaunted for its precision in handling disparate delimiters, didn’t live up to its promise. It fell short of even its predecessor’s performance.

Undeterred, I prompted ChatGPT one third time. Yet, when I put its revised implementation to the test, it stumbled, unable to bridge the gap between expectation and reality:

Comparison of the Human & ChatGPT Approaches

ChatGPT approach

  • Simplicity and Readability: The first two implementations iteratively search for each delimiter and extract the relevant substring. This strategy is simple and effective. But it worked just for a limited number of scenarios.
  • Advanced Regex Usage: Using regex in the last ChatGPT solution is an advanced technique that handles complex delimiter scenarios better. However, this could further increase the complexity and reduce readability.
  • Hybrid Approach: Combining regex for initial splitting with custom logic for handling special cases might strike a balance between efficiency and capability to handle complex scenarios.
  • Time Complexity: The time complexity mainly depends on the use of indexOf() in a loop. This function has a linear time complexity, O(n), for each search. In the worst case, the total time complexity would be O(m*n), if there are m delimiters.
  • Memory Usage: The method uses a single array fieldValues to store the results and doesn’t create additional copies of the input string. This approach is memory efficient.
  • Algorithmic Efficiency: The approach is straightforward and efficient for smaller texts and fewer delimiters. However, for large texts and many delimiters, the repeated scanning of the string can become inefficient.

Yet, all these AI approaches failed to return the expected outcome in many scenarios.

The human approach

The human implementation is more complex, with additional checks and a recursive approach for handling the remaining text.

  • Complexity: The complexity, initially seen as a downside, is actually necessary to handle more intricate scenarios. The use of recursion and careful delimiter handling is justified in this context.
  • Clean Code: The method seems like over-engineering in simpler cases. Yet, it proves valuable in dealing with more complicated delimiter arrangements.
  • Readability: While less clear than the other solutions, the structure might be understandable within the context of its advanced capabilities.
  • Maintainability: The usage of extractFieldValuesFromRemainingText() improves the modularity. Overall the maintainability is likely acceptable, especially if it’s well-documented.
  • Logical Precision: The method’s ability to handle various edge cases accurately indicates a high level of logical precision, which is not covered by the AI solutions.
  • Time Complexity: This method is more complex due to additional string operations like replace(), recursive calls, and handling of multiple delimiters. The time complexity could be higher than the first method.
  • Memory Usage: The method creates a copy of the original text and potentially more string copies due to the use of replace(). This approach can lead to higher memory usage, especially for large strings.
  • Algorithmic Efficiency: This method is more flexible and handles a variety of cases — like delimiters at the start of the text. Yet, due to its recursive nature and additional string manipulations, it could be slower, especially for large texts or when there are many delimiters.

Final Thought

The exploration in today’s case study peels back layers of complexity. It reveals that the art of programming is not a mere contest of algorithms, but a rich tapestry woven with ingenuity, foresight, and adaptability.

ChatGPT crafted solutions that are straightforward and effective within specific scenarios. Yet, when the terrain shifts to more rugged and intricate landscapes of code, the human intellect shows a deep understanding of the subtle intricacies that AI has yet to fully grasp.

The human approach, with its recursive depth and meticulous checks, emerges as a masterful orchestration of logic, precisely attuned to the symphony of complex scenarios.

This story is not just a testament to the strengths and limitations of AI and human ingenuity. It’s also a reminder to aspiring developers and seasoned programmers alike, not just to code. But to dive deeper, think, innovate, and transcend.

Are you ready to embark on this transformative journey and elevate your skills?

Join 30K+ students and enroll in my Udemy video course: Web Performance 101: Your Guide to Boost Your Web Apps, where I unravel the secrets of efficient and effective web applications 🚀.

Want more?

I write about engineering, technology, and leadership for a community of smart, curious people 🧠💡. Join my free email newsletter for exclusive access.

If you enjoyed this article, consider trying out the AI service I recommend. It provides the same performance and functions to ChatGPT Plus(GPT-4) but more cost-effective, at just $6/month (Special offer for $1/month). Click here to try ZAI.chat.

Technology
Programming
ChatGPT
Web Development
AI
Recommended from ReadMedium