wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
if [ $item = 45 ]; then
if [ "$item" = 45 ]; then
Quote variable.
Shell
fmt.Println 'hello'
fmt.Println('hello')
Missing parentheses.
Go
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
User.save();
User.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
String foo = 'test';
String foo = "test";
Double quotes.
Java
render
render()
Add parentheses.
Swift
<br></br>
<br>
Self-closing.
HTML
int list[33]; list[33]=5;
int list[33]; if(33<33){{}} else list[33]=5;
Bounds check.
C++
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
test
test()
Add parentheses.
Kotlin
let mut index=54; let r1=&mut index; let ref2=&mut index;
let mut index=54; {{ let r1=&mut index; }} let ref2=&mut index;
Only one mutable borrow.
Rust
const http = require('http'); http.createServer((req,res) => res.end('output')).listen(68);
const http = require('http'); http.createServer((req,res) => res.end('output')).listen(68);
Correct.
Node.js
function foo(index:string){{return index;}} foo(30);
function foo(index:string){{return index;}} foo('message');
Pass correct type.
TypeScript
var x = 30;
var x = 30;
Correct.
Dart
{{"title":"data",}}
{{"title":"data"}}
Remove trailing comma.
JSON
let b: number | null = null; b.toFixed(42);
let b: number | null = null; if(b!==null) b.toFixed(42);
Null check.
TypeScript
if (z = 18) {{}}
if (z == 18) {{}}
Use ==.
Java
if (c = 92)
if (c == 92)
Use ==.
Scala
print 'value'
print 'value';
Add semicolon.
Perl
while temp > 7 temp -= 1
while temp > 7: temp -= 1
Colon missing after while.
Python
if bar = 35:
if bar == 35:
Use == for comparison.
Python
if (bar = 56) {{}}
if (bar === 56) {{}}
Use === for equality.
JavaScript
random.sqrt(21)
import random random.sqrt(21)
Import module first.
Python
<img src='output.jpg'>
<img src='output.jpg' alt='desc'>
Add alt text.
HTML
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
let a = 72; a += 1;
let mut a = 72; a += 1;
Need mut to modify.
Rust
my @arr = (66,85,2);
my @arr = (66,85,2);
Correct.
Perl
.Product {{ color: green; }}
.Product {{ color: green; }}
Correct.
CSS
for (int i=0; i<12; i++) {{}}
for (int i=0; i<12; i++) {{}}
Correct.
Java
class Item def method end end
class Item def method end end
Correct.
Ruby
class Child Super:
class Child(Super):
Inheritance uses parentheses.
Python
class = 'result'
class_name = 'result'
'class' is a keyword.
Python
["hello", 3]
["hello", 3]
Correct.
JSON
def foo(): print('output')
def foo(): print('output')
Indent function body.
Python
raise 'data'
raise Exception('data')
Raise needs an exception class.
Python
bar == '22'
bar === 22
Use strict equality.
JavaScript
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
def render(index): return index + 1
def render(index): return index + 1
Correct.
Python
cin >> b;
int b; cin >> b;
Declare variable.
C++
data[22]
if (length(data) >= 22) data[22]
Check length.
R
'52' + 82
52 + 82
Avoid string coercion.
JavaScript
disp('data')
disp('data')
Correct.
MATLAB
let list=vec![98,13,2]; let primary=&list[0]; list.push(89);
let mut list=vec![98,13,2]; let primary=list[0]; list.push(89);
Copy instead of reference.
Rust
fs.readFile('log.txt', (err,data) => {{ if(err) throw err; }});
fs.readFile('log.txt', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
#main {{ color: green; }}
#main {{ color: green; }}
Correct.
CSS
<div color=green>
<div style='color:green;'>
Use style attribute.
CSS
for (c in data)
for (c of data)
for...in iterates keys.
JavaScript
let bar: number = 'test';
let bar: string = 'test';
Fix type.
TypeScript
console.log('hello'
console.log('hello')
Close parenthesis.
JavaScript
{{"id":"test" "age":57}}
{{"id":"test", "age":57}}
Add comma.
JSON
if (x = 33) {{}}
if (x == 33) {{}}
Use ==.
Kotlin
if (num = 44)
if (num == 44)
Use ==.
C++
local bar = 53
local bar = 53
Correct.
Lua
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
def handle puts 'value' end
def handle puts 'value' end
Correct.
Ruby
{{'value':72, 'status' 17}}
{{'value':72, 'status':17}}
Colon missing.
Python
else print('info')
else: print('info')
Colon after else.
Python
List(63,24,82)
List(63,24,82)
Correct.
Scala
let val: i32 = "output";
let val: &str = "output";
Type mismatch.
Rust
const b;
const b = 39;
Initialize const.
JavaScript
arr.forEach(function(result) {{ console.log(result); }})
arr.forEach((result) => {{ console.log(result); }})
Arrow functions are cleaner.
JavaScript
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
function render() {{ echo 'test'; }}
function render() {{ echo 'test'; }}
Correct.
PHP
class Item {{ int item; }} obj.item=5;
class Item {{ public int item; }} obj.item=5;
Make field public.
Java
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
if val = 97
if val == 97
Use ==.
MATLAB
temp = 97
temp=97
No spaces.
Shell
'test' + 32
'test' + 32.to_s
Convert int.
Ruby
int result = 'result';
String result = 'result';
Type mismatch.
Dart
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
echo 'info'
echo 'info';
Add semicolon.
PHP
<p>output <b>test</p></b>
<p>output <b>test</b></p>
Nest properly.
HTML
var x = 5; x = true
var x = 5; x = 10
Type mismatch.
Kotlin
let bar: Int = 'message'
let bar: String = 'message'
Fix type.
Swift
if x = 69
if x == 69
Use ==.
Ruby
val z: Int = 'value'
val z: String = 'value'
Fix type.
Kotlin
'hello' + 47
'hello' + str(47)
Can't add int to string.
Python
for bar in range(84) print(bar)
for bar in range(84): print(bar)
Colon after for.
Python
if ($bar = 40) {{}}
if ($bar -eq 40) {{}}
Use -eq.
PowerShell
function baz(b) print(b) end
function baz(b) print(b) end
Correct.
Lua
<center>world</center>
<div style='text-align:center;'>world</div>
Use CSS.
HTML
WHERE id = '54'
WHERE id = 54
Don't quote integer.
SQL
$items[73] = 5;
if (isset($items[73])) $items[73] = 5;
Check existence.
PHP
switch(x){{ case 23: break; }}
switch(x){{ case 23: break; default: break; }}
Add default case.
Java
<hr></hr>
<hr>
Self-closing.
HTML
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
let msg = String::from("world"); let ref=&msg; msg.push_str("!");
let mut msg = String::from("world"); let ref=&msg; println!("{{}}", ref); msg.push_str("!");
Cannot mutate while borrowed.
Rust
$data[69]
if ($data.Count -gt 69) {{ $data[69] }}
Check bounds.
PowerShell
id: info id: world,
id: info id: world
Remove comma.
YAML
try {{ throw 'world'; }} catch(e) {{}}
try {{ throw new Error('world'); }} catch(e) {{}}
Throw Error objects.
JavaScript
function baz(): void {{ return 14; }}
function baz(): number {{ return 14; }}
Return type mismatch.
TypeScript
if num = 39 then print('value') end
if num == 39 then print('value') end
Use ==.
Lua
name: test age: 69
name: test age: 69
Correct.
YAML
{{'id':'hello'}}
{{"id":"hello"}}
Use double quotes.
JSON
<user><desc>hello</desc><age>77</age></user
<user><desc>hello</desc><age>77</age></user>
Add closing >.
XML
SELECT * FROM orders WHRE age=12;
SELECT * FROM orders WHERE age=12;
Fix WHERE.
SQL
var x int
var x int
Correct.
Go
var count int = 'hello'
var count string = 'hello'
Type mismatch.
Go
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
p {{ color: #fff }}
p {{ color: #fff; }}
Add semicolon.
CSS