Re: [RFC] Object Initializer

From: Date: Sat, 14 Sep 2019 09:21:32 +0000
Subject: Re: [RFC] Object Initializer
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-107090@lists.php.net to get a copy of this message
Hi Sebastian, sob., 14 wrz 2019 o 11:05 Sebastian Bergmann <sebastian@php.net> napisał(a): > Am 13.09.2019 um 15:23 schrieb Matthew Brown: > > Though this is truly a stylistic complaint, I think it will lead to > > harder-to-analyse code. > > Fully agreed, and not just harder-to-analyse for a static analysis tool > but also for humans that read the code. -1 from me. > > Could you explain why additional noise caused by constantly repeating instance variable name and arrow in front of property name and assignment part is easier to read? Just wondering why you see object initializer as harder to read which I've view as easier to read. Example - easy to read: function createCustomerDTO(): Customer { $customer = new Customer(); $customer->id = Uuid::uuid4; $customer->name = "John Doe"; $customer->address = "Customer Street 16"; $customer->city = "London"; $customer->country = "GB"; $customer->phoneNumber = PhoneNumber::fromString("+1555 010 020"); $customer->birthDate = new DateTimeImmutable("1983-01-01"); $customer->email = Email::fromString("john.doe@example.com"); return $customer; } Example - hard to read: Why? function createCustomerDTO(): Customer { return new Customer { id = Uuid::uuid4, name = "John Doe", address = "Customer Street 16", city = "London", country = "GB", phoneNumber = PhoneNumber::fromString("+1555 010 020"), birthDate = new DateTimeImmutable("1983-01-01"), email = Email::fromString("john.doe@example.com"), }; } Thanks in advance, Michał Brzuchalski

« previous php.internals (#107090) next »