1
\$\begingroup\$
private function getFeatureByParam ($classname, $module, $action) {
 $needFeature = array();
 foreach ($this->featureConf as $featureIndex => $feature) {
 if ($feature['classname'] == $classname && 
 $feature['module'] == $module && 
 $feature['action'] == $action) {
 $needFeature = $feature;
 break;
 }
 }
 return $needFeature;
}
private function getFeatureByParam ($classname, $module, $action) {
 foreach ($this->featureConf as $featureIndex => $feature) {
 if ($feature['classname'] == $classname && 
 $feature['module'] == $module && 
 $feature['action'] == $action) {
 return $feature;
 }
 }
 return array();
}

The first style of writing is more understandability.

The second style of writing is more clean and efficient.

But witch one is more better? (Or they are too bad....)

asked Jan 5, 2013 at 10:03
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

Personally, option 2.

In option 1, by breaking out of the loop, you have to look down until after the end to see what happens, just to see a return. In option 2, it's obvious what you want and when you get it. Although, I think the condition is a little complex.

There are, of course, other options:

private function getFeatureByParam ($classname, $module, $action) {
 foreach ($this->featureConf as $featureIndex => $feature) {
 if ($feature['classname'] != $classname) continue;
 if ($feature['module'] != $module) continue;
 if ($feature['action'] != $action) continue;
 return $feature;
 }
 return array();
}
private function getFeatureByParam ($classname, $module, $action) {
 foreach ($this->featureConf as $featureIndex => $feature) {
 if ( featureMatches( $feature, $classname, $module, $action ) ) {
 return $feature;
 }
 }
 return array();
}

I'm inclined to option 4 :)

answered Jan 5, 2013 at 17:55
\$\endgroup\$
3
  • \$\begingroup\$ Wow,i had never thought the writing style of option 4. It is more clean and packaging the matching operation.Thank you very much~ \$\endgroup\$ Commented Jan 6, 2013 at 7:07
  • \$\begingroup\$ My only issue with this answer is your braceless syntax. But that is more of a point of preference. \$\endgroup\$ Commented Jan 7, 2013 at 17:44
  • \$\begingroup\$ I only have braceless when it's a precondition-style break, continue, or return. Anything else gets one - it's too dangerous not to. \$\endgroup\$ Commented Jan 7, 2013 at 20:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.