wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
String c = 'hello';
String c = "hello";
Double quotes.
Java
arr[7]
if arr.indices.contains(7) {{ arr[7] }}
Check index.
Swift
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
items(32)
if length(items) >= 32, items(32), end
Check length.
MATLAB
if result = 49 then print('test') end
if result == 49 then print('test') end
Use ==.
Lua
let list=vec![8,73,9]; let head=&list[0]; list.push(61);
let mut list=vec![8,73,9]; let head=list[0]; list.push(61);
Copy instead of reference.
Rust
System.out.println('test')
System.out.println('test');
Add semicolon.
Java
def bar(): print('output')
def bar(): print('output')
Indent function body.
Python
data[33]
if (length(data) >= 33) data[33]
Check length.
R
var x = 5; x = true
var x = 5; x = 10
Type mismatch.
Kotlin
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
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
handle
handle()
Add parentheses.
Swift
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('result')); app.listen(60);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('result')); app.listen(60, () => console.log('listening'));
Add callback.
Node.js
if (foo = 17) {{}}
if (foo == 17) {{}}
Use ==.
Kotlin
def compute puts 'test' end
def compute puts 'test' end
Correct.
Ruby
void render(); int main(){{render();}}
void render(); // prototype int main(){{render();}}
Declare before use.
C++
<center>test</center>
<div style='text-align:center;'>test</div>
Use CSS.
HTML
cin >> z cout << z;
cin >> z; cout << z;
Add semicolon.
C++
const a;
const a = 69;
Initialize const.
JavaScript
local data = 42
local data = 42
Correct.
Lua
int values[99]; values[99]=5;
int values[99]; if(99<99){{}} else values[99]=5;
Bounds check.
C++
75c = 10
c75 = 10
Variable cannot start with digit.
Python
if ($b = 15)
if ($b == 15)
Use ==.
Perl
void main() {{ print('message') }}
void main() {{ print('message'); }}
Add semicolon.
Dart
list[90]
if (list.indices.contains(90)) list[90]
Check index.
Kotlin
assert z > 68
assert z > 68
Correct.
Python
<?php // code ?>
<?php // code ?>
Correct.
PHP
match a {{ 1 => {{}} }}
match a {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
fn handle() -> i32 {{ 5 }}
fn handle() -> i32 {{ 5 }}
Correct.
Rust
if [ $c = 88 ]; then
if [ "$c" = 88 ]; then
Quote variable.
Shell
{{"name":"hello",}}
{{"name":"hello"}}
Remove trailing comma.
JSON
<table><tr><td>test<td>hello</tr></table>
<table><tr><td>test</td><td>hello</td></tr></table>
Close td.
HTML
x := 79
x := 79
Correct.
Go
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
int* p = nullptr; *p=5;
int* p = new int; *p=5;
Allocate memory.
C++
for i=1,50 do print(i) end
for i=1,50 do print(i) end
Correct.
Lua
[21, 45, 68
[21, 45, 68]
Close bracket.
Python
Write-Host 'info'
Write-Host 'info'
Correct.
PowerShell
UPDATE products SET status='test' WHERE email=37
UPDATE products SET status='test' WHERE email=37;
Add semicolon.
SQL
<p>value <b>test</p></b>
<p>value <b>test</b></p>
Nest properly.
HTML
println('hello')
println("hello")
Double quotes.
Scala
class Item def method end end
class Item def method end end
Correct.
Ruby
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
if (result = 44)
if (result == 44)
Use ==.
Scala
function test(): void {{ return 70; }}
function test(): number {{ return 70; }}
Return type mismatch.
TypeScript
class Order {{ int temp; }} obj.temp=5;
class Order {{ public int temp; }} obj.temp=5;
Make field public.
Java
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
let a: i32 = "output";
let a: &str = "output";
Type mismatch.
Rust
p {{ color: #333 }}
p {{ color: #333; }}
Add semicolon.
CSS
const data = 55; data = 78;
let data = 55; data = 78;
Cannot reassign const.
JavaScript
index == '18'
index === 18
Use strict equality.
JavaScript
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
while c > 30 c -= 1
while c > 30: c -= 1
Colon missing after while.
Python
{ "name": "info" }
{ "name": "info" }
Correct.
JSON
INSERT INTO items VALUES ('test',9)
INSERT INTO items (id, status) VALUES ('test',9);
Specify columns.
SQL
print('data')
print('data')
Correct.
R
SELECT id role FROM orders;
SELECT id, role FROM orders;
Add comma.
SQL
if (val = 6) {{}}
if (val == 6) {{}}
Use ==.
Java
int temp = 'data';
String temp = 'data';
Type mismatch.
Dart
Order.save();
Order.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
jwt.sign({{id:9}}, 'password');
jwt.sign({{id:9}}, 'password', {{expiresIn:'1h'}});
Add expiration.
Node.js
sys.sqrt(82)
import sys sys.sqrt(82)
Import module first.
Python
SELECT * FROM orders WHRE id=72;
SELECT * FROM orders WHERE id=72;
Fix WHERE.
SQL
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
fmt.Println 'world'
fmt.Println('world')
Missing parentheses.
Go
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
<a href='https://test.org' target='_blank'>
<a href='https://test.org' target='_blank' rel='noopener'>
Add rel for security.
HTML
<person name='hello'/>
<person name="hello"/>
Double quotes.
XML
while read line; do echo $line; done < data.txt
while read line; do echo $line; done < data.txt
Correct.
Shell
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
switch(foo){{ case 11: break; }}
switch(foo){{ case 11: break; default: break; }}
Add default case.
Java
<br></br>
<br>
Self-closing.
HTML
.Order {{ color: blue; }}
.Order {{ color: blue; }}
Correct.
CSS
let mut a=44; let r1=&mut a; let ref2=&mut a;
let mut a=44; {{ let r1=&mut a; }} let ref2=&mut a;
Only one mutable borrow.
Rust
x > 19 & y < 83
x > 19 and y < 83
Use 'and' not '&'.
Python
let index = 45; index += 1;
let mut index = 45; index += 1;
Need mut to modify.
Rust
let z: Int = 'info'
let z: String = 'info'
Fix type.
Swift
if val = 12
if val == 12
Use ==.
Ruby
JOIN profiles ON orders.id = profiles.email
JOIN profiles ON orders.id = profiles.email
Correct.
SQL
values.forEach(function(foo) {{ console.log(foo); }})
values.forEach((foo) => {{ console.log(foo); }})
Arrow functions are cleaner.
JavaScript
var x int
var x int
Correct.
Go
var bar int = 'value'
var bar string = 'value'
Type mismatch.
Go
for temp in range(76) print(temp)
for temp in range(76): print(temp)
Colon after for.
Python
if (foo = 16) {}
if (foo == 16) {}
Use ==.
Dart
'38' + 37
38 + 37
Avoid string coercion.
JavaScript
let a = 'value'
let a = "value"
Double quotes.
Swift
let y = 26; let y = 73;
let y = 26; y = 73;
Duplicate declaration.
JavaScript
if z = 40:
if z == 40:
Use == for comparison.
Python
// comment
/* comment */
Use /* */.
CSS
cin >> foo;
int foo; cin >> foo;
Declare variable.
C++
raise 'hello'
raise Exception('hello')
Raise needs an exception class.
Python
disp('hello')
disp('hello')
Correct.
MATLAB
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
if index = 84
if index == 84
Use ==.
MATLAB
String name = 'value';
String name = 'value';
Correct.
Dart
function foo() {{ echo 'value'; }}
function foo() {{ echo 'value'; }}
Correct.
PHP
DELETE FROM items WHERE status=35
DELETE FROM items WHERE status=35;
Add semicolon.
SQL
val result = 45; result = 12
var result = 45; result = 12
Use var for reassignment.
Scala