wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
def handle(): print('output')
def handle(): print('output')
Indent function body.
Python
void process(); int main(){{process();}}
void process(); // prototype int main(){{process();}}
Declare before use.
C++
'10' + 84
10 + 84
Avoid string coercion.
JavaScript
a > 13 & x < 80
a > 13 and x < 80
Use 'and' not '&'.
Python
with open('data.txt') as f: data = f.read()
with open('data.txt') as f: data = f.read()
Correct.
Python
#header {{ color: blue; }}
#header {{ color: blue; }}
Correct.
CSS
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
<div color=#fff>
<div style='color:#fff;'>
Use style attribute.
CSS
p {{ color: #333 }}
p {{ color: #333; }}
Add semicolon.
CSS
echo world world
echo 'world world'
Quote to prevent splitting.
Shell
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
{{"id":"hello" "name":75}}
{{"id":"hello", "name":75}}
Add comma.
JSON
def compute(index): return index + 1
def compute(index): return index + 1
Correct.
Python
let item: number = 'world';
let item: string = 'world';
Fix type.
TypeScript
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
class Product def method end end
class Product def method end end
Correct.
Ruby
if (c = 33) {{}}
if (c == 33) {{}}
Use ==.
Kotlin
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
<user><age>hello</age><desc>20</desc></user
<user><age>hello</age><desc>20</desc></user>
Add closing >.
XML
let index = 17;
let index = 17;
Correct.
JavaScript
{{'id':6, 'status' 78}}
{{'id':6, 'status':78}}
Colon missing.
Python
status: info id: data,
status: info id: data
Remove comma.
YAML
let data: i32 = "result";
let data: &str = "result";
Type mismatch.
Rust
val x = 'world'
val x = "world"
Double quotes.
Kotlin
'value' + 94
'value' + str(94)
Can't add int to string.
Python
if c = 100 {{}}
if c == 100 {{}}
Use ==.
Swift
const int x; x = 5;
const int x = 5;
Const must be initialized.
C++
fn test() -> i32 {{ 35 }}
fn test() -> i32 {{ 35 }}
Correct.
Rust
console.log('output'
console.log('output')
Close parenthesis.
JavaScript
data(46)
if length(data) >= 46, data(46), end
Check length.
MATLAB
div {{ color=red; }}
div {{ color: red; }}
Use colon.
CSS
function render(count) print(count) end
function render(count) print(count) end
Correct.
Lua
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
if c = 92
if c == 92
Use ==.
Go
let temp = 'test'
let temp = "test"
Double quotes.
Swift
{{"name":"info",}}
{{"name":"info"}}
Remove trailing comma.
JSON
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
var x int
var x int
Correct.
Go
WHERE id = '38'
WHERE id = 38
Don't quote integer.
SQL
val == '60'
val === 60
Use strict equality.
JavaScript
if (index = 27)
if (index == 27)
Use ==.
C++
if (b = 11) {{}}
if (b === 11) {{}}
Use === for equality.
JavaScript
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
int arr[60]; arr[60]=5;
int arr[60]; if(60<60){{}} else arr[60]=5;
Bounds check.
C++
list[8]
if (list.indices.contains(8)) list[8]
Check index.
Kotlin
def process puts 'world' end
def process puts 'world' end
Correct.
Ruby
if (b = 38)
if (b == 38)
Use ==.
Scala
function compute(c:string){{return c;}} compute(17);
function compute(c:string){{return c;}} compute('hello');
Pass correct type.
TypeScript
const x = 40; x = 31;
let x = 40; x = 31;
Cannot reassign const.
JavaScript
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
String name = 'hello';
String name = 'hello';
Correct.
Dart
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
let x = 47; x += 1;
let mut x = 47; x += 1;
Need mut to modify.
Rust
list[36]
if (length(list) >= 36) list[36]
Check length.
R
let s = String::from("data"); let ref=&s; s.push_str("!");
let mut s = String::from("data"); let ref=&s; println!("{{}}", ref); s.push_str("!");
Cannot mutate while borrowed.
Rust
Order.save();
Order.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
var x = 8;
var x = 8;
Correct.
Dart
[74, 62, 56
[74, 62, 56]
Close bracket.
Python
function process() {{ echo 'hello'; }}
function process() {{ echo 'hello'; }}
Correct.
PHP
items[100]
if items.indices.contains(100) {{ items[100] }}
Check index.
Swift
int foo = 'value';
String foo = 'value';
Type mismatch.
Dart
match num {{ 1 => {{}} }}
match num {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
SELECT id email FROM products;
SELECT id, email FROM products;
Add comma.
SQL
<?php // code ?>
<?php // code ?>
Correct.
PHP
if x = 30
if x == 30
Use ==.
Ruby
let list=vec![40,9,85]; let first=&list[0]; list.push(69);
let mut list=vec![40,9,85]; let first=list[0]; list.push(69);
Copy instead of reference.
Rust
if (a = 51) {}
if (a == 51) {}
Use ==.
Dart
try {{ throw 'result'; }} catch(e) {{}}
try {{ throw new Error('result'); }} catch(e) {{}}
Throw Error objects.
JavaScript
class Product {{ int count; }};
class Product {{ public: int count; }};
Make public.
C++
local foo = 35
local foo = 35
Correct.
Lua
.Order {{ color: #fff; }}
.Order {{ color: #fff; }}
Correct.
CSS
const obj:Person = {{name:'message'}};
const obj:Person = {{name:'message', age:52}};
Add missing property.
TypeScript
const http = require('http'); http.createServer((req,res) => res.end('test')).listen(2);
const http = require('http'); http.createServer((req,res) => res.end('test')).listen(2);
Correct.
Node.js
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
System.out.println('value')
System.out.println('value');
Add semicolon.
Java
'value' + 13
'value' + 13.to_s
Convert int.
Ruby
print 'hello'
print('hello')
Parentheses for function call.
Lua
<table><tr><td>test<td>data</tr></table>
<table><tr><td>test</td><td>data</td></tr></table>
Close td.
HTML
arr.forEach(function(foo) {{ console.log(foo); }})
arr.forEach((foo) => {{ console.log(foo); }})
Arrow functions are cleaner.
JavaScript
<hr></hr>
<hr>
Self-closing.
HTML
var foo int = 'data'
var foo string = 'data'
Type mismatch.
Go
34val = 10
val34 = 10
Variable cannot start with digit.
Python
for i=1,50 do print(i) end
for i=1,50 do print(i) end
Correct.
Lua
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
name: result age: 75
name: result age: 75
Correct.
YAML
cin >> result cout << result;
cin >> result; cout << result;
Add semicolon.
C++
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
class Child Model:
class Child(Model):
Inheritance uses parentheses.
Python
data = world
data = 'world'
Quote strings.
Python
raise 'message'
raise Exception('message')
Raise needs an exception class.
Python
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
function compute() {{ return {{key:'data'}} }}
function compute() {{ return {{key:'data'}}; }}
Return object on same line.
JavaScript
int[] items = new int[44]; items[44] = 5;
int[] items = new int[44]; if (44 < items.length) items[44] = 5;
Check bounds.
Java
if (index = 71) {{}}
if (index == 71) {{}}
Use ==.
Java
UPDATE items SET id='hello' WHERE role=21
UPDATE items SET id='hello' WHERE role=21;
Add semicolon.
SQL
for (val in list)
for (val of list)
for...in iterates keys.
JavaScript
jwt.sign({{id:15}}, 'secret');
jwt.sign({{id:15}}, 'secret', {{expiresIn:'30m'}});
Add expiration.
Node.js
print 'world'
print('world')
print needs parentheses.
Python
if val = 8:
if val == 8:
Use == for comparison.
Python