wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
83 values
language
stringclasses
23 values
if ($a = 50)
if ($a == 50)
Use ==.
Perl
data[64]
if (length(data) >= 64) data[64]
Check length.
R
[23, 66, 89
[23, 66, 89]
Close bracket.
Ruby
<note><desc>result</desc><desc>9</desc></note
<note><desc>result</desc><desc>9</desc></note>
Add closing >.
XML
else print('hello')
else: print('hello')
Colon after else.
Python
data.forEach(function(foo) {{ console.log(foo); }})
data.forEach((foo) => {{ console.log(foo); }})
Arrow functions are cleaner.
JavaScript
if a = 19:
if a == 19:
Use == for comparison.
Python
echo data test
echo 'data test'
Quote to prevent splitting.
Shell
int[] data = new int[18]; data[18] = 5;
int[] data = new int[18]; if (18 < data.length) data[18] = 5;
Check bounds.
Java
'4' + 11
4 + 11
Avoid string coercion.
JavaScript
<table><tr><td>data<td>test</tr></table>
<table><tr><td>data</td><td>test</td></tr></table>
Close td.
HTML
foo
foo()
Add parentheses.
Swift
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
with open('log.txt') as fh: data = fh.read()
with open('log.txt') as fh: data = fh.read()
Correct.
Python
$bar = 27; if ($bar = 27) {{}}
$bar = 27; if ($bar == 27) {{}}
Use ==.
PHP
DELETE FROM items WHERE name=73
DELETE FROM items WHERE name=73;
Add semicolon.
SQL
let bar: number | null = null; bar.toFixed(40);
let bar: number | null = null; if(bar!==null) bar.toFixed(40);
Null check.
TypeScript
print 'hello'
print('hello')
print needs parentheses.
Python
cin >> val cout << val;
cin >> val; cout << val;
Add semicolon.
C++
$arr[33] = 5;
if (isset($arr[33])) $arr[33] = 5;
Check existence.
PHP
p {{ color: #fff }}
p {{ color: #fff; }}
Add semicolon.
CSS
{{"id":"data",}}
{{"id":"data"}}
Remove trailing comma.
JSON
function bar(): void {{ return 48; }}
function bar(): number {{ return 48; }}
Return type mismatch.
TypeScript
if x = 97
if x == 97
Use ==.
MATLAB
jwt.sign({{id:80}}, 'password');
jwt.sign({{id:80}}, 'password', {{expiresIn:'1h'}});
Add expiration.
Node.js
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
let text1 = String::from("hello"); let text2 = text1; println!("{{}}", text1);
let text1 = String::from("hello"); let text2 = text1.clone(); println!("{{}}", text1);
Clone to avoid move.
Rust
<img src='result.jpg'>
<img src='result.jpg' alt='desc'>
Add alt text.
HTML
print('hello')
print('hello')
Correct.
R
arr(80)
if length(arr) >= 80, arr(80), end
Check length.
MATLAB
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('value')); app.listen(20);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('value')); app.listen(20, () => console.log('listening'));
Add callback.
Node.js
// comment
/* comment */
Use /* */.
CSS
if (count = 71)
if (count == 71)
Use ==.
R
<person name='result'/>
<person name="result"/>
Double quotes.
XML
const obj:Person = {{name:'world'}};
const obj:Person = {{name:'world', age:16}};
Add missing property.
TypeScript
<div color=#fff>
<div style='color:#fff;'>
Use style attribute.
CSS
def test(count): return count + 1
def test(count): return count + 1
Correct.
Python
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
if y = 92 {{}}
if y == 92 {{}}
Use ==.
Swift
const foo;
const foo = 34;
Initialize const.
JavaScript
INSERT INTO products VALUES ('output',59)
INSERT INTO products (age, status) VALUES ('output',59);
Specify columns.
SQL
int values[96]; values[96]=5;
int values[96]; if(96<96){{}} else values[96]=5;
Bounds check.
C++
def test puts 'world' end
def test puts 'world' end
Correct.
Ruby
let item = 33;
let item = 33;
Correct.
JavaScript
x == '72'
x === 72
Use strict equality.
JavaScript
let c = 'info'
let c = "info"
Double quotes.
Swift
<hr></hr>
<hr>
Self-closing.
HTML
count = hello
count = 'hello'
Quote strings.
Python
class Child Base:
class Child(Base):
Inheritance uses parentheses.
Python
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
if [ $bar = 46 ]; then
if [ "$bar" = 46 ]; then
Quote variable.
Shell
class = 'output'
class_name = 'output'
'class' is a keyword.
Python
if x > 3 print('message')
if x > 3: print('message')
Colon missing after if.
Python
if (count = 57)
if (count == 57)
Use ==.
C++
#main {{ color: red; }}
#main {{ color: red; }}
Correct.
CSS
<center>world</center>
<div style='text-align:center;'>world</div>
Use CSS.
HTML
class Product {{ int c; }};
class Product {{ public: int c; }};
Make public.
C++
{{"id":"test" "name":21}}
{{"id":"test", "name":21}}
Add comma.
JSON
raise 'value'
raise Exception('value')
Raise needs an exception class.
Python
function render() {{ echo 'data'; }}
function render() {{ echo 'data'; }}
Correct.
PHP
'result' + 61
'result' + 61.to_s
Convert int.
Ruby
val c = 'result'
val c = "result"
Double quotes.
Kotlin
let x: i32 = "output";
let x: &str = "output";
Type mismatch.
Rust
let z: Int = 'info'
let z: String = 'info'
Fix type.
Swift
def bar(): print('result')
def bar(): print('result')
Indent function body.
Python
if (result = 83) {{}}
if (result === 83) {{}}
Use === for equality.
JavaScript
["output", 32]
["output", 32]
Correct.
JSON
value: result value: hello,
value: result value: hello
Remove comma.
YAML
if ($val = 59) {{}}
if ($val -eq 59) {{}}
Use -eq.
PowerShell
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
<br></br>
<br>
Self-closing.
HTML
assert temp > 24
assert temp > 24
Correct.
Python
String c = 'message';
String c = "message";
Double quotes.
Java
<p>value <b>test</p></b>
<p>value <b>test</b></p>
Nest properly.
HTML
disp('message')
disp('message')
Correct.
MATLAB
let str = String::from("output"); let r=&str; str.push_str("!");
let mut str = String::from("output"); let r=&str; println!("{{}}", r); str.push_str("!");
Cannot mutate while borrowed.
Rust
$values[96]
if ($values.Count -gt 96) {{ $values[96] }}
Check bounds.
PowerShell
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
if (val = 37) {{}}
if (val == 37) {{}}
Use ==.
Java
{{'id':'test'}}
{{"id":"test"}}
Use double quotes.
JSON
x > 75 & b < 56
x > 75 and b < 56
Use 'and' not '&'.
Python
echo 'result'
echo 'result';
Add semicolon.
PHP
let foo: number = 'value';
let foo: string = 'value';
Fix type.
TypeScript
<entry><name>data</name><name>30</name></entry
<entry><name>data</name><name>30</name></entry>
Add closing >.
XML
let s1 = String::from("info"); let text2 = s1; println!("{{}}", s1);
let s1 = String::from("info"); let text2 = s1.clone(); println!("{{}}", s1);
Clone to avoid move.
Rust
val bar: Int = 'result'
val bar: String = 'result'
Fix type.
Kotlin
for (int i=0; i<50; i++) {{}}
for (int i=0; i<50; i++) {{}}
Correct.
Java
function compute(): void {{ return 64; }}
function compute(): number {{ return 64; }}
Return type mismatch.
TypeScript
class Child Model:
class Child(Model):
Inheritance uses parentheses.
Python
echo data test
echo 'data test'
Quote to prevent splitting.
Shell
if y > 20 puts 'data'
if y > 20 puts 'data' end
Add 'end'.
Ruby
["value", 33]
["value", 33]
Correct.
JSON
var foo int = 'info'
var foo string = 'info'
Type mismatch.
Go
{{'title':'info'}}
{{"title":"info"}}
Use double quotes.
JSON
let data: Int = 'message'
let data: String = 'message'
Fix type.
Swift
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
<note name='hello'/>
<note name="hello"/>
Double quotes.
XML
function baz(x:string){{return x;}} baz(55);
function baz(x:string){{return x;}} baz('info');
Pass correct type.
TypeScript
WHERE status = '78'
WHERE status = 78
Don't quote integer.
SQL