Christian Riesen
Fullstack Web Developer at Liip. Creator of rokka.io. Focus on API's, image handling and large scale applications.
I will, in fact, claim that the difference between a bad programmer and a good one is whether he considers his code or his data structures more important. Bad programmers worry about the code. Good programmers worry about data structures and their relationships.
Linus Torvalds
Creator of the Linux Kernel
Show me your code and conceal your data structures, and I shall continue to be mystified. Show me your data structures, and I won't usually need your code; it'll be obvious.
class Customer
{
protected $id;
public function getId()
{
return $this->id;
}
}
class Item
{
protected $id;
protected $price;
public function getId()
{
return $this->id;
}
public function setPrice($price)
{
$this->price = $price;
return $this;
}
public function getPrice()
{
return $this->price;
}
}
class Order
{
protected $id;
protected $customer;
protected $created;
public function getId()
{
return $this->id;
}
public function setCustomer(Customer $customer)
{
$this->customer = $customer;
return $this;
}
public function getCustomer()
{
return $this->customer;
}
public function setCreated(\DateTime $created)
{
$this->created = $created;
return $this;
}
public function getCreated()
{
return $this->created;
}
}
class Orderitem
{
protected $id;
protected $item;
protected $price;
protected $order;
public function getId() {
return $this->id;
}
public function setItem(Item $item) {
$this->item = $item;
return $this;
}
public function getItem() {
return $this->item;
}
public function setPrice($price) {
$this->price = $price;
return $this;
}
public function getPrice() {
return $this->price;
}
public function setOrder(Order $order) {
$this->order = $order;
return $this;
}
public function getOrder() {
return $this->order;
}
}
$customer = new Customer();
$item = new Item();
$order = new Order();
$order
->setCustomer($customer)
->setCreated(new \DateTime());
$orderitem = new Orderitem();
$orderitem
->setItem($item)
->setPrice($item->getPrice())
->setOrder($order);
class Order
{
protected $id;
protected $customer;
protected $created;
public function __construct(Customer $customer)
{
$this->customer = $customer;
$this->created = new \DateTime();
}
public function getId()
{
return $this->id;
}
public function getCustomer()
{
return $this->customer;
}
public function getCreated()
{
return $this->created;
}
}
class Orderitem
{
protected $id;
protected $item;
protected $price;
protected $order;
public function __construct(Item $item, Order $order)
{
$this->item = $item;
$this->price = $item->getPrice();
$this->order = $order;
}
public function getId()
{
return $this->id;
}
public function getItem()
{
return $this->item;
}
public function getPrice()
{
return $this->price;
}
public function getOrder()
{
return $this->order;
}
}
$customer = new Customer();
$item = new Item();
$order = new Order($customer);
$orderitem = new Orderitem($item, $order);
$customer = new Customer();
$item = new Item();
$order = new Order();
$order
->setCustomer($customer)
->setCreated(new \DateTime());
$orderitem = new Orderitem();
$orderitem
->setItem($item)
->setPrice($item->getPrice())
->setOrder($order);
Smart data structures and dumb code works a lot better than the other way around.
Eric Raymond
By Christian Riesen
How to be a good programmer and make the smart decisions when it comes to data structures.
Fullstack Web Developer at Liip. Creator of rokka.io. Focus on API's, image handling and large scale applications.