Is shorter code better?

this thread was split off from Making variableArray to work - #5

In PHP 8.1 this would be

$myArray = array_map(trim(...), $myArray);

:slightly_smiling_face:

1 Like

Wow what an improvement. Another 30 characters less to type :stuck_out_tongue:

I don’t care about 30 characters less to write, I do care about 30 characters less to read :slight_smile:

I dc a30c let. I dc a30c let.

I preference more characters to read. It makes it mostly easier to understand

Okay, so maybe the small things don’t really count.

But consider this, this was PHP 5.6:

class Person
{
    /**
     * @var string
     */
    private $id;

    /**
     * @var string
     */
    private $firstName;

    /**
     * @var string
     */
    private $lastName;

    /**
     * @var string
     */
    private $emailAddress;

    public function __construct($id, $firstName, $lastName, $emailAddress)
    {
        // type checks left out for brevity
        $this->id = $id;
        $this->firstName = $firstName;
        $this->lastName = $lastName;
        $this->emailAddress = $emailAddress;
    }

    /**
     * @return string
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return string
     */
    public function getFirstName()
    {
        return $this->firstName;
    }

    /**
     * @return string
     */
    public function getLastName()
    {
        return $this->lastName;
    }

    /**
     * @return string
     */
    public function getEmailAddress()
    {
        return $this->emailAddress;
    }
}

vs this in PHP 8.1:

readonly class Person
{
    public function __construct(
        public string $id,
        public string $firstName,
        public string $lastName,
        public string $emailAddress
    ) {
    }
}

exact same functionality.

Which one do you like better?

Well the first example you used is moot because this is what do prior to 8.1…

$myArray = array_map(trim, $myArray);

So that is a non-starter. However, your second example is legit to express the point.

Is shorter code better? Not necessarily. Back on Dream.In.Code (now closed) we use to play a game called “Code golf” where you would try to write an algorithm with as few characters as possible. Shortest code wins. Oh man, we had some winners who could write an algorithm in like 30 characters total and it was not at all readable.

So in short, shorter code is only better if it is perfectly readable, ideally maintains or improves performance, but readability is definitely the key.

PHP 8+ has certainly provided some great changes that made what was longer tedious code into something easier to read and maintain. PHP has really turned itself around lately and I think is becoming a really powerful and flexible language that has knocked off a serious amount of rust from its past.

:slight_smile:

3 Likes

With respect to your Person example, I suspect this was basically a typo but private/public have very different functionality.

On a more subtle note the @var string annotation informs various static analyzers making it fairly obvious if you attempt to assign something other than a string. However, unlike public string $firstName there is no runtime checking. With annotations you can assign non-string values and let the type conversion stuff do it’s thing. With actual types you get an error. So no, not the exact same functionality.

With respect to the original question, sure less code is generally better than more code but sometimes it gets taken to extremes and actually makes the code more difficult to maintain.

And while I have not really researched this lately many of the array_ type functions were actually slower (sometimes noticeable slower) then good ole fashion foreach loops. I’m sure they have been optimized by now but that was a bit annoying.

2 Likes

I think this is the big line that most if not all people will agree with: There are extremes on either end of the spectrum to be avoided, for exactly the same reasonings in both directions. Find your goldilocks spot, whether it be individually or in your team.

1 Like

The entire class is made readonly, so I can read all variables, but not write to them. Same as the PHP 5.6 example :slightly_smiling_face:

Yes. This is a good conclusion to this thread I think.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.