One Of The Most Common Mistakes in Coding. Ever.

preview_player
Показать описание
Let me share an example code and see if you spot a bug. And then I will explain.

- - - - -
Support the channel by checking out my products:

- - - - -
Other places to follow:
Рекомендации по теме
Комментарии
Автор

I can't recommend Larastan enough. Having static analysis prevents so many bugs and gets rid of many tests

spicynoodle
Автор

Technically $licenseType is a list of licenses because even if you pass a string it will be turn to an array, so it should be $licenseTypes (plural). When I have a loop in my code, I always try to use plural for the container and singular for the item. That's how I protect myself from that mistake...but of course an error can always occur. :D

IlPandax
Автор

Definitely this is the most annoying mistake we make and then didn't know what is happening especially doing loops in blade with same variables, what I used to overcome this problem was using an underscore at the beginning of same variable in loop, but testing is more reliable solution.

shahsawoodshinwari
Автор

Solution for this would also be to move the whole $hasLicense logic to a separate function. Then variable overwrite will be impossible that way.

NoOorZ
Автор

i think the biggest problem is parameter type string|array, it would look better to be just one or the other, lets say array and function to be called like has_license(['some_licence']) even if it has one element... inside collect($type) and use (or array functions if not in Laravel) to see if some element have "user_" and "team_" and return the value, function would have nice fluent code

aleksandarstevanovic
Автор

That's what I used to do and get annoyed at the end! Thank you for making this video. Hats off to you

asfiaaiman
Автор

Gosh I’m feeling proud of my self that I’ve never made this mistake to this day. If i really really really need to name the value same as the parameter i use $_type as an inline parameter

VERone
Автор

Yeah these are really annoying bugs. So hard to see. I think one healthy habit is to ALWAYS use a plural variable name for things you're looping over, and singular for the individual loop items.

Example:
foreach(Arr::wrap($types) as $type)
foreach($order->getItems() as $item) // getItems is plural

Laravel has some built in magic for doing singular/plural on naming models/tables/controllers. I wonder if you could also get that as a static analysis rule, checking the naming of things in foreach loops?

laubannenberg
Автор

Can we use add unset($type) ; line after foreach loop ?

AlexandrPeters
Автор

That is why i prefer to prefix the param name with an _, just to enforce that the parama are not local variables.

AbhinavKulshreshtha
Автор

i never encountered this error and never made it myself. It not ever got in my mind to give two different variables the same name... But other error i see is two "ifs" inside foreach loop with same result. That's obvious mistake.

elgoogssie
Автор

havent coded in php since 2008 or coded in general and caught it in 10 seconds haha . good one though

snkehead-
Автор

This mistake is especially relevant nowadays. Because that damned Copilot introduces this bug in every place it can.

vitiok
Автор

you have more errors here
- dont set hasLicense=true. error in logic. initialy, it should be false, and you check and make it true if match some condition
- dont make parameter type string|array - its very bad style and could cause more mistakes
- $varType = is_array() ? : - why not using gettype()

titov