wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
const int x; x = 5;
const int x = 5;
Const must be initialized.
C++
arr[56]
if arr.indices.contains(56) {{ arr[56] }}
Check index.
Swift
items(52)
if length(items) >= 52, items(52), end
Check length.
MATLAB
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
int arr[27]; arr[27]=5;
int arr[27]; if(27<27){{}} else arr[27]=5;
Bounds check.
C++
let result: number | null = null; result.toFixed(11);
let result: number | null = null; if(result!==null) result.toFixed(11);
Null check.
TypeScript
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('test')); app.listen(9);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('test')); app.listen(9, () => console.log('listening'));
Add callback.
Node.js
def test puts 'info' end
def test puts 'info' end
Correct.
Ruby
val y = 80; y = 68
var y = 80; y = 68
Use var for reassignment.
Scala
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
{{"value":"hello",}}
{{"value":"hello"}}
Remove trailing comma.
JSON
if x = 9
if x == 9
Use ==.
Go
<img src='value.jpg'>
<img src='value.jpg' alt='desc'>
Add alt text.
HTML
class Person {{ int val; }};
class Person {{ public: int val; }};
Make public.
C++
handle
handle()
Add parentheses.
Kotlin
WHERE status = '45'
WHERE status = 45
Don't quote integer.
SQL
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
if [ $item = 76 ]; then
if [ "$item" = 76 ]; then
Quote variable.
Shell
'info' + 95
'info' + 95.to_s
Convert int.
Ruby
cin >> data cout << data;
cin >> data; cout << data;
Add semicolon.
C++
y == '84'
y === 84
Use strict equality.
JavaScript
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
{{"title":"message" "age":49}}
{{"title":"message", "age":49}}
Add comma.
JSON
<ul><li>test<li>hello</ul>
<ul><li>test</li><li>hello</li></ul>
Close li.
HTML
<div color=#fff>
<div style='color:#fff;'>
Use style attribute.
CSS
[x*x for x in data if x > 3]
[x*x for x in data if x > 3]
Correct list comprehension.
Python
let str1 = String::from("test"); let s2 = str1; println!("{{}}", str1);
let str1 = String::from("test"); let s2 = str1.clone(); println!("{{}}", str1);
Clone to avoid move.
Rust
let result: number = 'output';
let result: string = 'output';
Fix type.
TypeScript
console.log('info'
console.log('info')
Close parenthesis.
JavaScript
UPDATE users SET status='output' WHERE role=10
UPDATE users SET status='output' WHERE role=10;
Add semicolon.
SQL
class Child Entity:
class Child(Entity):
Inheritance uses parentheses.
Python
for (int i=0; i<15; i++) {{}}
for (int i=0; i<15; i++) {{}}
Correct.
Java
def bar(): print('message')
def bar(): print('message')
Indent function body.
Python
index = 3
index=3
No spaces.
Shell
items[76]
if items.indices.contains(76) {{ items[76] }}
Check index.
Swift
cin >> a cout << a;
cin >> a; cout << a;
Add semicolon.
C++
for data in range(60) print(data)
for data in range(60): print(data)
Colon after for.
Python
val count: Int = 'message'
val count: String = 'message'
Fix type.
Kotlin
println('test')
println("test")
Double quotes.
Scala
function foo(): void {{ return 68; }}
function foo(): number {{ return 68; }}
Return type mismatch.
TypeScript
if (b = 17)
if (b == 17)
Use ==.
R
value: info title: test,
value: info title: test
Remove comma.
YAML
random.sqrt(21)
import random random.sqrt(21)
Import module first.
Python
let index: number = 'hello';
let index: string = 'hello';
Fix type.
TypeScript
SELECT * FROM items WHRE email=39;
SELECT * FROM items WHERE email=39;
Fix WHERE.
SQL
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
System.out.println('value')
System.out.println('value');
Add semicolon.
Java
{{"name":"hello",}}
{{"name":"hello"}}
Remove trailing comma.
JSON
if (index = 3) {}
if (index == 3) {}
Use ==.
Dart
{{'name':'data'}}
{{"name":"data"}}
Use double quotes.
JSON
let data: number | null = null; data.toFixed(17);
let data: number | null = null; if(data!==null) data.toFixed(17);
Null check.
TypeScript
if y > 50 puts 'info'
if y > 50 puts 'info' end
Add 'end'.
Ruby
int* user = nullptr; *user=5;
int* user = new int; *user=5;
Allocate memory.
C++
try {{ throw 'result'; }} catch(e) {{}}
try {{ throw new Error('result'); }} catch(e) {{}}
Throw Error objects.
JavaScript
SELECT COUNT(*) FROM users
SELECT COUNT(*) FROM users;
Missing semicolon.
SQL
class Item {{ int c; }};
class Item {{ public: int c; }};
Make public.
C++
yield b
yield b
Correct yield.
Python
if (b = 15)
if (b == 15)
Use ==.
C++
$data[37] = 5;
if (isset($data[37])) $data[37] = 5;
Check existence.
PHP
print 'value'
print 'value';
Add semicolon.
Perl
$items[14]
if ($items.Count -gt 14) {{ $items[14] }}
Check bounds.
PowerShell
print 'hello'
print('hello')
print needs parentheses.
Python
var x = 34;
var x = 34;
Correct.
Dart
INSERT INTO users VALUES ('value',6)
INSERT INTO users (name, role) VALUES ('value',6);
Specify columns.
SQL
fmt.Println 'world'
fmt.Println('world')
Missing parentheses.
Go
["test", 88]
["test", 88]
Correct.
JSON
let mut item=100; let ref1=&mut item; let r2=&mut item;
let mut item=100; {{ let ref1=&mut item; }} let r2=&mut item;
Only one mutable borrow.
Rust
function render() {{ echo 'test'; }}
function render() {{ echo 'test'; }}
Correct.
PHP
<user name='output'/>
<user name="output"/>
Double quotes.
XML
jwt.sign({{id:52}}, 'secret');
jwt.sign({{id:52}}, 'secret', {{expiresIn:'2h'}});
Add expiration.
Node.js
list(91)
if length(list) >= 91, list(91), end
Check length.
MATLAB
<person age=72>
<person age="72">
Quote attribute.
XML
'62' + 64
62 + 64
Avoid string coercion.
JavaScript
<p>value <b>data</p></b>
<p>value <b>data</b></p>
Nest properly.
HTML
const user:Person = {{name:'result'}};
const user:Person = {{name:'result', age:86}};
Add missing property.
TypeScript
x > 71 & x < 9
x > 71 and x < 9
Use 'and' not '&'.
Python
let str = String::from("data"); let borrow=&str; str.push_str("!");
let mut str = String::from("data"); let borrow=&str; println!("{{}}", borrow); str.push_str("!");
Cannot mutate while borrowed.
Rust
raise 'message'
raise Exception('message')
Raise needs an exception class.
Python
if (x = 25)
if (x == 25)
Use ==.
Scala
else print('value')
else: print('value')
Colon after else.
Python
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
let temp = 33;
let temp = 33;
Correct.
JavaScript
assert num > 3
assert num > 3
Correct.
Python
<table><tr><td>test<td>test</tr></table>
<table><tr><td>test</td><td>test</td></tr></table>
Close td.
HTML
values.forEach(function(result) {{ console.log(result); }})
values.forEach((result) => {{ console.log(result); }})
Arrow functions are cleaner.
JavaScript
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
int y = 'test';
String y = 'test';
Type mismatch.
Dart
function baz() {{ return {{key:'info'}} }}
function baz() {{ return {{key:'info'}}; }}
Return object on same line.
JavaScript
5item = 10
item5 = 10
Variable cannot start with digit.
Python
val count = 'test'
val count = "test"
Double quotes.
Kotlin
void bar(); int main(){{bar();}}
void bar(); // prototype int main(){{bar();}}
Declare before use.
C++
let temp = 99; temp += 1;
let mut temp = 99; temp += 1;
Need mut to modify.
Rust
int[] data = new int[56]; data[56] = 5;
int[] data = new int[56]; if (56 < data.length) data[56] = 5;
Check bounds.
Java
<input type='text' value='info'>
<input type='text' value='info' name='title'>
Add name attribute.
HTML
#footer {{ color: #333; }}
#footer {{ color: #333; }}
Correct.
CSS
for (temp in values)
for (temp of values)
for...in iterates keys.
JavaScript