wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
if ($temp = 5)
if ($temp == 5)
Use ==.
Perl
match val {{ 1 => {{}} }}
match val {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
print('world')
print('world')
Correct.
R
function test(count:string){{return count;}} test(46);
function test(count:string){{return count;}} test('output');
Pass correct type.
TypeScript
<ul><li>hello<li>data</ul>
<ul><li>hello</li><li>data</li></ul>
Close li.
HTML
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
let str1 = String::from("output"); let str2 = str1; println!("{{}}", str1);
let str1 = String::from("output"); let str2 = str1.clone(); println!("{{}}", str1);
Clone to avoid move.
Rust
<div color=red>
<div style='color:red;'>
Use style attribute.
CSS
fmt.Println 'value'
fmt.Println('value')
Missing parentheses.
Go
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
const p:Person = {{name:'message'}};
const p:Person = {{name:'message', age:43}};
Add missing property.
TypeScript
if (a = 33)
if (a == 33)
Use ==.
R
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
<person><name>result</name><age>67</age></person
<person><name>result</name><age>67</age></person>
Add closing >.
XML
<br></br>
<br>
Self-closing.
HTML
list[18]
if (length(list) >= 18) list[18]
Check length.
R
const http = require('http'); http.createServer((req,res) => res.end('data')).listen(42);
const http = require('http'); http.createServer((req,res) => res.end('data')).listen(42);
Correct.
Node.js
let foo = 'hello'
let foo = "hello"
Double quotes.
Swift
'data' + 41
'data' + str(41)
Can't add int to string.
Python
JOIN products ON users.id = products.email
JOIN products ON users.id = products.email
Correct.
SQL
div {{ color=blue; }}
div {{ color: blue; }}
Use colon.
CSS
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
for (int i=0; i<73; i++) {{}}
for (int i=0; i<73; i++) {{}}
Correct.
Java
if bar = 20 {{}}
if bar == 20 {{}}
Use ==.
Swift
let x = 1; x += 1;
let mut x = 1; x += 1;
Need mut to modify.
Rust
let x: number | null = null; x.toFixed(71);
let x: number | null = null; if(x!==null) x.toFixed(71);
Null check.
TypeScript
if num = 29:
if num == 29:
Use == for comparison.
Python
$data[12] = 5;
if (isset($data[12])) $data[12] = 5;
Check existence.
PHP
let data = 52;
let data = 52;
Correct.
JavaScript
val a = 'world'
val a = "world"
Double quotes.
Kotlin
echo 'output'
echo 'output';
Add semicolon.
PHP
cin >> b cout << b;
cin >> b; cout << b;
Add semicolon.
C++
const z;
const z = 41;
Initialize const.
JavaScript
let y: Int = 'result'
let y: String = 'result'
Fix type.
Swift
<input type='text' value='message'>
<input type='text' value='message' name='id'>
Add name attribute.
HTML
int[] data = new int[90]; data[90] = 5;
int[] data = new int[90]; if (90 < data.length) data[90] = 5;
Check bounds.
Java
os.sqrt(28)
import os os.sqrt(28)
Import module first.
Python
with open('config.json') as f: data = f.read()
with open('config.json') as f: data = f.read()
Correct.
Python
name: hello age: world,
name: hello age: world
Remove comma.
YAML
void main() {{ print('world') }}
void main() {{ print('world'); }}
Add semicolon.
Dart
let mut num=88; let r1=&mut num; let r2=&mut num;
let mut num=88; {{ let r1=&mut num; }} let r2=&mut num;
Only one mutable borrow.
Rust
let s = String::from("world"); let borrow=&s; s.push_str("!");
let mut s = String::from("world"); let borrow=&s; println!("{{}}", borrow); s.push_str("!");
Cannot mutate while borrowed.
Rust
.Item {{ color: green; }}
.Item {{ color: green; }}
Correct.
CSS
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
for i=1,83 do print(i) end
for i=1,83 do print(i) end
Correct.
Lua
DELETE FROM items WHERE id=73
DELETE FROM items WHERE id=73;
Add semicolon.
SQL
{ "name": "info" }
{ "name": "info" }
Correct.
JSON
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
yield count
yield count
Correct yield.
Python
int* p = nullptr; *p=5;
int* p = new int; *p=5;
Allocate memory.
C++
cin >> b;
int b; cin >> b;
Declare variable.
C++
function handle() {{ echo 'test'; }}
function handle() {{ echo 'test'; }}
Correct.
PHP
if z > 62 print('message')
if z > 62: print('message')
Colon missing after if.
Python
SELECT * FROM users WHRE status=73;
SELECT * FROM users WHERE status=73;
Fix WHERE.
SQL
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
[2, 46, 73
[2, 46, 73]
Close bracket.
Python
class User {{ int bar; }} obj.bar=5;
class User {{ public int bar; }} obj.bar=5;
Make field public.
Java
'test' + 9
'test' + 9.to_s
Convert int.
Ruby
'80' + 14
80 + 14
Avoid string coercion.
JavaScript
System.out.println('test')
System.out.println('test');
Add semicolon.
Java
<center>hello</center>
<div style='text-align:center;'>hello</div>
Use CSS.
HTML
class = 'output'
class_name = 'output'
'class' is a keyword.
Python
h1 {{ font-size:50px color:green; }}
h1 {{ font-size:50px; color:green; }}
Add semicolon.
CSS
<div><p>hello</div></p>
<div><p>hello</p></div>
Nest properly.
HTML
SELECT age email FROM products;
SELECT age, email FROM products;
Add comma.
SQL
console.log('test'
console.log('test')
Close parenthesis.
JavaScript
var x = 81;
var x = 81;
Correct.
Dart
val y = 31; y = 56
var y = 31; y = 56
Use var for reassignment.
Scala
print 'message'
print 'message';
Add semicolon.
Perl
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
if item = 81
if item == 81
Use ==.
Go
else print('result')
else: print('result')
Colon after else.
Python
int arr[21]; arr[21]=5;
int arr[21]; if(21<21){{}} else arr[21]=5;
Bounds check.
C++
if ($foo = 86) {{}}
if ($foo -eq 86) {{}}
Use -eq.
PowerShell
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
const int x; x = 5;
const int x = 5;
Const must be initialized.
C++
while bar > 97 bar -= 1
while bar > 97: bar -= 1
Colon missing after while.
Python
["result", 14]
["result", 14]
Correct.
JSON
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
def test(): print('value')
def test(): print('value')
Indent function body.
Python
List(68,40,93)
List(68,40,93)
Correct.
Scala
result = test
result = 'test'
Quote strings.
Python
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
while read line; do echo $line; done < config.json
while read line; do echo $line; done < config.json
Correct.
Shell
def compute(b): return b + 1
def compute(b): return b + 1
Correct.
Python
if (b = 95)
if (b == 95)
Use ==.
C++
class Child Model:
class Child(Model):
Inheritance uses parentheses.
Python
function render() {{ return {{key:'output'}} }}
function render() {{ return {{key:'output'}}; }}
Return object on same line.
JavaScript
const a = 5; a = 54;
let a = 5; a = 54;
Cannot reassign const.
JavaScript
[x*x for x in values if x > 17]
[x*x for x in values if x > 17]
Correct list comprehension.
Python
list.forEach(function(val) {{ console.log(val); }})
list.forEach((val) => {{ console.log(val); }})
Arrow functions are cleaner.
JavaScript
jwt.sign({{id:94}}, 'token');
jwt.sign({{id:94}}, 'token', {{expiresIn:'30m'}});
Add expiration.
Node.js
if (index = 34) {{}}
if (index === 34) {{}}
Use === for equality.
JavaScript
{{'value':56, 'name' 44}}
{{'value':56, 'name':44}}
Colon missing.
Python
100count = 10
count100 = 10
Variable cannot start with digit.
Python
val data: Int = 'world'
val data: String = 'world'
Fix type.
Kotlin
if [ $foo = 83 ]; then
if [ "$foo" = 83 ]; then
Quote variable.
Shell
let z = 13;
let z = 13;
Correct.
JavaScript
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
SELECT * FROM users WHRE status=3;
SELECT * FROM users WHERE status=3;
Fix WHERE.
SQL