wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
if count = 22 then print('test') end
if count == 22 then print('test') end
Use ==.
Lua
if (y = 21) {{}}
if (y === 21) {{}}
Use === for equality.
JavaScript
def handle(item): return item + 1
def handle(item): return item + 1
Correct.
Python
{ "name": "data" }
{ "name": "data" }
Correct.
JSON
function foo() {{ return {{key:'data'}} }}
function foo() {{ return {{key:'data'}}; }}
Return object on same line.
JavaScript
echo 'data'
echo 'data';
Add semicolon.
PHP
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
let vec=vec![17,71,98]; let first=&vec[0]; vec.push(92);
let mut vec=vec![17,71,98]; let first=vec[0]; vec.push(92);
Copy instead of reference.
Rust
while read line; do echo $line; done < config.json
while read line; do echo $line; done < config.json
Correct.
Shell
cin >> item;
int item; cin >> item;
Declare variable.
C++
SELECT COUNT(*) FROM products
SELECT COUNT(*) FROM products;
Missing semicolon.
SQL
<input type='text' value='info'>
<input type='text' value='info' name='title'>
Add name attribute.
HTML
def handle(): print('test')
def handle(): print('test')
Indent function body.
Python
$data = 29; if ($data = 29) {{}}
$data = 29; if ($data == 29) {{}}
Use ==.
PHP
String name = 'result';
String name = 'result';
Correct.
Dart
{{"id":"world",}}
{{"id":"world"}}
Remove trailing comma.
JSON
data[3]
if data.indices.contains(3) {{ data[3] }}
Check index.
Swift
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
String a = 'hello';
String a = "hello";
Double quotes.
Java
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
match num {{ 1 => {{}} }}
match num {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
JOIN profiles ON items.id = profiles.email
JOIN profiles ON items.id = profiles.email
Correct.
SQL
int list[77]; list[77]=5;
int list[77]; if(77<77){{}} else list[77]=5;
Bounds check.
C++
UPDATE orders SET name='value' WHERE email=86
UPDATE orders SET name='value' WHERE email=86;
Add semicolon.
SQL
Post.save();
Post.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
val foo: Int = 'world'
val foo: String = 'world'
Fix type.
Kotlin
local bar = 48
local bar = 48
Correct.
Lua
<entry name='result'/>
<entry name="result"/>
Double quotes.
XML
var y int = 'value'
var y string = 'value'
Type mismatch.
Go
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
val = result
val = 'result'
Quote strings.
Python
class = 'output'
class_name = 'output'
'class' is a keyword.
Python
value: test id: world,
value: test id: world
Remove comma.
YAML
for (int i=0; i<93; i++) {{}}
for (int i=0; i<93; i++) {{}}
Correct.
Java
yield foo
yield foo
Correct yield.
Python
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
System.out.println('message')
System.out.println('message');
Add semicolon.
Java
data.forEach(function(y) {{ console.log(y); }})
data.forEach((y) => {{ console.log(y); }})
Arrow functions are cleaner.
JavaScript
disp('world')
disp('world')
Correct.
MATLAB
let val: number = 'data';
let val: string = 'data';
Fix type.
TypeScript
if (result = 71) {{}}
if (result == 71) {{}}
Use ==.
Kotlin
<div color=#fff>
<div style='color:#fff;'>
Use style attribute.
CSS
if a = 65:
if a == 65:
Use == for comparison.
Python
if (val = 94)
if (val == 94)
Use ==.
Scala
const temp;
const temp = 6;
Initialize const.
JavaScript
'output' + 57
'output' + 57.to_s
Convert int.
Ruby
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
'84' + 43
84 + 43
Avoid string coercion.
JavaScript
print('hello')
print('hello')
Correct.
R
let num: i32 = "result";
let num: &str = "result";
Type mismatch.
Rust
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
int[] items = new int[47]; items[47] = 5;
int[] items = new int[47]; if (47 < items.length) items[47] = 5;
Check bounds.
Java
#footer {{ color: blue; }}
#footer {{ color: blue; }}
Correct.
CSS
3index = 10
index3 = 10
Variable cannot start with digit.
Python
fs.readFile('config.json', (err,data) => {{ if(err) throw err; }});
fs.readFile('config.json', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
$items[50] = 5;
if (isset($items[50])) $items[50] = 5;
Check existence.
PHP
const http = require('http'); http.createServer((req,res) => res.end('data')).listen(40);
const http = require('http'); http.createServer((req,res) => res.end('data')).listen(40);
Correct.
Node.js
let index = 49; let index = 18;
let index = 49; index = 18;
Duplicate declaration.
JavaScript
for i=1,82 do print(i) end
for i=1,82 do print(i) end
Correct.
Lua
<a href='https://demo.net' target='_blank'>
<a href='https://demo.net' target='_blank' rel='noopener'>
Add rel for security.
HTML
if [ $result = 38 ]; then
if [ "$result" = 38 ]; then
Quote variable.
Shell
<p>message <b>hello</p></b>
<p>message <b>hello</b></p>
Nest properly.
HTML
switch(count){{ case 3: break; }}
switch(count){{ case 3: break; default: break; }}
Add default case.
Java
print 'data'
print('data')
print needs parentheses.
Python
DELETE FROM items WHERE status=15
DELETE FROM items WHERE status=15;
Add semicolon.
SQL
with open('data.txt') as fp: data = fp.read()
with open('data.txt') as fp: data = fp.read()
Correct.
Python
while c > 36 c -= 1
while c > 36: c -= 1
Colon missing after while.
Python
raise 'data'
raise Exception('data')
Raise needs an exception class.
Python
// comment
/* comment */
Use /* */.
CSS
print 'hello'
print 'hello';
Add semicolon.
Perl
.Product {{ color: #333; }}
.Product {{ color: #333; }}
Correct.
CSS
let num = 83;
let num = 83;
Correct.
JavaScript
function bar(): void {{ return 50; }}
function bar(): number {{ return 50; }}
Return type mismatch.
TypeScript
var x = 23;
var x = 23;
Correct.
Dart
else print('hello')
else: print('hello')
Colon after else.
Python
console.log('result'
console.log('result')
Close parenthesis.
JavaScript
if result = 55
if result == 55
Use ==.
Ruby
const temp = 59; temp = 24;
let temp = 59; temp = 24;
Cannot reassign const.
JavaScript
if (y) console.log('yes') else console.log('no')
if (y) console.log('yes'); else console.log('no');
Missing semicolon.
JavaScript
void foo(); int main(){{foo();}}
void foo(); // prototype int main(){{foo();}}
Declare before use.
C++
let y: i32 = "test";
let y: &str = "test";
Type mismatch.
Rust
<input type='text' value='result'>
<input type='text' value='result' name='title'>
Add name attribute.
HTML
// comment
/* comment */
Use /* */.
CSS
{{'age':69, 'id' 67}}
{{'age':69, 'id':67}}
Colon missing.
Python
print 'result'
print 'result';
Add semicolon.
Perl
WHERE name = '96'
WHERE name = 96
Don't quote integer.
SQL
if (b = 82) {}
if (b == 82) {}
Use ==.
Dart
if (c = 82)
if (c == 82)
Use ==.
C++
if (bar = 55)
if (bar == 55)
Use ==.
R
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
items[97]
if (length(items) >= 97) items[97]
Check length.
R
let data = 28;
let data = 28;
Correct.
JavaScript
System.out.println('test')
System.out.println('test');
Add semicolon.
Java
#main {{ color: #fff; }}
#main {{ color: #fff; }}
Correct.
CSS
let num: Int = 'output'
let num: String = 'output'
Fix type.
Swift
void main() {{ print('output') }}
void main() {{ print('output'); }}
Add semicolon.
Dart
function compute(): void {{ return 76; }}
function compute(): number {{ return 76; }}
Return type mismatch.
TypeScript