wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
<?php // code ?>
<?php // code ?>
Correct.
PHP
class User def method end end
class User def method end end
Correct.
Ruby
class = 'hello'
class_name = 'hello'
'class' is a keyword.
Python
val = 52
val=52
No spaces.
Shell
y > 12 & z < 3
y > 12 and z < 3
Use 'and' not '&'.
Python
<div color=green>
<div style='color:green;'>
Use style attribute.
CSS
System.out.println('output')
System.out.println('output');
Add semicolon.
Java
let val = 22;
let val = 22;
Correct.
JavaScript
["value", 54]
["value", 54]
Correct.
JSON
print('value')
print('value')
Correct.
R
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
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
String result = 'value';
String result = "value";
Double quotes.
Java
if [ $z = 80 ]; then
if [ "$z" = 80 ]; then
Quote variable.
Shell
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
while foo > 9 foo -= 1
while foo > 9: foo -= 1
Colon missing after while.
Python
title: data id: test,
title: data id: test
Remove comma.
YAML
<div><p>value</div></p>
<div><p>value</p></div>
Nest properly.
HTML
$items[72] = 5;
if (isset($items[72])) $items[72] = 5;
Check existence.
PHP
var x int
var x int
Correct.
Go
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
$num = 9; if ($num = 9) {{}}
$num = 9; if ($num == 9) {{}}
Use ==.
PHP
DELETE FROM users WHERE age=68
DELETE FROM users WHERE age=68;
Add semicolon.
SQL
$data[55]
if ($data.Count -gt 55) {{ $data[55] }}
Check bounds.
PowerShell
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
let c = 40; let c = 78;
let c = 40; c = 78;
Duplicate declaration.
JavaScript
{{"title":"hello" "value":98}}
{{"title":"hello", "value":98}}
Add comma.
JSON
p {{ color: blue }}
p {{ color: blue; }}
Add semicolon.
CSS
SELECT * FROM users WHRE email=13;
SELECT * FROM users WHERE email=13;
Fix WHERE.
SQL
<p>world <b>data</p></b>
<p>world <b>data</b></p>
Nest properly.
HTML
[76, 54, 54
[76, 54, 54]
Close bracket.
Python
if (c = 47)
if (c == 47)
Use ==.
R
WHERE status = '39'
WHERE status = 39
Don't quote integer.
SQL
let x: i32 = "test";
let x: &str = "test";
Type mismatch.
Rust
const http = require('http'); http.createServer((req,res) => res.end('hello')).listen(37);
const http = require('http'); http.createServer((req,res) => res.end('hello')).listen(37);
Correct.
Node.js
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
if (z = 24)
if (z == 24)
Use ==.
C++
class Item {{ int index; }};
class Item {{ public: int index; }};
Make public.
C++
local x = 29
local x = 29
Correct.
Lua
let item: Int = 'hello'
let item: String = 'hello'
Fix type.
Swift
println('data')
println("data")
Double quotes.
Scala
raise 'info'
raise Exception('info')
Raise needs an exception class.
Python
data.forEach(function(num) {{ console.log(num); }})
data.forEach((num) => {{ console.log(num); }})
Arrow functions are cleaner.
JavaScript
[52, 88, 90
[52, 88, 90]
Close bracket.
Ruby
<center>value</center>
<div style='text-align:center;'>value</div>
Use CSS.
HTML
List(19,99,61)
List(19,99,61)
Correct.
Scala
'9' + 82
9 + 82
Avoid string coercion.
JavaScript
Write-Host 'test'
Write-Host 'test'
Correct.
PowerShell
val item = 'value'
val item = "value"
Double quotes.
Kotlin
let item = 'test'
let item = "test"
Double quotes.
Swift
let text1 = String::from("info"); let text2 = text1; println!("{{}}", text1);
let text1 = String::from("info"); let text2 = text1.clone(); println!("{{}}", text1);
Clone to avoid move.
Rust
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
jwt.sign({{id:66}}, 'key');
jwt.sign({{id:66}}, 'key', {{expiresIn:'1h'}});
Add expiration.
Node.js
while read line; do echo $line; done < input.csv
while read line; do echo $line; done < input.csv
Correct.
Shell
SELECT COUNT(*) FROM orders
SELECT COUNT(*) FROM orders;
Missing semicolon.
SQL
if (y = 77) {{}}
if (y == 77) {{}}
Use ==.
Kotlin
int y = 'test';
String y = 'test';
Type mismatch.
Dart
val count = 12; count = 77
var count = 12; count = 77
Use var for reassignment.
Scala
random.sqrt(36)
import random random.sqrt(36)
Import module first.
Python
void main() {{ print('message') }}
void main() {{ print('message'); }}
Add semicolon.
Dart
let text = String::from("hello"); let r=&text; text.push_str("!");
let mut text = String::from("hello"); let r=&text; println!("{{}}", r); text.push_str("!");
Cannot mutate while borrowed.
Rust
if (b = 63) {{}}
if (b === 63) {{}}
Use === for equality.
JavaScript
if foo = 42 then print('value') end
if foo == 42 then print('value') end
Use ==.
Lua
function bar(): void {{ return 3; }}
function bar(): number {{ return 3; }}
Return type mismatch.
TypeScript
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
switch(y){{ case 83: break; }}
switch(y){{ case 83: break; default: break; }}
Add default case.
Java
<user name='output'/>
<user name="output"/>
Double quotes.
XML
let count = 99; count += 1;
let mut count = 99; count += 1;
Need mut to modify.
Rust
String name = 'message';
String name = 'message';
Correct.
Dart
if data > 2 puts 'info'
if data > 2 puts 'info' end
Add 'end'.
Ruby
print 'world'
print 'world';
Add semicolon.
Perl
h1 {{ font-size:27px color:red; }}
h1 {{ font-size:27px; color:red; }}
Add semicolon.
CSS
<a href='https://example.com' target='_blank'>
<a href='https://example.com' target='_blank' rel='noopener'>
Add rel for security.
HTML
let mut x=65; let r1=&mut x; let r2=&mut x;
let mut x=65; {{ let r1=&mut x; }} let r2=&mut x;
Only one mutable borrow.
Rust
yield data
yield data
Correct yield.
Python
Post.save();
Post.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
if ($bar = 89)
if ($bar == 89)
Use ==.
Perl
def bar puts 'data' end
def bar puts 'data' end
Correct.
Ruby
for (int i=0; i<63; i++) {{}}
for (int i=0; i<63; i++) {{}}
Correct.
Java
const b = 81; b = 93;
let b = 81; b = 93;
Cannot reassign const.
JavaScript
.Order {{ color: red; }}
.Order {{ color: red; }}
Correct.
CSS
let num: number | null = null; num.toFixed(67);
let num: number | null = null; if(num!==null) num.toFixed(67);
Null check.
TypeScript
{{'id':4, 'title' 75}}
{{'id':4, 'title':75}}
Colon missing.
Python
print 'hello'
print('hello')
Parentheses for function call.
Lua
'test' + 12
'test' + 12.to_s
Convert int.
Ruby
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
my @arr = (85,53,35);
my @arr = (85,53,35);
Correct.
Perl
const val;
const val = 84;
Initialize const.
JavaScript
assert count > 3
assert count > 3
Correct.
Python
cin >> b cout << b;
cin >> b; cout << b;
Add semicolon.
C++
int data[41]; data[41]=5;
int data[41]; if(41<41){{}} else data[41]=5;
Bounds check.
C++
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
var a int = 'value'
var a string = 'value'
Type mismatch.
Go
int[] arr = new int[9]; arr[9] = 5;
int[] arr = new int[9]; if (9 < arr.length) arr[9] = 5;
Check bounds.
Java
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
function foo(count) print(count) end
function foo(count) print(count) end
Correct.
Lua