Tachnology

# ChatGPT Vs Bard: Which is better for coding?

## ChatGPT and Bard: What’s the difference?

The biggest difference between ChatGPT and Bard is the Large Language Models (LLMs) they are powered by. ChatGPT uses the Generative Pre-trained Transformer 4 (GPT-4), while Bard uses the Language Model for Dialogue Applications (LaMBDA). Also, ChatGPT is developed by OpenAI, while Bard was built by Google.

In terms of capabilities, both do very similar things. Programmers can use ChatGPT for:

• Suggestions: Both models can suggest the correct syntax and parameters for functions and other code constructs.
• Completion: It can complete code that you have started writing.
• Explanation: It can explain the code that it generates, or code you input.

Both models were trained on a massive dataset, including Common Crawl, Wikipedia, books, articles, documents, and content scraped from the internet. However, Bard is a little different in that it was trained on conversations and dialogues from the web, while ChatGPT was trained mostly on scraped general content.

Both products are still under development, Bard a bit more so than ChatGPT. But to really show how these differences actually matter in a practical sense, here’s how they work when tested against each other.

## Testing ChatGPT and Bard against each other

We chose seven categories to test the chatbots against each other, based on what we felt were the most relevant: code generationproblem solvingrefactoring codedebugging assistancethird-party plugins/UI extensionease of use, and cost. Let’s preface this by saying this is not a rigorous scientific comparison, but rather a handful of tests mixed with our hands-on experience to date.

### 1. Code generation

The bread and butter of what we want it for, right? For this, we fed in the following prompt: “Write a function in python that returns a list of 100 prime numbers.”

#### Bard’s coding attempt

``````def get_prime_numbers(n):
"""Returns a list of prime numbers up to n."""
primes = []
for i in range(2, n + 1):
is_prime = True
for j in range(2, i):
if i % j == 0:
is_prime = False
break
if is_prime:
primes.append(i)
return primes

def main():
"""Prints a list of 100 prime numbers."""
primes = get_prime_numbers(100)
print(primes)

if __name__ == "__main__":
main()
``````

Looks solid, but does it work? In our experience, chatbots can sometimes create code that doesn’t run, even going as far as to invent functions! That’s one reason having AI-generated code in production with no human oversight would be very bad. So let’s test it out and see.

#### ChatGPT’s coding attempt

``````def generate_primes(n):
primes = []
sieve = [True] * (n + 1)
for current in range(2, n + 1):
if sieve[current] != 0:
primes.append(current)
for multiple in range(current, n + 1, current):
sieve[multiple] = False
return primes

# Generate first 100 prime numbers
# The 100th prime number is 541, so we set the limit to 550 to make sure we get at least 100 primes
primes_100 = generate_primes(550)[:100]
print(primes_100)
``````

When we run the same prompt by ChatGPT, this code defines a function generate_primes(n) that takes an integer n as input and returns a list of all prime numbers up to n. It uses the Sieve of Eratosthenes algorithm to generate the primes.

Both of these produced a functional result. ChatGPT’s example is better in this case, since it’s cleaner and has much less code. Also, it interpreted our vague prompt correctly. However, this is only one use-case: it’s known that chatbots will vary a lot in their response depending on the language they are using (With less common or documented languages having more errors) or the complexity of the query. So let’s see how Bard and ChatGPT handle different use cases.

### 2. Problem Solving

Let’s see how of these LLMs handle a problem. Rather than just asking them to write a generic function, let’s throw a programming problem at them from Leetcode:

Write JavaScript code that enhances all arrays such that you can call the array.last() method on any array and it will return the last element. If there are no elements in the array, it should return -1.

#### ChatGPT’s problem solving

``````Here's how you would do it in JavaScript:

Array.prototype.last = function() {
if (this.length === 0) {
return -1;
} else {
return this[this.length - 1];
}
};

In this code:

Array.prototype.last adds a new method called last to the Array.prototype object. This ensures that all arrays will have this method.

this refers to the array object on which the last method is being called.
this.length gives the number of elements in the array.
If the array is empty (this.length === 0), the method returns -1.
Otherwise, the method returns the last element of the array: this[this.length - 1].

Here is how you would use it:

let array = [1, 2, 3, 4, 5];
console.log(array.last()); // prints: 5

array = [];
console.log(array.last()); // prints: -1

Remember that extending built-in JavaScript objects like Array is generally not recommended, as it can lead to conflicts if the JavaScript environment (like a browser or Node.js) adds a method with the same name. This is generally called monkey patching and is usually avoided in production-grade code. But for quick and dirty hacks, or to understand how JavaScript works, it's a fun tool to use!``````

Not only does ChatGPT solve the problem, but it explains the problem. Neat! Let’s run the code through leetcode and see if it actually passes the tests, though.

It looks like ChatGPT solved the problem on the first try! Now it’s time for Bard to step up to the plate.

#### Bard’s problem solving

``````Sure, here is the JavaScript code that enhances all arrays such that you can call the array.last() method on any array and it will return the last element. If there are no elements in the array, it should return -1:

// Define a new method on the Array prototype
Array.prototype.last = function() {
// If the array is empty, return -1
if (this.length === 0) {
return -1;
}

// Otherwise, return the last element
return this[this.length - 1];
};

Here is an example of how to use the array.last() method:

const array = [1, 2, 3, 4, 5];

// Get the last element of the array
const lastElement = array.last();

// Print the last element of the array
console.log(lastElement); // 5``````

Okay, so we didn’t get as thorough an explanation from Bard about how the code works. However, it’s nearly identical when you compare what ChatGPT and Bard produced. Were they looking at each other’s homework?

Naturally, we can’t take Bard’s word at it, so we run the test through Leetcode, and…

<a target="_blank" href="https://shareasale.com/r.cfm?b=2271950&amp;u=3802034&amp;m=133410&amp;urllink=&amp;afftrack="><img src="https://static.shareasale.com/image/133410/5.jpg" border="0" /></a>

×
Video