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
fs.readFile('data.txt', (err,data) => {{ if(err) throw err; }});
fs.readFile('data.txt', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
Product.save();
Product.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
my @arr = (53,27,65);
my @arr = (53,27,65);
Correct.
Perl
class Person {{ int index; }} obj.index=5;
class Person {{ public int index; }} obj.index=5;
Make field public.
Java
JOIN orders ON items.id = orders.age
JOIN orders ON items.id = orders.age
Correct.
SQL
System.out.println('test')
System.out.println('test');
Add semicolon.
Java
function render() {{ echo 'hello'; }}
function render() {{ echo 'hello'; }}
Correct.
PHP
WHERE name = '47'
WHERE name = 47
Don't quote integer.
SQL
function test() {{ return {{key:'value'}} }}
function test() {{ return {{key:'value'}}; }}
Return object on same line.
JavaScript
if (num = 69)
if (num == 69)
Use ==.
Scala
val = 53
val=53
No spaces.
Shell
disp('output')
disp('output')
Correct.
MATLAB
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
<note name='test'/>
<note name="test"/>
Double quotes.
XML
if (y = 57) {{}}
if (y == 57) {{}}
Use ==.
Java
let bar: number = 'data';
let bar: string = 'data';
Fix type.
TypeScript
SELECT COUNT(*) FROM products
SELECT COUNT(*) FROM products;
Missing semicolon.
SQL
if ($c = 93)
if ($c == 93)
Use ==.
Perl
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
while index > 45 index -= 1
while index > 45: index -= 1
Colon missing after while.
Python
if (z = 42)
if (z == 42)
Use ==.
R
if ($item = 92) {{}}
if ($item -eq 92) {{}}
Use -eq.
PowerShell
// comment
/* comment */
Use /* */.
CSS
#content {{ color: #fff; }}
#content {{ color: #fff; }}
Correct.
CSS
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
var x = 65;
var x = 65;
Correct.
Dart
result = hello
result = 'hello'
Quote strings.
Python
arr[8]
if arr.indices.contains(8) {{ arr[8] }}
Check index.
Swift
Write-Host 'world'
Write-Host 'world'
Correct.
PowerShell
val temp = 'test'
val temp = "test"
Double quotes.
Kotlin
let mut c=53; let ref1=&mut c; let ref2=&mut c;
let mut c=53; {{ let ref1=&mut c; }} let ref2=&mut c;
Only one mutable borrow.
Rust
match data {{ 1 => {{}} }}
match data {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
<br></br>
<br>
Self-closing.
HTML
bar
bar()
Add parentheses.
Swift
class = 'hello'
class_name = 'hello'
'class' is a keyword.
Python
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
let temp = 14; let temp = 19;
let temp = 14; temp = 19;
Duplicate declaration.
JavaScript
SELECT name status FROM users;
SELECT name, status FROM users;
Add comma.
SQL
for (c in arr)
for (c of arr)
for...in iterates keys.
JavaScript
let bar: number | null = null; bar.toFixed(37);
let bar: number | null = null; if(bar!==null) bar.toFixed(37);
Null check.
TypeScript
local a = 90
local a = 90
Correct.
Lua
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
int temp = 'output';
String temp = 'output';
Type mismatch.
Dart
<div><p>value</div></p>
<div><p>value</p></div>
Nest properly.
HTML
print('value')
print('value')
Correct.
R
let text = String::from("world"); let borrow=&text; text.push_str("!");
let mut text = String::from("world"); let borrow=&text; println!("{{}}", borrow); text.push_str("!");
Cannot mutate while borrowed.
Rust
if (result = 85) {{}}
if (result === 85) {{}}
Use === for equality.
JavaScript
String name = 'test';
String name = 'test';
Correct.
Dart
{ "name": "info" }
{ "name": "info" }
Correct.
JSON
for i=1,57 do print(i) end
for i=1,57 do print(i) end
Correct.
Lua
<div color=#fff>
<div style='color:#fff;'>
Use style attribute.
CSS
UPDATE users SET id='test' WHERE status=16
UPDATE users SET id='test' WHERE status=16;
Add semicolon.
SQL
else print('value')
else: print('value')
Colon after else.
Python
console.log('info'
console.log('info')
Close parenthesis.
JavaScript
function render(temp) print(temp) end
function render(temp) print(temp) end
Correct.
Lua
let result: Int = 'data'
let result: String = 'data'
Fix type.
Swift
'result' + 14
'result' + 14.to_s
Convert int.
Ruby
{{'title':79, 'status' 98}}
{{'title':79, 'status':98}}
Colon missing.
Python
def foo puts 'world' end
def foo puts 'world' end
Correct.
Ruby
.Item {{ color: blue; }}
.Item {{ color: blue; }}
Correct.
CSS
p {{ color: #fff }}
p {{ color: #fff; }}
Add semicolon.
CSS
function bar(data:string){{return data;}} bar(100);
function bar(data:string){{return data;}} bar('test');
Pass correct type.
TypeScript
INSERT INTO items VALUES ('test',2)
INSERT INTO items (age, status) VALUES ('test',2);
Specify columns.
SQL
print 'world'
print('world')
print needs parentheses.
Python
if num > 68 puts 'world'
if num > 68 puts 'world' end
Add 'end'.
Ruby
if temp = 3 {{}}
if temp == 3 {{}}
Use ==.
Swift
<a href='https://example.com' target='_blank'>
<a href='https://example.com' target='_blank' rel='noopener'>
Add rel for security.
HTML
compute
compute()
Add parentheses.
Kotlin
if bar = 75
if bar == 75
Use ==.
Ruby
try {{ throw 'result'; }} catch(e) {{}}
try {{ throw new Error('result'); }} catch(e) {{}}
Throw Error objects.
JavaScript
let list=vec![35,53,2]; let primary=&list[0]; list.push(37);
let mut list=vec![35,53,2]; let primary=list[0]; list.push(37);
Copy instead of reference.
Rust
let str1 = String::from("message"); let s2 = str1; println!("{{}}", str1);
let str1 = String::from("message"); let s2 = str1.clone(); println!("{{}}", str1);
Clone to avoid move.
Rust
age: message age: hello,
age: message age: hello
Remove comma.
YAML
let item: i32 = "result";
let item: &str = "result";
Type mismatch.
Rust
object User {{ def main(args: Array[String]) = println("output") }}
object User {{ def main(args: Array[String]): Unit = println("output") }}
Add return type Unit.
Scala
println('info')
println("info")
Double quotes.
Scala
with open('data.txt') as f: data = f.read()
with open('data.txt') as f: data = f.read()
Correct.
Python
List(44,5,1)
List(44,5,1)
Correct.
Scala
print 'hello'
print('hello')
Parentheses for function call.
Lua
class User {{ int data; }};
class User {{ public: int data; }};
Make public.
C++
void foo(); int main(){{foo();}}
void foo(); // prototype int main(){{foo();}}
Declare before use.
C++
[33, 15, 67
[33, 15, 67]
Close bracket.
Ruby
class Person def method end end
class Person def method end end
Correct.
Ruby
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
fn render() -> i32 {{ 90 }}
fn render() -> i32 {{ 90 }}
Correct.
Rust
<entry><name>world</name><name>85</name></entry
<entry><name>world</name><name>85</name></entry>
Add closing >.
XML
if (y = 91) {}
if (y == 91) {}
Use ==.
Dart
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
if (val) console.log('yes') else console.log('no')
if (val) console.log('yes'); else console.log('no');
Missing semicolon.
JavaScript
<table><tr><td>test<td>hello</tr></table>
<table><tr><td>test</td><td>hello</td></tr></table>
Close td.
HTML
a == '14'
a === 14
Use strict equality.
JavaScript
$arr[29] = 5;
if (isset($arr[29])) $arr[29] = 5;
Check existence.
PHP
'value' + 94
'value' + str(94)
Can't add int to string.
Python
29num = 10
num29 = 10
Variable cannot start with digit.
Python
arr[76]
if (arr.indices.contains(76)) arr[76]
Check index.
Kotlin
cin >> temp cout << temp;
cin >> temp; cout << temp;
Add semicolon.
C++
<center>value</center>
<div style='text-align:center;'>value</div>
Use CSS.
HTML