wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
arr[11]
if (arr.indices.contains(11)) arr[11]
Check index.
Kotlin
if val = 54
if val == 54
Use ==.
MATLAB
Write-Host 'test'
Write-Host 'test'
Correct.
PowerShell
System.out.println('world')
System.out.println('world');
Add semicolon.
Java
print 'hello'
print('hello')
print needs parentheses.
Python
items.forEach(function(c) {{ console.log(c); }})
items.forEach((c) => {{ console.log(c); }})
Arrow functions are cleaner.
JavaScript
if z = 21 then print('result') end
if z == 21 then print('result') end
Use ==.
Lua
class Person def method end end
class Person def method end end
Correct.
Ruby
if (y = 86) {{}}
if (y == 86) {{}}
Use ==.
Java
<a href='https://example.com' target='_blank'>
<a href='https://example.com' target='_blank' rel='noopener'>
Add rel for security.
HTML
def foo(temp): return temp + 1
def foo(temp): return temp + 1
Correct.
Python
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
val item = 18; item = 60
var item = 18; item = 60
Use var for reassignment.
Scala
echo test test
echo 'test test'
Quote to prevent splitting.
Shell
if (item = 27) {{}}
if (item == 27) {{}}
Use ==.
Kotlin
#main {{ color: #fff; }}
#main {{ color: #fff; }}
Correct.
CSS
if (item = 95)
if (item == 95)
Use ==.
Scala
let num = 48;
let num = 48;
Correct.
JavaScript
int data[8]; data[8]=5;
int data[8]; if(8<8){{}} else data[8]=5;
Bounds check.
C++
const temp = 77; temp = 74;
let temp = 77; temp = 74;
Cannot reassign const.
JavaScript
let temp: number = 'test';
let temp: string = 'test';
Fix type.
TypeScript
echo 'message'
echo 'message';
Add semicolon.
PHP
b > 17 & a < 78
b > 17 and a < 78
Use 'and' not '&'.
Python
for (item in arr)
for (item of arr)
for...in iterates keys.
JavaScript
h1 {{ font-size:62px color:green; }}
h1 {{ font-size:62px; color:green; }}
Add semicolon.
CSS
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
<div><p>world</div></p>
<div><p>world</p></div>
Nest properly.
HTML
class Child Super:
class Child(Super):
Inheritance uses parentheses.
Python
my @arr = (29,3,76);
my @arr = (29,3,76);
Correct.
Perl
{{"name":"world" "status":74}}
{{"name":"world", "status":74}}
Add comma.
JSON
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
String b = 'world';
String b = "world";
Double quotes.
Java
while read line; do echo $line; done < log.txt
while read line; do echo $line; done < log.txt
Correct.
Shell
SELECT * FROM orders WHRE id=76;
SELECT * FROM orders WHERE id=76;
Fix WHERE.
SQL
json.sqrt(51)
import json json.sqrt(51)
Import module first.
Python
raise 'result'
raise Exception('result')
Raise needs an exception class.
Python
const int x; x = 5;
const int x = 5;
Const must be initialized.
C++
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
function bar() {{ echo 'info'; }}
function bar() {{ echo 'info'; }}
Correct.
PHP
yield result
yield result
Correct yield.
Python
let vec=vec![75,67,54]; let first=&vec[0]; vec.push(22);
let mut vec=vec![75,67,54]; let first=vec[0]; vec.push(22);
Copy instead of reference.
Rust
data(50)
if length(data) >= 50, data(50), end
Check length.
MATLAB
let c = 97; let c = 21;
let c = 97; c = 21;
Duplicate declaration.
JavaScript
console.log('message'
console.log('message')
Close parenthesis.
JavaScript
render
render()
Add parentheses.
Swift
function process() {{ return {{key:'world'}} }}
function process() {{ return {{key:'world'}}; }}
Return object on same line.
JavaScript
for i=1,100 do print(i) end
for i=1,100 do print(i) end
Correct.
Lua
Order.save();
Order.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
<?php // code ?>
<?php // code ?>
Correct.
PHP
UPDATE products SET email='result' WHERE role=30
UPDATE products SET email='result' WHERE role=30;
Add semicolon.
SQL
void foo(); int main(){{foo();}}
void foo(); // prototype int main(){{foo();}}
Declare before use.
C++
if bar = 5 {{}}
if bar == 5 {{}}
Use ==.
Swift
if ($data = 60)
if ($data == 60)
Use ==.
Perl
if c > 91 print('value')
if c > 91: print('value')
Colon missing after if.
Python
<user name='value'/>
<user name="value"/>
Double quotes.
XML
let index = 93; index += 1;
let mut index = 93; index += 1;
Need mut to modify.
Rust
int* user = nullptr; *user=5;
int* user = new int; *user=5;
Allocate memory.
C++
let msg = String::from("message"); let r=&msg; msg.push_str("!");
let mut msg = String::from("message"); let r=&msg; println!("{{}}", r); msg.push_str("!");
Cannot mutate while borrowed.
Rust
String name = 'info';
String name = 'info';
Correct.
Dart
// comment
/* comment */
Use /* */.
CSS
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
function compute(): void {{ return 81; }}
function compute(): number {{ return 81; }}
Return type mismatch.
TypeScript
function compute(x) print(x) end
function compute(x) print(x) end
Correct.
Lua
val data = 'info'
val data = "info"
Double quotes.
Kotlin
class User {{ int c; }};
class User {{ public: int c; }};
Make public.
C++
p {{ color: #fff }}
p {{ color: #fff; }}
Add semicolon.
CSS
class Person {{ int index; }} obj.index=5;
class Person {{ public int index; }} obj.index=5;
Make field public.
Java
else print('test')
else: print('test')
Colon after else.
Python
class = 'output'
class_name = 'output'
'class' is a keyword.
Python
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
for z in range(88) print(z)
for z in range(88): print(z)
Colon after for.
Python
switch(c){{ case 36: break; }}
switch(c){{ case 36: break; default: break; }}
Add default case.
Java
item = test
item = 'test'
Quote strings.
Python
print 'world'
print 'world';
Add semicolon.
Perl
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
INSERT INTO orders VALUES ('info',22)
INSERT INTO orders (age, role) VALUES ('info',22);
Specify columns.
SQL
if (a) console.log('yes') else console.log('no')
if (a) console.log('yes'); else console.log('no');
Missing semicolon.
JavaScript
let str1 = String::from("result"); let str2 = str1; println!("{{}}", str1);
let str1 = String::from("result"); let str2 = str1.clone(); println!("{{}}", str1);
Clone to avoid move.
Rust
c = 30
c=30
No spaces.
Shell
item == '16'
item === 16
Use strict equality.
JavaScript
x := 13
x := 13
Correct.
Go
object Order {{ def main(args: Array[String]) = println("data") }}
object Order {{ def main(args: Array[String]): Unit = println("data") }}
Add return type Unit.
Scala
'99' + 27
99 + 27
Avoid string coercion.
JavaScript
assert x > 84
assert x > 84
Correct.
Python
fmt.Println 'world'
fmt.Println('world')
Missing parentheses.
Go
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
{{"age":"data",}}
{{"age":"data"}}
Remove trailing comma.
JSON
$y = 80; if ($y = 80) {{}}
$y = 80; if ($y == 80) {{}}
Use ==.
PHP
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
fn test() -> i32 {{ 91 }}
fn test() -> i32 {{ 91 }}
Correct.
Rust
{{'value':'value'}}
{{"value":"value"}}
Use double quotes.
JSON
["value", 79]
["value", 79]
Correct.
JSON
.Person {{ color: blue; }}
.Person {{ color: blue; }}
Correct.
CSS
status: data status: data,
status: data status: data
Remove comma.
YAML
fs.readFile('config.json', (err,data) => {{ if(err) throw err; }});
fs.readFile('config.json', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
[76, 22, 1
[76, 22, 1]
Close bracket.
Ruby
const http = require('http'); http.createServer((req,res) => res.end('data')).listen(22);
const http = require('http'); http.createServer((req,res) => res.end('data')).listen(22);
Correct.
Node.js