Data Exposure and Encapsulation

TL;DR – Scala lets you expose internal data, then later change underpinnings while maintaining the same interface. The functional Way is an open Way. Internal data is available to anyone who wishes to extract it. “But what about encapsulation??” – horrified OO programmer“We do not fear your code, for you cannot screw up our immutable … Read moreData Exposure and Encapsulation

Def and Val in Scala constructors

TL;DR – use def all the time, and things just work. Remember that old adage from Java, “Never call a nonfinal method in your constructor”?Yeah, that still applies in Scala, and it keeps biting me. It’s easier to get wrong in Scala. Here’s a refresher:When a class is instantiated, the superclass is initialized first. Its … Read moreDef and Val in Scala constructors

What’s dirtier than comments? Exceptions!

I postulate that comments are a code smell. Craig Buchek suggests a possible counterexample: # Let this raise its exception if the fields don’t exist as expected.user = c.get_setting(‘username’) How else could one express the intention here, he asks? I say, the output of a function should be expressed in its return value. Exceptions are … Read moreWhat’s dirtier than comments? Exceptions!

Property-based testing: what is it?

Writing tests first forces you to think about the problem you’re solving. Writing property-based tests forces you to think way harder.— Jessica Kerr (@jessitron) April 25, 2013 What is this property-based thing? Property-based tests make statements about the output of your code based on the input, and these statements are verified for many different possible … Read moreProperty-based testing: what is it?

Scala: when is a val not a val?

TL;DR: before it’s initialized. Do vals in Scala always contain the same value?Not quite. I hit this the other day when I used a val inside a def inside a val. In the superclass: abstract class GeneralTestCase {   def createArray = Array(1,2,3,4)   val defaultInput = createArray // defaultInput = [1,2,3,4] } and then in the subclass, customized behavior to make … Read moreScala: when is a val not a val?

From imperative to data flow to functional style

Functional style makes code more maintainable. How? One way makes processing into a flow of data with discrete steps. Another way separates flow from context, making the context swappable. This post illustrates both. We’ll go from an imperative style to a more and more functional style. Scala is a good language for this illustration, since … Read moreFrom imperative to data flow to functional style