S
S
Sergey2021-10-24 01:40:23
PHP
Sergey, 2021-10-24 01:40:23

How useful is splitting into functions and classes?

There is a custom project without a framework, a separate part of the functionality consists of several intertwined classes.
Slightly confusing is that for 3 classes -> 1000 lines of code -> ~75 functions actively calling each other.
The author of this goodness declares that it is absolutely normal practice to put every couple of lines into a separate function.
Is it normal?

Upd. Added part of one of the classes, the names of the methods were a little depersonalized for privacy purposes)

<?php
class SomeClass {
    public function __construct($inner_class_name) {
        if (file_exists($inner_class_name)) {
            include($inner_class_name);
            $this->inner_class_name = new $inner_class_name;
        }
    }
    public function function_1() {
        $a = $this->function_2();
        $some_cond = $this->function_6($a);
        if ($some_cond) {
            $a = $this->function_3($a);
        } else {
            $a = $this->function_4($a);
        }
        if ($this->function_5($a)) {
            return $a;
        } else {
            return null;
        }
    }

    private function function_5($a) {
        $b = $this->function_7($a);
        if ($b && $this->function_8($b)) {
            return $this->function_9($a);
        } else {
            if ($this->function_10($b)) {
                return null;
            } else {
                return $this->function_11($b);
            }
        }
    }
    private function function_2() {}
    private function function_3($a) {}
    private function function_4($a) {}
    private function function_6($a) {}
    private function function_7($a) {}
    private function function_8($b) {}
    private function function_9($b) {}
    private function function_10($b) {
        return $this->inner_class_name->some_func_1($b);
    }
    private function function_11($b) {
        return $this->inner_class_name->some_func_2($b);
    }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ipatiev, 2021-10-24
@paper_castle

This is fine.
Well, that is, you can’t tell about a specific code without seeing it, but “putting every couple of lines into a separate function” is normal. Much more normal than writing the whole thousand in one.
I recommend finding the video "How to write code that your tests will like" from the first PHPRussia. There it is very intelligibly told about why it is necessary to do simple methods. Not just for tests. And also for reuse and to simplify code maintenance. When you need to read the main logic of a module, even cutting it in half (one method instead of two lines of code) makes the task much easier.
And even if the test understood what your code does, then the person will understand even more so :)
As you wrote above, it's all about the names of the methods. If they are informative

if ($this->function_7($a) && $this->function_8($b))
reads like coherent English text , then mission accomplished! Functions serve their purpose: it's easiest to read code that isn't there. Instead of parsing what this or that code does, you can just read a short text in English.

V
Vasily Bannikov, 2021-10-24
@vabka

The author of this goodness declares that it is absolutely normal practice to put every couple of lines into a separate function.
Is it normal?

Only the Sith make everything absolute. Without a code, it is impossible to say whether it is normal or not.
In principle - if these functions are so well divided, then why not?
1000 lines of code for 3 classes is not a lot. It's only 300 lines per class.
75 functions - this results in an average of about 10-15 lines per function, which is also ok in principle.
According to your words, nothing more can be said objectively.

N
Nadim Zakirov, 2021-10-24
@zkrvndm

Look at the names of the functions, if they immediately understand what each one does, then everything is fine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question