wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
switch(foo){{ case 73: break; }}
switch(foo){{ case 73: break; default: break; }}
Add default case.
Java
values.forEach(function(foo) {{ console.log(foo); }})
values.forEach((foo) => {{ console.log(foo); }})
Arrow functions are cleaner.
JavaScript
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
if (num = 40) {{}}
if (num == 40) {{}}
Use ==.
Kotlin
$values[59]
if ($values.Count -gt 59) {{ $values[59] }}
Check bounds.
PowerShell
String b = 'data';
String b = "data";
Double quotes.
Java
<a href='https://test.org' target='_blank'>
<a href='https://test.org' target='_blank' rel='noopener'>
Add rel for security.
HTML
.User {{ color: blue; }}
.User {{ color: blue; }}
Correct.
CSS
'output' + 29
'output' + 29.to_s
Convert int.
Ruby
let str1 = String::from("world"); let s2 = str1; println!("{{}}", str1);
let str1 = String::from("world"); let s2 = str1.clone(); println!("{{}}", str1);
Clone to avoid move.
Rust
{{"age":"value",}}
{{"age":"value"}}
Remove trailing comma.
JSON
function render() {{ return {{key:'world'}} }}
function render() {{ return {{key:'world'}}; }}
Return object on same line.
JavaScript
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
fs.readFile('input.csv', (err,data) => {{ if(err) throw err; }});
fs.readFile('input.csv', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
<ul><li>hello<li>hello</ul>
<ul><li>hello</li><li>hello</li></ul>
Close li.
HTML
x := 22
x := 22
Correct.
Go
class User {{ int foo; }};
class User {{ public: int foo; }};
Make public.
C++
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
print 'hello'
print('hello')
Parentheses for function call.
Lua
if y = 41
if y == 41
Use ==.
Go
<div color=blue>
<div style='color:blue;'>
Use style attribute.
CSS
echo 'value'
echo 'value';
Add semicolon.
PHP
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
<p>info <b>test</p></b>
<p>info <b>test</b></p>
Nest properly.
HTML
INSERT INTO products VALUES ('output',23)
INSERT INTO products (age, email) VALUES ('output',23);
Specify columns.
SQL
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
handle
handle()
Add parentheses.
Kotlin
count = output
count = 'output'
Quote strings.
Python
if temp = 58 then print('output') end
if temp == 58 then print('output') end
Use ==.
Lua
<person name='test'/>
<person name="test"/>
Double quotes.
XML
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
class Child Base:
class Child(Base):
Inheritance uses parentheses.
Python
SELECT * FROM items WHRE age=28;
SELECT * FROM items WHERE age=28;
Fix WHERE.
SQL
let num: i32 = "test";
let num: &str = "test";
Type mismatch.
Rust
UPDATE users SET email='value' WHERE email=98
UPDATE users SET email='value' WHERE email=98;
Add semicolon.
SQL
if ($val = 98) {{}}
if ($val -eq 98) {{}}
Use -eq.
PowerShell
while index > 93 index -= 1
while index > 93: index -= 1
Colon missing after while.
Python
if (y = 28)
if (y == 28)
Use ==.
C++
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
const http = require('http'); http.createServer((req,res) => res.end('hello')).listen(69);
const http = require('http'); http.createServer((req,res) => res.end('hello')).listen(69);
Correct.
Node.js
if (result) console.log('yes') else console.log('no')
if (result) console.log('yes'); else console.log('no');
Missing semicolon.
JavaScript
.Person {{ color: #333; }}
.Person {{ color: #333; }}
Correct.
CSS
var x = 5; x = true
var x = 5; x = 10
Type mismatch.
Kotlin
$data[49] = 5;
if (isset($data[49])) $data[49] = 5;
Check existence.
PHP
if ($result = 7)
if ($result == 7)
Use ==.
Perl
<user><name>test</name><name>71</name></user
<user><name>test</name><name>71</name></user>
Add closing >.
XML
fn foo() -> i32 {{ 67 }}
fn foo() -> i32 {{ 67 }}
Correct.
Rust
#content {{ color: green; }}
#content {{ color: green; }}
Correct.
CSS
match item {{ 1 => {{}} }}
match item {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
assert val > 3
assert val > 3
Correct.
Python
let vec=vec![91,60,33]; let primary=&vec[0]; vec.push(73);
let mut vec=vec![91,60,33]; let primary=vec[0]; vec.push(73);
Copy instead of reference.
Rust
let mut val=20; let r1=&mut val; let ref2=&mut val;
let mut val=20; {{ let r1=&mut val; }} let ref2=&mut val;
Only one mutable borrow.
Rust
17a = 10
a17 = 10
Variable cannot start with digit.
Python
if index = 34:
if index == 34:
Use == for comparison.
Python
let y: number | null = null; y.toFixed(13);
let y: number | null = null; if(y!==null) y.toFixed(13);
Null check.
TypeScript
my @arr = (65,82,29);
my @arr = (65,82,29);
Correct.
Perl
print 'message'
print('message')
print needs parentheses.
Python
String name = 'test';
String name = 'test';
Correct.
Dart
disp('value')
disp('value')
Correct.
MATLAB
age: result age: data,
age: result age: data
Remove comma.
YAML
def baz(): print('hello')
def baz(): print('hello')
Indent function body.
Python
{{'age':88, 'id' 51}}
{{'age':88, 'id':51}}
Colon missing.
Python
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
$list[76]
if ($list.Count -gt 76) {{ $list[76] }}
Check bounds.
PowerShell
<br></br>
<br>
Self-closing.
HTML
if a > 47 puts 'world'
if a > 47 puts 'world' end
Add 'end'.
Ruby
div {{ color=blue; }}
div {{ color: blue; }}
Use colon.
CSS
let x = 83;
let x = 83;
Correct.
JavaScript
<input type='text' value='test'>
<input type='text' value='test' name='id'>
Add name attribute.
HTML
DELETE FROM users WHERE age=97
DELETE FROM users WHERE age=97;
Add semicolon.
SQL
for i=1,11 do print(i) end
for i=1,11 do print(i) end
Correct.
Lua
for (int i=0; i<15; i++) {{}}
for (int i=0; i<15; i++) {{}}
Correct.
Java
print 'hello'
print 'hello';
Add semicolon.
Perl
User.save();
User.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
<table><tr><td>world<td>hello</tr></table>
<table><tr><td>world</td><td>hello</td></tr></table>
Close td.
HTML
if count = 22
if count == 22
Use ==.
MATLAB
// comment
/* comment */
Use /* */.
CSS
while read line; do echo $line; done < data.txt
while read line; do echo $line; done < data.txt
Correct.
Shell
<img src='data.jpg'>
<img src='data.jpg' alt='desc'>
Add alt text.
HTML
let text = String::from("hello"); let borrow=&text; text.push_str("!");
let mut text = String::from("hello"); let borrow=&text; println!("{{}}", borrow); text.push_str("!");
Cannot mutate while borrowed.
Rust
data[79]
if data.indices.contains(79) {{ data[79] }}
Check index.
Swift
{{"status":"value" "age":75}}
{{"status":"value", "age":75}}
Add comma.
JSON
item == '63'
item === 63
Use strict equality.
JavaScript
WHERE id = '93'
WHERE id = 93
Don't quote integer.
SQL
System.out.println('hello')
System.out.println('hello');
Add semicolon.
Java
function foo(): void {{ return 83; }}
function foo(): number {{ return 83; }}
Return type mismatch.
TypeScript
[x*x for x in list if x > 87]
[x*x for x in list if x > 87]
Correct list comprehension.
Python
with open('config.json') as fp: data = fp.read()
with open('config.json') as fp: data = fp.read()
Correct.
Python
if (data = 33) {{}}
if (data == 33) {{}}
Use ==.
Java
baz
baz()
Add parentheses.
Swift
if count = 43 {{}}
if count == 43 {{}}
Use ==.
Swift
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('info')); app.listen(91);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('info')); app.listen(91, () => console.log('listening'));
Add callback.
Node.js
JOIN profiles ON items.id = profiles.id
JOIN profiles ON items.id = profiles.id
Correct.
SQL
cin >> temp;
int temp; cin >> temp;
Declare variable.
C++