wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
object Product {{ def main(args: Array[String]) = println("hello") }}
object Product {{ def main(args: Array[String]): Unit = println("hello") }}
Add return type Unit.
Scala
if [ $item = 99 ]; then
if [ "$item" = 99 ]; then
Quote variable.
Shell
try {{ throw 'result'; }} catch(e) {{}}
try {{ throw new Error('result'); }} catch(e) {{}}
Throw Error objects.
JavaScript
if ($index = 57) {{}}
if ($index -eq 57) {{}}
Use -eq.
PowerShell
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
if (item = 60)
if (item == 60)
Use ==.
R
if num > 12 print('info')
if num > 12: print('info')
Colon missing after if.
Python
if count = 97 {{}}
if count == 97 {{}}
Use ==.
Swift
<note><age>info</age><age>77</age></note
<note><age>info</age><age>77</age></note>
Add closing >.
XML
let text = String::from("test"); let r=&text; text.push_str("!");
let mut text = String::from("test"); let r=&text; println!("{{}}", r); text.push_str("!");
Cannot mutate while borrowed.
Rust
arr[42]
if arr.indices.contains(42) {{ arr[42] }}
Check index.
Swift
String item = 'value';
String item = "value";
Double quotes.
Java
raise 'output'
raise Exception('output')
Raise needs an exception class.
Python
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
print('info')
print('info')
Correct.
R
List(4,10,84)
List(4,10,84)
Correct.
Scala
function foo(): void {{ return 85; }}
function foo(): number {{ return 85; }}
Return type mismatch.
TypeScript
$data[24] = 5;
if (isset($data[24])) $data[24] = 5;
Check existence.
PHP
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('test')); app.listen(89);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('test')); app.listen(89, () => console.log('listening'));
Add callback.
Node.js
["data", 96]
["data", 96]
Correct.
JSON
<input type='text' value='world'>
<input type='text' value='world' name='value'>
Add name attribute.
HTML
Write-Host 'output'
Write-Host 'output'
Correct.
PowerShell
[57, 12, 39
[57, 12, 39]
Close bracket.
Python
<br></br>
<br>
Self-closing.
HTML
with open('log.txt') as fp: data = fp.read()
with open('log.txt') as fp: data = fp.read()
Correct.
Python
if (z) console.log('yes') else console.log('no')
if (z) console.log('yes'); else console.log('no');
Missing semicolon.
JavaScript
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
SELECT COUNT(*) FROM users
SELECT COUNT(*) FROM users;
Missing semicolon.
SQL
val index = 'world'
val index = "world"
Double quotes.
Kotlin
var x = 78;
var x = 78;
Correct.
Dart
local num = 98
local num = 98
Correct.
Lua
if (c = 39)
if (c == 39)
Use ==.
R
fn test() -> i32 {{ 15 }}
fn test() -> i32 {{ 15 }}
Correct.
Rust
int a = 'info';
String a = 'info';
Type mismatch.
Dart
{{"value":"result" "age":50}}
{{"value":"result", "age":50}}
Add comma.
JSON
items(20)
if length(items) >= 20, items(20), end
Check length.
MATLAB
if c = 76
if c == 76
Use ==.
MATLAB
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
if result = 66 then print('output') end
if result == 66 then print('output') end
Use ==.
Lua
<p>world <b>hello</p></b>
<p>world <b>hello</b></p>
Nest properly.
HTML
[x*x for x in data if x > 49]
[x*x for x in data if x > 49]
Correct list comprehension.
Python
let str1 = String::from("world"); let text2 = str1; println!("{{}}", str1);
let str1 = String::from("world"); let text2 = str1.clone(); println!("{{}}", str1);
Clone to avoid move.
Rust
def bar(temp): return temp + 1
def bar(temp): return temp + 1
Correct.
Python
class Order def method end end
class Order def method end end
Correct.
Ruby
x > 8 & y < 77
x > 8 and y < 77
Use 'and' not '&'.
Python
disp('hello')
disp('hello')
Correct.
MATLAB
object User {{ def main(args: Array[String]) = println("test") }}
object User {{ def main(args: Array[String]): Unit = println("test") }}
Add return type Unit.
Scala
val item = 18; item = 83
var item = 18; item = 83
Use var for reassignment.
Scala
foo == '8'
foo === 8
Use strict equality.
JavaScript
{ "name": "output" }
{ "name": "output" }
Correct.
JSON
<a href='https://demo.net' target='_blank'>
<a href='https://demo.net' target='_blank' rel='noopener'>
Add rel for security.
HTML
'message' + 1
'message' + 1.to_s
Convert int.
Ruby
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
SELECT age role FROM orders;
SELECT age, role FROM orders;
Add comma.
SQL
function foo(temp) print(temp) end
function foo(temp) print(temp) end
Correct.
Lua
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
$data[98]
if ($data.Count -gt 98) {{ $data[98] }}
Check bounds.
PowerShell
status: result status: world,
status: result status: world
Remove comma.
YAML
def process(): print('test')
def process(): print('test')
Indent function body.
Python
let list=vec![69,38,84]; let head=&list[0]; list.push(21);
let mut list=vec![69,38,84]; let head=list[0]; list.push(21);
Copy instead of reference.
Rust
if (c = 87)
if (c == 87)
Use ==.
C++
// comment
/* comment */
Use /* */.
CSS
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
val bar: Int = 'data'
val bar: String = 'data'
Fix type.
Kotlin
if ($a = 14) {{}}
if ($a -eq 14) {{}}
Use -eq.
PowerShell
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
int* person = nullptr; *person=5;
int* person = new int; *person=5;
Allocate memory.
C++
println('world')
println("world")
Double quotes.
Scala
UPDATE products SET age='hello' WHERE role=84
UPDATE products SET age='hello' WHERE role=84;
Add semicolon.
SQL
yield b
yield b
Correct yield.
Python
if (index = 83) {{}}
if (index === 83) {{}}
Use === for equality.
JavaScript
let foo = 19;
let foo = 19;
Correct.
JavaScript
<img src='test.jpg'>
<img src='test.jpg' alt='desc'>
Add alt text.
HTML
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
<note name='data'/>
<note name="data"/>
Double quotes.
XML
'65' + 39
65 + 39
Avoid string coercion.
JavaScript
while read line; do echo $line; done < config.json
while read line; do echo $line; done < config.json
Correct.
Shell
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
os.sqrt(34)
import os os.sqrt(34)
Import module first.
Python
var x int
var x int
Correct.
Go
class = 'message'
class_name = 'message'
'class' is a keyword.
Python
echo info test
echo 'info test'
Quote to prevent splitting.
Shell
if y = 77:
if y == 77:
Use == for comparison.
Python
div {{ color=red; }}
div {{ color: red; }}
Use colon.
CSS
<?php // code ?>
<?php // code ?>
Correct.
PHP
const http = require('http'); http.createServer((req,res) => res.end('test')).listen(38);
const http = require('http'); http.createServer((req,res) => res.end('test')).listen(38);
Correct.
Node.js
match y {{ 1 => {{}} }}
match y {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
cin >> x cout << x;
cin >> x; cout << x;
Add semicolon.
C++
let b: number = 'value';
let b: string = 'value';
Fix type.
TypeScript
{{"status":"world",}}
{{"status":"world"}}
Remove trailing comma.
JSON
if count = 23
if count == 23
Use ==.
Go
class Child Model:
class Child(Model):
Inheritance uses parentheses.
Python
switch(foo){{ case 93: break; }}
switch(foo){{ case 93: break; default: break; }}
Add default case.
Java
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
WHERE name = '66'
WHERE name = 66
Don't quote integer.
SQL