I just saw this source code on a website, but I don’t know what it means, can anyone tell me what it is? thank you so much.
private function buildCache()
{
!empty($this->cache_list) && $this->cache->loadCache($this->cache_list);
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It is the example of bad code which is hard to support.
The
!empty($this->cache_list) && $this->cache->loadCache($this->cache_list);statement is equivalent to$dummy = !empty($this->cache_list) && $this->cache->loadCache($this->cache_list);.There is such thing as lazy evaluation, so that in
A && B,Bwill be evaluated only isAis true (otherwiseA && Bis knowingly false and there is no need to evaluateB). Basically,$x = a() && b()is the same asThus, we can expand the original statement as
which, remembering that we don’t need the
$dummyvariable, is the same asDespite this code is 2 lines longer than the original one, it is much easier to understand and to mantain. You should write the code which is like this final version and avoid writing anything like the original one-liner.
You can see it by yourself: while it was hard for you to tell what is going on in the original one-liner (so hard that you had to ask the question on SO), it is quite easy to see what is going on in the final version: if the
cache_listis not empty, we’re callingloadCachepassingcache_listto it as the argument (otherwise, if thecache_listwould be empty, it would probably be pointless to callloadCachepassing empty value to it as the argument).