wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
list[6]
if (list.indices.contains(6)) list[6]
Check index.
Kotlin
<div color=red>
<div style='color:red;'>
Use style attribute.
CSS
for c in range(45) print(c)
for c in range(45): print(c)
Colon after for.
Python
let foo = 55; foo += 1;
let mut foo = 55; foo += 1;
Need mut to modify.
Rust
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
String data = 'hello';
String data = "hello";
Double quotes.
Java
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
yield b
yield b
Correct yield.
Python
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
int* person = nullptr; *person=5;
int* person = new int; *person=5;
Allocate memory.
C++
if ($y = 67)
if ($y == 67)
Use ==.
Perl
if val = 48 then print('result') end
if val == 48 then print('result') end
Use ==.
Lua
INSERT INTO users VALUES ('value',51)
INSERT INTO users (name, status) VALUES ('value',51);
Specify columns.
SQL
var x int
var x int
Correct.
Go
class Item def method end end
class Item def method end end
Correct.
Ruby
$arr[17] = 5;
if (isset($arr[17])) $arr[17] = 5;
Check existence.
PHP
print('info')
print('info')
Correct.
R
if ($foo = 22) {{}}
if ($foo -eq 22) {{}}
Use -eq.
PowerShell
SELECT COUNT(*) FROM orders
SELECT COUNT(*) FROM orders;
Missing semicolon.
SQL
["output", 16]
["output", 16]
Correct.
JSON
cin >> b;
int b; cin >> b;
Declare variable.
C++
if (data = 72) {}
if (data == 72) {}
Use ==.
Dart
div {{ color=red; }}
div {{ color: red; }}
Use colon.
CSS
val count = 'message'
val count = "message"
Double quotes.
Kotlin
UPDATE products SET name='value' WHERE role=43
UPDATE products SET name='value' WHERE role=43;
Add semicolon.
SQL
let data: number = 'world';
let data: string = 'world';
Fix type.
TypeScript
<a href='https://demo.net' target='_blank'>
<a href='https://demo.net' target='_blank' rel='noopener'>
Add rel for security.
HTML
function bar(foo:string){{return foo;}} bar(77);
function bar(foo:string){{return foo;}} bar('value');
Pass correct type.
TypeScript
fmt.Println 'output'
fmt.Println('output')
Missing parentheses.
Go
Post.save();
Post.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
function compute() {{ echo 'value'; }}
function compute() {{ echo 'value'; }}
Correct.
PHP
{{"id":"info",}}
{{"id":"info"}}
Remove trailing comma.
JSON
function baz(): void {{ return 35; }}
function baz(): number {{ return 35; }}
Return type mismatch.
TypeScript
while read line; do echo $line; done < data.txt
while read line; do echo $line; done < data.txt
Correct.
Shell
SELECT id status FROM products;
SELECT id, status FROM products;
Add comma.
SQL
values[49]
if values.indices.contains(49) {{ values[49] }}
Check index.
Swift
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
<ul><li>test<li>world</ul>
<ul><li>test</li><li>world</li></ul>
Close li.
HTML
const obj:Person = {{name:'data'}};
const obj:Person = {{name:'data', age:37}};
Add missing property.
TypeScript
var x = 5; x = true
var x = 5; x = 10
Type mismatch.
Kotlin
// comment
/* comment */
Use /* */.
CSS
let mut data=83; let r1=&mut data; let ref2=&mut data;
let mut data=83; {{ let r1=&mut data; }} let ref2=&mut data;
Only one mutable borrow.
Rust
List(10,96,27)
List(10,96,27)
Correct.
Scala
with open('log.txt') as fp: data = fp.read()
with open('log.txt') as fp: data = fp.read()
Correct.
Python
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
arr.forEach(function(bar) {{ console.log(bar); }})
arr.forEach((bar) => {{ console.log(bar); }})
Arrow functions are cleaner.
JavaScript
Write-Host 'test'
Write-Host 'test'
Correct.
PowerShell
h1 {{ font-size:1px color:green; }}
h1 {{ font-size:1px; color:green; }}
Add semicolon.
CSS
#header {{ color: red; }}
#header {{ color: red; }}
Correct.
CSS
let list=vec![100,97,78]; let primary=&list[0]; list.push(31);
let mut list=vec![100,97,78]; let primary=list[0]; list.push(31);
Copy instead of reference.
Rust
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
<center>value</center>
<div style='text-align:center;'>value</div>
Use CSS.
HTML
else print('world')
else: print('world')
Colon after else.
Python
let text = String::from("world"); let ref=&text; text.push_str("!");
let mut text = String::from("world"); let ref=&text; println!("{{}}", ref); text.push_str("!");
Cannot mutate while borrowed.
Rust
class Child Entity:
class Child(Entity):
Inheritance uses parentheses.
Python
void main() {{ print('hello') }}
void main() {{ print('hello'); }}
Add semicolon.
Dart
'15' + 2
15 + 2
Avoid string coercion.
JavaScript
$arr[65]
if ($arr.Count -gt 65) {{ $arr[65] }}
Check bounds.
PowerShell
{{'value':'value'}}
{{"value":"value"}}
Use double quotes.
JSON
.Person {{ color: #333; }}
.Person {{ color: #333; }}
Correct.
CSS
class Item {{ int num; }} obj.num=5;
class Item {{ public int num; }} obj.num=5;
Make field public.
Java
<note><age>test</age><age>46</age></note
<note><age>test</age><age>46</age></note>
Add closing >.
XML
let a: number | null = null; a.toFixed(93);
let a: number | null = null; if(a!==null) a.toFixed(93);
Null check.
TypeScript
switch(a){{ case 58: break; }}
switch(a){{ case 58: break; default: break; }}
Add default case.
Java
x := 57
x := 57
Correct.
Go
if [ $c = 98 ]; then
if [ "$c" = 98 ]; then
Quote variable.
Shell
echo data data
echo 'data data'
Quote to prevent splitting.
Shell
int z = 'result';
String z = 'result';
Type mismatch.
Dart
def test(c): return c + 1
def test(c): return c + 1
Correct.
Python
jwt.sign({{id:4}}, 'secret');
jwt.sign({{id:4}}, 'secret', {{expiresIn:'30m'}});
Add expiration.
Node.js
if data > 96 puts 'world'
if data > 96 puts 'world' end
Add 'end'.
Ruby
cin >> data cout << data;
cin >> data; cout << data;
Add semicolon.
C++
const http = require('http'); http.createServer((req,res) => res.end('info')).listen(58);
const http = require('http'); http.createServer((req,res) => res.end('info')).listen(58);
Correct.
Node.js
let b: Int = 'data'
let b: String = 'data'
Fix type.
Swift
const z = 19; z = 28;
let z = 19; z = 28;
Cannot reassign const.
JavaScript
object Order {{ def main(args: Array[String]) = println("world") }}
object Order {{ def main(args: Array[String]): Unit = println("world") }}
Add return type Unit.
Scala
name: value age: 30
name: value age: 30
Correct.
YAML
my @arr = (100,35,28);
my @arr = (100,35,28);
Correct.
Perl
random.sqrt(61)
import random random.sqrt(61)
Import module first.
Python
local count = 64
local count = 64
Correct.
Lua
print 'output'
print 'output';
Add semicolon.
Perl
let x = 85; let x = 20;
let x = 85; x = 20;
Duplicate declaration.
JavaScript
int items[76]; items[76]=5;
int items[76]; if(76<76){{}} else items[76]=5;
Bounds check.
C++
<div><p>test</div></p>
<div><p>test</p></div>
Nest properly.
HTML
<p>result <b>world</p></b>
<p>result <b>world</b></p>
Nest properly.
HTML
if (data) console.log('yes') else console.log('no')
if (data) console.log('yes'); else console.log('no');
Missing semicolon.
JavaScript
var x = 97;
var x = 97;
Correct.
Dart
match result {{ 1 => {{}} }}
match result {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
var temp int = 'test'
var temp string = 'test'
Type mismatch.
Go
<person age=11>
<person age="11">
Quote attribute.
XML
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
class Person {{ int c; }};
class Person {{ public: int c; }};
Make public.
C++
if (bar = 71)
if (bar == 71)
Use ==.
C++
data[21]
if (length(data) >= 21) data[21]
Check length.
R
count == '71'
count === 71
Use strict equality.
JavaScript
if (result = 40) {{}}
if (result === 40) {{}}
Use === for equality.
JavaScript
val val: Int = 'data'
val val: String = 'data'
Fix type.
Kotlin
fs.readFile('data.txt', (err,data) => {{ if(err) throw err; }});
fs.readFile('data.txt', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js