wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
83 values
language
stringclasses
23 values
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
x > 51 & z < 96
x > 51 and z < 96
Use 'and' not '&'.
Python
if item = 74:
if item == 74:
Use == for comparison.
Python
Write-Host 'hello'
Write-Host 'hello'
Correct.
PowerShell
WHERE email = '34'
WHERE email = 34
Don't quote integer.
SQL
val y: Int = 'test'
val y: String = 'test'
Fix type.
Kotlin
match y {{ 1 => {{}} }}
match y {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
<ul><li>test<li>world</ul>
<ul><li>test</li><li>world</li></ul>
Close li.
HTML
<div><p>hello</div></p>
<div><p>hello</p></div>
Nest properly.
HTML
if z = 18
if z == 18
Use ==.
MATLAB
{{'id':66, 'title' 30}}
{{'id':66, 'title':30}}
Colon missing.
Python
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('world')); app.listen(97);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('world')); app.listen(97, () => console.log('listening'));
Add callback.
Node.js
arr(12)
if length(arr) >= 12, arr(12), end
Check length.
MATLAB
String item = 'output';
String item = "output";
Double quotes.
Java
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
'68' + 20
68 + 20
Avoid string coercion.
JavaScript
disp('data')
disp('data')
Correct.
MATLAB
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
sys.sqrt(18)
import sys sys.sqrt(18)
Import module first.
Python
function render(): void {{ return 9; }}
function render(): number {{ return 9; }}
Return type mismatch.
TypeScript
function bar() {{ echo 'test'; }}
function bar() {{ echo 'test'; }}
Correct.
PHP
if (result = 35) {{}}
if (result == 35) {{}}
Use ==.
Java
// comment
/* comment */
Use /* */.
CSS
$list[77] = 5;
if (isset($list[77])) $list[77] = 5;
Check existence.
PHP
SELECT id email FROM products;
SELECT id, email FROM products;
Add comma.
SQL
let v=vec![11,14,29]; let first=&v[0]; v.push(22);
let mut v=vec![11,14,29]; let first=v[0]; v.push(22);
Copy instead of reference.
Rust
INSERT INTO users VALUES ('hello',14)
INSERT INTO users (name, email) VALUES ('hello',14);
Specify columns.
SQL
try {{ throw 'test'; }} catch(e) {{}}
try {{ throw new Error('test'); }} catch(e) {{}}
Throw Error objects.
JavaScript
<person name='output'/>
<person name="output"/>
Double quotes.
XML
div {{ color=#333; }}
div {{ color: #333; }}
Use colon.
CSS
{{"id":"data",}}
{{"id":"data"}}
Remove trailing comma.
JSON
for data in range(27) print(data)
for data in range(27): print(data)
Colon after for.
Python
print 'message'
print 'message';
Add semicolon.
Perl
p {{ color: red }}
p {{ color: red; }}
Add semicolon.
CSS
let z: number | null = null; z.toFixed(6);
let z: number | null = null; if(z!==null) z.toFixed(6);
Null check.
TypeScript
if [ $y = 45 ]; then
if [ "$y" = 45 ]; then
Quote variable.
Shell
cin >> foo cout << foo;
cin >> foo; cout << foo;
Add semicolon.
C++
<img src='hello.jpg'>
<img src='hello.jpg' alt='desc'>
Add alt text.
HTML
SELECT * FROM orders WHRE email=57;
SELECT * FROM orders WHERE email=57;
Fix WHERE.
SQL
let item: Int = 'value'
let item: String = 'value'
Fix type.
Swift
{{"name":"info" "status":39}}
{{"name":"info", "status":39}}
Add comma.
JSON
arr[31]
if (arr.indices.contains(31)) arr[31]
Check index.
Kotlin
let x = 57;
let x = 57;
Correct.
JavaScript
let text1 = String::from("world"); let text2 = text1; println!("{{}}", text1);
let text1 = String::from("world"); let text2 = text1.clone(); println!("{{}}", text1);
Clone to avoid move.
Rust
my @arr = (66,62,98);
my @arr = (66,62,98);
Correct.
Perl
let mut c=4; let r1=&mut c; let r2=&mut c;
let mut c=4; {{ let r1=&mut c; }} let r2=&mut c;
Only one mutable borrow.
Rust
Product.save();
Product.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
[3, 32, 16
[3, 32, 16]
Close bracket.
Python
if data > 51 puts 'result'
if data > 51 puts 'result' end
Add 'end'.
Ruby
["message", 12]
["message", 12]
Correct.
JSON
int values[3]; values[3]=5;
int values[3]; if(3<3){{}} else values[3]=5;
Bounds check.
C++
def bar(): print('data')
def bar(): print('data')
Indent function body.
Python
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
<?php // code ?>
<?php // code ?>
Correct.
PHP
arr[13]
if (length(arr) >= 13) arr[13]
Check length.
R
for (b in data)
for (b of data)
for...in iterates keys.
JavaScript
data = test
data = 'test'
Quote strings.
Python
DELETE FROM items WHERE id=21
DELETE FROM items WHERE id=21;
Add semicolon.
SQL
echo value test
echo 'value test'
Quote to prevent splitting.
Shell
assert index > 42
assert index > 42
Correct.
Python
$y = 80; if ($y = 80) {{}}
$y = 80; if ($y == 80) {{}}
Use ==.
PHP
class Child Entity:
class Child(Entity):
Inheritance uses parentheses.
Python
function foo(a:string){{return a;}} foo(10);
function foo(a:string){{return a;}} foo('result');
Pass correct type.
TypeScript
var foo int = 'data'
var foo string = 'data'
Type mismatch.
Go
print('result')
print('result')
Correct.
R
data.forEach(function(z) {{ console.log(z); }})
data.forEach((z) => {{ console.log(z); }})
Arrow functions are cleaner.
JavaScript
print 'world'
print('world')
print needs parentheses.
Python
10y = 10
y10 = 10
Variable cannot start with digit.
Python
let temp: number = 'hello';
let temp: string = 'hello';
Fix type.
TypeScript
bar
bar()
Add parentheses.
Swift
let result: i32 = "data";
let result: &str = "data";
Type mismatch.
Rust
if b = 86
if b == 86
Use ==.
Go
if (c = 56) {{}}
if (c == 56) {{}}
Use ==.
Kotlin
if (foo = 53)
if (foo == 53)
Use ==.
R
<div color=#fff>
<div style='color:#fff;'>
Use style attribute.
CSS
x := 17
x := 17
Correct.
Go
{{'title':5, 'id' 42}}
{{'title':5, 'id':42}}
Colon missing.
Python
if (temp = 80) {{}}
if (temp === 80) {{}}
Use === for equality.
JavaScript
if [ $z = 45 ]; then
if [ "$z" = 45 ]; then
Quote variable.
Shell
h1 {{ font-size:5px color:#fff; }}
h1 {{ font-size:5px; color:#fff; }}
Add semicolon.
CSS
my @arr = (77,44,77);
my @arr = (77,44,77);
Correct.
Perl
foo == '44'
foo === 44
Use strict equality.
JavaScript
function render() {{ return {{key:'data'}} }}
function render() {{ return {{key:'data'}}; }}
Return object on same line.
JavaScript
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
disp('test')
disp('test')
Correct.
MATLAB
val foo = 'test'
val foo = "test"
Double quotes.
Kotlin
<a href='https://test.org' target='_blank'>
<a href='https://test.org' target='_blank' rel='noopener'>
Add rel for security.
HTML
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
INSERT INTO orders VALUES ('hello',39)
INSERT INTO orders (age, status) VALUES ('hello',39);
Specify columns.
SQL
if ($num = 55) {{}}
if ($num -eq 55) {{}}
Use -eq.
PowerShell
try {{ throw 'message'; }} catch(e) {{}}
try {{ throw new Error('message'); }} catch(e) {{}}
Throw Error objects.
JavaScript
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('output')); app.listen(26);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('output')); app.listen(26, () => console.log('listening'));
Add callback.
Node.js
{{'age':'hello'}}
{{"age":"hello"}}
Use double quotes.
JSON
let list=vec![58,46,52]; let first=&list[0]; list.push(74);
let mut list=vec![58,46,52]; let first=list[0]; list.push(74);
Copy instead of reference.
Rust
.Item {{ color: #333; }}
.Item {{ color: #333; }}
Correct.
CSS
num = 27
num=27
No spaces.
Shell
class = 'info'
class_name = 'info'
'class' is a keyword.
Python
let mut val=24; let ref1=&mut val; let ref2=&mut val;
let mut val=24; {{ let ref1=&mut val; }} let ref2=&mut val;
Only one mutable borrow.
Rust
else print('hello')
else: print('hello')
Colon after else.
Python
{{"status":"value" "id":29}}
{{"status":"value", "id":29}}
Add comma.
JSON