You don't need a Builder in Scala
EronAlves1996

EronAlves1996 @eronalves1996

About: Java/Angular developer and Computer Science enthusiast

Joined:
Jul 27, 2021

You don't need a Builder in Scala

Publish Date: Apr 14
0 0

In java, due to the language inflexibility, we need a way to pass properties under a name when some object gains some complexibility.

Consider this example:

public class Computer {
    public String cpu;
    public String ram;
    public String storage;
    public String gpu;
    public String soundCard;
    public String networkCard;
    public boolean liquidCooling;
    public int numberOfFans;
    public String operatingSystem;
    public boolean overclocked;
    public String powerSupply;
    public List<String> peripherals;
    public String primaryDisplay;
    public String secondaryDisplay;
    public boolean biometricLogin;
    public boolean tpmChip;
}
Enter fullscreen mode Exit fullscreen mode

This class now is raterly complex, and, by using only the constructor, it's quite difficult to instatiate the class, because method parameters in Java are positional.

Consider a possible constructor for this class:

public Computer(String cpu, String ram, String storage, String gpu, String soundCard, 
               String networkCard, boolean liquidCooling, int numberOfFans, 
               String operatingSystem, boolean overclocked, String powerSupply,
               List<String> peripherals, String primaryDisplay, String secondaryDisplay,
               boolean biometricLogin, boolean tpmChip) {
    this.cpu = cpu;
    this.ram = ram;
    this.storage = storage;
    this.gpu = gpu;
    this.soundCard = soundCard;
    this.networkCard = networkCard;
    this.liquidCooling = liquidCooling;
    this.numberOfFans = numberOfFans;
    this.operatingSystem = operatingSystem;
    this.overclocked = overclocked;
    this.powerSupply = powerSupply;
    this.peripherals = peripherals;
    this.primaryDisplay = primaryDisplay;
    this.secondaryDisplay = secondaryDisplay;
    this.biometricLogin = biometricLogin;
    this.tpmChip = tpmChip;
}
Enter fullscreen mode Exit fullscreen mode

Looks so complex.
By the way, to solve the problem, we should use some sort of composition, by separating some fields into new classes to simplify the constructor, at the cost of indirection, or use the Builder pattern, by crafting your own builder or by using Lombok @Builder.

Java would be better if it haves two things:

  1. Named parameters
  2. Default parameters

For our luck, Scala have this two features.

We can rewrite the class in Scala, and define some default parameters, taking some advantage with a cleaner and less verbose syntax:

class Computer(
    cpu: String,
    ram: String,
    storage: String,
    gpu: String = "Integrated",
    soundCard: String = "None",
    networkCard: String = "Ethernet 10/100",
    liquidCooling: Boolean = false,
    numberOfFans: Integer = 1,
    operatingSystem: String = "Windows 10",
    overclocked: Boolean = false,
    powerSupply: String = "500W",
    peripherals: List[String] = List.empty,
    primaryDisplay: String = "1080p 60Hz",
    secondaryDisplay: Option[String] = None,
    biometricLogin: Boolean = false,
    tpmChip: Boolean = false
)
Enter fullscreen mode Exit fullscreen mode

By the way, we can use this class with positional parameters or with named parameters:

val gamingPc = Computer(
    cpu = "Intel i9-12900K",
    ram = "32GB DDR5",
    storage = "1TB NVMe SSD",
    gpu = "NVIDIA RTX 4090",
    liquidCooling = true,
    overclocked = true,
    operatingSystem = "Windows 11 Pro",
    primaryDisplay = "4K 144Hz",
    secondaryDisplay = Some("1440p 165Hz"),
    tpmChip = true,
    peripherals = List("Mechanical Keyboard", "Gaming Mouse", "RGB Headse")
  )
Enter fullscreen mode Exit fullscreen mode

And this is more nice and easy to instatiate.

Comments 0 total

    Add comment