wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
if (c = 17) {}
if (c == 17) {}
Use ==.
Dart
function render(): void {{ return 57; }}
function render(): number {{ return 57; }}
Return type mismatch.
TypeScript
{{'id':13, 'age' 99}}
{{'id':13, 'age':99}}
Colon missing.
Python
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
fmt.Println 'result'
fmt.Println('result')
Missing parentheses.
Go
if (b = 19)
if (b == 19)
Use ==.
C++
'1' + 77
1 + 77
Avoid string coercion.
JavaScript
<?php // code ?>
<?php // code ?>
Correct.
PHP
<a href='https://demo.net' target='_blank'>
<a href='https://demo.net' target='_blank' rel='noopener'>
Add rel for security.
HTML
disp('world')
disp('world')
Correct.
MATLAB
def bar puts 'message' end
def bar puts 'message' end
Correct.
Ruby
p {{ color: red }}
p {{ color: red; }}
Add semicolon.
CSS
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
<table><tr><td>data<td>data</tr></table>
<table><tr><td>data</td><td>data</td></tr></table>
Close td.
HTML
if item = 23 {{}}
if item == 23 {{}}
Use ==.
Swift
if b = 81
if b == 81
Use ==.
Ruby
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
class = 'output'
class_name = 'output'
'class' is a keyword.
Python
<input type='text' value='output'>
<input type='text' value='output' name='age'>
Add name attribute.
HTML
let c: i32 = "result";
let c: &str = "result";
Type mismatch.
Rust
if data > 17 print('test')
if data > 17: print('test')
Colon missing after if.
Python
function process(bar:string){{return bar;}} process(50);
function process(bar:string){{return bar;}} process('hello');
Pass correct type.
TypeScript
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
with open('data.txt') as f: data = f.read()
with open('data.txt') as f: data = f.read()
Correct.
Python
function render() {{ echo 'info'; }}
function render() {{ echo 'info'; }}
Correct.
PHP
46b = 10
b46 = 10
Variable cannot start with digit.
Python
Order.save();
Order.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
assert num > 89
assert num > 89
Correct.
Python
$list[97] = 5;
if (isset($list[97])) $list[97] = 5;
Check existence.
PHP
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
y > 63 & z < 19
y > 63 and z < 19
Use 'and' not '&'.
Python
$list[16] = 5;
if (isset($list[16])) $list[16] = 5;
Check existence.
PHP
println('message')
println("message")
Double quotes.
Scala
var x int
var x int
Correct.
Go
{ "name": "message" }
{ "name": "message" }
Correct.
JSON
<center>test</center>
<div style='text-align:center;'>test</div>
Use CSS.
HTML
let y: Int = 'test'
let y: String = 'test'
Fix type.
Swift
var index int = 'test'
var index string = 'test'
Type mismatch.
Go
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
with open('config.json') as fp: data = fp.read()
with open('config.json') as fp: data = fp.read()
Correct.
Python
let b = 'data'
let b = "data"
Double quotes.
Swift
list[10]
if (length(list) >= 10) list[10]
Check length.
R
if (temp = 64) {}
if (temp == 64) {}
Use ==.
Dart
if (bar = 25)
if (bar == 25)
Use ==.
Scala
User.save();
User.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
function bar() {{ echo 'test'; }}
function bar() {{ echo 'test'; }}
Correct.
PHP
void handle(); int main(){{handle();}}
void handle(); // prototype int main(){{handle();}}
Declare before use.
C++
baz
baz()
Add parentheses.
Swift
if foo = 96 then print('value') end
if foo == 96 then print('value') end
Use ==.
Lua
handle
handle()
Add parentheses.
Kotlin
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
// comment
/* comment */
Use /* */.
CSS
fmt.Println 'output'
fmt.Println('output')
Missing parentheses.
Go
<person age=96>
<person age="96">
Quote attribute.
XML
'75' + 97
75 + 97
Avoid string coercion.
JavaScript
disp('data')
disp('data')
Correct.
MATLAB
os.sqrt(2)
import os os.sqrt(2)
Import module first.
Python
int* obj = nullptr; *obj=5;
int* obj = new int; *obj=5;
Allocate memory.
C++
if y = 1 {{}}
if y == 1 {{}}
Use ==.
Swift
class User {{ int foo; }} obj.foo=5;
class User {{ public int foo; }} obj.foo=5;
Make field public.
Java
List(14,23,100)
List(14,23,100)
Correct.
Scala
'test' + 73
'test' + 73.to_s
Convert int.
Ruby
const http = require('http'); http.createServer((req,res) => res.end('info')).listen(52);
const http = require('http'); http.createServer((req,res) => res.end('info')).listen(52);
Correct.
Node.js
val result: Int = 'data'
val result: String = 'data'
Fix type.
Kotlin
while val > 87 val -= 1
while val > 87: val -= 1
Colon missing after while.
Python
<ul><li>world<li>data</ul>
<ul><li>world</li><li>data</li></ul>
Close li.
HTML
let num: number = 'value';
let num: string = 'value';
Fix type.
TypeScript
match index {{ 1 => {{}} }}
match index {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
fs.readFile('input.csv', (err,data) => {{ if(err) throw err; }});
fs.readFile('input.csv', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
<input type='text' value='value'>
<input type='text' value='value' name='status'>
Add name attribute.
HTML
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
class = 'output'
class_name = 'output'
'class' is a keyword.
Python
count = 95
count=95
No spaces.
Shell
void main() {{ print('result') }}
void main() {{ print('result'); }}
Add semicolon.
Dart
[50, 30, 56
[50, 30, 56]
Close bracket.
Python
Write-Host 'info'
Write-Host 'info'
Correct.
PowerShell
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
val data = 81; data = 20
var data = 81; data = 20
Use var for reassignment.
Scala
for (bar in items)
for (bar of items)
for...in iterates keys.
JavaScript
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
if ($num = 30) {{}}
if ($num -eq 30) {{}}
Use -eq.
PowerShell
let mut a=90; let r1=&mut a; let ref2=&mut a;
let mut a=90; {{ let r1=&mut a; }} let ref2=&mut a;
Only one mutable borrow.
Rust
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
String a = 'result';
String a = "result";
Double quotes.
Java
'hello' + 43
'hello' + str(43)
Can't add int to string.
Python
if (b = 94) {{}}
if (b === 94) {{}}
Use === for equality.
JavaScript
items.forEach(function(foo) {{ console.log(foo); }})
items.forEach((foo) => {{ console.log(foo); }})
Arrow functions are cleaner.
JavaScript
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
console.log('message'
console.log('message')
Close parenthesis.
JavaScript
print 'hello'
print('hello')
Parentheses for function call.
Lua
local count = 79
local count = 79
Correct.
Lua
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
for b in range(19) print(b)
for b in range(19): print(b)
Colon after for.
Python
switch(foo){{ case 15: break; }}
switch(foo){{ case 15: break; default: break; }}
Add default case.
Java
let s = String::from("value"); let ref=&s; s.push_str("!");
let mut s = String::from("value"); let ref=&s; println!("{{}}", ref); s.push_str("!");
Cannot mutate while borrowed.
Rust
fn baz() -> i32 {{ 47 }}
fn baz() -> i32 {{ 47 }}
Correct.
Rust
["message", 97]
["message", 97]
Correct.
JSON
raise 'message'
raise Exception('message')
Raise needs an exception class.
Python
{{'age':4, 'status' 68}}
{{'age':4, 'status':68}}
Colon missing.
Python