Learning Scala Programming
上QQ阅读APP看书,第一时间看更新

Shaping our Scala Program

"I conclude that there are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult."
- C. A. R. Hoare

A program is a solution specific to a problem. The solution we write is broken down to different tasks. Tasks can be specific to a step or a part of the solution. While coding for such tasks, we use constructs as tools to make them easier, readable, and performant. These tools make it possible to shape up your program at hand.

To code for a task that is repetitive requires either a construct that can repeat a given logic for you, or write the same snippet of code by hand again and again. I bet the first one is far better and that's why we have a few constructs, we call them looping constructs. Scala supports for, while, and do while loops. We'll see how these loops work in Scala. From there, we'll have a quick look at for expressions. We'll also go through the FP (functional programming) way of doing loops through recursion. Then, we'll start taking a look at Scala's conditional statements if and end, with learning how we can shape up program flow using pattern matching. Here's a quick card for what's in there for us in this chapter:

  • Looping
    • for, while, and do while loops
  • The for expressions: a quick go-through
  • Recursion
  • Conditional statements
    • if, and if else
  • Pattern matching