New answers tagged

1

If instead of: $result = (object) ['isfound' => null, 'issubscribed' => null]; the initial values were: $result = (object) ['isfound' => false, 'issubscribed' => false]; most of the other code could be simplified. E.g. if (!($found->data->recipient_count > 0)) { $result->isfound = false; $result->issubscribed = false; ...


1

This is a good use case for using Exception, if the language supports exception. You can just write: public function getSubscriberStatus(string $email): object { $endpoint = "contactdb/recipients/search?email=$email"; $found = $this->urlget($endpoint, 200); // this validation is probably unnecessary, looping over empty object has the same ...


2

Let's discuss options. The option you seem to be dismissing is the one that is actually really clean: public void resolveChanges() { boolean isChanged = false; isChanged = isChanged || oldTrans.getfOrder().equals(newTrans.getfOrder()); isChanged = isChanged || oldRight.getfRight().equals(newRight.getfRight()); // etc. compound....


1

If you like detailed line information in your exceptions: bool changeDetected = false; changeDetected = changeDetected || oldTrans.getfOrder().equals(newTrans.getfOrder()); changeDetected = changeDetected || oldTrans.getgOrder().equals(newTrans.getgOrder()); if (changeDetected) { compound.setIsStateChanged(true); LOGGER.info("major change detected"...


1

Adding to Victor's comment, simplifying and exploiting ES6 for varargs function getInfo(howFn, ...info) { try { return howFn(...info); } catch (error) { logger.error(this part needs a little work); } } // Calls let shopInfo = getInfo(shopConfigModel.getShopInfo, phone); let shopInfo = getInfo(shopConfigModel.getShopInfoById, shopId); ...


0

A fairly good rule of thumb is that variables should exist in the smallest scope necessary. This is because the more variables you have in scope at any time, the harder the code can be to understand, and the greater the chance of weird side-effects and interactions. For this reason, defining a variable at class-level when it only needs to exist at method-...


2

It's a typical case of reading something and getting it completely wrong. Temporary variables in their simplest, least ugly, most understable, most maintainable, most efficient form are local variables, introduced exactly at the point where they are needed, going out of scope immediately when they are not needed anymore. Your code systematically removes ...


8

Your question seems to be based on a misunderstanding. Fowler does not "dislike temps" in general. Quite the opposite, he provided a specific refactoring for introducing such variables, he called it Extract Variable (a.k.a. "Introduce Explaining Variable") Of course, he also gave the inverse operation a name: Inline Variable From this I hope it gets ...


5

I don't see much value in reduction of temporary variables as a goal unto itself, especially if those variables are being used to clarify the code. Consider these two code examples from refactoring.guru: void renderBanner() { if ((platform.toUpperCase().indexOf("MAC") > -1) && (browser.toUpperCase().indexOf("IE") > -1) && ...


Top 50 recent answers are included