Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How To Write Unmaintainable Code - PHPDD 2024

How To Write Unmaintainable Code - PHPDD 2024

How do I write code that is as "good" as possible? What does that even mean? I have been working on large open source frameworks (initially TYPO3, now Neos and Flow) for around 20 years. In that time, I have produced a lot of code that I would never write in the same way today - and from which we can learn a lot together. I will critically discuss principles such as extensibility and DRY, show what mistakes I have made when developing frameworks and show which principles we can use to gradually improve our code. The whole thing will be a joint journey into large codebases. I'm looking forward to it!

Sebastian Kurfürst

September 20, 2024
Tweet

More Decks by Sebastian Kurfürst

Other Decks in Technology

Transcript

  1. #me

  2. Bene fi ts we prevented lots of security issues (SQL

    Injection, CSRF, ...) Drawbacks "Magic" - di ff i cult to follow extensible, but not extensible
  3. Less Lines of Code explicit Code Self-Documenting Code Documented Code

    Work with Objects Immutable & Functional Patterns
  4. final readonly class Node { private function __construct( public ContentSubgraphIdentity

    $subgraphIdentity, public NodeAggregateId $nodeAggregateId, public OriginDimensionSpacePoint $originDimensionSpacePoint, public NodeAggregateClassification $classification, public NodeTypeName $nodeTypeName, public ?NodeType $nodeType, public PropertyCollection $properties, public ?NodeName $nodeName, public Timestamps $timestamps, ) { } public static function create(ContentSubgraphIdentity $subgraphIdentity, NodeAggregateId $nodeAggregateId, OriginDimensionSpacePoint $originDimensionSpacePoint, NodeAggregateClassification $classification, NodeTypeName $nodeTypeName, ?NodeType $nodeType, PropertyCollection $properties, ?NodeName $nodeName, Timestamps $timestamps): self { return new self($subgraphIdentity, $nodeAggregateId, $originDimensionSpacePoint, $classification, $nodeTypeName, $nodeType, $properties, $nodeName, $timestamps); } public function getProperty(string $propertyName): mixed { return $this->properties->offsetGet($propertyName); } public function hasProperty(string $propertyName): bool { return $this->properties->offsetExists($propertyName); } public function equals(Node $other): bool { return $this->subgraphIdentity->equals($other->subgraphIdentity) && $this->nodeAggregateId->equals($other->nodeAggregateId); } }
  5. calculateLabel(Node) Node Pure Functions no side e ff ects deterministic

    (same input => same output) result a() b() c() d()
  6. Complexity = all parts making it di ffi cult to

    understand software or to change it. Root Causes of Complexity = Dependencies + Obscurity
  7. Dokumentation "Why" DESIGN COMMENTS ... The design comment basically states

    how and why a given piece of code uses certain algorithms, techniques, tricks, and implementation. It is an higher level overview of what you'll see implemented in the code. With such background, reading the code will be simpler. Moreover I tend to trust more code where I can find design notes. At least I know that some kind of explicit design phase happened, at some point, during the development process.
  8. Summary How to write unmaintainable Code What is Good Code?

    Property Mapper example: Goals change over time Elements Of Maintainable Code Intrinsic Similarity Immutability & Pure Functions Libraries over Frameworks (e2e) Tests with little mocks Reduce Complexity by building deep modules Document WHY Write Concepts Review and Discuss