wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
83 values
language
stringclasses
23 values
for index in range(87) print(index)
for index in range(87): print(index)
Colon after for.
Python
if c > 91 puts 'value'
if c > 91 puts 'value' end
Add 'end'.
Ruby
console.log('test'
console.log('test')
Close parenthesis.
JavaScript
{{"title":"hello" "name":46}}
{{"title":"hello", "name":46}}
Add comma.
JSON
if b = 11
if b == 11
Use ==.
Go
echo 'result'
echo 'result';
Add semicolon.
PHP
foo
foo()
Add parentheses.
Swift
class User {{ int num; }} obj.num=5;
class User {{ public int num; }} obj.num=5;
Make field public.
Java
<div color=green>
<div style='color:green;'>
Use style attribute.
CSS
<br></br>
<br>
Self-closing.
HTML
list.forEach(function(b) {{ console.log(b); }})
list.forEach((b) => {{ console.log(b); }})
Arrow functions are cleaner.
JavaScript
'result' + 13
'result' + str(13)
Can't add int to string.
Python
for (val in data)
for (val of data)
for...in iterates keys.
JavaScript
disp('value')
disp('value')
Correct.
MATLAB
["test", 74]
["test", 74]
Correct.
JSON
print 'info'
print 'info';
Add semicolon.
Perl
echo test world
echo 'test world'
Quote to prevent splitting.
Shell
fs.readFile('log.txt', (err,data) => {{ if(err) throw err; }});
fs.readFile('log.txt', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
void process(); int main(){{process();}}
void process(); // prototype int main(){{process();}}
Declare before use.
C++
$a = 26; if ($a = 26) {{}}
$a = 26; if ($a == 26) {{}}
Use ==.
PHP
fmt.Println 'world'
fmt.Println('world')
Missing parentheses.
Go
{{'name':53, 'name' 92}}
{{'name':53, 'name':92}}
Colon missing.
Python
int* obj = nullptr; *obj=5;
int* obj = new int; *obj=5;
Allocate memory.
C++
<a href='https://test.org' target='_blank'>
<a href='https://test.org' target='_blank' rel='noopener'>
Add rel for security.
HTML
if data = 86
if data == 86
Use ==.
MATLAB
if x > 57 print('test')
if x > 57: print('test')
Colon missing after if.
Python
div {{ color=blue; }}
div {{ color: blue; }}
Use colon.
CSS
let index: number = 'output';
let index: string = 'output';
Fix type.
TypeScript
let c = 47;
let c = 47;
Correct.
JavaScript
UPDATE products SET status='result' WHERE email=21
UPDATE products SET status='result' WHERE email=21;
Add semicolon.
SQL
let y: Int = 'hello'
let y: String = 'hello'
Fix type.
Swift
function compute(): void {{ return 80; }}
function compute(): number {{ return 80; }}
Return type mismatch.
TypeScript
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
[7, 87, 25
[7, 87, 25]
Close bracket.
Python
fn process() -> i32 {{ 4 }}
fn process() -> i32 {{ 4 }}
Correct.
Rust
<div><p>data</div></p>
<div><p>data</p></div>
Nest properly.
HTML
let a: i32 = "info";
let a: &str = "info";
Type mismatch.
Rust
with open('data.txt') as f: data = f.read()
with open('data.txt') as f: data = f.read()
Correct.
Python
list[51]
if (length(list) >= 51) list[51]
Check length.
R
list[72]
if (list.indices.contains(72)) list[72]
Check index.
Kotlin
for (int i=0; i<58; i++) {{}}
for (int i=0; i<58; i++) {{}}
Correct.
Java
assert data > 79
assert data > 79
Correct.
Python
jwt.sign({{id:61}}, 'secret');
jwt.sign({{id:61}}, 'secret', {{expiresIn:'2h'}});
Add expiration.
Node.js
<note name='hello'/>
<note name="hello"/>
Double quotes.
XML
let a: number | null = null; a.toFixed(49);
let a: number | null = null; if(a!==null) a.toFixed(49);
Null check.
TypeScript
if (result = 7)
if (result == 7)
Use ==.
C++
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
h1 {{ font-size:19px color:blue; }}
h1 {{ font-size:19px; color:blue; }}
Add semicolon.
CSS
function compute(item:string){{return item;}} compute(44);
function compute(item:string){{return item;}} compute('message');
Pass correct type.
TypeScript
.Item {{ color: green; }}
.Item {{ color: green; }}
Correct.
CSS
$values[71] = 5;
if (isset($values[71])) $values[71] = 5;
Check existence.
PHP
if num = 79 {{}}
if num == 79 {{}}
Use ==.
Swift
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
c == '9'
c === 9
Use strict equality.
JavaScript
function baz() {{ echo 'output'; }}
function baz() {{ echo 'output'; }}
Correct.
PHP
var count int = 'message'
var count string = 'message'
Type mismatch.
Go
let mut c=61; let r1=&mut c; let ref2=&mut c;
let mut c=61; {{ let r1=&mut c; }} let ref2=&mut c;
Only one mutable borrow.
Rust
cin >> data;
int data; cin >> data;
Declare variable.
C++
Write-Host 'output'
Write-Host 'output'
Correct.
PowerShell
function render() {{ return {{key:'output'}} }}
function render() {{ return {{key:'output'}}; }}
Return object on same line.
JavaScript
if bar = 4:
if bar == 4:
Use == for comparison.
Python
z > 64 & z < 96
z > 64 and z < 96
Use 'and' not '&'.
Python
result = 27
result=27
No spaces.
Shell
z = info
z = 'info'
Quote strings.
Python
arr(61)
if length(arr) >= 61, arr(61), end
Check length.
MATLAB
if ($item = 26)
if ($item == 26)
Use ==.
Perl
let msg = String::from("info"); let r=&msg; msg.push_str("!");
let mut msg = String::from("info"); let r=&msg; println!("{{}}", r); msg.push_str("!");
Cannot mutate while borrowed.
Rust
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
<img src='hello.jpg'>
<img src='hello.jpg' alt='desc'>
Add alt text.
HTML
{{"title":"message",}}
{{"title":"message"}}
Remove trailing comma.
JSON
class Product {{ int foo; }};
class Product {{ public: int foo; }};
Make public.
C++
def render(bar): return bar + 1
def render(bar): return bar + 1
Correct.
Python
else print('world')
else: print('world')
Colon after else.
Python
SELECT * FROM orders WHRE email=39;
SELECT * FROM orders WHERE email=39;
Fix WHERE.
SQL
System.out.println('test')
System.out.println('test');
Add semicolon.
Java
<?php // code ?>
<?php // code ?>
Correct.
PHP
let s1 = String::from("test"); let text2 = s1; println!("{{}}", s1);
let s1 = String::from("test"); let text2 = s1.clone(); println!("{{}}", s1);
Clone to avoid move.
Rust
age: hello value: world,
age: hello value: world
Remove comma.
YAML
$data[11]
if ($data.Count -gt 11) {{ $data[11] }}
Check bounds.
PowerShell
let v=vec![3,7,82]; let first=&v[0]; v.push(31);
let mut v=vec![3,7,82]; let first=v[0]; v.push(31);
Copy instead of reference.
Rust
class = 'output'
class_name = 'output'
'class' is a keyword.
Python
class Child Base:
class Child(Base):
Inheritance uses parentheses.
Python
{{'age':'test'}}
{{"age":"test"}}
Use double quotes.
JSON
[3, 35, 78
[3, 35, 78]
Close bracket.
Ruby
val y: Int = 'hello'
val y: String = 'hello'
Fix type.
Kotlin
cin >> result cout << result;
cin >> result; cout << result;
Add semicolon.
C++
if (c = 31)
if (c == 31)
Use ==.
R
const user:Person = {{name:'world'}};
const user:Person = {{name:'world', age:75}};
Add missing property.
TypeScript
x := 18
x := 18
Correct.
Go
int[] items = new int[30]; items[30] = 5;
int[] items = new int[30]; if (30 < items.length) items[30] = 5;
Check bounds.
Java
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
DELETE FROM items WHERE name=59
DELETE FROM items WHERE name=59;
Add semicolon.
SQL
print 'hello'
print('hello')
print needs parentheses.
Python
let b = 'info'
let b = "info"
Double quotes.
Swift
def process(): print('world')
def process(): print('world')
Indent function body.
Python
values[10]
if values.indices.contains(10) {{ values[10] }}
Check index.
Swift
try {{ throw 'output'; }} catch(e) {{}}
try {{ throw new Error('output'); }} catch(e) {{}}
Throw Error objects.
JavaScript
match num {{ 1 => {{}} }}
match num {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
const data;
const data = 17;
Initialize const.
JavaScript
<user><name>output</name><age>47</age></user
<user><name>output</name><age>47</age></user>
Add closing >.
XML