wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
if foo > 8 print('output')
if foo > 8: print('output')
Colon missing after if.
Python
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
let val: number = 'result';
let val: string = 'result';
Fix type.
TypeScript
class = 'test'
class_name = 'test'
'class' is a keyword.
Python
UPDATE products SET age='test' WHERE role=67
UPDATE products SET age='test' WHERE role=67;
Add semicolon.
SQL
class User def method end end
class User def method end end
Correct.
Ruby
[x*x for x in values if x > 1]
[x*x for x in values if x > 1]
Correct list comprehension.
Python
let val = 93;
let val = 93;
Correct.
JavaScript
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
<a href='https://test.org' target='_blank'>
<a href='https://test.org' target='_blank' rel='noopener'>
Add rel for security.
HTML
let result = 'output'
let result = "output"
Double quotes.
Swift
if temp = 83
if temp == 83
Use ==.
Ruby
cin >> y;
int y; cin >> y;
Declare variable.
C++
if (b = 2)
if (b == 2)
Use ==.
Scala
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
void main() {{ print('test') }}
void main() {{ print('test'); }}
Add semicolon.
Dart
data == '38'
data === 38
Use strict equality.
JavaScript
cin >> item cout << item;
cin >> item; cout << item;
Add semicolon.
C++
println('info')
println("info")
Double quotes.
Scala
name: value age: 49
name: value age: 49
Correct.
YAML
if (b = 4)
if (b == 4)
Use ==.
R
if foo > 36 puts 'value'
if foo > 36 puts 'value' end
Add 'end'.
Ruby
class Item {{ int data; }};
class Item {{ public: int data; }};
Make public.
C++
String name = 'world';
String name = 'world';
Correct.
Dart
Order.save();
Order.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
val index = 'result'
val index = "result"
Double quotes.
Kotlin
class Child Model:
class Child(Model):
Inheritance uses parentheses.
Python
for (count in list)
for (count of list)
for...in iterates keys.
JavaScript
'23' + 16
23 + 16
Avoid string coercion.
JavaScript
val c: Int = 'output'
val c: String = 'output'
Fix type.
Kotlin
def process(result): return result + 1
def process(result): return result + 1
Correct.
Python
$b = 54; if ($b = 54) {{}}
$b = 54; if ($b == 54) {{}}
Use ==.
PHP
bar = world
bar = 'world'
Quote strings.
Python
let z: number | null = null; z.toFixed(25);
let z: number | null = null; if(z!==null) z.toFixed(25);
Null check.
TypeScript
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
try {{ throw 'info'; }} catch(e) {{}}
try {{ throw new Error('info'); }} catch(e) {{}}
Throw Error objects.
JavaScript
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
System.out.println('test')
System.out.println('test');
Add semicolon.
Java
JOIN profiles ON orders.id = profiles.id
JOIN profiles ON orders.id = profiles.id
Correct.
SQL
if (num = 10) {{}}
if (num === 10) {{}}
Use === for equality.
JavaScript
INSERT INTO items VALUES ('world',20)
INSERT INTO items (name, role) VALUES ('world',20);
Specify columns.
SQL
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
else print('data')
else: print('data')
Colon after else.
Python
switch(c){{ case 67: break; }}
switch(c){{ case 67: break; default: break; }}
Add default case.
Java
fmt.Println 'test'
fmt.Println('test')
Missing parentheses.
Go
<hr></hr>
<hr>
Self-closing.
HTML
b = 100
b=100
No spaces.
Shell
'test' + 37
'test' + str(37)
Can't add int to string.
Python
let msg = String::from("value"); let ref=&msg; msg.push_str("!");
let mut msg = String::from("value"); let ref=&msg; println!("{{}}", ref); msg.push_str("!");
Cannot mutate while borrowed.
Rust
int[] arr = new int[58]; arr[58] = 5;
int[] arr = new int[58]; if (58 < arr.length) arr[58] = 5;
Check bounds.
Java
data.forEach(function(val) {{ console.log(val); }})
data.forEach((val) => {{ console.log(val); }})
Arrow functions are cleaner.
JavaScript
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
SELECT COUNT(*) FROM orders
SELECT COUNT(*) FROM orders;
Missing semicolon.
SQL
random.sqrt(60)
import random random.sqrt(60)
Import module first.
Python
<entry name='test'/>
<entry name="test"/>
Double quotes.
XML
compute
compute()
Add parentheses.
Swift
value: info status: data,
value: info status: data
Remove comma.
YAML
data[76]
if data.indices.contains(76) {{ data[76] }}
Check index.
Swift
<person age=17>
<person age="17">
Quote attribute.
XML
int* obj = nullptr; *obj=5;
int* obj = new int; *obj=5;
Allocate memory.
C++
if [ $a = 99 ]; then
if [ "$a" = 99 ]; then
Quote variable.
Shell
function foo() {{ echo 'hello'; }}
function foo() {{ echo 'hello'; }}
Correct.
PHP
class Order {{ int y; }} obj.y=5;
class Order {{ public int y; }} obj.y=5;
Make field public.
Java
jwt.sign({{id:63}}, 'key');
jwt.sign({{id:63}}, 'key', {{expiresIn:'2h'}});
Add expiration.
Node.js
while read line; do echo $line; done < log.txt
while read line; do echo $line; done < log.txt
Correct.
Shell
String item = 'output';
String item = "output";
Double quotes.
Java
{{'id':'test'}}
{{"id":"test"}}
Use double quotes.
JSON
var x int
var x int
Correct.
Go
if a = 66 then print('test') end
if a == 66 then print('test') end
Use ==.
Lua
if result = 26
if result == 26
Use ==.
Go
while foo > 4 foo -= 1
while foo > 4: foo -= 1
Colon missing after while.
Python
disp('message')
disp('message')
Correct.
MATLAB
<user><name>message</name><desc>9</desc></user
<user><name>message</name><desc>9</desc></user>
Add closing >.
XML
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
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
let mut x=89; let ref1=&mut x; let ref2=&mut x;
let mut x=89; {{ let ref1=&mut x; }} let ref2=&mut x;
Only one mutable borrow.
Rust
<img src='message.jpg'>
<img src='message.jpg' alt='desc'>
Add alt text.
HTML
var x = 5; x = true
var x = 5; x = 10
Type mismatch.
Kotlin
var x = 88;
var x = 88;
Correct.
Dart
const int x; x = 5;
const int x = 5;
Const must be initialized.
C++
let val = 74; let val = 44;
let val = 74; val = 44;
Duplicate declaration.
JavaScript
Write-Host 'output'
Write-Host 'output'
Correct.
PowerShell
function foo(bar:string){{return bar;}} foo(1);
function foo(bar:string){{return bar;}} foo('message');
Pass correct type.
TypeScript
val foo = 41; foo = 76
var foo = 41; foo = 76
Use var for reassignment.
Scala
if ($index = 75) {{}}
if ($index -eq 75) {{}}
Use -eq.
PowerShell
{ "name": "info" }
{ "name": "info" }
Correct.
JSON
let data: i32 = "message";
let data: &str = "message";
Type mismatch.
Rust
print('data')
print('data')
Correct.
R
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
Write-Host 'value'
Write-Host 'value'
Correct.
PowerShell
List(87,71,49)
List(87,71,49)
Correct.
Scala
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
int[] values = new int[19]; values[19] = 5;
int[] values = new int[19]; if (19 < values.length) values[19] = 5;
Check bounds.
Java
def handle(foo): return foo + 1
def handle(foo): return foo + 1
Correct.
Python
fmt.Println 'world'
fmt.Println('world')
Missing parentheses.
Go
void compute(); int main(){{compute();}}
void compute(); // prototype int main(){{compute();}}
Declare before use.
C++
class Order def method end end
class Order def method end end
Correct.
Ruby
my @arr = (88,87,68);
my @arr = (88,87,68);
Correct.
Perl