This page is no longer maintained — Please continue to the home page at www.scala-lang.org

"Good thing"?

7 replies
bmaso
Joined: 2009-10-04,
User offline. Last seen 2 years 40 weeks ago.
Not sure if its a good thing or a bad thing, but the better I get at Scala, the more I keep writing methods that reduce down to a single line of code. Seems like overkill to create a whole method for 1 line, but then again the name of the method acts like a comment -- tells exactly what I am trying to implement.

Best regards,
Brian Maso
(949) 395-8551
brian@blumenfeld-maso.com
twitter: @bmaso
skype: brian.maso
LinkedIn: http://www.linkedin.com/in/brianmaso
Dave Griffith
Joined: 2009-01-14,
User offline. Last seen 42 years 45 weeks ago.
Re: "Good thing"?

I find this as well, and feel that it is certainly a "good thing". The
reason my methods are shorter in Scala is fundamentally because they can
utilize much more powerful and usable libraries than my methods in other
languages. More powerful libraries mean fewer intermediate steps, fewer
intermediate values, and fewer places to screw up. This in turn is works
because libraries in Scala can be so much more powerful and usable than
libraries in the other languages I work in.

(My code is also much shorter due to lessened amounts of framework
boilerplate, but that's mostly a matter of reducing the number of methods,
not making the individual methods shorter).

--Dave

Brian Maso wrote:
>
> Not sure if its a good thing or a bad thing, but the better I get at
> Scala,
> the more I keep writing methods that reduce down to a single line of code.
> Seems like overkill to create a whole method for 1 line, but then again
> the
> name of the method acts like a comment -- tells exactly what I am trying
> to
> implement.
>
> Best regards,
> Brian Maso
> (949) 395-8551
> brian@blumenfeld-maso.com
> twitter: @bmaso
> skype: brian.maso
> LinkedIn: http://www.linkedin.com/in/brianmaso
>
>

David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: "Good thing"?


On Wed, Apr 14, 2010 at 1:03 PM, Dave Griffith <dave.l.griffith@gmail.com> wrote:


I find this as well, and feel that it is certainly a "good thing".   The
reason my methods are shorter in Scala is fundamentally because they can
utilize much more powerful and usable libraries than my methods in other
languages.   More powerful libraries mean fewer intermediate steps, fewer
intermediate values, and fewer places to screw up.  This in turn is works
because  libraries in Scala can be so much more powerful and usable than
libraries in the other languages I work in.

(My code is also much shorter due to lessened amounts of framework
boilerplate, but that's mostly a matter of reducing the number of methods,
not making the individual methods shorter).

FWIW,

I think 1-line methods are very good things.  They make code very readable.

Lift went through a phone where the code size dropped dramatically (LoC dropped by 40%) while Lift became more featureful.  This happened when the Lift committers (primarily me at the time) shifted from a Ruby/Java style of coding to a Scala/FP style of coding.


 

--Dave



Brian Maso wrote:
>
> Not sure if its a good thing or a bad thing, but the better I get at
> Scala,
> the more I keep writing methods that reduce down to a single line of code.
> Seems like overkill to create a whole method for 1 line, but then again
> the
> name of the method acts like a comment -- tells exactly what I am trying
> to
> implement.
>
> Best regards,
> Brian Maso
> (949) 395-8551
> brian@blumenfeld-maso.com
> twitter: @bmaso
> skype: brian.maso
> LinkedIn: http://www.linkedin.com/in/brianmaso
>
>

--
View this message in context: http://old.nabble.com/%22Good-thing%22--tp28247007p28247522.html
Sent from the Scala - User mailing list archive at Nabble.com.




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics
Dave Griffith
Joined: 2009-01-14,
User offline. Last seen 42 years 45 weeks ago.
Re: "Good thing"?

Forgot to include this link, explaining a similar phenomenon with using
powerful Python libraries:

http://www.vetta.org/2008/05/scipy-the-embarrassing-way-to-code/

--Dave

Detering Dirk
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
RE: "Good thing"?

> Not sure if its a good thing or a bad thing, but the better I get at
> Scala, the more I keep writing methods that reduce down to a single
> line of code. Seems like overkill to create a whole method for 1 line,
> but then again the name of the method acts like a comment -- tells
> exactly what I am trying to implement.

Well, that is exactly how Scala changed the way I write
Java code now (I remember a thread with that title).
I much more tend to extract multi-line expressions,
like loops or conditions into methods. Before that the expressions
had a commenting line above to describe what the next lines do,
now that became the method name.

As the result is not implicit part of the expression body any more
but explicit return value to further work with, the original multi-line
method reduces down to a method-call chain which may end even in a one-liner.

This absolutely leads to better maintainable code, and better reuse, and so
is a "good thing".
The only thing I sorely miss in Java now is closures
(http://openjdk.java.net/projects/lambda/), which would lead to a much, much
better reuse and code structure.

KR
Det

Razvan Cojocaru 2
Joined: 2009-11-20,
User offline. Last seen 42 years 45 weeks ago.
RE: "Good thing"?

Yes - same here...I constantly find myself doing two things:
1. pause for a brief moment before writing any collection-related code
(include Option here for if/else replacement) to concentrate what i need
into a chain of functions
2. wish more and more that I had a widescreen.

Here's my latest contraption:

def build : List[WfAct] = {
a.foldRight (Nil:List[WfAct])((x,l) =>
WfProxy(x,l.headOption.map(WL(x,_)).toList:_*) :: l)
}

note the curlies - remains from the first draft of the method which was a 2
liner

Do you really expect me to remember this 6 months from now? it obviously
needs a better name!

-----
Razvan Cojocaru,
Work: http://www.sigma-systems.com
Playground: http://wiki.homecloud.ca
Latest cool toy: http://scripster.codewitter.com
Follow me: http://feeds.razie.com/RazvanTech RSS Feed ,
http://twitter.com/razie Twitter , http://github.com/razie GitHub .

Razvan Cojocaru 2
Joined: 2009-11-20,
User offline. Last seen 42 years 45 weeks ago.
RE: "Good thing"?

You know what - If I remember the "foldRight....headOption.map" pattern, I
think I will get this 6 months from now...

Besides, I love answering my own posts...

Razvan Cojocaru wrote:
>
>
> Do you really expect me to remember this 6 months from now? it obviously
> needs a better name!
>
>

-----
Razvan Cojocaru,
Work: http://www.sigma-systems.com
Playground: http://wiki.homecloud.ca
Latest cool toy: http://scripster.codewitter.com
Follow me: http://feeds.razie.com/RazvanTech RSS Feed ,
http://twitter.com/razie Twitter , http://github.com/razie GitHub .

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: "Good thing"?
Well, Thinking Forth was describing that as the way to program in Forth -- and as being a good thing -- some two or three decades ago.
At any rate, I find that this is a natural result of programming in a functional style. When you cease to need a "context" for the mutable stuff,  you lose incentive to write long procedures.

On Wed, Apr 14, 2010 at 4:18 PM, Brian Maso <brian@blumenfeld-maso.com> wrote:
Not sure if its a good thing or a bad thing, but the better I get at Scala, the more I keep writing methods that reduce down to a single line of code. Seems like overkill to create a whole method for 1 line, but then again the name of the method acts like a comment -- tells exactly what I am trying to implement.

Best regards,
Brian Maso
(949) 395-8551
brian@blumenfeld-maso.com
twitter: @bmaso
skype: brian.maso
LinkedIn: http://www.linkedin.com/in/brianmaso



--
Daniel C. Sobral

I travel to the future all the time.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland