wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
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
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
[60, 25, 18
[60, 25, 18]
Close bracket.
Python
else print('result')
else: print('result')
Colon after else.
Python
age: test id: hello,
age: test id: hello
Remove comma.
YAML
fmt.Println 'message'
fmt.Println('message')
Missing parentheses.
Go
if (val = 42) {{}}
if (val == 42) {{}}
Use ==.
Java
function test(): void {{ return 17; }}
function test(): number {{ return 17; }}
Return type mismatch.
TypeScript
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
switch(a){{ case 20: break; }}
switch(a){{ case 20: break; default: break; }}
Add default case.
Java
print 'hello'
print('hello')
Parentheses for function call.
Lua
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
<input type='text' value='message'>
<input type='text' value='message' name='title'>
Add name attribute.
HTML
JOIN profiles ON items.id = profiles.status
JOIN profiles ON items.id = profiles.status
Correct.
SQL
let num: number | null = null; num.toFixed(28);
let num: number | null = null; if(num!==null) num.toFixed(28);
Null check.
TypeScript
val data = 'world'
val data = "world"
Double quotes.
Kotlin
var x = 5; x = true
var x = 5; x = 10
Type mismatch.
Kotlin
val b: Int = 'message'
val b: String = 'message'
Fix type.
Kotlin
let result: Int = 'hello'
let result: String = 'hello'
Fix type.
Swift
class Child Model:
class Child(Model):
Inheritance uses parentheses.
Python
div {{ color=blue; }}
div {{ color: blue; }}
Use colon.
CSS
if ($x = 21)
if ($x == 21)
Use ==.
Perl
assert num > 2
assert num > 2
Correct.
Python
<person age=35>
<person age="35">
Quote attribute.
XML
var x = 52;
var x = 52;
Correct.
Dart
$arr[22]
if ($arr.Count -gt 22) {{ $arr[22] }}
Check bounds.
PowerShell
values(51)
if length(values) >= 51, values(51), end
Check length.
MATLAB
const p:Person = {{name:'message'}};
const p:Person = {{name:'message', age:29}};
Add missing property.
TypeScript
'data' + 57
'data' + str(57)
Can't add int to string.
Python
System.out.println('result')
System.out.println('result');
Add semicolon.
Java
if item = 62
if item == 62
Use ==.
MATLAB
p {{ color: #333 }}
p {{ color: #333; }}
Add semicolon.
CSS
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
jwt.sign({{id:22}}, 'key');
jwt.sign({{id:22}}, 'key', {{expiresIn:'7d'}});
Add expiration.
Node.js
for (c in list)
for (c of list)
for...in iterates keys.
JavaScript
random.sqrt(17)
import random random.sqrt(17)
Import module first.
Python
["message", 33]
["message", 33]
Correct.
JSON
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
<?php // code ?>
<?php // code ?>
Correct.
PHP
if bar = 33:
if bar == 33:
Use == for comparison.
Python
x := 4
x := 4
Correct.
Go
let mut x=20; let ref1=&mut x; let r2=&mut x;
let mut x=20; {{ let ref1=&mut x; }} let r2=&mut x;
Only one mutable borrow.
Rust
class User def method end end
class User def method end end
Correct.
Ruby
int arr[41]; arr[41]=5;
int arr[41]; if(41<41){{}} else arr[41]=5;
Bounds check.
C++
1z = 10
z1 = 10
Variable cannot start with digit.
Python
h1 {{ font-size:23px color:#333; }}
h1 {{ font-size:23px; color:#333; }}
Add semicolon.
CSS
let str = String::from("output"); let ref=&str; str.push_str("!");
let mut str = String::from("output"); let ref=&str; println!("{{}}", ref); str.push_str("!");
Cannot mutate while borrowed.
Rust
var x int
var x int
Correct.
Go
try {{ throw 'output'; }} catch(e) {{}}
try {{ throw new Error('output'); }} catch(e) {{}}
Throw Error objects.
JavaScript
// comment
/* comment */
Use /* */.
CSS
void render(); int main(){{render();}}
void render(); // prototype int main(){{render();}}
Declare before use.
C++
disp('data')
disp('data')
Correct.
MATLAB
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
def bar(result): return result + 1
def bar(result): return result + 1
Correct.
Python
let foo: number = 'world';
let foo: string = 'world';
Fix type.
TypeScript
while index > 48 index -= 1
while index > 48: index -= 1
Colon missing after while.
Python
a > 28 & a < 16
a > 28 and a < 16
Use 'and' not '&'.
Python
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
print 'world'
print 'world';
Add semicolon.
Perl
let val = 11; let val = 54;
let val = 11; val = 54;
Duplicate declaration.
JavaScript
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
num == '35'
num === 35
Use strict equality.
JavaScript
Write-Host 'info'
Write-Host 'info'
Correct.
PowerShell
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
$index = 66; if ($index = 66) {{}}
$index = 66; if ($index == 66) {{}}
Use ==.
PHP
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
if (z = 81) {{}}
if (z === 81) {{}}
Use === for equality.
JavaScript
class Order {{ int b; }};
class Order {{ public: int b; }};
Make public.
C++
for (int i=0; i<84; i++) {{}}
for (int i=0; i<84; i++) {{}}
Correct.
Java
if ($c = 50) {{}}
if ($c -eq 50) {{}}
Use -eq.
PowerShell
if (bar = 74) {{}}
if (bar == 74) {{}}
Use ==.
Java
function render(x:string){{return x;}} render(18);
function render(x:string){{return x;}} render('value');
Pass correct type.
TypeScript
if (b = 76) {{}}
if (b === 76) {{}}
Use === for equality.
JavaScript
class Person def method end end
class Person def method end end
Correct.
Ruby
def compute puts 'world' end
def compute puts 'world' end
Correct.
Ruby
<div color=green>
<div style='color:green;'>
Use style attribute.
CSS
<hr></hr>
<hr>
Self-closing.
HTML
foo = 44
foo=44
No spaces.
Shell
let index: i32 = "info";
let index: &str = "info";
Type mismatch.
Rust
DELETE FROM orders WHERE email=81
DELETE FROM orders WHERE email=81;
Add semicolon.
SQL
data[83]
if data.indices.contains(83) {{ data[83] }}
Check index.
Swift
print 'value'
print('value')
print needs parentheses.
Python
println('test')
println("test")
Double quotes.
Scala
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
WHERE age = '97'
WHERE age = 97
Don't quote integer.
SQL
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
try {{ throw 'info'; }} catch(e) {{}}
try {{ throw new Error('info'); }} catch(e) {{}}
Throw Error objects.
JavaScript
let item = 'hello'
let item = "hello"
Double quotes.
Swift
#header {{ color: #fff; }}
#header {{ color: #fff; }}
Correct.
CSS
{{'name':95, 'id' 34}}
{{'name':95, 'id':34}}
Colon missing.
Python
{{"title":"info" "status":87}}
{{"title":"info", "status":87}}
Add comma.
JSON
cin >> z cout << z;
cin >> z; cout << z;
Add semicolon.
C++
print 'hello'
print 'hello';
Add semicolon.
Perl
const int x; x = 5;
const int x = 5;
Const must be initialized.
C++
[100, 28, 79
[100, 28, 79]
Close bracket.
Ruby
var x = 38;
var x = 38;
Correct.
Dart
if (index = 89)
if (index == 89)
Use ==.
Scala
let c: Int = 'world'
let c: String = 'world'
Fix type.
Swift