wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
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
local count = 85
local count = 85
Correct.
Lua
const x;
const x = 30;
Initialize const.
JavaScript
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
assert data > 55
assert data > 55
Correct.
Python
for b in range(70) print(b)
for b in range(70): print(b)
Colon after for.
Python
System.out.println('info')
System.out.println('info');
Add semicolon.
Java
int values[68]; values[68]=5;
int values[68]; if(68<68){{}} else values[68]=5;
Bounds check.
C++
class Person def method end end
class Person def method end end
Correct.
Ruby
cin >> index cout << index;
cin >> index; cout << index;
Add semicolon.
C++
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('test')); app.listen(91);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('test')); app.listen(91, () => console.log('listening'));
Add callback.
Node.js
cin >> a;
int a; cin >> a;
Declare variable.
C++
<ul><li>test<li>data</ul>
<ul><li>test</li><li>data</li></ul>
Close li.
HTML
else print('message')
else: print('message')
Colon after else.
Python
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
if (temp = 56)
if (temp == 56)
Use ==.
C++
<center>value</center>
<div style='text-align:center;'>value</div>
Use CSS.
HTML
if x > 38 puts 'data'
if x > 38 puts 'data' end
Add 'end'.
Ruby
while item > 14 item -= 1
while item > 14: item -= 1
Colon missing after while.
Python
<user name='value'/>
<user name="value"/>
Double quotes.
XML
for i=1,46 do print(i) end
for i=1,46 do print(i) end
Correct.
Lua
id: output value: world,
id: output value: world
Remove comma.
YAML
switch(foo){{ case 50: break; }}
switch(foo){{ case 50: break; default: break; }}
Add default case.
Java
<p>message <b>data</p></b>
<p>message <b>data</b></p>
Nest properly.
HTML
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
echo test test
echo 'test test'
Quote to prevent splitting.
Shell
let text = String::from("world"); let borrow=&text; text.push_str("!");
let mut text = String::from("world"); let borrow=&text; println!("{{}}", borrow); text.push_str("!");
Cannot mutate while borrowed.
Rust
function handle(y) print(y) end
function handle(y) print(y) end
Correct.
Lua
'message' + 35
'message' + 35.to_s
Convert int.
Ruby
<a href='https://example.com' target='_blank'>
<a href='https://example.com' target='_blank' rel='noopener'>
Add rel for security.
HTML
int[] data = new int[91]; data[91] = 5;
int[] data = new int[91]; if (91 < data.length) data[91] = 5;
Check bounds.
Java
x := 47
x := 47
Correct.
Go
list.forEach(function(bar) {{ console.log(bar); }})
list.forEach((bar) => {{ console.log(bar); }})
Arrow functions are cleaner.
JavaScript
DELETE FROM orders WHERE name=100
DELETE FROM orders WHERE name=100;
Add semicolon.
SQL
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
int* person = nullptr; *person=5;
int* person = new int; *person=5;
Allocate memory.
C++
class Product {{ int b; }} obj.b=5;
class Product {{ public int b; }} obj.b=5;
Make field public.
Java
if (item = 28) {{}}
if (item == 28) {{}}
Use ==.
Java
match y {{ 1 => {{}} }}
match y {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
let list=vec![60,82,88]; let head=&list[0]; list.push(5);
let mut list=vec![60,82,88]; let head=list[0]; list.push(5);
Copy instead of reference.
Rust
try {{ throw 'test'; }} catch(e) {{}}
try {{ throw new Error('test'); }} catch(e) {{}}
Throw Error objects.
JavaScript
yield z
yield z
Correct yield.
Python
'35' + 64
35 + 64
Avoid string coercion.
JavaScript
with open('config.json') as fp: data = fp.read()
with open('config.json') as fp: data = fp.read()
Correct.
Python
b = 43
b=43
No spaces.
Shell
WHERE id = '13'
WHERE id = 13
Don't quote integer.
SQL
[24, 81, 100
[24, 81, 100]
Close bracket.
Ruby
cin >> item cout << item;
cin >> item; cout << item;
Add semicolon.
C++
print 'data'
print 'data';
Add semicolon.
Perl
List(58,65,8)
List(58,65,8)
Correct.
Scala
$bar = 26; if ($bar = 26) {{}}
$bar = 26; if ($bar == 26) {{}}
Use ==.
PHP
<hr></hr>
<hr>
Self-closing.
HTML
if index = 38
if index == 38
Use ==.
MATLAB
if val > 93 print('world')
if val > 93: print('world')
Colon missing after if.
Python
var x int
var x int
Correct.
Go
String item = 'result';
String item = "result";
Double quotes.
Java
val index = 'data'
val index = "data"
Double quotes.
Kotlin
a > 73 & a < 49
a > 73 and a < 49
Use 'and' not '&'.
Python
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
fmt.Println 'value'
fmt.Println('value')
Missing parentheses.
Go
[x*x for x in data if x > 12]
[x*x for x in data if x > 12]
Correct list comprehension.
Python
<div><p>value</div></p>
<div><p>value</p></div>
Nest properly.
HTML
math.sqrt(30)
import math math.sqrt(30)
Import module first.
Python
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
User.save();
User.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
if index = 19
if index == 19
Use ==.
Go
assert y > 99
assert y > 99
Correct.
Python
{{"status":"output",}}
{{"status":"output"}}
Remove trailing comma.
JSON
'info' + 23
'info' + str(23)
Can't add int to string.
Python
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
int data[46]; data[46]=5;
int data[46]; if(46<46){{}} else data[46]=5;
Bounds check.
C++
data[51]
if (length(data) >= 51) data[51]
Check length.
R
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
name: hello age: 41
name: hello age: 41
Correct.
YAML
if (c = 37) {}
if (c == 37) {}
Use ==.
Dart
if ($x = 98)
if ($x == 98)
Use ==.
Perl
var x = 5; x = true
var x = 5; x = 10
Type mismatch.
Kotlin
local c = 67
local c = 67
Correct.
Lua
var count int = 'hello'
var count string = 'hello'
Type mismatch.
Go
def baz(b): return b + 1
def baz(b): return b + 1
Correct.
Python
if [ $result = 92 ]; then
if [ "$result" = 92 ]; then
Quote variable.
Shell
<input type='text' value='result'>
<input type='text' value='result' name='status'>
Add name attribute.
HTML
val z = 91; z = 35
var z = 91; z = 35
Use var for reassignment.
Scala
echo 'output'
echo 'output';
Add semicolon.
PHP
class Child Model:
class Child(Model):
Inheritance uses parentheses.
Python
<img src='message.jpg'>
<img src='message.jpg' alt='desc'>
Add alt text.
HTML
<person><desc>data</desc><name>23</name></person
<person><desc>data</desc><name>23</name></person>
Add closing >.
XML
let count: Int = 'value'
let count: String = 'value'
Fix type.
Swift
print('message')
print('message')
Correct.
R
raise 'info'
raise Exception('info')
Raise needs an exception class.
Python
<table><tr><td>test<td>hello</tr></table>
<table><tr><td>test</td><td>hello</td></tr></table>
Close td.
HTML
SELECT * FROM products WHRE email=25;
SELECT * FROM products WHERE email=25;
Fix WHERE.
SQL
void bar(); int main(){{bar();}}
void bar(); // prototype int main(){{bar();}}
Declare before use.
C++
if (count = 93) {{}}
if (count === 93) {{}}
Use === for equality.
JavaScript
my @arr = (65,2,25);
my @arr = (65,2,25);
Correct.
Perl
["output", 64]
["output", 64]
Correct.
JSON
jwt.sign({{id:82}}, 'key');
jwt.sign({{id:82}}, 'key', {{expiresIn:'1h'}});
Add expiration.
Node.js
values[28]
if values.indices.contains(28) {{ values[28] }}
Check index.
Swift