Defensive Programming | Laravel find()
  • Defensive Programming

One of the things I’ve learned in my career is to code defensively.

Laravel makes it very easy to ask for an object, but it also makes it very easy to get it wrong. If you request an object using the find method and it doesn’t find it, you don’t get an error. You get a null. If you then request a property on that object, without first checking for a null, you will get a run-time error.

This is pretty basic stuff, but it never hurts to go over it again.

Wrong:

$name = Widget::find(23)->name;

 

 

Right:

if ($widget = Widget::find(23)) { $name = $widget->name; }

 

 

The first block will throw an error if Widget #23 doesn’t exist in the database. The second checks for it’s existence and then only reaches for the name if it found it.

 

 

Are you also a PHP programmer? I’ve got other blog entries that you might be interested in!

 

More about me

I have been programming for almost 20 years. It all started with Basic and RPG III way back in high school, and have played around with many languages. PHP is my main language today.

Some sites I have built:

For fans of BMX legend Scotty Cranmer: https://ScottyFan.com

Meet my Basenji’s Graham and Ginger: https://BarooBoos.com

Meet my newest Basenji, Zulu: https://ZuluJoy.com

Search engine for programmers – SymbolHound Search

Another great discovery

I found an interesting tool today called Symbol Hound. It is a search engine for programmers that allows you to include symbols in your search. Google tends to remove them, and as such is not really a good search engine for programmers. So if you’re looking for something like ‘php $’, or ‘#’, it doesn’t always return what you’d expect. This site indexes only major programming related sites like Stack Overflow. The owners say this is to help keep costs down. It costs a lot to house the data they crawl. They even politely ask you to turn off your ad blocker to support them. I turned mine off because I like to support other programmers, and I know how expensive it is to run a server.

Useful? Yes, but…

Personally, I don’t know how often I’ll use this tool, but I feel that it is valuable enough to write a blog post about it. It isn’t every day you need a search engine for programmers, but when you do, it’s nice to know there is one out there!

SearchHound logo

Check it out at http://SearchHound.com

*One thing to note is that it does not support https at the time of this posting. I have no idea why, since it doesn’t cost anything to set up a free SSL certificate.

 

Are you also a PHP programmer? I’ve got other blog entries that you might be interested in!

 

More about me

I have been programming for almost 20 years. It all started with Basic and RPG III way back in high school, and have played around with many languages. More recently, I worked with ColdFusion for 12 years. (Far too many!) And have since worked with Obj-C, Swift, C#, and PHP. I mostly stay focused on PHP and Laravel these days.

You can check out some of my other sites if you’d like:

A site for fans of BMX legend Scotty Cranmer: https://ScottyFan.com

Meet my Basenji’s Graham and Ginger: https://BarooBoos.com

Meet my newest Basenji, Zulu: https://ZuluJoy.com