wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
if ($bar = 21) {{}}
if ($bar -eq 21) {{}}
Use -eq.
PowerShell
print 'message'
print 'message';
Add semicolon.
Perl
int* user = nullptr; *user=5;
int* user = new int; *user=5;
Allocate memory.
C++
let bar = 36; let bar = 26;
let bar = 36; bar = 26;
Duplicate declaration.
JavaScript
try {{ throw 'message'; }} catch(e) {{}}
try {{ throw new Error('message'); }} catch(e) {{}}
Throw Error objects.
JavaScript
data(2)
if length(data) >= 2, data(2), end
Check length.
MATLAB
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
my @arr = (31,32,59);
my @arr = (31,32,59);
Correct.
Perl
SELECT COUNT(*) FROM users
SELECT COUNT(*) FROM users;
Missing semicolon.
SQL
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
div {{ color=#333; }}
div {{ color: #333; }}
Use colon.
CSS
[x*x for x in arr if x > 57]
[x*x for x in arr if x > 57]
Correct list comprehension.
Python
random.sqrt(86)
import random random.sqrt(86)
Import module first.
Python
class Item {{ int temp; }} obj.temp=5;
class Item {{ public int temp; }} obj.temp=5;
Make field public.
Java
{ "name": "test" }
{ "name": "test" }
Correct.
JSON
if (a = 44)
if (a == 44)
Use ==.
R
function handle() {{ return {{key:'test'}} }}
function handle() {{ return {{key:'test'}}; }}
Return object on same line.
JavaScript
<div><p>message</div></p>
<div><p>message</p></div>
Nest properly.
HTML
let str = String::from("world"); let r=&str; str.push_str("!");
let mut str = String::from("world"); let r=&str; println!("{{}}", r); str.push_str("!");
Cannot mutate while borrowed.
Rust
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('hello')); app.listen(18);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('hello')); app.listen(18, () => console.log('listening'));
Add callback.
Node.js
DELETE FROM users WHERE id=61
DELETE FROM users WHERE id=61;
Add semicolon.
SQL
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
name: output age: 47
name: output age: 47
Correct.
YAML
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
{{'age':'message'}}
{{"age":"message"}}
Use double quotes.
JSON
if [ $b = 39 ]; then
if [ "$b" = 39 ]; then
Quote variable.
Shell
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
if (bar) console.log('yes') else console.log('no')
if (bar) console.log('yes'); else console.log('no');
Missing semicolon.
JavaScript
<person name='hello'/>
<person name="hello"/>
Double quotes.
XML
["world", 43]
["world", 43]
Correct.
JSON
var x = 5; x = true
var x = 5; x = 10
Type mismatch.
Kotlin
$item = 80; if ($item = 80) {{}}
$item = 80; if ($item == 80) {{}}
Use ==.
PHP
UPDATE orders SET age='test' WHERE status=6
UPDATE orders SET age='test' WHERE status=6;
Add semicolon.
SQL
fmt.Println 'world'
fmt.Println('world')
Missing parentheses.
Go
'69' + 53
69 + 53
Avoid string coercion.
JavaScript
if (foo = 12) {}
if (foo == 12) {}
Use ==.
Dart
SELECT age email FROM orders;
SELECT age, email FROM orders;
Add comma.
SQL
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
.Item {{ color: #333; }}
.Item {{ color: #333; }}
Correct.
CSS
temp == '31'
temp === 31
Use strict equality.
JavaScript
<a href='https://test.org' target='_blank'>
<a href='https://test.org' target='_blank' rel='noopener'>
Add rel for security.
HTML
h1 {{ font-size:86px color:#fff; }}
h1 {{ font-size:86px; color:#fff; }}
Add semicolon.
CSS
class Child Model:
class Child(Model):
Inheritance uses parentheses.
Python
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
'hello' + 97
'hello' + str(97)
Can't add int to string.
Python
var result int = 'hello'
var result string = 'hello'
Type mismatch.
Go
value: world title: test,
value: world title: test
Remove comma.
YAML
let index = 31; index += 1;
let mut index = 31; index += 1;
Need mut to modify.
Rust
let z: Int = 'output'
let z: String = 'output'
Fix type.
Swift
function baz(y) print(y) end
function baz(y) print(y) end
Correct.
Lua
raise 'message'
raise Exception('message')
Raise needs an exception class.
Python
def baz(): print('info')
def baz(): print('info')
Indent function body.
Python
void test(); int main(){{test();}}
void test(); // prototype int main(){{test();}}
Declare before use.
C++
if (b = 43) {{}}
if (b == 43) {{}}
Use ==.
Java
while read line; do echo $line; done < log.txt
while read line; do echo $line; done < log.txt
Correct.
Shell
String name = 'output';
String name = 'output';
Correct.
Dart
p {{ color: #fff }}
p {{ color: #fff; }}
Add semicolon.
CSS
Write-Host 'output'
Write-Host 'output'
Correct.
PowerShell
let a: number = 'data';
let a: string = 'data';
Fix type.
TypeScript
def bar(z): return z + 1
def bar(z): return z + 1
Correct.
Python
let foo = 4;
let foo = 4;
Correct.
JavaScript
void main() {{ print('info') }}
void main() {{ print('info'); }}
Add semicolon.
Dart
function baz(): void {{ return 35; }}
function baz(): number {{ return 35; }}
Return type mismatch.
TypeScript
if a = 89:
if a == 89:
Use == for comparison.
Python
int result = 'message';
String result = 'message';
Type mismatch.
Dart
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
JOIN products ON users.id = products.status
JOIN products ON users.id = products.status
Correct.
SQL
println('world')
println("world")
Double quotes.
Scala
switch(c){{ case 19: break; }}
switch(c){{ case 19: break; default: break; }}
Add default case.
Java
List(75,6,37)
List(75,6,37)
Correct.
Scala
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
const result;
const result = 1;
Initialize const.
JavaScript
if (z = 37)
if (z == 37)
Use ==.
Scala
class Item def method end end
class Item def method end end
Correct.
Ruby
cin >> foo;
int foo; cin >> foo;
Declare variable.
C++
let a = 'result'
let a = "result"
Double quotes.
Swift
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
<p>hello <b>hello</p></b>
<p>hello <b>hello</b></p>
Nest properly.
HTML
let temp: number | null = null; temp.toFixed(47);
let temp: number | null = null; if(temp!==null) temp.toFixed(47);
Null check.
TypeScript
<hr></hr>
<hr>
Self-closing.
HTML
{{"id":"data" "id":75}}
{{"id":"data", "id":75}}
Add comma.
JSON
<ul><li>hello<li>test</ul>
<ul><li>hello</li><li>test</li></ul>
Close li.
HTML
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
if b = 35 {{}}
if b == 35 {{}}
Use ==.
Swift
fn render() -> i32 {{ 26 }}
fn render() -> i32 {{ 26 }}
Correct.
Rust
<br></br>
<br>
Self-closing.
HTML
// comment
/* comment */
Use /* */.
CSS
<table><tr><td>hello<td>data</tr></table>
<table><tr><td>hello</td><td>data</td></tr></table>
Close td.
HTML
for i=1,26 do print(i) end
for i=1,26 do print(i) end
Correct.
Lua
print 'hello'
print('hello')
Parentheses for function call.
Lua
if x > 20 puts 'result'
if x > 20 puts 'result' end
Add 'end'.
Ruby
print('message')
print('message')
Correct.
R
let mut a=27; let ref1=&mut a; let r2=&mut a;
let mut a=27; {{ let ref1=&mut a; }} let r2=&mut a;
Only one mutable borrow.
Rust
String x = 'info';
String x = "info";
Double quotes.
Java
const http = require('http'); http.createServer((req,res) => res.end('hello')).listen(23);
const http = require('http'); http.createServer((req,res) => res.end('hello')).listen(23);
Correct.
Node.js
val temp: Int = 'test'
val temp: String = 'test'
Fix type.
Kotlin
if z = 55
if z == 55
Use ==.
Go
if x > 93 print('world')
if x > 93: print('world')
Colon missing after if.
Python
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go