Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
rant1ng44626yanyway, it's wrong, returns true instead of a modified object, which I think was the intent, because he was using transform
I personally like it, yeah its a big long string of code, but its readable
its formatted for no distraction mode in sublime...
Although the correct version isn't technically really a one liner anymore -
rant1ng44626yIt grabs all orders from the DB, applying a transform using a closure to add highlight = true parameter to the object. Vue uses this to highlight the row.
I personally think its pretty cool, i mean, you can literally almost read it in english. -
argosen546y@rant1ng I think it is unreadable. All the steps should be clear in a first view. In this case, you have to check in detail the long string to start making conclusions. Refactor asap.
-
For PHP this is quite readable. I'd refactor the highlighthing or fetching the orders out to a separate method though, you know, something about single responsibility. But I've seen much less complex PHP code that was way harder to read.
-
fml892276yIt’s fine - I’m not fond of static function but I’ve seen far worse. Maybe be a bit more specific in the docblock
-
Okay.
....
$affliateUser = Auth::User();
$affliateOrders = Order::where('affiliate_id', $affliateUser->id);
if (!$orders) {
// What now? Empty collection?
}
$createdBeg = new Carbon(...);
$createdEnd = new Carbon(...);
$ordersHighlighted = $affliateOrders
->whereBetween('created_at', [$createdBeg, $createdEnd])->get();
if (!$ordersHighlighted) {
// What now? Empty collection?
}
return $affliateOrders->transform(
function(??? $order) use (??? $ordersHighlighted) {
$order->Highlight = in_array(
$order,
$ordersHighlighted,
true
);
}
); -
Sorry for bad formatting.
Point is:
Don't execute statements multiple times.
Document object classes, use interface / object class in function.
Validate object sanity.
Pass objects instead of arrays / scalar values.
Assure that the return of the object is what you expect - be concise.
Checking via
is_array($bla) && !empty($bla)
makes it very obvious that the returned value is an array.
Don't initialize objects multiple times.
Objects are passed via reference and should always be preferred to scalar values / arrays.
Don't chain 'blindly' calls.
If the API changes or you need to debug - you are fucked.
...
I know it seems readable and tempting.
But it's really a mess and clearly non optimal.
Variables are our friends - and object documentation is our hot lover, hmkay? -
Wack61916yI'm not sure about laravel(?) Orm but, if I'm reading it correctly it's executing the inner query once for every item and for every item it fetches a bunch and checks with an in array.
I guess there would be room for optimization by a) moving the inner function out and/or b) using select for the inner query, to see if there is something matching instead of fetching a collection and traverse it.
Oh and params/return type on collections. Not sure about laravel but AFAIK for plain doctrine if you fetch a collection of the vlass `Foo` the convention is
/**
* @return Foo[]
*/
public function getFoo: array
{
...
}
Note the `Foo[]` part isn't php, but it's understood by IDEs as, there is an either empty array being returned or an array containing (only) `Foo` objects
(Often anything itteratatble is accepted) -
Don't execute statements multiple times.
Edit: especially Database Statements !!!
Performance aside, race conditions might occur on heavy write tables - you'll get different results in each loop as new data is added!!! -
-vim-31256yBurn it in hell fire, I still have nightmares of my python one-liners, now imagine [c#?]
-
rant1ng44626y@GodlikeBlock not my code. I agree fira.
Theme is cobalt though, I like it. What do you use? -
Trithon10296y@rant1ng i made my "own" colorscheme for VSCode - a port from the vim theme "srcery"; feel free to try it :3
I also like the color schemes:
Seth, spirited away, xhi
Related Rants
As someone in charge of reviewing this code, how would you react to this function that contains, what could be, the longest ever readable return statement.
rant
programming
but i get it
long ass return statement
php