Skip to content

Files

Latest commit

 

History

History
42 lines (31 loc) · 704 Bytes

UselessPostfixExpression.md

File metadata and controls

42 lines (31 loc) · 704 Bytes

Pattern: Unnecessary postfix expression

Issue: -

Description

This can lead to confusion as a reader of the code might think the value will be incremented/decremented. However the value is replaced with the original value which might lead to bugs.

Example of incorrect code:

var i = 0
i = i--
i = 1 + i++
i = i++ + 1

fun foo(): Int {
    var i = 0
    // ...
    return i++
}

Example of correct code:

var i = 0
i--
i = i + 2
i = i + 2

fun foo(): Int {
    var i = 0
    // ...
    i++
    return i
}

Further Reading