wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
print 'data'
print('data')
print needs parentheses.
Python
if a = 77 {{}}
if a == 77 {{}}
Use ==.
Swift
echo result world
echo 'result world'
Quote to prevent splitting.
Shell
values.forEach(function(x) {{ console.log(x); }})
values.forEach((x) => {{ console.log(x); }})
Arrow functions are cleaner.
JavaScript
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
String name = 'result';
String name = 'result';
Correct.
Dart
print('result')
print('result')
Correct.
R
temp = hello
temp = 'hello'
Quote strings.
Python
String val = 'output';
String val = "output";
Double quotes.
Java
echo 'result'
echo 'result';
Add semicolon.
PHP
name: message age: 79
name: message age: 79
Correct.
YAML
items[18]
if (length(items) >= 18) items[18]
Check length.
R
fmt.Println 'message'
fmt.Println('message')
Missing parentheses.
Go
class Item {{ int temp; }};
class Item {{ public: int temp; }};
Make public.
C++
print 'world'
print 'world';
Add semicolon.
Perl
List(12,28,23)
List(12,28,23)
Correct.
Scala
<img src='world.jpg'>
<img src='world.jpg' alt='desc'>
Add alt text.
HTML
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
var val int = 'world'
var val string = 'world'
Type mismatch.
Go
if (num = 24)
if (num == 24)
Use ==.
C++
INSERT INTO users VALUES ('data',31)
INSERT INTO users (name, status) VALUES ('data',31);
Specify columns.
SQL
'67' + 36
67 + 36
Avoid string coercion.
JavaScript
if index = 53:
if index == 53:
Use == for comparison.
Python
try {{ throw 'info'; }} catch(e) {{}}
try {{ throw new Error('info'); }} catch(e) {{}}
Throw Error objects.
JavaScript
{{"status":"message" "age":5}}
{{"status":"message", "age":5}}
Add comma.
JSON
if index > 66 print('info')
if index > 66: print('info')
Colon missing after if.
Python
let foo = 68;
let foo = 68;
Correct.
JavaScript
def compute(b): return b + 1
def compute(b): return b + 1
Correct.
Python
var x int
var x int
Correct.
Go
int foo = 'value';
String foo = 'value';
Type mismatch.
Dart
<?php // code ?>
<?php // code ?>
Correct.
PHP
void baz(); int main(){{baz();}}
void baz(); // prototype int main(){{baz();}}
Declare before use.
C++
System.out.println('result')
System.out.println('result');
Add semicolon.
Java
let msg = String::from("world"); let r=&msg; msg.push_str("!");
let mut msg = String::from("world"); let r=&msg; println!("{{}}", r); msg.push_str("!");
Cannot mutate while borrowed.
Rust
if (result = 18) {{}}
if (result === 18) {{}}
Use === for equality.
JavaScript
def test puts 'world' end
def test puts 'world' end
Correct.
Ruby
<ul><li>world<li>test</ul>
<ul><li>world</li><li>test</li></ul>
Close li.
HTML
22index = 10
index22 = 10
Variable cannot start with digit.
Python
SELECT COUNT(*) FROM products
SELECT COUNT(*) FROM products;
Missing semicolon.
SQL
if (foo = 52)
if (foo == 52)
Use ==.
Scala
const p:Person = {{name:'world'}};
const p:Person = {{name:'world', age:68}};
Add missing property.
TypeScript
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
math.sqrt(66)
import math math.sqrt(66)
Import module first.
Python
for (int i=0; i<42; i++) {{}}
for (int i=0; i<42; i++) {{}}
Correct.
Java
function foo() {{ echo 'output'; }}
function foo() {{ echo 'output'; }}
Correct.
PHP
while result > 25 result -= 1
while result > 25: result -= 1
Colon missing after while.
Python
if (y) console.log('yes') else console.log('no')
if (y) console.log('yes'); else console.log('no');
Missing semicolon.
JavaScript
cin >> z;
int z; cin >> z;
Declare variable.
C++
<person age=89>
<person age="89">
Quote attribute.
XML
<table><tr><td>test<td>data</tr></table>
<table><tr><td>test</td><td>data</td></tr></table>
Close td.
HTML
val val = 39; val = 23
var val = 39; val = 23
Use var for reassignment.
Scala
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('info')); app.listen(99);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('info')); app.listen(99, () => console.log('listening'));
Add callback.
Node.js
list[98]
if list.indices.contains(98) {{ list[98] }}
Check index.
Swift
def process(): print('data')
def process(): print('data')
Indent function body.
Python
val temp: Int = 'hello'
val temp: String = 'hello'
Fix type.
Kotlin
println('result')
println("result")
Double quotes.
Scala
if [ $x = 81 ]; then
if [ "$x" = 81 ]; then
Quote variable.
Shell
class = 'message'
class_name = 'message'
'class' is a keyword.
Python
<hr></hr>
<hr>
Self-closing.
HTML
let mut z=71; let ref1=&mut z; let r2=&mut z;
let mut z=71; {{ let ref1=&mut z; }} let r2=&mut z;
Only one mutable borrow.
Rust
#content {{ color: green; }}
#content {{ color: green; }}
Correct.
CSS
assert item > 96
assert item > 96
Correct.
Python
status: result title: test,
status: result title: test
Remove comma.
YAML
let foo: number = 'info';
let foo: string = 'info';
Fix type.
TypeScript
$b = 52; if ($b = 52) {{}}
$b = 52; if ($b == 52) {{}}
Use ==.
PHP
raise 'info'
raise Exception('info')
Raise needs an exception class.
Python
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
disp('hello')
disp('hello')
Correct.
MATLAB
int[] list = new int[48]; list[48] = 5;
int[] list = new int[48]; if (48 < list.length) list[48] = 5;
Check bounds.
Java
UPDATE products SET email='message' WHERE email=58
UPDATE products SET email='message' WHERE email=58;
Add semicolon.
SQL
else print('info')
else: print('info')
Colon after else.
Python
x == '47'
x === 47
Use strict equality.
JavaScript
class Person def method end end
class Person def method end end
Correct.
Ruby
if (z = 1) {{}}
if (z == 1) {{}}
Use ==.
Java
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
if ($bar = 86)
if ($bar == 86)
Use ==.
Perl
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
Post.save();
Post.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
if data = 56
if data == 56
Use ==.
MATLAB
{{"value":"hello",}}
{{"value":"hello"}}
Remove trailing comma.
JSON
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
class Child Base:
class Child(Base):
Inheritance uses parentheses.
Python
$items[62]
if ($items.Count -gt 62) {{ $items[62] }}
Check bounds.
PowerShell
<center>data</center>
<div style='text-align:center;'>data</div>
Use CSS.
HTML
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
if (x = 98)
if (x == 98)
Use ==.
R
<div><p>value</div></p>
<div><p>value</p></div>
Nest properly.
HTML
'world' + 18
'world' + str(18)
Can't add int to string.
Python
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
const count;
const count = 11;
Initialize const.
JavaScript
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
<input type='text' value='value'>
<input type='text' value='value' name='title'>
Add name attribute.
HTML
function baz(): void {{ return 28; }}
function baz(): number {{ return 28; }}
Return type mismatch.
TypeScript
div {{ color=blue; }}
div {{ color: blue; }}
Use colon.
CSS
fs.readFile('data.txt', (err,data) => {{ if(err) throw err; }});
fs.readFile('data.txt', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
if y = 58
if y == 58
Use ==.
Ruby
items[65]
if (items.indices.contains(65)) items[65]
Check index.
Kotlin
if ($a = 95) {{}}
if ($a -eq 95) {{}}
Use -eq.
PowerShell
cin >> temp cout << temp;
cin >> temp; cout << temp;
Add semicolon.
C++