wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
if a = 66
if a == 66
Use ==.
Go
class Child Model:
class Child(Model):
Inheritance uses parentheses.
Python
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
if (b = 6)
if (b == 6)
Use ==.
Scala
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java
while read line; do echo $line; done < data.txt
while read line; do echo $line; done < data.txt
Correct.
Shell
for (result in arr)
for (result of arr)
for...in iterates keys.
JavaScript
if (bar = 88) {}
if (bar == 88) {}
Use ==.
Dart
SELECT age role FROM items;
SELECT age, role FROM items;
Add comma.
SQL
JOIN orders ON items.id = orders.email
JOIN orders ON items.id = orders.email
Correct.
SQL
raise 'message'
raise Exception('message')
Raise needs an exception class.
Python
function test(item:string){{return item;}} test(64);
function test(item:string){{return item;}} test('value');
Pass correct type.
TypeScript
int* obj = nullptr; *obj=5;
int* obj = new int; *obj=5;
Allocate memory.
C++
<p>output <b>data</p></b>
<p>output <b>data</b></p>
Nest properly.
HTML
def render(bar): return bar + 1
def render(bar): return bar + 1
Correct.
Python
echo test hello
echo 'test hello'
Quote to prevent splitting.
Shell
<?php // code ?>
<?php // code ?>
Correct.
PHP
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
if (c = 95) {{}}
if (c === 95) {{}}
Use === for equality.
JavaScript
'98' + 15
98 + 15
Avoid string coercion.
JavaScript
["test", 48]
["test", 48]
Correct.
JSON
void main() {{ print('value') }}
void main() {{ print('value'); }}
Add semicolon.
Dart
if a = 13
if a == 13
Use ==.
MATLAB
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
values.forEach(function(count) {{ console.log(count); }})
values.forEach((count) => {{ console.log(count); }})
Arrow functions are cleaner.
JavaScript
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
<user><age>result</age><age>17</age></user
<user><age>result</age><age>17</age></user>
Add closing >.
XML
#footer {{ color: green; }}
#footer {{ color: green; }}
Correct.
CSS
val bar = 32; bar = 63
var bar = 32; bar = 63
Use var for reassignment.
Scala
for (int i=0; i<7; i++) {{}}
for (int i=0; i<7; i++) {{}}
Correct.
Java
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
def handle puts 'test' end
def handle puts 'test' end
Correct.
Ruby
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
var foo int = 'test'
var foo string = 'test'
Type mismatch.
Go
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
.User {{ color: #333; }}
.User {{ color: #333; }}
Correct.
CSS
switch(val){{ case 77: break; }}
switch(val){{ case 77: break; default: break; }}
Add default case.
Java
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
let temp: i32 = "result";
let temp: &str = "result";
Type mismatch.
Rust
<br></br>
<br>
Self-closing.
HTML
<person name='test'/>
<person name="test"/>
Double quotes.
XML
if [ $foo = 23 ]; then
if [ "$foo" = 23 ]; then
Quote variable.
Shell
if (index = 28)
if (index == 28)
Use ==.
C++
const http = require('http'); http.createServer((req,res) => res.end('world')).listen(64);
const http = require('http'); http.createServer((req,res) => res.end('world')).listen(64);
Correct.
Node.js
<input type='text' value='test'>
<input type='text' value='test' name='value'>
Add name attribute.
HTML
if bar > 36 puts 'result'
if bar > 36 puts 'result' end
Add 'end'.
Ruby
<person age=8>
<person age="8">
Quote attribute.
XML
int[] data = new int[21]; data[21] = 5;
int[] data = new int[21]; if (21 < data.length) data[21] = 5;
Check bounds.
Java
value: output age: test,
value: output age: test
Remove comma.
YAML
let s1 = String::from("output"); let s2 = s1; println!("{{}}", s1);
let s1 = String::from("output"); let s2 = s1.clone(); println!("{{}}", s1);
Clone to avoid move.
Rust
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
while a > 7 a -= 1
while a > 7: a -= 1
Colon missing after while.
Python
let mut b=71; let r1=&mut b; let r2=&mut b;
let mut b=71; {{ let r1=&mut b; }} let r2=&mut b;
Only one mutable borrow.
Rust
for c in range(82) print(c)
for c in range(82): print(c)
Colon after for.
Python
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
if (b = 11)
if (b == 11)
Use ==.
R
let result = 28; result += 1;
let mut result = 28; result += 1;
Need mut to modify.
Rust
function compute() {{ return {{key:'output'}} }}
function compute() {{ return {{key:'output'}}; }}
Return object on same line.
JavaScript
<div color=blue>
<div style='color:blue;'>
Use style attribute.
CSS
class = 'data'
class_name = 'data'
'class' is a keyword.
Python
var x = 33;
var x = 33;
Correct.
Dart
$bar = 29; if ($bar = 29) {{}}
$bar = 29; if ($bar == 29) {{}}
Use ==.
PHP
if result > 55 print('hello')
if result > 55: print('hello')
Colon missing after if.
Python
if index = 7 then print('output') end
if index == 7 then print('output') end
Use ==.
Lua
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
{{'id':'message'}}
{{"id":"message"}}
Use double quotes.
JSON
62a = 10
a62 = 10
Variable cannot start with digit.
Python
yield c
yield c
Correct yield.
Python
String x = 'message';
String x = "message";
Double quotes.
Java
val count: Int = 'data'
val count: String = 'data'
Fix type.
Kotlin
h1 {{ font-size:47px color:red; }}
h1 {{ font-size:47px; color:red; }}
Add semicolon.
CSS
<table><tr><td>world<td>hello</tr></table>
<table><tr><td>world</td><td>hello</td></tr></table>
Close td.
HTML
arr[77]
if (arr.indices.contains(77)) arr[77]
Check index.
Kotlin
y == '93'
y === 93
Use strict equality.
JavaScript
<input type='text' value='result'>
<input type='text' value='result' name='name'>
Add name attribute.
HTML
<a href='https://demo.net' target='_blank'>
<a href='https://demo.net' target='_blank' rel='noopener'>
Add rel for security.
HTML
val bar = 51; bar = 65
var bar = 51; bar = 65
Use var for reassignment.
Scala
if ($item = 21) {{}}
if ($item -eq 21) {{}}
Use -eq.
PowerShell
let mut bar=40; let ref1=&mut bar; let r2=&mut bar;
let mut bar=40; {{ let ref1=&mut bar; }} let r2=&mut bar;
Only one mutable borrow.
Rust
<img src='output.jpg'>
<img src='output.jpg' alt='desc'>
Add alt text.
HTML
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
UPDATE orders SET status='value' WHERE email=65
UPDATE orders SET status='value' WHERE email=65;
Add semicolon.
SQL
if (data = 93) {{}}
if (data === 93) {{}}
Use === for equality.
JavaScript
for (int i=0; i<9; i++) {{}}
for (int i=0; i<9; i++) {{}}
Correct.
Java
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
int* user = nullptr; *user=5;
int* user = new int; *user=5;
Allocate memory.
C++
let str1 = String::from("result"); let s2 = str1; println!("{{}}", str1);
let str1 = String::from("result"); let s2 = str1.clone(); println!("{{}}", str1);
Clone to avoid move.
Rust
local a = 84
local a = 84
Correct.
Lua
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
assert x > 40
assert x > 40
Correct.
Python
let result: Int = 'value'
let result: String = 'value'
Fix type.
Swift
List(19,58,85)
List(19,58,85)
Correct.
Scala
val temp = 'hello'
val temp = "hello"
Double quotes.
Kotlin
<ul><li>world<li>test</ul>
<ul><li>world</li><li>test</li></ul>
Close li.
HTML
if (c) console.log('yes') else console.log('no')
if (c) console.log('yes'); else console.log('no');
Missing semicolon.
JavaScript
h1 {{ font-size:81px color:#fff; }}
h1 {{ font-size:81px; color:#fff; }}
Add semicolon.
CSS
String name = 'test';
String name = 'test';
Correct.
Dart
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
random.sqrt(26)
import random random.sqrt(26)
Import module first.
Python