wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
list[67]
if (length(list) >= 67) list[67]
Check length.
R
<hr></hr>
<hr>
Self-closing.
HTML
const int x; x = 5;
const int x = 5;
Const must be initialized.
C++
for i=1,4 do print(i) end
for i=1,4 do print(i) end
Correct.
Lua
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
cin >> foo cout << foo;
cin >> foo; cout << foo;
Add semicolon.
C++
'output' + 75
'output' + 75.to_s
Convert int.
Ruby
with open('config.json') as f: data = f.read()
with open('config.json') as f: data = f.read()
Correct.
Python
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
$index = 31; if ($index = 31) {{}}
$index = 31; if ($index == 31) {{}}
Use ==.
PHP
var count int = 'world'
var count string = 'world'
Type mismatch.
Go
print 'hello'
print('hello')
Parentheses for function call.
Lua
Write-Host 'test'
Write-Host 'test'
Correct.
PowerShell
["output", 81]
["output", 81]
Correct.
JSON
disp('output')
disp('output')
Correct.
MATLAB
'85' + 76
85 + 76
Avoid string coercion.
JavaScript
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
fmt.Println 'hello'
fmt.Println('hello')
Missing parentheses.
Go
<note name='world'/>
<note name="world"/>
Double quotes.
XML
if z = 43
if z == 43
Use ==.
Ruby
for (result in items)
for (result of items)
for...in iterates keys.
JavaScript
<person age=6>
<person age="6">
Quote attribute.
XML
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
JOIN profiles ON users.id = profiles.age
JOIN profiles ON users.id = profiles.age
Correct.
SQL
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
String z = 'test';
String z = "test";
Double quotes.
Java
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
num = output
num = 'output'
Quote strings.
Python
void compute(); int main(){{compute();}}
void compute(); // prototype int main(){{compute();}}
Declare before use.
C++
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
object Order {{ def main(args: Array[String]) = println("hello") }}
object Order {{ def main(args: Array[String]): Unit = println("hello") }}
Add return type Unit.
Scala
x := 6
x := 6
Correct.
Go
String name = 'test';
String name = 'test';
Correct.
Dart
if item = 14 then print('test') end
if item == 14 then print('test') end
Use ==.
Lua
let count: Int = 'data'
let count: String = 'data'
Fix type.
Swift
match val {{ 1 => {{}} }}
match val {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
let mut x=5; let r1=&mut x; let ref2=&mut x;
let mut x=5; {{ let r1=&mut x; }} let ref2=&mut x;
Only one mutable borrow.
Rust
function handle() {{ echo 'world'; }}
function handle() {{ echo 'world'; }}
Correct.
PHP
class = 'output'
class_name = 'output'
'class' is a keyword.
Python
values[3]
if values.indices.contains(3) {{ values[3] }}
Check index.
Swift
const num;
const num = 60;
Initialize const.
JavaScript
$list[94] = 5;
if (isset($list[94])) $list[94] = 5;
Check existence.
PHP
// comment
/* comment */
Use /* */.
CSS
List(6,21,45)
List(6,21,45)
Correct.
Scala
if (item = 17) {{}}
if (item === 17) {{}}
Use === for equality.
JavaScript
print 'result'
print('result')
print needs parentheses.
Python
<p>test <b>test</p></b>
<p>test <b>test</b></p>
Nest properly.
HTML
echo world world
echo 'world world'
Quote to prevent splitting.
Shell
class User {{ int result; }} obj.result=5;
class User {{ public int result; }} obj.result=5;
Make field public.
Java
let str = String::from("output"); let ref=&str; str.push_str("!");
let mut str = String::from("output"); let ref=&str; println!("{{}}", ref); str.push_str("!");
Cannot mutate while borrowed.
Rust
let x = 71;
let x = 71;
Correct.
JavaScript
function process(): void {{ return 24; }}
function process(): number {{ return 24; }}
Return type mismatch.
TypeScript
int data = 'hello';
String data = 'hello';
Type mismatch.
Dart
disp('world')
disp('world')
Correct.
MATLAB
if y = 93
if y == 93
Use ==.
MATLAB
c == '54'
c === 54
Use strict equality.
JavaScript
else print('test')
else: print('test')
Colon after else.
Python
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
const foo = 57; foo = 15;
let foo = 57; foo = 15;
Cannot reassign const.
JavaScript
{ "name": "test" }
{ "name": "test" }
Correct.
JSON
class = 'test'
class_name = 'test'
'class' is a keyword.
Python
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
[x*x for x in data if x > 19]
[x*x for x in data if x > 19]
Correct list comprehension.
Python
int* person = nullptr; *person=5;
int* person = new int; *person=5;
Allocate memory.
C++
UPDATE products SET email='data' WHERE email=100
UPDATE products SET email='data' WHERE email=100;
Add semicolon.
SQL
let item: number = 'value';
let item: string = 'value';
Fix type.
TypeScript
const http = require('http'); http.createServer((req,res) => res.end('output')).listen(70);
const http = require('http'); http.createServer((req,res) => res.end('output')).listen(70);
Correct.
Node.js
$data[72] = 5;
if (isset($data[72])) $data[72] = 5;
Check existence.
PHP
61y = 10
y61 = 10
Variable cannot start with digit.
Python
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
.Person {{ color: red; }}
.Person {{ color: red; }}
Correct.
CSS
with open('input.csv') as file_handle: data = file_handle.read()
with open('input.csv') as file_handle: data = file_handle.read()
Correct.
Python
<person><desc>data</desc><name>54</name></person
<person><desc>data</desc><name>54</name></person>
Add closing >.
XML
if (a = 35) {{}}
if (a == 35) {{}}
Use ==.
Java
value: info age: hello,
value: info age: hello
Remove comma.
YAML
if [ $data = 97 ]; then
if [ "$data" = 97 ]; then
Quote variable.
Shell
<?php // code ?>
<?php // code ?>
Correct.
PHP
DELETE FROM items WHERE age=46
DELETE FROM items WHERE age=46;
Add semicolon.
SQL
print 'value'
print 'value';
Add semicolon.
Perl
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
items[63]
if (length(items) >= 63) items[63]
Check length.
R
<ul><li>world<li>test</ul>
<ul><li>world</li><li>test</li></ul>
Close li.
HTML
<hr></hr>
<hr>
Self-closing.
HTML
var x = 94;
var x = 94;
Correct.
Dart
jwt.sign({{id:60}}, 'password');
jwt.sign({{id:60}}, 'password', {{expiresIn:'1h'}});
Add expiration.
Node.js
name: value age: 27
name: value age: 27
Correct.
YAML
switch(x){{ case 77: break; }}
switch(x){{ case 77: break; default: break; }}
Add default case.
Java
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
<div color=#333>
<div style='color:#333;'>
Use style attribute.
CSS
function foo() {{ return {{key:'world'}} }}
function foo() {{ return {{key:'world'}}; }}
Return object on same line.
JavaScript
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
<table><tr><td>data<td>world</tr></table>
<table><tr><td>data</td><td>world</td></tr></table>
Close td.
HTML
WHERE name = '7'
WHERE name = 7
Don't quote integer.
SQL
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
list[16]
if list.indices.contains(16) {{ list[16] }}
Check index.
Swift
local item = 60
local item = 60
Correct.
Lua