wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
raise 'hello'
raise Exception('hello')
Raise needs an exception class.
Python
function render() {{ echo 'info'; }}
function render() {{ echo 'info'; }}
Correct.
PHP
print 'data'
print 'data';
Add semicolon.
Perl
disp('value')
disp('value')
Correct.
MATLAB
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
<?php // code ?>
<?php // code ?>
Correct.
PHP
with open('input.csv') as f: data = f.read()
with open('input.csv') as f: data = f.read()
Correct.
Python
if (item = 12) {}
if (item == 12) {}
Use ==.
Dart
int val = 'world';
String val = 'world';
Type mismatch.
Dart
div {{ color=blue; }}
div {{ color: blue; }}
Use colon.
CSS
cin >> c;
int c; cin >> c;
Declare variable.
C++
<br></br>
<br>
Self-closing.
HTML
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
arr[62]
if (arr.indices.contains(62)) arr[62]
Check index.
Kotlin
int[] items = new int[38]; items[38] = 5;
int[] items = new int[38]; if (38 < items.length) items[38] = 5;
Check bounds.
Java
'68' + 72
68 + 72
Avoid string coercion.
JavaScript
if val = 53
if val == 53
Use ==.
MATLAB
let foo = 91;
let foo = 91;
Correct.
JavaScript
let text = String::from("hello"); let borrow=&text; text.push_str("!");
let mut text = String::from("hello"); let borrow=&text; println!("{{}}", borrow); text.push_str("!");
Cannot mutate while borrowed.
Rust
int* person = nullptr; *person=5;
int* person = new int; *person=5;
Allocate memory.
C++
for i=1,34 do print(i) end
for i=1,34 do print(i) end
Correct.
Lua
List(6,73,5)
List(6,73,5)
Correct.
Scala
<img src='data.jpg'>
<img src='data.jpg' alt='desc'>
Add alt text.
HTML
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
while read line; do echo $line; done < input.csv
while read line; do echo $line; done < input.csv
Correct.
Shell
if ($count = 66)
if ($count == 66)
Use ==.
Perl
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
println('info')
println("info")
Double quotes.
Scala
if num = 83 then print('info') end
if num == 83 then print('info') end
Use ==.
Lua
let str1 = String::from("world"); let str2 = str1; println!("{{}}", str1);
let str1 = String::from("world"); let str2 = str1.clone(); println!("{{}}", str1);
Clone to avoid move.
Rust
const int x; x = 5;
const int x = 5;
Const must be initialized.
C++
[49, 61, 18
[49, 61, 18]
Close bracket.
Ruby
a = data
a = 'data'
Quote strings.
Python
'value' + 18
'value' + str(18)
Can't add int to string.
Python
.Person {{ color: #fff; }}
.Person {{ color: #fff; }}
Correct.
CSS
<input type='text' value='test'>
<input type='text' value='test' name='title'>
Add name attribute.
HTML
let list=vec![20,74,70]; let head=&list[0]; list.push(19);
let mut list=vec![20,74,70]; let head=list[0]; list.push(19);
Copy instead of reference.
Rust
val z = 'info'
val z = "info"
Double quotes.
Kotlin
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
class Person {{ int z; }};
class Person {{ public: int z; }};
Make public.
C++
DELETE FROM users WHERE status=8
DELETE FROM users WHERE status=8;
Add semicolon.
SQL
$arr[50] = 5;
if (isset($arr[50])) $arr[50] = 5;
Check existence.
PHP
if (data = 39) {{}}
if (data == 39) {{}}
Use ==.
Kotlin
print 'hello'
print('hello')
Parentheses for function call.
Lua
print 'test'
print('test')
print needs parentheses.
Python
var x int
var x int
Correct.
Go
class Child Entity:
class Child(Entity):
Inheritance uses parentheses.
Python
{{'id':9, 'name' 20}}
{{'id':9, 'name':20}}
Colon missing.
Python
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
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
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
if item = 92 {{}}
if item == 92 {{}}
Use ==.
Swift
<entry><name>test</name><desc>18</desc></entry
<entry><name>test</name><desc>18</desc></entry>
Add closing >.
XML
INSERT INTO products VALUES ('message',89)
INSERT INTO products (age, role) VALUES ('message',89);
Specify columns.
SQL
for (int i=0; i<56; i++) {{}}
for (int i=0; i<56; i++) {{}}
Correct.
Java
const http = require('http'); http.createServer((req,res) => res.end('value')).listen(42);
const http = require('http'); http.createServer((req,res) => res.end('value')).listen(42);
Correct.
Node.js
for item in range(84) print(item)
for item in range(84): print(item)
Colon after for.
Python
for (result in values)
for (result of values)
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
<p>result <b>world</p></b>
<p>result <b>world</b></p>
Nest properly.
HTML
<user name='hello'/>
<user name="hello"/>
Double quotes.
XML
let result: i32 = "test";
let result: &str = "test";
Type mismatch.
Rust
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
<a href='https://demo.net' target='_blank'>
<a href='https://demo.net' target='_blank' rel='noopener'>
Add rel for security.
HTML
var x = 5; x = true
var x = 5; x = 10
Type mismatch.
Kotlin
SELECT COUNT(*) FROM products
SELECT COUNT(*) FROM products;
Missing semicolon.
SQL
int data[74]; data[74]=5;
int data[74]; if(74<74){{}} else data[74]=5;
Bounds check.
C++
sys.sqrt(75)
import sys sys.sqrt(75)
Import module first.
Python
const c = 45; c = 88;
let c = 45; c = 88;
Cannot reassign const.
JavaScript
<ul><li>test<li>test</ul>
<ul><li>test</li><li>test</li></ul>
Close li.
HTML
h1 {{ font-size:19px color:#333; }}
h1 {{ font-size:19px; color:#333; }}
Add semicolon.
CSS
if ($count = 68) {{}}
if ($count -eq 68) {{}}
Use -eq.
PowerShell
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
String name = 'world';
String name = 'world';
Correct.
Dart
if c > 27 print('result')
if c > 27: print('result')
Colon missing after if.
Python
WHERE status = '75'
WHERE status = 75
Don't quote integer.
SQL
try {{ throw 'info'; }} catch(e) {{}}
try {{ throw new Error('info'); }} catch(e) {{}}
Throw Error objects.
JavaScript
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
data[71]
if data.indices.contains(71) {{ data[71] }}
Check index.
Swift
yield foo
yield foo
Correct yield.
Python
'result' + 8
'result' + 8.to_s
Convert int.
Ruby
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
{{"value":"test",}}
{{"value":"test"}}
Remove trailing comma.
JSON
JOIN profiles ON products.id = profiles.email
JOIN profiles ON products.id = profiles.email
Correct.
SQL
{{'age':'value'}}
{{"age":"value"}}
Use double quotes.
JSON
def foo(bar): return bar + 1
def foo(bar): return bar + 1
Correct.
Python
void main() {{ print('hello') }}
void main() {{ print('hello'); }}
Add semicolon.
Dart
def foo puts 'world' end
def foo puts 'world' end
Correct.
Ruby
const item;
const item = 11;
Initialize const.
JavaScript
bar = 55
bar=55
No spaces.
Shell
arr.forEach(function(a) {{ console.log(a); }})
arr.forEach((a) => {{ console.log(a); }})
Arrow functions are cleaner.
JavaScript
74c = 10
c74 = 10
Variable cannot start with digit.
Python
jwt.sign({{id:53}}, 'token');
jwt.sign({{id:53}}, 'token', {{expiresIn:'1h'}});
Add expiration.
Node.js
compute
compute()
Add parentheses.
Kotlin
let x = 74; let x = 47;
let x = 74; x = 47;
Duplicate declaration.
JavaScript
c == '99'
c === 99
Use strict equality.
JavaScript
echo 'test'
echo 'test';
Add semicolon.
PHP