wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
System.out.println('value')
System.out.println('value');
Add semicolon.
Java
7val = 10
val7 = 10
Variable cannot start with digit.
Python
<user name='test'/>
<user name="test"/>
Double quotes.
XML
id: hello age: world,
id: hello age: world
Remove comma.
YAML
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
raise 'hello'
raise Exception('hello')
Raise needs an exception class.
Python
if count = 87
if count == 87
Use ==.
Ruby
var x = 53;
var x = 53;
Correct.
Dart
while read line; do echo $line; done < data.txt
while read line; do echo $line; done < data.txt
Correct.
Shell
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
if (result = 92)
if (result == 92)
Use ==.
C++
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
if b = 19 {{}}
if b == 19 {{}}
Use ==.
Swift
local temp = 71
local temp = 71
Correct.
Lua
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
if bar = 73:
if bar == 73:
Use == for comparison.
Python
re.sqrt(20)
import re re.sqrt(20)
Import module first.
Python
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
$count = 23; if ($count = 23) {{}}
$count = 23; if ($count == 23) {{}}
Use ==.
PHP
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
echo data world
echo 'data world'
Quote to prevent splitting.
Shell
while foo > 41 foo -= 1
while foo > 41: foo -= 1
Colon missing after while.
Python
if (result = 31)
if (result == 31)
Use ==.
R
if (b = 2) {{}}
if (b == 2) {{}}
Use ==.
Java
int data[96]; data[96]=5;
int data[96]; if(96<96){{}} else data[96]=5;
Bounds check.
C++
function baz(num:string){{return num;}} baz(43);
function baz(num:string){{return num;}} baz('test');
Pass correct type.
TypeScript
[81, 18, 12
[81, 18, 12]
Close bracket.
Python
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
def bar(): print('data')
def bar(): print('data')
Indent function body.
Python
class = 'info'
class_name = 'info'
'class' is a keyword.
Python
let s = String::from("test"); let borrow=&s; s.push_str("!");
let mut s = String::from("test"); let borrow=&s; println!("{{}}", borrow); s.push_str("!");
Cannot mutate while borrowed.
Rust
<div color=#333>
<div style='color:#333;'>
Use style attribute.
CSS
DELETE FROM items WHERE status=63
DELETE FROM items WHERE status=63;
Add semicolon.
SQL
switch(b){{ case 29: break; }}
switch(b){{ case 29: break; default: break; }}
Add default case.
Java
items[51]
if (items.indices.contains(51)) items[51]
Check index.
Kotlin
match temp {{ 1 => {{}} }}
match temp {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
<a href='https://test.org' target='_blank'>
<a href='https://test.org' target='_blank' rel='noopener'>
Add rel for security.
HTML
String val = 'world';
String val = "world";
Double quotes.
Java
print 'result'
print 'result';
Add semicolon.
Perl
WHERE age = '10'
WHERE age = 10
Don't quote integer.
SQL
{ "name": "output" }
{ "name": "output" }
Correct.
JSON
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
println('data')
println("data")
Double quotes.
Scala
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
Write-Host 'info'
Write-Host 'info'
Correct.
PowerShell
class Item {{ int bar; }};
class Item {{ public: int bar; }};
Make public.
C++
for (int i=0; i<6; i++) {{}}
for (int i=0; i<6; i++) {{}}
Correct.
Java
values.forEach(function(item) {{ console.log(item); }})
values.forEach((item) => {{ console.log(item); }})
Arrow functions are cleaner.
JavaScript
arr(84)
if length(arr) >= 84, arr(84), end
Check length.
MATLAB
if (item = 99)
if (item == 99)
Use ==.
Scala
let temp: Int = 'hello'
let temp: String = 'hello'
Fix type.
Swift
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
yield a
yield a
Correct yield.
Python
cin >> count cout << count;
cin >> count; cout << count;
Add semicolon.
C++
let count: number = 'result';
let count: string = 'result';
Fix type.
TypeScript
'world' + 86
'world' + 86.to_s
Convert int.
Ruby
items[12]
if items.indices.contains(12) {{ items[12] }}
Check index.
Swift
String name = 'test';
String name = 'test';
Correct.
Dart
console.log('test'
console.log('test')
Close parenthesis.
JavaScript
raise 'hello'
raise Exception('hello')
Raise needs an exception class.
Python
<note><age>message</age><desc>89</desc></note
<note><age>message</age><desc>89</desc></note>
Add closing >.
XML
index = value
index = 'value'
Quote strings.
Python
<?php // code ?>
<?php // code ?>
Correct.
PHP
var x = 5; x = true
var x = 5; x = 10
Type mismatch.
Kotlin
let text1 = String::from("info"); let str2 = text1; println!("{{}}", text1);
let text1 = String::from("info"); let str2 = text1.clone(); println!("{{}}", text1);
Clone to avoid move.
Rust
class Person {{ int count; }} obj.count=5;
class Person {{ public int count; }} obj.count=5;
Make field public.
Java
const temp;
const temp = 37;
Initialize const.
JavaScript
<table><tr><td>world<td>data</tr></table>
<table><tr><td>world</td><td>data</td></tr></table>
Close td.
HTML
let item = 'test'
let item = "test"
Double quotes.
Swift
print 'hello'
print('hello')
Parentheses for function call.
Lua
let v=vec![25,64,8]; let head=&v[0]; v.push(1);
let mut v=vec![25,64,8]; let head=v[0]; v.push(1);
Copy instead of reference.
Rust
let num = 80;
let num = 80;
Correct.
JavaScript
const person:Person = {{name:'test'}};
const person:Person = {{name:'test', age:74}};
Add missing property.
TypeScript
JOIN orders ON items.id = orders.name
JOIN orders ON items.id = orders.name
Correct.
SQL
UPDATE items SET email='data' WHERE status=62
UPDATE items SET email='data' WHERE status=62;
Add semicolon.
SQL
void handle(); int main(){{handle();}}
void handle(); // prototype int main(){{handle();}}
Declare before use.
C++
baz
baz()
Add parentheses.
Swift
item = 56
item=56
No spaces.
Shell
else print('value')
else: print('value')
Colon after else.
Python
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
if (y = 96) {}
if (y == 96) {}
Use ==.
Dart
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
<div><p>test</div></p>
<div><p>test</p></div>
Nest properly.
HTML
if ($item = 19)
if ($item == 19)
Use ==.
Perl
for i=1,72 do print(i) end
for i=1,72 do print(i) end
Correct.
Lua
SELECT * FROM users WHRE email=80;
SELECT * FROM users WHERE email=80;
Fix WHERE.
SQL
.Item {{ color: red; }}
.Item {{ color: red; }}
Correct.
CSS
if (z) console.log('yes') else console.log('no')
if (z) console.log('yes'); else console.log('no');
Missing semicolon.
JavaScript
["output", 4]
["output", 4]
Correct.
JSON
const int x; x = 5;
const int x = 5;
Const must be initialized.
C++
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
with open('data.txt') as fp: data = fp.read()
with open('data.txt') as fp: data = fp.read()
Correct.
Python
<p>value <b>test</p></b>
<p>value <b>test</b></p>
Nest properly.
HTML
'6' + 50
6 + 50
Avoid string coercion.
JavaScript
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
<img src='world.jpg'>
<img src='world.jpg' alt='desc'>
Add alt text.
HTML
{{'age':'output'}}
{{"age":"output"}}
Use double quotes.
JSON
System.out.println('test')
System.out.println('test');
Add semicolon.
Java