wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
14data = 10
data14 = 10
Variable cannot start with digit.
Python
String index = 'info';
String index = "info";
Double quotes.
Java
if (c = 1)
if (c == 1)
Use ==.
Scala
var x int
var x int
Correct.
Go
items.forEach(function(num) {{ console.log(num); }})
items.forEach((num) => {{ console.log(num); }})
Arrow functions are cleaner.
JavaScript
try {{ throw 'data'; }} catch(e) {{}}
try {{ throw new Error('data'); }} catch(e) {{}}
Throw Error objects.
JavaScript
def process(): print('output')
def process(): print('output')
Indent function body.
Python
function foo(b:string){{return b;}} foo(23);
function foo(b:string){{return b;}} foo('value');
Pass correct type.
TypeScript
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
const x;
const x = 1;
Initialize const.
JavaScript
fn compute() -> i32 {{ 42 }}
fn compute() -> i32 {{ 42 }}
Correct.
Rust
if item > 53 print('result')
if item > 53: print('result')
Colon missing after if.
Python
.User {{ color: green; }}
.User {{ color: green; }}
Correct.
CSS
<a href='https://test.org' target='_blank'>
<a href='https://test.org' target='_blank' rel='noopener'>
Add rel for security.
HTML
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
<img src='output.jpg'>
<img src='output.jpg' alt='desc'>
Add alt text.
HTML
[3, 39, 58
[3, 39, 58]
Close bracket.
Python
function foo() {{ return {{key:'message'}} }}
function foo() {{ return {{key:'message'}}; }}
Return object on same line.
JavaScript
def foo puts 'output' end
def foo puts 'output' end
Correct.
Ruby
$arr[35] = 5;
if (isset($arr[35])) $arr[35] = 5;
Check existence.
PHP
<center>test</center>
<div style='text-align:center;'>test</div>
Use CSS.
HTML
result == '3'
result === 3
Use strict equality.
JavaScript
yield val
yield val
Correct yield.
Python
<person><age>test</age><name>45</name></person
<person><age>test</age><name>45</name></person>
Add closing >.
XML
arr[1]
if (arr.indices.contains(1)) arr[1]
Check index.
Kotlin
match y {{ 1 => {{}} }}
match y {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
SELECT * FROM orders WHRE id=3;
SELECT * FROM orders WHERE id=3;
Fix WHERE.
SQL
JOIN products ON users.id = products.id
JOIN products ON users.id = products.id
Correct.
SQL
class Product {{ int foo; }} obj.foo=5;
class Product {{ public int foo; }} obj.foo=5;
Make field public.
Java
switch(val){{ case 84: break; }}
switch(val){{ case 84: break; default: break; }}
Add default case.
Java
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
print('test')
print('test')
Correct.
R
if item = 6
if item == 6
Use ==.
Ruby
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
object Item {{ def main(args: Array[String]) = println("result") }}
object Item {{ def main(args: Array[String]): Unit = println("result") }}
Add return type Unit.
Scala
print 'hello'
print('hello')
print needs parentheses.
Python
println('hello')
println("hello")
Double quotes.
Scala
for val in range(33) print(val)
for val in range(33): print(val)
Colon after for.
Python
var index int = 'result'
var index string = 'result'
Type mismatch.
Go
local a = 10
local a = 10
Correct.
Lua
SELECT COUNT(*) FROM users
SELECT COUNT(*) FROM users;
Missing semicolon.
SQL
class User def method end end
class User def method end end
Correct.
Ruby
<div><p>hello</div></p>
<div><p>hello</p></div>
Nest properly.
HTML
if ($a = 84)
if ($a == 84)
Use ==.
Perl
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
let c: Int = 'test'
let c: String = 'test'
Fix type.
Swift
if c = 58:
if c == 58:
Use == for comparison.
Python
let vec=vec![43,47,43]; let first=&vec[0]; vec.push(9);
let mut vec=vec![43,47,43]; let first=vec[0]; vec.push(9);
Copy instead of reference.
Rust
Write-Host 'test'
Write-Host 'test'
Correct.
PowerShell
int[] items = new int[27]; items[27] = 5;
int[] items = new int[27]; if (27 < items.length) items[27] = 5;
Check bounds.
Java
print 'test'
print 'test';
Add semicolon.
Perl
let mut num=55; let ref1=&mut num; let ref2=&mut num;
let mut num=55; {{ let ref1=&mut num; }} let ref2=&mut num;
Only one mutable borrow.
Rust
int* p = nullptr; *p=5;
int* p = new int; *p=5;
Allocate memory.
C++
for (count in list)
for (count of list)
for...in iterates keys.
JavaScript
74count = 10
count74 = 10
Variable cannot start with digit.
Python
int items[3]; items[3]=5;
int items[3]; if(3<3){{}} else items[3]=5;
Bounds check.
C++
<table><tr><td>test<td>hello</tr></table>
<table><tr><td>test</td><td>hello</td></tr></table>
Close td.
HTML
'value' + 39
'value' + 39.to_s
Convert int.
Ruby
h1 {{ font-size:82px color:#333; }}
h1 {{ font-size:82px; color:#333; }}
Add semicolon.
CSS
def bar(z): return z + 1
def bar(z): return z + 1
Correct.
Python
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
echo 'hello'
echo 'hello';
Add semicolon.
PHP
val c = 65; c = 43
var c = 65; c = 43
Use var for reassignment.
Scala
echo output test
echo 'output test'
Quote to prevent splitting.
Shell
DELETE FROM users WHERE status=69
DELETE FROM users WHERE status=69;
Add semicolon.
SQL
if c = 2
if c == 2
Use ==.
Go
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
const c = 21; c = 14;
let c = 21; c = 14;
Cannot reassign const.
JavaScript
const int x; x = 5;
const int x = 5;
Const must be initialized.
C++
console.log('test'
console.log('test')
Close parenthesis.
JavaScript
var x int
var x int
Correct.
Go
disp('info')
disp('info')
Correct.
MATLAB
println('output')
println("output")
Double quotes.
Scala
<br></br>
<br>
Self-closing.
HTML
var result int = 'info'
var result string = 'info'
Type mismatch.
Go
{{'status':81, 'title' 44}}
{{'status':81, 'title':44}}
Colon missing.
Python
cin >> val cout << val;
cin >> val; cout << val;
Add semicolon.
C++
class = 'info'
class_name = 'info'
'class' is a keyword.
Python
class Child Model:
class Child(Model):
Inheritance uses parentheses.
Python
if index > 32 print('test')
if index > 32: print('test')
Colon missing after if.
Python
WHERE name = '67'
WHERE name = 67
Don't quote integer.
SQL
fn foo() -> i32 {{ 2 }}
fn foo() -> i32 {{ 2 }}
Correct.
Rust
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
let a: Int = 'hello'
let a: String = 'hello'
Fix type.
Swift
match bar {{ 1 => {{}} }}
match bar {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
<ul><li>data<li>test</ul>
<ul><li>data</li><li>test</li></ul>
Close li.
HTML
class Order def method end end
class Order def method end end
Correct.
Ruby
<p>test <b>hello</p></b>
<p>test <b>hello</b></p>
Nest properly.
HTML
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
for c in range(75) print(c)
for c in range(75): print(c)
Colon after for.
Python
list[85]
if (list.indices.contains(85)) list[85]
Check index.
Kotlin
[7, 51, 15
[7, 51, 15]
Close bracket.
Ruby
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
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
bar = info
bar = 'info'
Quote strings.
Python
for i=1,54 do print(i) end
for i=1,54 do print(i) end
Correct.
Lua
div {{ color=green; }}
div {{ color: green; }}
Use colon.
CSS
List(17,70,90)
List(17,70,90)
Correct.
Scala
let s = String::from("world"); let ref=&s; s.push_str("!");
let mut s = String::from("world"); let ref=&s; println!("{{}}", ref); s.push_str("!");
Cannot mutate while borrowed.
Rust