NYCPHP Meetup

NYPHP.org

No subject


Mon Mar 11 08:43:17 EDT 2013


On Mar 15, 2013, at 10:35 PM, Joseph Crawford <codebowl at gmail.com> wrote:

I personally would disagree with you on that one.  I think the single
return point is more confusing.  Though that is more of a preference.

The single return path leads to extra processing that would not be required=
.
On Mar 15, 2013 9:12 PM, "Robert Stoll" <rstoll at tutteli.ch> wrote:

> I would say the first version is ok but only because your function is
> small enough.****
>
> If your function gets bigger and bigger, then it is better to have only
> one exit point  in terms of readability.****
>
> ** **
>
> *Von:* talk-bounces at lists.nyphp.org [mailto:talk-bounces at lists.nyphp.org]
> *Im Auftrag von *Joseph Crawford
> *Gesendet:* Freitag, 15. M=E4rz 2013 22:15
> *An:* NYPHP Talk
> *Betreff:* Re: [nycphp-talk] style question: returning from a function
> while you're in a foreach****
>
> ** **
>
> The only reason would be poorly designed code where the return value coul=
d
> change based on multiple separate conditionals.  I have seen it in some
> legacy code.****
>
> On Mar 15, 2013 5:50 PM, "Anthony Ferrara" <ircmaxell at gmail.com> wrote:
> >
> > The first is fine. There's no reason not to do it...
> >
> >
> > On Fri, Mar 15, 2013 at 5:30 PM, Joseph Crawford <codebowl at gmail.com>
> wrote:
> >>
> >> There are a few things to learn from here.
> >>
> >> First it is not only a styling thing.  In the first one you have
> several return
> >> statements and in the second you only have 1 return statement.
> >>
> >> It will depend on the code logic but I tend to return when I have the
> value I need rather
> >> than assign it to a variable and have it continue to process through
> the rest of the function.
> >>
> >> If you only return after the entire function is complete but your know=
n
> case was the first check in
> >> the function you are "over" processing and will only add time to your
> sites load time.
> >>
> >> Thanks,
> >> Joseph Crawford
> >>
> >> On Mar 15, 2013, at 5:08 PM, David Mintz wrote:
> >>
> >>> function whatever(Array $array) {
> >>>
> >>>     foreach ($array as $key =3D> $value) {
> >>>         if  ($something)  {
> >>>              return true;
> >>>         }
> >>>     }
> >>>     return false;
> >>> }
> >>>
> >>> Is there any reason  -- style, legibility, whatever -- not to do the
> above? Or should you do something like
> >>>
> >>> function whatever(Array $array)  {
> >>>
> >>>    $return =3D false;
> >>>     foreach ($array as $key =3D> $value) {
> >>>         if  ($something)  {
> >>>              $return =3D true;
> >>>              break;
> >>>         }
> >>>     }
> >>>     return $return;
> >>> }
> >>>
> >>> Thanks.
> >>>
> >>>
> >>> --
> >>> David Mintz
> >>> http://davidmintz.org/
> >>> Fight for social equality:
> >>> http://socialequality.com/
> >>>
> >>> _______________________________________________
> >>> New York PHP User Group Community Talk Mailing List
> >>> http://lists.nyphp.org/mailman/listinfo/talk
> >>>
> >>> http://www.nyphp.org/show-participation
> >>
> >>
> >>
> >> _______________________________________________
> >> New York PHP User Group Community Talk Mailing List
> >> http://lists.nyphp.org/mailman/listinfo/talk
> >>
> >> http://www.nyphp.org/show-participation
> >
> >
> >
> > _______________________________________________
> > New York PHP User Group Community Talk Mailing List
> > http://lists.nyphp.org/mailman/listinfo/talk
> >
> > http://www.nyphp.org/show-participation****
>
> _______________________________________________
> New York PHP User Group Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> http://www.nyphp.org/show-participation
>
_______________________________________________
New York PHP User Group Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk

http://www.nyphp.org/show-participation

--e89a8fb1f806d6361004d80d2d3d
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"auto"><div style>+1 for the first, because -3 =
lines of code -1 variable, but really either are good.</div><div><br></div>=
<div>From my iPhone</div><div><br>On Mar 15, 2013, at 10:35 PM, Joseph Craw=
ford &lt;<a href=3D"mailto:codebowl at gmail.com" target=3D"_blank">codebowl at g=
mail.com</a>&gt; wrote:<br>

<br></div><blockquote type=3D"cite"><div><p>I personally would disagree wit=
h you on that one.=A0 I think the single return point is more confusing.=A0=
 Though that is more of a preference.</p>
<p>The single return path leads to extra processing that would not be requi=
red.</p>
<div class=3D"gmail_quote">On Mar 15, 2013 9:12 PM, &quot;Robert Stoll&quot=
; &lt;<a href=3D"mailto:rstoll at tutteli.ch" target=3D"_blank">rstoll at tutteli=
.ch</a>&gt; wrote:<br type=3D"attribution"><blockquote class=3D"gmail_quote=
" style=3D"margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color=
:rgb(204,204,204);border-left-style:solid;padding-left:1ex">



<div lang=3D"DE-CH" link=3D"blue" vlink=3D"purple"><div><p class=3D""><span=
 lang=3D"EN-GB" style=3D"font-size:11pt;font-family:Calibri,sans-serif;colo=
r:rgb(31,73,125)">I would say the first version is ok but only because your=
 function is small enough.<u></u><u></u></span></p>


<p class=3D""><span lang=3D"EN-GB" style=3D"font-size:11pt;font-family:Cali=
bri,sans-serif;color:rgb(31,73,125)">If your function gets bigger and bigge=
r, then it is better to have only one exit point=A0 in terms of readability=
.<u></u><u></u></span></p>


<p class=3D""><span lang=3D"EN-GB" style=3D"font-size:11pt;font-family:Cali=
bri,sans-serif;color:rgb(31,73,125)"><u></u>=A0<u></u></span></p><p class=
=3D""><b><span lang=3D"DE" style=3D"font-size:10pt;font-family:Tahoma,sans-=
serif">Von:</span></b><span lang=3D"DE" style=3D"font-size:10pt;font-family=
:Tahoma,sans-serif"> <a href=3D"mailto:talk-bounces at lists.nyphp.org" target=
=3D"_blank">talk-bounces at lists.nyphp.org</a> [mailto:<a href=3D"mailto:talk=
-bounces at lists.nyphp.org" target=3D"_blank">talk-bounces at lists.nyphp.org</a=
>] <b>Im Auftrag von </b>Joseph Crawford<br>


<b>Gesendet:</b> Freitag, 15. M=E4rz 2013 22:15<br><b>An:</b> NYPHP Talk<br=
><b>Betreff:</b> Re: [nycphp-talk] style question: returning from a function=
 while you&#39;re in a foreach<u></u><u></u></span></p><p class=3D"">
<u></u>=A0<u></u></p><p>The only reason would be poorly designed code where=
 the return value could change based on multiple separate conditionals.=A0 =
I have seen it in some legacy code.<u></u><u></u></p><p>On Mar 15, 2013 5:5=
0 PM, &quot;Anthony Ferrara&quot; &lt;<a href=3D"mailto:ircmaxell at gmail.com=
" target=3D"_blank">ircmaxell at gmail.com</a>&gt; wrote:<br>


&gt;<br>&gt; The first is fine. There&#39;s no reason not to do it...<br>&g=
t;<br>&gt;<br>&gt; On Fri, Mar 15, 2013 at 5:30 PM, Joseph Crawford &lt;<a =
href=3D"mailto:codebowl at gmail.com" target=3D"_blank">codebowl at gmail.com</a>=
&gt; wrote:<br>


&gt;&gt;<br>&gt;&gt; There are a few things to learn from here.<br>&gt;&gt;=
<br>&gt;&gt; First it is not only a styling thing. =A0In the first one you =
have several return<br>&gt;&gt; statements and in the second you only have =
1 return statement.<br>


&gt;&gt;<br>&gt;&gt; It will depend on the code logic but I tend to return =
when I have the value I need rather=A0<br>&gt;&gt; than assign it to a vari=
able and have it continue to process through the rest of the function.<br>


&gt;&gt;<br>&gt;&gt; If you only return after the entire function is comple=
te but your known case was the first check in<br>&gt;&gt; the function you =
are &quot;over&quot; processing and will only add time to your sites load t=
ime.<br>


&gt;&gt;<br>&gt;&gt; Thanks,<br>&gt;&gt; Joseph Crawford<br>&gt;&gt;<br>&gt=
;&gt; On Mar 15, 2013, at 5:08 PM, David Mintz wrote:<br>&gt;&gt;<br>&gt;&g=
t;&gt; function whatever(Array $array) {<br>&gt;&gt;&gt;<br>&gt;&gt;&gt; =
=A0 =A0 foreach ($array as $key =3D&gt; $value) {<br>


&gt;&gt;&gt; =A0 =A0 =A0 =A0 if =A0($something) =A0{<br>&gt;&gt;&gt; =A0 =
=A0 =A0 =A0 =A0 =A0 =A0return true;<br>&gt;&gt;&gt; =A0 =A0 =A0 =A0 }<br>&g=
t;&gt;&gt; =A0 =A0 }<br>&gt;&gt;&gt; =A0 =A0 return false;<br>&gt;&gt;&gt; =
}<br>&gt;&gt;&gt;<br>&gt;&gt;&gt; Is there any reason =A0-- style, legibili=
ty, whatever -- not to do the above? Or should you do something like<br>


&gt;&gt;&gt;<br>&gt;&gt;&gt; function whatever(Array $array) =A0{<br>&gt;&g=
t;&gt;<br>&gt;&gt;&gt; =A0 =A0$return =3D false;<br>&gt;&gt;&gt; =A0 =A0 fo=
reach ($array as $key =3D&gt; $value) {<br>&gt;&gt;&gt; =A0 =A0 =A0 =A0 if =
=A0($something) =A0{<br>


&gt;&gt;&gt; =A0 =A0 =A0 =A0 =A0 =A0 =A0$return =3D true;<br>&gt;&gt;&gt; =
=A0 =A0 =A0 =A0 =A0 =A0 =A0break;<br>&gt;&gt;&gt; =A0 =A0 =A0 =A0 }<br>&gt;=
&gt;&gt; =A0 =A0 }<br>&gt;&gt;&gt; =A0 =A0 return $return;<br>&gt;&gt;&gt; =
}<br>&gt;&gt;&gt;<br>&gt;&gt;&gt; Thanks.<br>


&gt;&gt;&gt;<br>&gt;&gt;&gt;<br>&gt;&gt;&gt; -- <br>&gt;&gt;&gt; David Mint=
z<br>&gt;&gt;&gt; <a href=3D"http://davidmintz.org/" target=3D"_blank">http=
://davidmintz.org/</a><br>&gt;&gt;&gt; Fight for social equality:<br>&gt;&g=
t;&gt; <a href=3D"http://socialequality.com/" target=3D"_blank">http://soci=
alequality.com/</a><br>


&gt;&gt;&gt;<br>&gt;&gt;&gt; ______________________________________________=
_<br>&gt;&gt;&gt; New York PHP User Group Community Talk Mailing List<br>&g=
t;&gt;&gt; <a href=3D"http://lists.nyphp.org/mailman/listinfo/talk" target=
=3D"_blank">http://lists.nyphp.org/mailman/listinfo/talk</a><br>


&gt;&gt;&gt;<br>&gt;&gt;&gt; <a href=3D"http://www.nyphp.org/show-participa=
tion" target=3D"_blank">http://www.nyphp.org/show-participation</a><br>&gt;=
&gt;<br>&gt;&gt;<br>&gt;&gt;<br>&gt;&gt; __________________________________=
_____________<br>


&gt;&gt; New York PHP User Group Community Talk Mailing List<br>&gt;&gt; <a=
 href=3D"http://lists.nyphp.org/mailman/listinfo/talk" target=3D"_blank">ht=
tp://lists.nyphp.org/mailman/listinfo/talk</a><br>&gt;&gt;<br>&gt;&gt; <a h=
ref=3D"http://www.nyphp.org/show-participation" target=3D"_blank">http://ww=
w.nyphp.org/show-participation</a><br>


&gt;<br>&gt;<br>&gt;<br>&gt; ______________________________________________=
_<br>&gt; New York PHP User Group Community Talk Mailing List<br>&gt; <a hr=
ef=3D"http://lists.nyphp.org/mailman/listinfo/talk" target=3D"_blank">http:=
//lists.nyphp.org/mailman/listinfo/talk</a><br>


&gt;<br>&gt; <a href=3D"http://www.nyphp.org/show-participation" target=3D"=
_blank">http://www.nyphp.org/show-participation</a><u></u><u></u></p></div>=
</div><br>_______________________________________________<br>
New York PHP User Group Community Talk Mailing List<br>
<a href=3D"http://lists.nyphp.org/mailman/listinfo/talk" target=3D"_blank">=
http://lists.nyphp.org/mailman/listinfo/talk</a><br>
<br>
<a href=3D"http://www.nyphp.org/show-participation" target=3D"_blank">http:=
//www.nyphp.org/show-participation</a><br></blockquote></div>
</div></blockquote><blockquote type=3D"cite"><div><span>___________________=
____________________________</span><br><span>New York PHP User Group Commun=
ity Talk Mailing List</span><br><span><a href=3D"http://lists.nyphp.org/mai=
lman/listinfo/talk" target=3D"_blank">http://lists.nyphp.org/mailman/listin=
fo/talk</a></span><br>

<span></span><br><span><a href=3D"http://www.nyphp.org/show-participation" =
target=3D"_blank">http://www.nyphp.org/show-participation</a></span></div><=
/blockquote></div></div>

--e89a8fb1f806d6361004d80d2d3d--


More information about the talk mailing list