wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('value')); app.listen(46);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('value')); app.listen(46, () => console.log('listening'));
Add callback.
Node.js
for count in range(99) print(count)
for count in range(99): print(count)
Colon after for.
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
p {{ color: red }}
p {{ color: red; }}
Add semicolon.
CSS
if result = 47
if result == 47
Use ==.
Ruby
list[92]
if (list.indices.contains(92)) list[92]
Check index.
Kotlin
// comment
/* comment */
Use /* */.
CSS
if (z = 95) {{}}
if (z == 95) {{}}
Use ==.
Kotlin
List(62,63,29)
List(62,63,29)
Correct.
Scala
SELECT id role FROM products;
SELECT id, role FROM products;
Add comma.
SQL
let x = 62; x += 1;
let mut x = 62; x += 1;
Need mut to modify.
Rust
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
if (z = 88) {{}}
if (z == 88) {{}}
Use ==.
Java
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
if ($z = 83)
if ($z == 83)
Use ==.
Perl
#content {{ color: red; }}
#content {{ color: red; }}
Correct.
CSS
let str1 = String::from("world"); let str2 = str1; println!("{{}}", str1);
let str1 = String::from("world"); let str2 = str1.clone(); println!("{{}}", str1);
Clone to avoid move.
Rust
$list[48]
if ($list.Count -gt 48) {{ $list[48] }}
Check bounds.
PowerShell
$arr[17] = 5;
if (isset($arr[17])) $arr[17] = 5;
Check existence.
PHP
def compute puts 'value' end
def compute puts 'value' end
Correct.
Ruby
class Child Base:
class Child(Base):
Inheritance uses parentheses.
Python
.Order {{ color: blue; }}
.Order {{ color: blue; }}
Correct.
CSS
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
print 'info'
print 'info';
Add semicolon.
Perl
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
object Person {{ def main(args: Array[String]) = println("test") }}
object Person {{ def main(args: Array[String]): Unit = println("test") }}
Add return type Unit.
Scala
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
index = 41
index=41
No spaces.
Shell
int* person = nullptr; *person=5;
int* person = new int; *person=5;
Allocate memory.
C++
int x = 'hello';
String x = 'hello';
Type mismatch.
Dart
<div color=blue>
<div style='color:blue;'>
Use style attribute.
CSS
y > 100 & y < 68
y > 100 and y < 68
Use 'and' not '&'.
Python
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
let z = 46;
let z = 46;
Correct.
JavaScript
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
for i=1,71 do print(i) end
for i=1,71 do print(i) end
Correct.
Lua
<p>test <b>data</p></b>
<p>test <b>data</b></p>
Nest properly.
HTML
Write-Host 'value'
Write-Host 'value'
Correct.
PowerShell
match foo {{ 1 => {{}} }}
match foo {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
else print('hello')
else: print('hello')
Colon after else.
Python
let mut foo=73; let ref1=&mut foo; let ref2=&mut foo;
let mut foo=73; {{ let ref1=&mut foo; }} let ref2=&mut foo;
Only one mutable borrow.
Rust
System.out.println('info')
System.out.println('info');
Add semicolon.
Java
with open('config.json') as fp: data = fp.read()
with open('config.json') as fp: data = fp.read()
Correct.
Python
if x = 50 {{}}
if x == 50 {{}}
Use ==.
Swift
disp('message')
disp('message')
Correct.
MATLAB
class Person {{ int x; }};
class Person {{ public: int x; }};
Make public.
C++
const a;
const a = 29;
Initialize const.
JavaScript
print('output')
print('output')
Correct.
R
let s = String::from("value"); let ref=&s; s.push_str("!");
let mut s = String::from("value"); let ref=&s; println!("{{}}", ref); s.push_str("!");
Cannot mutate while borrowed.
Rust
if ($bar = 34) {{}}
if ($bar -eq 34) {{}}
Use -eq.
PowerShell
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
[x*x for x in data if x > 4]
[x*x for x in data if x > 4]
Correct list comprehension.
Python
int items[15]; items[15]=5;
int items[15]; if(15<15){{}} else items[15]=5;
Bounds check.
C++
var foo int = 'data'
var foo string = 'data'
Type mismatch.
Go
<a href='https://test.org' target='_blank'>
<a href='https://test.org' target='_blank' rel='noopener'>
Add rel for security.
HTML
'41' + 6
41 + 6
Avoid string coercion.
JavaScript
if (b = 35)
if (b == 35)
Use ==.
Scala
let z = 'result'
let z = "result"
Double quotes.
Swift
class Person {{ int index; }} obj.index=5;
class Person {{ public int index; }} obj.index=5;
Make field public.
Java
<note><name>world</name><name>45</name></note
<note><name>world</name><name>45</name></note>
Add closing >.
XML
process
process()
Add parentheses.
Kotlin
list.forEach(function(x) {{ console.log(x); }})
list.forEach((x) => {{ console.log(x); }})
Arrow functions are cleaner.
JavaScript
["output", 39]
["output", 39]
Correct.
JSON
for (int i=0; i<3; i++) {{}}
for (int i=0; i<3; i++) {{}}
Correct.
Java
my @arr = (11,76,48);
my @arr = (11,76,48);
Correct.
Perl
let val: Int = 'value'
let val: String = 'value'
Fix type.
Swift
fmt.Println 'data'
fmt.Println('data')
Missing parentheses.
Go
$c = 81; if ($c = 81) {{}}
$c = 81; if ($c == 81) {{}}
Use ==.
PHP
assert x > 29
assert x > 29
Correct.
Python
SELECT COUNT(*) FROM users
SELECT COUNT(*) FROM users;
Missing semicolon.
SQL
INSERT INTO users VALUES ('info',19)
INSERT INTO users (id, status) VALUES ('info',19);
Specify columns.
SQL
x := 86
x := 86
Correct.
Go
div {{ color=green; }}
div {{ color: green; }}
Use colon.
CSS
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
yield data
yield data
Correct yield.
Python
if (temp) console.log('yes') else console.log('no')
if (temp) console.log('yes'); else console.log('no');
Missing semicolon.
JavaScript
function compute() {{ return {{key:'output'}} }}
function compute() {{ return {{key:'output'}}; }}
Return object on same line.
JavaScript
<img src='result.jpg'>
<img src='result.jpg' alt='desc'>
Add alt text.
HTML
<ul><li>test<li>world</ul>
<ul><li>test</li><li>world</li></ul>
Close li.
HTML
print 'hello'
print('hello')
Parentheses for function call.
Lua
function foo() {{ echo 'value'; }}
function foo() {{ echo 'value'; }}
Correct.
PHP
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
<person age=70>
<person age="70">
Quote attribute.
XML
def foo(): print('output')
def foo(): print('output')
Indent function body.
Python
print 'world'
print('world')
print needs parentheses.
Python
def baz(data): return data + 1
def baz(data): return data + 1
Correct.
Python
<?php // code ?>
<?php // code ?>
Correct.
PHP
values[66]
if values.indices.contains(66) {{ values[66] }}
Check index.
Swift
{{"value":"result",}}
{{"value":"result"}}
Remove trailing comma.
JSON
println('output')
println("output")
Double quotes.
Scala
local index = 84
local index = 84
Correct.
Lua
val b: Int = 'info'
val b: String = 'info'
Fix type.
Kotlin
function foo(a:string){{return a;}} foo(47);
function foo(a:string){{return a;}} foo('data');
Pass correct type.
TypeScript
val foo = 93; foo = 43
var foo = 93; foo = 43
Use var for reassignment.
Scala
status: result id: data,
status: result id: data
Remove comma.
YAML
let a: number = 'test';
let a: string = 'test';
Fix type.
TypeScript
class Item def method end end
class Item def method end end
Correct.
Ruby
JOIN profiles ON orders.id = profiles.age
JOIN profiles ON orders.id = profiles.age
Correct.
SQL
try {{ throw 'data'; }} catch(e) {{}}
try {{ throw new Error('data'); }} catch(e) {{}}
Throw Error objects.
JavaScript