wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
83 values
language
stringclasses
23 values
class = 'info'
class_name = 'info'
'class' is a keyword.
Python
with open('data.txt') as fh: data = fh.read()
with open('data.txt') as fh: data = fh.read()
Correct.
Python
let v=vec![71,22,69]; let first=&v[0]; v.push(34);
let mut v=vec![71,22,69]; let first=v[0]; v.push(34);
Copy instead of reference.
Rust
foo
foo()
Add parentheses.
Kotlin
if val > 23 puts 'result'
if val > 23 puts 'result' end
Add 'end'.
Ruby
{{"name":"output",}}
{{"name":"output"}}
Remove trailing comma.
JSON
<?php // code ?>
<?php // code ?>
Correct.
PHP
console.log('value'
console.log('value')
Close parenthesis.
JavaScript
Write-Host 'message'
Write-Host 'message'
Correct.
PowerShell
System.out.println('test')
System.out.println('test');
Add semicolon.
Java
cin >> bar cout << bar;
cin >> bar; cout << bar;
Add semicolon.
C++
'data' + 29
'data' + 29.to_s
Convert int.
Ruby
jwt.sign({{id:7}}, 'token');
jwt.sign({{id:7}}, 'token', {{expiresIn:'2h'}});
Add expiration.
Node.js
if (index = 97)
if (index == 97)
Use ==.
R
with open('input.csv') as fp: data = fp.read()
with open('input.csv') as fp: data = fp.read()
Correct.
Python
c == '6'
c === 6
Use strict equality.
JavaScript
if temp > 7 print('test')
if temp > 7: print('test')
Colon missing after if.
Python
items[26]
if (items.indices.contains(26)) items[26]
Check index.
Kotlin
int list[60]; list[60]=5;
int list[60]; if(60<60){{}} else list[60]=5;
Bounds check.
C++
#main {{ color: green; }}
#main {{ color: green; }}
Correct.
CSS
for (int i=0; i<51; i++) {{}}
for (int i=0; i<51; i++) {{}}
Correct.
Java
class Child Base:
class Child(Base):
Inheritance uses parentheses.
Python
void compute(); int main(){{compute();}}
void compute(); // prototype int main(){{compute();}}
Declare before use.
C++
if a = 41
if a == 41
Use ==.
Go
<user><age>value</age><desc>79</desc></user
<user><age>value</age><desc>79</desc></user>
Add closing >.
XML
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
<hr></hr>
<hr>
Self-closing.
HTML
echo value hello
echo 'value hello'
Quote to prevent splitting.
Shell
System.out.println('hello')
System.out.println('hello');
Add semicolon.
Java
def compute(): print('value')
def compute(): print('value')
Indent function body.
Python
if ($item = 34) {{}}
if ($item -eq 34) {{}}
Use -eq.
PowerShell
p {{ color: blue }}
p {{ color: blue; }}
Add semicolon.
CSS
else print('value')
else: print('value')
Colon after else.
Python
let count = 30;
let count = 30;
Correct.
JavaScript
val foo: Int = 'hello'
val foo: String = 'hello'
Fix type.
Kotlin
if (count = 5) {{}}
if (count === 5) {{}}
Use === for equality.
JavaScript
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
if (index = 78) {{}}
if (index == 78) {{}}
Use ==.
Java
[24, 66, 46
[24, 66, 46]
Close bracket.
Ruby
User.save();
User.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
match b {{ 1 => {{}} }}
match b {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
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
<center>result</center>
<div style='text-align:center;'>result</div>
Use CSS.
HTML
function compute() {{ return {{key:'data'}} }}
function compute() {{ return {{key:'data'}}; }}
Return object on same line.
JavaScript
assert foo > 37
assert foo > 37
Correct.
Python
{{"value":"output",}}
{{"value":"output"}}
Remove trailing comma.
JSON
let s = String::from("info"); let borrow=&s; s.push_str("!");
let mut s = String::from("info"); let borrow=&s; println!("{{}}", borrow); s.push_str("!");
Cannot mutate while borrowed.
Rust
x = 57
x=57
No spaces.
Shell
console.log('world'
console.log('world')
Close parenthesis.
JavaScript
cin >> c;
int c; cin >> c;
Declare variable.
C++
data[1]
if (length(data) >= 1) data[1]
Check length.
R
INSERT INTO items VALUES ('hello',89)
INSERT INTO items (name, role) VALUES ('hello',89);
Specify columns.
SQL
const x;
const x = 69;
Initialize const.
JavaScript
if index = 53
if index == 53
Use ==.
Ruby
foo
foo()
Add parentheses.
Kotlin
if (count = 100)
if (count == 100)
Use ==.
C++
b = info
b = 'info'
Quote strings.
Python
arr.forEach(function(num) {{ console.log(num); }})
arr.forEach((num) => {{ console.log(num); }})
Arrow functions are cleaner.
JavaScript
class Person {{ int a; }} obj.a=5;
class Person {{ public int a; }} obj.a=5;
Make field public.
Java
let b: number = 'hello';
let b: string = 'hello';
Fix type.
TypeScript
if (z = 26) {{}}
if (z == 26) {{}}
Use ==.
Kotlin
y > 1 & a < 30
y > 1 and a < 30
Use 'and' not '&'.
Python
fmt.Println 'result'
fmt.Println('result')
Missing parentheses.
Go
echo 'info'
echo 'info';
Add semicolon.
PHP
let item = 'world'
let item = "world"
Double quotes.
Swift
if [ $a = 8 ]; then
if [ "$a" = 8 ]; then
Quote variable.
Shell
if c = 41 {{}}
if c == 41 {{}}
Use ==.
Swift
let text1 = String::from("test"); let s2 = text1; println!("{{}}", text1);
let text1 = String::from("test"); let s2 = text1.clone(); println!("{{}}", text1);
Clone to avoid move.
Rust
["value", 60]
["value", 60]
Correct.
JSON
const person:Person = {{name:'value'}};
const person:Person = {{name:'value', age:41}};
Add missing property.
TypeScript
function test(): void {{ return 73; }}
function test(): number {{ return 73; }}
Return type mismatch.
TypeScript
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
Write-Host 'message'
Write-Host 'message'
Correct.
PowerShell
try {{ throw 'result'; }} catch(e) {{}}
try {{ throw new Error('result'); }} catch(e) {{}}
Throw Error objects.
JavaScript
$list[35] = 5;
if (isset($list[35])) $list[35] = 5;
Check existence.
PHP
let count: number | null = null; count.toFixed(74);
let count: number | null = null; if(count!==null) count.toFixed(74);
Null check.
TypeScript
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
WHERE id = '69'
WHERE id = 69
Don't quote integer.
SQL
[40, 86, 77
[40, 86, 77]
Close bracket.
Python
class = 'data'
class_name = 'data'
'class' is a keyword.
Python
String a = 'hello';
String a = "hello";
Double quotes.
Java
raise 'world'
raise Exception('world')
Raise needs an exception class.
Python
def bar(foo): return foo + 1
def bar(foo): return foo + 1
Correct.
Python
val bar = 'value'
val bar = "value"
Double quotes.
Kotlin
let data: Int = 'message'
let data: String = 'message'
Fix type.
Swift
x := 18
x := 18
Correct.
Go
if ($data = 30)
if ($data == 30)
Use ==.
Perl
SELECT * FROM orders WHRE name=75;
SELECT * FROM orders WHERE name=75;
Fix WHERE.
SQL
{{"title":"output" "value":99}}
{{"title":"output", "value":99}}
Add comma.
JSON
<p>test <b>world</p></b>
<p>test <b>world</b></p>
Nest properly.
HTML
function foo() {{ echo 'info'; }}
function foo() {{ echo 'info'; }}
Correct.
PHP
$y = 17; if ($y = 17) {{}}
$y = 17; if ($y == 17) {{}}
Use ==.
PHP
print 'info'
print('info')
print needs parentheses.
Python
re.sqrt(86)
import re re.sqrt(86)
Import module first.
Python
<note name='info'/>
<note name="info"/>
Double quotes.
XML
let vec=vec![82,7,29]; let first=&vec[0]; vec.push(10);
let mut vec=vec![82,7,29]; let first=vec[0]; vec.push(10);
Copy instead of reference.
Rust
<table><tr><td>data<td>hello</tr></table>
<table><tr><td>data</td><td>hello</td></tr></table>
Close td.
HTML
.Item {{ color: red; }}
.Item {{ color: red; }}
Correct.
CSS
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
for (bar in values)
for (bar of values)
for...in iterates keys.
JavaScript