Dlean Jeans
Dlean Jeans

Reputation: 986

[Clean Code]: Which is better? Variable or return one-line function?

Which is better? One-line returning function optionIsOutOfRange():

    public function deleteOption(index:int):void {
        if (optionIsOutOfRange(index)) {
            throw new Error("Option index is out of range! Cannot delete!");
        } else {
            options.splice(index, 1);
        }
    }

    private function optionIsOutOfRange(optionIndex:int):Boolean {
        return optionIndex > numOptions - 1;
    }

or variable here is optionIsOutOfRange:

    public function deleteOption(index:int):void {
        var optionIsOutOfRange:Boolean = index > numOptions - 1;
        if (optionIsOutOfRange(index)) {
            throw new Error("Option index is out of range! Cannot delete!");
        } else {
            options.splice(index, 1);
        }
    }

Upvotes: 1

Views: 552

Answers (1)

Andrei Nikolaenko
Andrei Nikolaenko

Reputation: 1074

A function is needed if you want to implement some checking logic that is used in multiple places and could change in the future.

For code maintainability and scalability a function is better, for performance an inline code is better.

Upvotes: 2

Related Questions