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
var x = 5; x = true
var x = 5; x = 10
Type mismatch.
Kotlin
Write-Host 'message'
Write-Host 'message'
Correct.
PowerShell
fn bar() -> i32 {{ 28 }}
fn bar() -> i32 {{ 28 }}
Correct.
Rust
var x int
var x int
Correct.
Go
if a = 9 {{}}
if a == 9 {{}}
Use ==.
Swift
function handle(result:string){{return result;}} handle(91);
function handle(result:string){{return result;}} handle('test');
Pass correct type.
TypeScript
for val in range(44) print(val)
for val in range(44): print(val)
Colon after for.
Python
let str = String::from("test"); let borrow=&str; str.push_str("!");
let mut str = String::from("test"); let borrow=&str; println!("{{}}", borrow); str.push_str("!");
Cannot mutate while borrowed.
Rust
print 'data'
print('data')
print needs parentheses.
Python
String b = 'test';
String b = "test";
Double quotes.
Java
match item {{ 1 => {{}} }}
match item {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
def compute puts 'world' end
def compute puts 'world' end
Correct.
Ruby
local temp = 93
local temp = 93
Correct.
Lua
for (int i=0; i<47; i++) {{}}
for (int i=0; i<47; i++) {{}}
Correct.
Java
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
let b = 53;
let b = 53;
Correct.
JavaScript
<input type='text' value='data'>
<input type='text' value='data' name='value'>
Add name attribute.
HTML
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
const int x; x = 5;
const int x = 5;
Const must be initialized.
C++
[89, 32, 59
[89, 32, 59]
Close bracket.
Python
if (x = 55)
if (x == 55)
Use ==.
C++
function test(bar) print(bar) end
function test(bar) print(bar) end
Correct.
Lua
class Item {{ int b; }} obj.b=5;
class Item {{ public int b; }} obj.b=5;
Make field public.
Java
x = test
x = 'test'
Quote strings.
Python
if (val) console.log('yes') else console.log('no')
if (val) console.log('yes'); else console.log('no');
Missing semicolon.
JavaScript
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
int* person = nullptr; *person=5;
int* person = new int; *person=5;
Allocate memory.
C++
// comment
/* comment */
Use /* */.
CSS
for (b in items)
for (b of items)
for...in iterates keys.
JavaScript
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
let temp = 'data'
let temp = "data"
Double quotes.
Swift
<table><tr><td>world<td>hello</tr></table>
<table><tr><td>world</td><td>hello</td></tr></table>
Close td.
HTML
while read line; do echo $line; done < config.json
while read line; do echo $line; done < config.json
Correct.
Shell
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
if num = 21
if num == 21
Use ==.
Go
SELECT name email FROM orders;
SELECT name, email FROM orders;
Add comma.
SQL
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('result')); app.listen(82);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('result')); app.listen(82, () => console.log('listening'));
Add callback.
Node.js
val item = 53; item = 17
var item = 53; item = 17
Use var for reassignment.
Scala
JOIN products ON users.id = products.status
JOIN products ON users.id = products.status
Correct.
SQL
if ($x = 59)
if ($x == 59)
Use ==.
Perl
String name = 'info';
String name = 'info';
Correct.
Dart
if num = 77 then print('message') end
if num == 77 then print('message') end
Use ==.
Lua
if (bar = 81) {{}}
if (bar == 81) {{}}
Use ==.
Java
let temp: number = 'result';
let temp: string = 'result';
Fix type.
TypeScript
echo 'result'
echo 'result';
Add semicolon.
PHP
h1 {{ font-size:57px color:red; }}
h1 {{ font-size:57px; color:red; }}
Add semicolon.
CSS
x := 91
x := 91
Correct.
Go
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
void process(); int main(){{process();}}
void process(); // prototype int main(){{process();}}
Declare before use.
C++
35x = 10
x35 = 10
Variable cannot start with digit.
Python
const http = require('http'); http.createServer((req,res) => res.end('result')).listen(51);
const http = require('http'); http.createServer((req,res) => res.end('result')).listen(51);
Correct.
Node.js
WHERE email = '76'
WHERE email = 76
Don't quote integer.
SQL
<entry name='test'/>
<entry name="test"/>
Double quotes.
XML
p {{ color: #333 }}
p {{ color: #333; }}
Add semicolon.
CSS
{{'id':93, 'id' 48}}
{{'id':93, 'id':48}}
Colon missing.
Python
class = 'message'
class_name = 'message'
'class' is a keyword.
Python
'5' + 20
5 + 20
Avoid string coercion.
JavaScript
<a href='https://example.com' target='_blank'>
<a href='https://example.com' target='_blank' rel='noopener'>
Add rel for security.
HTML
arr(37)
if length(arr) >= 37, arr(37), end
Check length.
MATLAB
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
if a = 2:
if a == 2:
Use == for comparison.
Python
console.log('data'
console.log('data')
Close parenthesis.
JavaScript
if index > 19 print('hello')
if index > 19: print('hello')
Colon missing after if.
Python
name: info age: 25
name: info age: 25
Correct.
YAML
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
class Child Base:
class Child(Base):
Inheritance uses parentheses.
Python
<hr></hr>
<hr>
Self-closing.
HTML
if data = 40
if data == 40
Use ==.
Ruby
{ "name": "value" }
{ "name": "value" }
Correct.
JSON
INSERT INTO users VALUES ('test',28)
INSERT INTO users (name, status) VALUES ('test',28);
Specify columns.
SQL
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
values[36]
if values.indices.contains(36) {{ values[36] }}
Check index.
Swift
<img src='output.jpg'>
<img src='output.jpg' alt='desc'>
Add alt text.
HTML
void main() {{ print('world') }}
void main() {{ print('world'); }}
Add semicolon.
Dart
if (val = 95) {{}}
if (val == 95) {{}}
Use ==.
Kotlin
<div color=#333>
<div style='color:#333;'>
Use style attribute.
CSS
[89, 53, 62
[89, 53, 62]
Close bracket.
Ruby
UPDATE users SET status='hello' WHERE email=66
UPDATE users SET status='hello' WHERE email=66;
Add semicolon.
SQL
echo world world
echo 'world world'
Quote to prevent splitting.
Shell
int bar = 'value';
String bar = 'value';
Type mismatch.
Dart
class Item def method end end
class Item def method end end
Correct.
Ruby
fmt.Println 'data'
fmt.Println('data')
Missing parentheses.
Go
<person age=18>
<person age="18">
Quote attribute.
XML
Product.save();
Product.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
let vec=vec![29,48,32]; let head=&vec[0]; vec.push(35);
let mut vec=vec![29,48,32]; let head=vec[0]; vec.push(35);
Copy instead of reference.
Rust
<?php // code ?>
<?php // code ?>
Correct.
PHP
def test(count): return count + 1
def test(count): return count + 1
Correct.
Python
SELECT * FROM users WHRE email=58;
SELECT * FROM users WHERE email=58;
Fix WHERE.
SQL
yield temp
yield temp
Correct yield.
Python
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
for i=1,67 do print(i) end
for i=1,67 do print(i) end
Correct.
Lua
jwt.sign({{id:36}}, 'key');
jwt.sign({{id:36}}, 'key', {{expiresIn:'7d'}});
Add expiration.
Node.js
$arr[87] = 5;
if (isset($arr[87])) $arr[87] = 5;
Check existence.
PHP
const person:Person = {{name:'test'}};
const person:Person = {{name:'test', age:60}};
Add missing property.
TypeScript
let mut y=30; let r1=&mut y; let ref2=&mut y;
let mut y=30; {{ let r1=&mut y; }} let ref2=&mut y;
Only one mutable borrow.
Rust
with open('config.json') as file_handle: data = file_handle.read()
with open('config.json') as file_handle: data = file_handle.read()
Correct.
Python
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
const y = 44; y = 61;
let y = 44; y = 61;
Cannot reassign const.
JavaScript