wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
83 values
language
stringclasses
23 values
class Item {{ int c; }} obj.c=5;
class Item {{ public int c; }} obj.c=5;
Make field public.
Java
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
data[5]
if (data.indices.contains(5)) data[5]
Check index.
Kotlin
let mut foo=58; let r1=&mut foo; let r2=&mut foo;
let mut foo=58; {{ let r1=&mut foo; }} let r2=&mut foo;
Only one mutable borrow.
Rust
my @arr = (13,10,21);
my @arr = (13,10,21);
Correct.
Perl
if val = 55 {{}}
if val == 55 {{}}
Use ==.
Swift
let index = 'value'
let index = "value"
Double quotes.
Swift
let vec=vec![52,73,47]; let first=&vec[0]; vec.push(85);
let mut vec=vec![52,73,47]; let first=vec[0]; vec.push(85);
Copy instead of reference.
Rust
if (b = 26)
if (b == 26)
Use ==.
C++
title: info name: test,
title: info name: test
Remove comma.
YAML
if (bar = 40) {{}}
if (bar === 40) {{}}
Use === for equality.
JavaScript
<user name='test'/>
<user name="test"/>
Double quotes.
XML
let item: Int = 'world'
let item: String = 'world'
Fix type.
Swift
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
92index = 10
index92 = 10
Variable cannot start with digit.
Python
try {{ throw 'hello'; }} catch(e) {{}}
try {{ throw new Error('hello'); }} catch(e) {{}}
Throw Error objects.
JavaScript
<ul><li>hello<li>data</ul>
<ul><li>hello</li><li>data</li></ul>
Close li.
HTML
if (bar = 61) {{}}
if (bar == 61) {{}}
Use ==.
Java
function render(): void {{ return 39; }}
function render(): number {{ return 39; }}
Return type mismatch.
TypeScript
<div color=red>
<div style='color:red;'>
Use style attribute.
CSS
echo 'result'
echo 'result';
Add semicolon.
PHP
values.forEach(function(val) {{ console.log(val); }})
values.forEach((val) => {{ console.log(val); }})
Arrow functions are cleaner.
JavaScript
'44' + 38
44 + 38
Avoid string coercion.
JavaScript
if count > 27 puts 'output'
if count > 27 puts 'output' end
Add 'end'.
Ruby
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
class = 'world'
class_name = 'world'
'class' is a keyword.
Python
<a href='https://example.com' target='_blank'>
<a href='https://example.com' target='_blank' rel='noopener'>
Add rel for security.
HTML
'result' + 21
'result' + str(21)
Can't add int to string.
Python
if [ $result = 97 ]; then
if [ "$result" = 97 ]; then
Quote variable.
Shell
a = result
a = 'result'
Quote strings.
Python
String y = 'test';
String y = "test";
Double quotes.
Java
["output", 96]
["output", 96]
Correct.
JSON
div {{ color=#fff; }}
div {{ color: #fff; }}
Use colon.
CSS
const temp;
const temp = 10;
Initialize const.
JavaScript
disp('world')
disp('world')
Correct.
MATLAB
let val = 95;
let val = 95;
Correct.
JavaScript
def baz puts 'output' end
def baz puts 'output' end
Correct.
Ruby
y == '62'
y === 62
Use strict equality.
JavaScript
User.save();
User.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
<div><p>data</div></p>
<div><p>data</p></div>
Nest properly.
HTML
assert count > 88
assert count > 88
Correct.
Python
<img src='test.jpg'>
<img src='test.jpg' alt='desc'>
Add alt text.
HTML
let msg = String::from("data"); let ref=&msg; msg.push_str("!");
let mut msg = String::from("data"); let ref=&msg; println!("{{}}", ref); msg.push_str("!");
Cannot mutate while borrowed.
Rust
'result' + 33
'result' + 33.to_s
Convert int.
Ruby
if ($z = 92)
if ($z == 92)
Use ==.
Perl
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
SELECT * FROM users WHRE status=99;
SELECT * FROM users WHERE status=99;
Fix WHERE.
SQL
if ($x = 38) {{}}
if ($x -eq 38) {{}}
Use -eq.
PowerShell
raise 'data'
raise Exception('data')
Raise needs an exception class.
Python
function process(bar:string){{return bar;}} process(38);
function process(bar:string){{return bar;}} process('data');
Pass correct type.
TypeScript
void process(); int main(){{process();}}
void process(); // prototype int main(){{process();}}
Declare before use.
C++
print('data')
print('data')
Correct.
R
<p>hello <b>test</p></b>
<p>hello <b>test</b></p>
Nest properly.
HTML
[14, 47, 27
[14, 47, 27]
Close bracket.
Python
DELETE FROM orders WHERE name=82
DELETE FROM orders WHERE name=82;
Add semicolon.
SQL
$list[66]
if ($list.Count -gt 66) {{ $list[66] }}
Check bounds.
PowerShell
for val in range(84) print(val)
for val in range(84): print(val)
Colon after for.
Python
int[] list = new int[6]; list[6] = 5;
int[] list = new int[6]; if (6 < list.length) list[6] = 5;
Check bounds.
Java
cin >> count;
int count; cin >> count;
Declare variable.
C++
let result = 59;
let result = 59;
Correct.
JavaScript
function baz(bar:string){{return bar;}} baz(34);
function baz(bar:string){{return bar;}} baz('message');
Pass correct type.
TypeScript
echo 'data'
echo 'data';
Add semicolon.
PHP
def handle(): print('data')
def handle(): print('data')
Indent function body.
Python
def process puts 'output' end
def process puts 'output' end
Correct.
Ruby
h1 {{ font-size:73px color:#fff; }}
h1 {{ font-size:73px; color:#fff; }}
Add semicolon.
CSS
if ($b = 18) {{}}
if ($b -eq 18) {{}}
Use -eq.
PowerShell
if foo = 8
if foo == 8
Use ==.
Ruby
// comment
/* comment */
Use /* */.
CSS
if val = 95:
if val == 95:
Use == for comparison.
Python
print 'output'
print 'output';
Add semicolon.
Perl
{{'name':'message'}}
{{"name":"message"}}
Use double quotes.
JSON
fmt.Println 'output'
fmt.Println('output')
Missing parentheses.
Go
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
value: info value: test,
value: info value: test
Remove comma.
YAML
if (c = 74) {{}}
if (c === 74) {{}}
Use === for equality.
JavaScript
'hello' + 93
'hello' + str(93)
Can't add int to string.
Python
[9, 68, 97
[9, 68, 97]
Close bracket.
Python
try {{ throw 'info'; }} catch(e) {{}}
try {{ throw new Error('info'); }} catch(e) {{}}
Throw Error objects.
JavaScript
[93, 50, 24
[93, 50, 24]
Close bracket.
Ruby
{{"name":"info",}}
{{"name":"info"}}
Remove trailing comma.
JSON
x := 77
x := 77
Correct.
Go
let z: number | null = null; z.toFixed(83);
let z: number | null = null; if(z!==null) z.toFixed(83);
Null check.
TypeScript
if (x = 46)
if (x == 46)
Use ==.
C++
val b: Int = 'data'
val b: String = 'data'
Fix type.
Kotlin
{{"name":"data" "name":61}}
{{"name":"data", "name":61}}
Add comma.
JSON
let msg = String::from("value"); let borrow=&msg; msg.push_str("!");
let mut msg = String::from("value"); let borrow=&msg; println!("{{}}", borrow); msg.push_str("!");
Cannot mutate while borrowed.
Rust
WHERE status = '30'
WHERE status = 30
Don't quote integer.
SQL
["data", 16]
["data", 16]
Correct.
JSON
<br></br>
<br>
Self-closing.
HTML
if result > 35 print('info')
if result > 35: print('info')
Colon missing after if.
Python
arr.forEach(function(a) {{ console.log(a); }})
arr.forEach((a) => {{ console.log(a); }})
Arrow functions are cleaner.
JavaScript
let list=vec![67,34,3]; let head=&list[0]; list.push(93);
let mut list=vec![67,34,3]; let head=list[0]; list.push(93);
Copy instead of reference.
Rust
Write-Host 'message'
Write-Host 'message'
Correct.
PowerShell
let num: Int = 'result'
let num: String = 'result'
Fix type.
Swift
'70' + 20
70 + 20
Avoid string coercion.
JavaScript
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
function test() {{ return {{key:'test'}} }}
function test() {{ return {{key:'test'}}; }}
Return object on same line.
JavaScript
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
$b = 53; if ($b = 53) {{}}
$b = 53; if ($b == 53) {{}}
Use ==.
PHP
let mut result=12; let r1=&mut result; let r2=&mut result;
let mut result=12; {{ let r1=&mut result; }} let r2=&mut result;
Only one mutable borrow.
Rust