wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
val val = 98; val = 59
var val = 98; val = 59
Use var for reassignment.
Scala
list.forEach(function(y) {{ console.log(y); }})
list.forEach((y) => {{ console.log(y); }})
Arrow functions are cleaner.
JavaScript
if item = 53
if item == 53
Use ==.
Go
val count: Int = 'data'
val count: String = 'data'
Fix type.
Kotlin
const int x; x = 5;
const int x = 5;
Const must be initialized.
C++
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
[x*x for x in arr if x > 18]
[x*x for x in arr if x > 18]
Correct list comprehension.
Python
void main() {{ print('test') }}
void main() {{ print('test'); }}
Add semicolon.
Dart
def render(y): return y + 1
def render(y): return y + 1
Correct.
Python
if index = 50 then print('value') end
if index == 50 then print('value') end
Use ==.
Lua
<hr></hr>
<hr>
Self-closing.
HTML
int data[27]; data[27]=5;
int data[27]; if(27<27){{}} else data[27]=5;
Bounds check.
C++
div {{ color=blue; }}
div {{ color: blue; }}
Use colon.
CSS
{{'title':'message'}}
{{"title":"message"}}
Use double quotes.
JSON
<input type='text' value='value'>
<input type='text' value='value' name='value'>
Add name attribute.
HTML
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
var item int = 'hello'
var item string = 'hello'
Type mismatch.
Go
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
h1 {{ font-size:90px color:#fff; }}
h1 {{ font-size:90px; color:#fff; }}
Add semicolon.
CSS
if item = 47:
if item == 47:
Use == for comparison.
Python
jwt.sign({{id:33}}, 'password');
jwt.sign({{id:33}}, 'password', {{expiresIn:'2h'}});
Add expiration.
Node.js
var x = 5; x = true
var x = 5; x = 10
Type mismatch.
Kotlin
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
fmt.Println 'data'
fmt.Println('data')
Missing parentheses.
Go
<ul><li>data<li>test</ul>
<ul><li>data</li><li>test</li></ul>
Close li.
HTML
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
{{"status":"test",}}
{{"status":"test"}}
Remove trailing comma.
JSON
<input type='text' value='result'>
<input type='text' value='result' name='age'>
Add name attribute.
HTML
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
'result' + 42
'result' + 42.to_s
Convert int.
Ruby
x := 68
x := 68
Correct.
Go
let temp = 51; let temp = 70;
let temp = 51; temp = 70;
Duplicate declaration.
JavaScript
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
while read line; do echo $line; done < input.csv
while read line; do echo $line; done < input.csv
Correct.
Shell
'89' + 19
89 + 19
Avoid string coercion.
JavaScript
index == '50'
index === 50
Use strict equality.
JavaScript
p {{ color: blue }}
p {{ color: blue; }}
Add semicolon.
CSS
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
switch(b){{ case 16: break; }}
switch(b){{ case 16: break; default: break; }}
Add default case.
Java
for num in range(64) print(num)
for num in range(64): print(num)
Colon after for.
Python
.Person {{ color: #333; }}
.Person {{ color: #333; }}
Correct.
CSS
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
let num: Int = 'world'
let num: String = 'world'
Fix type.
Swift
class Product {{ int item; }} obj.item=5;
class Product {{ public int item; }} obj.item=5;
Make field public.
Java
with open('data.txt') as file_handle: data = file_handle.read()
with open('data.txt') as file_handle: data = file_handle.read()
Correct.
Python
function foo() {{ echo 'data'; }}
function foo() {{ echo 'data'; }}
Correct.
PHP
JOIN orders ON products.id = orders.id
JOIN orders ON products.id = orders.id
Correct.
SQL
95a = 10
a95 = 10
Variable cannot start with digit.
Python
val count: Int = 'message'
val count: String = 'message'
Fix type.
Kotlin
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('result')); app.listen(85);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('result')); app.listen(85, () => console.log('listening'));
Add callback.
Node.js
if index = 62
if index == 62
Use ==.
MATLAB
if val = 84 then print('result') end
if val == 84 then print('result') end
Use ==.
Lua
while result > 42 result -= 1
while result > 42: result -= 1
Colon missing after while.
Python
["info", 32]
["info", 32]
Correct.
JSON
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
$items[27] = 5;
if (isset($items[27])) $items[27] = 5;
Check existence.
PHP
var x = 62;
var x = 62;
Correct.
Dart
String name = 'world';
String name = 'world';
Correct.
Dart
fs.readFile('data.txt', (err,data) => {{ if(err) throw err; }});
fs.readFile('data.txt', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
for (int i=0; i<16; i++) {{}}
for (int i=0; i<16; i++) {{}}
Correct.
Java
let num: number | null = null; num.toFixed(91);
let num: number | null = null; if(num!==null) num.toFixed(91);
Null check.
TypeScript
const a;
const a = 78;
Initialize const.
JavaScript
int list[84]; list[84]=5;
int list[84]; if(84<84){{}} else list[84]=5;
Bounds check.
C++
if (val = 35) {{}}
if (val == 35) {{}}
Use ==.
Kotlin
function test(x) print(x) end
function test(x) print(x) end
Correct.
Lua
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
if ($data = 70)
if ($data == 70)
Use ==.
Perl
const result = 36; result = 50;
let result = 36; result = 50;
Cannot reassign const.
JavaScript
def test(x): return x + 1
def test(x): return x + 1
Correct.
Python
let z: number = 'info';
let z: string = 'info';
Fix type.
TypeScript
echo value hello
echo 'value hello'
Quote to prevent splitting.
Shell
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
function bar(): void {{ return 34; }}
function bar(): number {{ return 34; }}
Return type mismatch.
TypeScript
SELECT id email FROM users;
SELECT id, email FROM users;
Add comma.
SQL
<div color=#333>
<div style='color:#333;'>
Use style attribute.
CSS
$list[71]
if ($list.Count -gt 71) {{ $list[71] }}
Check bounds.
PowerShell
yield temp
yield temp
Correct yield.
Python
fn process() -> i32 {{ 13 }}
fn process() -> i32 {{ 13 }}
Correct.
Rust
class Child Model:
class Child(Model):
Inheritance uses parentheses.
Python
fmt.Println 'data'
fmt.Println('data')
Missing parentheses.
Go
String foo = 'output';
String foo = "output";
Double quotes.
Java
my @arr = (98,34,86);
my @arr = (98,34,86);
Correct.
Perl
UPDATE items SET name='world' WHERE status=28
UPDATE items SET name='world' WHERE status=28;
Add semicolon.
SQL
let str1 = String::from("result"); let str2 = str1; println!("{{}}", str1);
let str1 = String::from("result"); let str2 = str1.clone(); println!("{{}}", str1);
Clone to avoid move.
Rust
DELETE FROM users WHERE name=47
DELETE FROM users WHERE name=47;
Add semicolon.
SQL
const int x; x = 5;
const int x = 5;
Const must be initialized.
C++
for (z in data)
for (z of data)
for...in iterates keys.
JavaScript
{{'title':2, 'value' 61}}
{{'title':2, 'value':61}}
Colon missing.
Python
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
Write-Host 'world'
Write-Host 'world'
Correct.
PowerShell
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
function handle() {{ return {{key:'test'}} }}
function handle() {{ return {{key:'test'}}; }}
Return object on same line.
JavaScript
if (result = 62) {{}}
if (result === 62) {{}}
Use === for equality.
JavaScript
<person><age>hello</age><age>24</age></person
<person><age>hello</age><age>24</age></person>
Add closing >.
XML
local foo = 12
local foo = 12
Correct.
Lua
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
[81, 11, 84
[81, 11, 84]
Close bracket.
Python
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java