Skip to content

Elegant Objects

Thomas Czogalik edited this page Jan 10, 2022 · 3 revisions

#Elegant Objects

Birth

never use -er name

  • do not name class after what class objects are doing.
  • name class after what it is
  • class is a Pixel, that can change color, not PixelColorChanger don't
class CashFormatter {
    private int dollars;
    CashFormatter(int dollar) {
        ...
    }
    public String format() {
        ...
    }
}

do

class Cash {
    private int dollars;
    Cash(int dollar) {
        ...
    }
    public String usDollar() {
        ...
    }
}

make on constructor primary

  • have many constructors and less methods
new Cash(30)
new Cash('30')
new Cash(30d)
new Cash(30f)
new Cash(30, 'USD')

keep contructor code free

  • init of object must be code-free -> instead wrap them
class Cash {
    private int dollars;
    Cash(String dollar) {
        this.dollars = Integer.parseInt(dollar)
    }
}
class Cash {
    private Number dollars;
    Cash(String dollar) {
        this.dollars = new StringAsInteger(dollar)
    }
}

class StringAsInteger implements Number{
    private String source;
    StringAsInteger(String source) {
        this.source = source
    }
    int intValue() {
        return Integer.parseInt(this.source)
    }
}
  • conversion is delayed until object initialization
  • first create object -> second allow it to work for us. Do not mix
App app = new App(new Data(), new Screen())
app.run()

Education

small objects are readable and maintainable

Encapsulate

  • as litte as possible
  • no more than 4 objects
  • in many languages state and identity are seperated
  • no encapsulation -> similar to static method

Interface

  • always use interfaces
  • no public method without interface, because user couples thightly with object

Method names

  • builder methods build something and return it (noun)
  • manipulator methods modify entity and return void (verb)
  • never mix

Do not use public constants

  • no constant classes
  • because class does not know what it is (no state, etc.)
class Constant {
    public static String Crlf = "\r\n"
}
  • use micro classes
class CrlfString {
    private String origin;
    CrlfString(String src) {
        this.origin = src
    }
    @Override
    String toString() {
        return String.format("%s\r\n", origin)
    }
}

Use Imutable

  • help readablity
  • no identity mutability:
    • compare 2 objects -> change one of them -> you think they are still equal but are not
  • failure atomicity: Objects are always complete
  • side effect free
Clone this wiki locally