Login  Register

Re: an elegant way to return a result

Posted by stepharo on Jul 02, 2015; 9:01pm
URL: https://forum.world.st/an-elegant-way-to-return-a-result-tp4834837p4835464.html

Hi matthieu

on my side:
   
    stream next ifNil: [^stream next].

When you execute a block (far away from the place it was created), it acts as an exception, it stops the execution of currently executed methods and
return to its home context (method that led to the creation of the block) Read deep into Pharo because this is a great chapter.
So people should avoid to put return statement into a block since a block returns already the value of its last expression.

So for me
    arg: [ ^ ... ]

is a big warning.

Stef



Le 1/7/15 23:55, Matthieu Lacaton a écrit :

This whole conversation really makes me want to ask: How do you know if some code is bad, good or completely ugly ? I've not been programming for long but I've already heard sentences like this: "Damn this piece of code is so bad !" so often that I am really wondering. What are the rules ? Performant code makes total sense but it seems there is something else.

Le 1 juil. 2015 23:36, "stepharo" <[hidden email]> a écrit :
I know :D

Le 1/7/15 15:19, [hidden email] a écrit :
Stef,

you're shooting the wrong man! ;-) The crime was committed in your team ;-))))

See you in Brescia

Joachim

Am 30.06.15 um 19:19 schrieb stepharo:


Le 30/6/15 14:39, [hidden email] a écrit :
Ah, Okay, you are mentioning the one and only one case in which this could make sense.... ;-)

Something like

stream next ifNil: [^stream next].

so ugly.
Please we should not teach how to do exception with blocks :)

So there may be cases where the two sends do not return the same result. But that is, of course, not what the OP suggested. Nevertheless, I was too fast in criticizing ;-)

I would consider code like that "less easy" to maintain, however.

Nice thinking Kata ;-)

Joachim



Am 30.06.15 um 14:22 schrieb Esteban Lorenzano:
ah yeah :)

I assumed

first "something aMsg" ~= second "something aMsg”

In case is the same, the correct way is as you say:

^ something aMsg ifNil: [ anotherResult ]

(sorry, he :P)

On 30 Jun 2015, at 13:43, [hidden email] wrote:

Am 30.06.15 um 13:40 schrieb Peter Uhnák:
Make sense if there's code in between.
not sure what you're saying. I mean, sending the message twice makes sense if you already know the outcome? Hmmm...

something aMsg ifNil:[ ^ something aMsg ]. "yay for guards!"
"much more code"
^ aNotherResult

Peter

--
-----------------------------------------------------------------------
Objektfabrik Joachim Tuchel mailto:[hidden email]
Fliederweg 1 http://www.objektfabrik.de
D-71640 Ludwigsburg http://joachimtuchel.wordpress.com
Telefon: <a moz-do-not-send="true" href="tel:%2B49%207141%2056%2010%2086%200" value="+4971415610860" target="_blank">+49 7141 56 10 86 0         Fax: <a moz-do-not-send="true" href="tel:%2B49%207141%2056%2010%2086%201" value="+4971415610861" target="_blank">+49 7141 56 10 86 1