Structured programming vs. OO programming vs. Functional programming
Structured programming is discipline imposed upon direct transfer of control.
Testability: software is like science: Science does not work by proving statements true, but rather by proving statements false. Structured programming forces us to recursively decompose a program into a set of small provable functions.
OO programming is discipline imposed upon indirect transfer of control.
Capsulation, inheritance, polymorphism(pointers to functions) are not unique to OO.
But OO makes polymorphism safe and convenient to use. And then enable the powerful ==plugin architecture== with dependency inversion
Source code denpendencies and flow of control are typically the same. However, if we make them both depend on interfaces, dependency is inverted.
Interfaces empower independent deployability. e.g. when deploying Solidity smart contracts, importing and using interfaces consume much less gases than doing so for the entire implementation.
Functional programming: Immutability. is discipline imposed upon variable assignment.
Why important? All race conditions, deadlock conditions, and concurrent update problems are due to mutable variables.
==Event sourcing== is a strategy wherein we store the transactions, but not the state. When state is required, we simply apply all the transactions from the beginning of time.
xxxxxxxxxx
public class Customer implements Discountable {
protected int customerId;
protected String customerName;
protected String customerCode;
public Customer() {
this.customerId = 0;
this.customerName = "";
this.customerCode = "";
}
public Customer(int customerId, String customerName, String customerCode) {
this.customerId = customerId;
this.customerName = customerName;
this.customerCode = customerCode;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerCode() {
return customerCode;
}
public void setCustomerCode(String customerCode) {
this.customerCode = customerCode;
}
public double customerType(String customerCode) {
double discount = 0;
if (customerCode.toLowerCase().equals("pre")) {
discount = 0.10;
} else if (customerCode.toLowerCase().equals("gen")) {
discount = 0.02;
} else if (customerCode.toLowerCase().equals("new")) {
discount = 0.05;
}
return discount;
}
@Override
public void grandTotal(String orderNumber, double total) {
double discount = customerType(customerCode);
double discountPercentage = total * discount;
double finalTotal = total - discountPercentage;
System.out.println("For " + getCustomerName()
+ " order Number " + orderNumber + " the grand total is: $" + finalTotal);
}
}