The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Coding conventions : Listing 8.1

2 replies on 1 page. Most recent reply: Nov 4, 2008 9:29 PM by Vladimir Kelman

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Bhaskar Maddala

Posts: 5
Nickname: maddalab
Registered: Feb, 2006

Coding conventions : Listing 8.1 Posted: Nov 4, 2008 7:13 AM
Reply to this message Reply
Advertisement

import scala.io.Source

object LongLines {
def processFile(filename: String, width: Int) {
val source = Source.fromFile(filename)
for (line <source.getLines)
processLine(filename, width, line)
}

private def processLine(filename: String,
width: Int, line: String) {
if (line.length > width)
println(filename +": "+ line.trim)
}
}

object FindLongLines {
def main(args: Array[String]) {
val width = args(0).toInt
for (arg <args.drop(1))
LongLines.processFile(arg, width)
}
}

Is it a coding standard/best practice in the scala world to implement main in its own type? In this example, Why is main not implemented in LongLines


j herber

Posts: 6
Nickname: 53144
Registered: Jan, 2008

Re: Coding conventions : Listing 8.1 Posted: Nov 4, 2008 3:57 PM
Reply to this message Reply
I suspect the author intended object "LongLines" to be used as a namespace containing a collection of function(s). For the purposes of the chapter, it is showing off functional decomposition of "processLine" within "processFile".

FindLongLines is a program used to illustrate the aforementioned concepts.

Sticking the main inside of the "LongLines" would compile, but would also "clutter" the example.

The best coding practices/conventions for Scala are common sense at this juncture. Consider your audience, your intent, and where your code base is headed. Scala's fusion of OO and functional often provides the tools to produce more than one solution.

Patterns are often a sign that your language is not powerful enough to codify a set of problems without Frameworks or Snippets. That said, here are some useful techniques identified in Scala: http://scala.sygneca.com/patterns/start

Missing is the excellent discussion on Dependency Injection floating around. See this blog post for more info: http://jonasboner.com/2008/10/06/real-world-scala-dependency-injection-di

Java currently leaning on a quite a growing framework for DI, interesting to see Scala come up with a partial solution.

twitter.com/jherber

Vladimir Kelman

Posts: 46
Nickname: vkelman
Registered: Feb, 2008

Re: Coding conventions : Listing 8.1 Posted: Nov 4, 2008 9:29 PM
Reply to this message Reply
Thanks a lot for interesting links.

Flat View: This topic has 2 replies on 1 page
Topic: Examples for Programming in Scala Previous Topic   Next Topic Topic: Problems with Element examples

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use