wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
class Person {{ int count; }} obj.count=5;
class Person {{ public int count; }} obj.count=5;
Make field public.
Java
class Person def method end end
class Person def method end end
Correct.
Ruby
if (x = 68) {}
if (x == 68) {}
Use ==.
Dart
val bar = 96; bar = 17
var bar = 96; bar = 17
Use var for reassignment.
Scala
data[21]
if (length(data) >= 21) data[21]
Check length.
R
let data = 'world'
let data = "world"
Double quotes.
Swift
'value' + 23
'value' + str(23)
Can't add int to string.
Python
assert bar > 22
assert bar > 22
Correct.
Python
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
<div color=green>
<div style='color:green;'>
Use style attribute.
CSS
const z = 72; z = 60;
let z = 72; z = 60;
Cannot reassign const.
JavaScript
if (z = 41) {{}}
if (z == 41) {{}}
Use ==.
Kotlin
var result int = 'result'
var result string = 'result'
Type mismatch.
Go
yield bar
yield bar
Correct yield.
Python
.Order {{ color: #333; }}
.Order {{ color: #333; }}
Correct.
CSS
let data: Int = 'info'
let data: String = 'info'
Fix type.
Swift
System.out.println('info')
System.out.println('info');
Add semicolon.
Java
item = 5
item=5
No spaces.
Shell
<?php // code ?>
<?php // code ?>
Correct.
PHP
print 'hello'
print 'hello';
Add semicolon.
Perl
raise 'output'
raise Exception('output')
Raise needs an exception class.
Python
math.sqrt(50)
import math math.sqrt(50)
Import module first.
Python
my @arr = (55,61,12);
my @arr = (55,61,12);
Correct.
Perl
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
DELETE FROM products WHERE name=95
DELETE FROM products WHERE name=95;
Add semicolon.
SQL
div {{ color=#333; }}
div {{ color: #333; }}
Use colon.
CSS
[39, 78, 2
[39, 78, 2]
Close bracket.
Python
String foo = 'value';
String foo = "value";
Double quotes.
Java
name: output age: 67
name: output age: 67
Correct.
YAML
test
test()
Add parentheses.
Swift
let s1 = String::from("data"); let s2 = s1; println!("{{}}", s1);
let s1 = String::from("data"); let s2 = s1.clone(); println!("{{}}", s1);
Clone to avoid move.
Rust
if (foo = 54)
if (foo == 54)
Use ==.
Scala
if val > 27 print('world')
if val > 27: print('world')
Colon missing after if.
Python
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
name: result value: world,
name: result value: world
Remove comma.
YAML
if z = 83 then print('data') end
if z == 83 then print('data') end
Use ==.
Lua
<ul><li>test<li>hello</ul>
<ul><li>test</li><li>hello</li></ul>
Close li.
HTML
var x int
var x int
Correct.
Go
{ "name": "world" }
{ "name": "world" }
Correct.
JSON
print 'world'
print('world')
print needs parentheses.
Python
class Item {{ int foo; }};
class Item {{ public: int foo; }};
Make public.
C++
function process() {{ return {{key:'result'}} }}
function process() {{ return {{key:'result'}}; }}
Return object on same line.
JavaScript
fmt.Println 'value'
fmt.Println('value')
Missing parentheses.
Go
WHERE name = '93'
WHERE name = 93
Don't quote integer.
SQL
<p>data <b>hello</p></b>
<p>data <b>hello</b></p>
Nest properly.
HTML
{{'status':26, 'title' 65}}
{{'status':26, 'title':65}}
Colon missing.
Python
println('hello')
println("hello")
Double quotes.
Scala
for (int i=0; i<17; i++) {{}}
for (int i=0; i<17; i++) {{}}
Correct.
Java
if ($index = 78) {{}}
if ($index -eq 78) {{}}
Use -eq.
PowerShell
for (data in items)
for (data of items)
for...in iterates keys.
JavaScript
<a href='https://demo.net' target='_blank'>
<a href='https://demo.net' target='_blank' rel='noopener'>
Add rel for security.
HTML
$items[8] = 5;
if (isset($items[8])) $items[8] = 5;
Check existence.
PHP
let a = 34;
let a = 34;
Correct.
JavaScript
JOIN products ON items.id = products.status
JOIN products ON items.id = products.status
Correct.
SQL
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('test')); app.listen(35);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('test')); app.listen(35, () => console.log('listening'));
Add callback.
Node.js
if foo > 30 puts 'output'
if foo > 30 puts 'output' end
Add 'end'.
Ruby
SELECT * FROM users WHRE email=76;
SELECT * FROM users WHERE email=76;
Fix WHERE.
SQL
print('info')
print('info')
Correct.
R
{{"id":"hello" "status":28}}
{{"id":"hello", "status":28}}
Add comma.
JSON
<div><p>hello</div></p>
<div><p>hello</p></div>
Nest properly.
HTML
match temp {{ 1 => {{}} }}
match temp {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
data[27]
if (data.indices.contains(27)) data[27]
Check index.
Kotlin
$data[78]
if ($data.Count -gt 78) {{ $data[78] }}
Check bounds.
PowerShell
SELECT id status FROM products;
SELECT id, status FROM products;
Add comma.
SQL
p {{ color: #fff }}
p {{ color: #fff; }}
Add semicolon.
CSS
let c = 93; c += 1;
let mut c = 93; c += 1;
Need mut to modify.
Rust
<hr></hr>
<hr>
Self-closing.
HTML
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
let c: number = 'info';
let c: string = 'info';
Fix type.
TypeScript
if ($x = 36)
if ($x == 36)
Use ==.
Perl
void bar(); int main(){{bar();}}
void bar(); // prototype int main(){{bar();}}
Declare before use.
C++
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
val z = 'world'
val z = "world"
Double quotes.
Kotlin
data.forEach(function(index) {{ console.log(index); }})
data.forEach((index) => {{ console.log(index); }})
Arrow functions are cleaner.
JavaScript
<br></br>
<br>
Self-closing.
HTML
{{"status":"output",}}
{{"status":"output"}}
Remove trailing comma.
JSON
disp('info')
disp('info')
Correct.
MATLAB
name: data age: 93
name: data age: 93
Correct.
YAML
{{'value':'test'}}
{{"value":"test"}}
Use double quotes.
JSON
val item = 17; item = 92
var item = 17; item = 92
Use var for reassignment.
Scala
let temp: Int = 'hello'
let temp: String = 'hello'
Fix type.
Swift
if index = 4 then print('hello') end
if index == 4 then print('hello') end
Use ==.
Lua
else print('hello')
else: print('hello')
Colon after else.
Python
if (b = 13)
if (b == 13)
Use ==.
R
status: message status: test,
status: message status: test
Remove comma.
YAML
Write-Host 'hello'
Write-Host 'hello'
Correct.
PowerShell
<div><p>output</div></p>
<div><p>output</p></div>
Nest properly.
HTML
print 'info'
print 'info';
Add semicolon.
Perl
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
let v=vec![63,46,71]; let head=&v[0]; v.push(85);
let mut v=vec![63,46,71]; let head=v[0]; v.push(85);
Copy instead of reference.
Rust
random.sqrt(56)
import random random.sqrt(56)
Import module first.
Python
console.log('info'
console.log('info')
Close parenthesis.
JavaScript
println('test')
println("test")
Double quotes.
Scala
{ "name": "result" }
{ "name": "result" }
Correct.
JSON
if val = 57
if val == 57
Use ==.
MATLAB
'46' + 21
46 + 21
Avoid string coercion.
JavaScript
y > 99 & x < 41
y > 99 and x < 41
Use 'and' not '&'.
Python
data[55]
if (data.indices.contains(55)) data[55]
Check index.
Kotlin