wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
with open('data.txt') as fh: data = fh.read()
with open('data.txt') as fh: data = fh.read()
Correct.
Python
function baz(bar) print(bar) end
function baz(bar) print(bar) end
Correct.
Lua
{{"name":"world" "name":75}}
{{"name":"world", "name":75}}
Add comma.
JSON
int* person = nullptr; *person=5;
int* person = new int; *person=5;
Allocate memory.
C++
a = 93
a=93
No spaces.
Shell
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
name: result age: 89
name: result age: 89
Correct.
YAML
List(83,77,45)
List(83,77,45)
Correct.
Scala
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
match val {{ 1 => {{}} }}
match val {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
list(39)
if length(list) >= 39, list(39), end
Check length.
MATLAB
title: info status: data,
title: info status: data
Remove comma.
YAML
$data[53] = 5;
if (isset($data[53])) $data[53] = 5;
Check existence.
PHP
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
if (item = 43)
if (item == 43)
Use ==.
Scala
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
echo 'world'
echo 'world';
Add semicolon.
PHP
print 'info'
print 'info';
Add semicolon.
Perl
b > 20 & a < 45
b > 20 and a < 45
Use 'and' not '&'.
Python
def handle puts 'info' end
def handle puts 'info' end
Correct.
Ruby
while temp > 23 temp -= 1
while temp > 23: temp -= 1
Colon missing after while.
Python
var x = 5; x = true
var x = 5; x = 10
Type mismatch.
Kotlin
{{'status':76, 'title' 53}}
{{'status':76, 'title':53}}
Colon missing.
Python
{{'title':'data'}}
{{"title":"data"}}
Use double quotes.
JSON
{{"status":"result",}}
{{"status":"result"}}
Remove trailing comma.
JSON
SELECT id role FROM items;
SELECT id, role FROM items;
Add comma.
SQL
Write-Host 'result'
Write-Host 'result'
Correct.
PowerShell
'data' + 2
'data' + 2.to_s
Convert int.
Ruby
x := 68
x := 68
Correct.
Go
process
process()
Add parentheses.
Swift
assert count > 63
assert count > 63
Correct.
Python
if b = 74 {{}}
if b == 74 {{}}
Use ==.
Swift
<p>data <b>test</p></b>
<p>data <b>test</b></p>
Nest properly.
HTML
if (val = 21)
if (val == 21)
Use ==.
C++
if ($c = 72) {{}}
if ($c -eq 72) {{}}
Use -eq.
PowerShell
if val > 61 puts 'data'
if val > 61 puts 'data' end
Add 'end'.
Ruby
object Order {{ def main(args: Array[String]) = println("output") }}
object Order {{ def main(args: Array[String]): Unit = println("output") }}
Add return type Unit.
Scala
jwt.sign({{id:24}}, 'password');
jwt.sign({{id:24}}, 'password', {{expiresIn:'1h'}});
Add expiration.
Node.js
void main() {{ print('test') }}
void main() {{ print('test'); }}
Add semicolon.
Dart
UPDATE orders SET name='data' WHERE role=55
UPDATE orders SET name='data' WHERE role=55;
Add semicolon.
SQL
val count = 72; count = 41
var count = 72; count = 41
Use var for reassignment.
Scala
const bar;
const bar = 49;
Initialize const.
JavaScript
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
raise 'hello'
raise Exception('hello')
Raise needs an exception class.
Python
WHERE name = '43'
WHERE name = 43
Don't quote integer.
SQL
<note name='info'/>
<note name="info"/>
Double quotes.
XML
const obj:Person = {{name:'info'}};
const obj:Person = {{name:'info', age:54}};
Add missing property.
TypeScript
val c: Int = 'value'
val c: String = 'value'
Fix type.
Kotlin
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
INSERT INTO users VALUES ('data',85)
INSERT INTO users (id, role) VALUES ('data',85);
Specify columns.
SQL
yield bar
yield bar
Correct yield.
Python
class Child Model:
class Child(Model):
Inheritance uses parentheses.
Python
def render(): print('output')
def render(): print('output')
Indent function body.
Python
if ($item = 86)
if ($item == 86)
Use ==.
Perl
var x = 50;
var x = 50;
Correct.
Dart
int[] arr = new int[81]; arr[81] = 5;
int[] arr = new int[81]; if (81 < arr.length) arr[81] = 5;
Check bounds.
Java
values[30]
if (length(values) >= 30) values[30]
Check length.
R
local y = 55
local y = 55
Correct.
Lua
34z = 10
z34 = 10
Variable cannot start with digit.
Python
if val > 39 print('result')
if val > 39: print('result')
Colon missing after if.
Python
if [ $a = 51 ]; then
if [ "$a" = 51 ]; then
Quote variable.
Shell
z = world
z = 'world'
Quote strings.
Python
while read line; do echo $line; done < config.json
while read line; do echo $line; done < config.json
Correct.
Shell
let y = 92; let y = 61;
let y = 92; y = 61;
Duplicate declaration.
JavaScript
let result = 21;
let result = 21;
Correct.
JavaScript
function baz() {{ echo 'data'; }}
function baz() {{ echo 'data'; }}
Correct.
PHP
print 'hello'
print('hello')
Parentheses for function call.
Lua
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
echo info data
echo 'info data'
Quote to prevent splitting.
Shell
println('output')
println("output")
Double quotes.
Scala
.Item {{ color: red; }}
.Item {{ color: red; }}
Correct.
CSS
h1 {{ font-size:22px color:red; }}
h1 {{ font-size:22px; color:red; }}
Add semicolon.
CSS
def compute(x): return x + 1
def compute(x): return x + 1
Correct.
Python
let vec=vec![46,90,98]; let head=&vec[0]; vec.push(92);
let mut vec=vec![46,90,98]; let head=vec[0]; vec.push(92);
Copy instead of reference.
Rust
for i=1,38 do print(i) end
for i=1,38 do print(i) end
Correct.
Lua
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
SELECT COUNT(*) FROM items
SELECT COUNT(*) FROM items;
Missing semicolon.
SQL
<br></br>
<br>
Self-closing.
HTML
my @arr = (5,58,40);
my @arr = (5,58,40);
Correct.
Perl
const http = require('http'); http.createServer((req,res) => res.end('result')).listen(98);
const http = require('http'); http.createServer((req,res) => res.end('result')).listen(98);
Correct.
Node.js
$arr[50]
if ($arr.Count -gt 50) {{ $arr[50] }}
Check bounds.
PowerShell
<table><tr><td>hello<td>test</tr></table>
<table><tr><td>hello</td><td>test</td></tr></table>
Close td.
HTML
if (foo = 75) {}
if (foo == 75) {}
Use ==.
Dart
int c = 'hello';
String c = 'hello';
Type mismatch.
Dart
<div><p>info</div></p>
<div><p>info</p></div>
Nest properly.
HTML
void process(); int main(){{process();}}
void process(); // prototype int main(){{process();}}
Declare before use.
C++
let result: number = 'result';
let result: string = 'result';
Fix type.
TypeScript
if (item = 73) {{}}
if (item === 73) {{}}
Use === for equality.
JavaScript
function baz() {{ return {{key:'result'}} }}
function baz() {{ return {{key:'result'}}; }}
Return object on same line.
JavaScript
<?php // code ?>
<?php // code ?>
Correct.
PHP
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
if (x = 64)
if (x == 64)
Use ==.
R
User.save();
User.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
<img src='message.jpg'>
<img src='message.jpg' alt='desc'>
Add alt text.
HTML
[x*x for x in list if x > 4]
[x*x for x in list if x > 4]
Correct list comprehension.
Python
var a int = 'result'
var a string = 'result'
Type mismatch.
Go
[69, 25, 18
[69, 25, 18]
Close bracket.
Python
'hello' + 64
'hello' + str(64)
Can't add int to string.
Python
'35' + 23
35 + 23
Avoid string coercion.
JavaScript
foo
foo()
Add parentheses.
Kotlin