The post Value Objects with PHP 8.1 appeared first on Entropy Wins.
]]>Back in the dark ages of 2016, shortly after PHP 7.0 was released, I wrote a Missing In PHP 7 blog post series. Four of the five posts describe functionality that is no longer missing, as of PHP 8.1 or earlier. Missing in PHP7: Value objects is one of those posts.
You can find a quick recap on what a Value Object is, and why you would want to use them, in Missing in PHP7: Value objects.
class ContactRequest { public function __construct( public readonly string $firstName = 'nyan', public readonly string $lastName = 'cat', public readonly string $emailAddress = 'something', ) { } } $request = new ContactRequest(firstName: 'foo', lastName: 'bar', emailAddress: 'baz'); $firstName = $request->firstName; $request->lastName = 'hax'; // Fails
Other new PHP features that I am excited about:
The post Value Objects with PHP 8.1 appeared first on Entropy Wins.
]]>The post New MediaWiki blog appeared first on Entropy Wins.
]]>Half a year ago I launched Professional Wiki together with Karsten Hoffmeyer. Professional Wiki is, as the name suggests, a company providing professional wiki services. We help companies create and manage wikis, we provide training and support and we offer fully managed wiki hosting.
Today we published our new blog featuring a first post on installing MediaWiki extensions with Composer. This blog will contain both wiki news and longer lived articles, such as the one about Composer.
In recent years I was hesitant to post MediaWiki specific content on this blog (EntropyWins) because it had evolved a focus on software design. The new Professional Wiki blog solves this problem, so you can expect more MediaWiki related posts from me again.
The post New MediaWiki blog appeared first on Entropy Wins.
]]>The post Applications as Frameworks appeared first on Entropy Wins.
]]>In your typical web application, the code handles a request and returns a response. Let’s assume we are using a web framework to handle common tasks such as routing. Let’s also assume that we think framework binding has a high cost, and are thus decoupling our application from the framework. The flow of control would look like this:
Execution starts with the framework. For PHP frameworks this will be in a file like public/index.php
. The framework then bootstraps itself and does a bunch of stuff. It’s safe to assume this stuff will include routing, and often it also includes things like dependency construction and error handling.
After the framework did the tasks you want it to do, it hands control over to your application. Your application does a bunch of application and domain logic and interacts with persistence. It likely uses a number of libraries, especially for infrastructure tasks like logging and database access. Even so, control stays with the application. (The key difference between frameworks and libraries is that you control/call libraries while frameworks control/call you.) Your application might also be calling the framework and use it as a library. Again, control stays with the application.
Finally when the application is done, it hands some kinda of result back to the framework. The framework then does another bunch of stuff, like template rendering and translations. In case of a web framework it then spits out a HTTP response and execution ends.
An application like this keeps you in control of what happens, making it easier to change things. This style also makes it easy to decouple from the framework. There are only two points where you need to decouple.
My post Implementing The Clean Architecture outlines one architectural approach that leads to this kind of application.
Let’s compare how frameworks and applications differ when they are used as a foundation for an/another application.
Frameworks don’t do stuff on their own. There is no application or domain logic. There is no set of existing web pages or API endpoints with their own structure and behavior. This is all defined by your application when using a framework. When building on top of an application that acts as a framework, you’ll need to deal with existing structure and behavior. You’ll need to insert your own stuff, change existing behavior in certain situations and prevent default behavior altogether in others.
I know that there are “frameworks” that do provide their own stuff out of the box. (Example: web shop framework.) While they might not be a full application on their own, for the purpose of this blog post they are the same as an application that gets used as a framework.
There is nothing inherently bad about building things on top of an application. Plugins and extensions are a very useful pattern. A plugin that interacts with a single plugin point can decouple itself when appropriate, and it is in control over itself. For smaller plugins that use many plugins points, framework decoupling might not be feasible or worth the effort.
This post is about using applications as framework foundation for sizable sets of code which are applications in their own right.
Let’s imagine we have an application that is used on some site for some use case. We’ll call this application FrameworkApp, since we’ll use it as framework for another application that powers another site.
When building our application on top of FrameworkApp, we’ll need to register new behavior and modify existing behavior. To make this possible, FrameworkApp needs to provide the appropriate extension points. Often these take the form of abstract classes or even systems, though the exact nature of the extension points is not important for our purposes.
This leads to a very different flow of control. Rather than calling us once, the FrameworkApp calls each extension point our application handles.
The diagram is showing just 6 extension points, though there can be 100s.
When visualized like this, it becomes easy to see how decoupling from the framework becomes almost impossible. Even if you manage to avoid coupling to framework code in your application, its whole structure is still defined by the framework. This means you are very limited in what you can do in your application and need to understand the framework to effectively develop the application. Framework coupling causes more issues than that, though a comprehensive overview of those is out of scope for this post.
Favor composition over inheritance
— OOP principle
Using an application as a framework is very similar to using inheritance for code reuse.
Just like with the application that is build on top of the app that acts as framework, the subclass might not be in control and be invoked many times from the base class. This is especially the case when using the Template Method Pattern and when having a deep inheritance hierarchy. The flow of control can bounce all over the place and decoupling the subclass from the classes up the hierarchy becomes all but impossible.
You can avoid this classical inheritance mess by using composition. Which suggests one way to move away from using an application as a framework or avoid doing so altogether: stop treating the framework as a base class. If there is code to share, use composition. This way you stay in control, can decouple easier and avoid The Fallacy of DRY.
Just like with class hierarchies you can always slap on an extra level.
The post Applications as Frameworks appeared first on Entropy Wins.
]]>The post Readable Functions: Guard Clause appeared first on Entropy Wins.
]]>function doThing() { var $thing = 'default'; if (someCondition()) { $thing = 'special case'; } return $thing; }
Using a Guard Clause we can simplify it to:
function doThing() { if (someCondition()) { return 'special case'; } return 'default'; }
The if statement in this simplified code is a Guard Clause. You can have multiple Guard Clauses in a function.
The simplification removes ALL the state in the function, including the nasty and completely not needed mutation in the first form of the code. You can read the new code sequentially, and the code after the Guard Clause is not polluted by extra complexity arising from the special case.
This post is part of the Readable Functions series which contains tricks like this one and two general principles: Minimize State and Do One Thing.
The post Readable Functions: Guard Clause appeared first on Entropy Wins.
]]>The post Readable Functions: Do One Thing appeared first on Entropy Wins.
]]>Often functions become less readable because they are doing multiple things. If your function Foo needs to do tasks A, B and C, then create a function for each of the tasks and call those from Foo.
Having small functions is OK. Having just 3 calls to other functions in your function is OK. Don’t be afraid of “too simple” code or of this style making things harder to follow (it does not (unless you are splitting things up stupidly)). And don’t be afraid of performance. In most programs the amount of function calls have effectively 0 impact on performance. There are exceptions, though unless you know you are dealing with one of these, don’t mash things together for performance reasons.
Doing one thing includes dealing with a single level of abstraction. For instance, suppose you have a function in which in some cases a message needs to be logged to the file system.
function doThing() { if (condition) { low_level_write_api_call('write-mode', $this->somePath, 'Some error message'); } }
Here the details of how the error message is logged are on a lower level of abstraction than the rest of the function. This makes it harder to tell what the function is actually doing, because details that don’t matter on the higher level of abstraction clutter the high level logic. These details should be in their own function.
function doThing() { if (condition) { $this->log('Some error message'); } } private function log(string $message) { low_level_write_api_call('write-mode', $this->somePath, $message); }
The post Readable Functions: Do One Thing appeared first on Entropy Wins.
]]>The post PHP Typed Properties appeared first on Entropy Wins.
]]>As of version 7.3, PHP supports types for function parameters and for function return values. Over the latest years many additions to PHP types where made, such as primitive (scalar) types like string
and int
(PHP 7.0), return types (PHP 7.0), nullable types (PHP 7.1) and parameter type widening (PHP 7.2). The introduction of typed properties (PHP 7.4) is thus a natural progression.
Typed properties work as follows:
class User { public int $id; public string $name; public function __construct(int $id, string $name) { $this->id = $id; $this->name = $name; } }
You can do in two simple lines what takes a lot more boilerplate in PHP 7.3 or earlier. In these versions, if you want to have type safety, you need a getter and setter for each property.
private $id; private $name; public function getId(): int { return $this->id; } public function setId(int $id): void { $this->id = $id; } public function getName(): string { return $this->name; } public function setName(string $name): void { $this->id = $name; }
Not only is it a lot more work to write all of these getters and setters, it is also easy to make mistakes when not automatically generating the code with some tool.
These advantages are what the hype is all about. People are saying it will save us from writing so much code. I think not, and I am afraid of the type of code those people will write using typed properties.
Let’s look at some of different types of classes we have in a typical well designed OO codebase.
Services are classes that allow doing something. Loggers are services, Repositories are services and LolcatPrinters are services. Services often need collaborators, which get injected via their constructor and stored in private fields. These collaborators are not visible from the outside. While services might have additional state, they normally do not have getters or setters. Typed properties thus do not save us from writing code when creating services and the added type safety they provide is negligible.
Entities (DDD term) encapsulate both data and behavior. Normally their constructors take a bunch of values, typically in the form of Value Objects. The methods on entities provide ways to manipulate these values via actions that make sense in the domain language. There might be some getters, though setters are rare. Having getters and setters for most of the values in your entities is an anti-pattern. Again typed properties do not save us from writing code in most cases.
Value Objects (DDD term) are immutable. This means you can have getters but not setters. Once again typed properties are of no real help. What would be really helpful however is a first-class Value Object construct part of the PHP language.
Typed properties are only useful when you have public mutable state with no encapsulation. (And in some cases where you assign to private fields after doing complicated things.) If you design your code well, you will have very little code that matches all of these criteria.
By throwing immutability and encapsulation out of the window, you can often condense code using typed properties. This standard Value Object …
class Name { private $firstName; private $lastName; public function __construct(string $firstName, string $lastName) { $this->firstName = $firstName; $this->lastName = $lastName; } public function getFirstName(): string { return $this->firstName; } public function getLastName(): string { return $this->lastName; } }
… becomes the much shorter
class Name { public string $firstName; public string $lastName; public function __construct(string $firstName, string $lastName) { $this->firstName = $firstName; $this->lastName = $lastName; } }
The same goes for Services and Entities: by giving up on encapsulation and immutability, you gain the ability to not write a few lines of simple code.
This trade-off might actually make sense if you are working on a small codebase on your own or with few people. It can also make sense if you create a throw away prototype that you then actually throw away. For codebases that are not small and are worked on by several people writing a few simple getters is a low price to pay for the advantages that encapsulation and immutability provide.
Typed properties marginally help with type safety and in some rare cases can help reduce boilerplate code. In most cases typed properties do not reduce the amount of code needed unless you throw the valuable properties of immutability and encapsulation out of the window. Due to the hype I expect many junior programmers to do exactly that.
The post PHP Typed Properties appeared first on Entropy Wins.
]]>The post Readable Functions: Minimize State appeared first on Entropy Wins.
]]>What makes functional programming so powerful? Why do developers that have mastered it say it makes them so much more productive? What amazing features or capabilities does the functional paradigm provide to enable this enhanced productivity? The answer is not what you might expect if you never looked into functional programming. The power of the functional paradigm does not come from new functionality, it comes from restricting something we are all familiar with: mutable state. By minimizing or altogether avoiding mutable state, functional programs skip a great source of complexity, thus becoming easier to understand and work with.
If you are doing Object Orientated Programming you are hopefully aware of the drawbacks of having mutable objects. Similar drawbacks apply to mutable state within function scope, even if those functions are part of a procedural program. Consider the below PHP code snippet:
function getThing() { var $thing = 'default'; if (someCondition()) { $thing = 'special case'; } return $thing; }
This function is needlessly complex because of mutable state. The variable $thing
is in scope in the entire function and it gets modified. Thus to understand the function you need to keep track of the value that was assigned and how that value might get modified/overridden. This mental overhead can easily be avoided by using what is called a Guard Clause:
function getThing() { if (someCondition()) { return 'special case'; } return 'default'; }
This code snippet is easier to understand because there is no state. The less state, the less things you need to remember while simulating the function in your head. Even though the logic in these code snippets is trivial, you can already notice how the Accidental Complexity created by the mutable state makes understanding the code take more time and effort. It pays to write your functions in a functional manner even if you are not doing functional programming.
While mutable state is particularly harmful, non-mutable state also comes with a cost. What is the return value of this function?
function getThing() { $foo = 1; $bar = 2; $baz = 3; $meh = $foo + $baz * 2; $baz = square($meh); print($baz); return $bar; }
It is a lot easier to tell what the return value is when refactored as follows:
function getThing() { $foo = 1; $baz = 3; $meh = $foo + $baz * 2; $baz = square($meh); print($baz); $bar = 2; return $bar; }
To understand the return value you need to know where the last assignment to $bar
happened. In the first snippet you, for no reason at all, need to scan up all the way to the first lines of the function. You can avoid this by minimizing the scope of $bar
. This is especially important if, like in PHP, you cannot declare function scope values as constants. In the first snippet you likely spotted that $bar = 2
before you went through the irrelevant details that follow. If instead the code had been const bar = 2
like you can do in JavaScript, you would not have needed to make that effort.
With this understanding we arrive at two guidelines for scope in functions that you can’t avoid altogether in the first place. Thou shalt:
Indeed, these are two very general directives that you can apply in many other areas of software design. Keep in mind that these are just guidelines that serve as a starting point. Sometimes a little state or mutability can help readability.
To minimize scope, create it as close as possible to where it is needed. The worst thing you can do is declare all state at the start of a function, as this maximizes scope. Yes, I’m looking at you JavaScript developers and university professors. If you find yourself in a team or community that follows the practice of declaring all variables at the start of a function, I recommend not going along with this custom because its harmful nature outweighs “consistency” and “tradition” benefits.
To minimize mutability, stop every time you are about to override a variable and ask yourself if you cannot simplify the code. The answer is nearly always that you can via tricks such as Guard Clauses, many of which I will share in follow up posts. I myself rarely end up mutating variables, less than once per thousand 1000 lines of code. Because each removal of harmful mutability makes your code easier to work with you reap the benefits incrementally and can start applying this style right away. If you are lucky enough to work with a language that has constants in function scopes, use them as default instead of variables.
Thanks to Gabriel Birke for proofreading and making some suggestions.
The post Readable Functions: Minimize State appeared first on Entropy Wins.
]]>The post Clean Architecture + Bounded Contexts diagram appeared first on Entropy Wins.
]]>I created these diagrams for Wikimedia Deutchland with the help of Jan Dittrich, Charlie Kritschmar and Hanna Petruschat. They represent the architecture of our fundraising codebase. I explain the rules of this architecture in my post Clean Architecture + Bounded Contexts. The new diagrams are based on the ones I published two years ago in my Clean Architecture Diagram post.
Diagram 1: Clean Architecture + DDD, generic version. Click to enlarge. Link: SVG version
Diagram 1: Clean Architecture + DDD, fundraising version. Click to enlarge. Link: SVG version
Sign up below to receive news on my upcoming Clean Architecture book, including a discount:
The post Clean Architecture + Bounded Contexts diagram appeared first on Entropy Wins.
]]>The post Base Libraries Should be Stable appeared first on Entropy Wins.
]]>Robert C Martin has proposed six Package Principles. Personally I find the (linked) description on Wikipedia rather confusing, especially if you do not already understand the principles. Here is my take on library design using short and simple points:
Libraries should
The rest of this post focuses on the importance of the last point.
I am assuming usage of a package manager such as Composer (PHP) or NPM. This means that each library defines its dependencies (if any) and the version ranges of those that it is compatible with. Libraries have releases and these follow semver. It also means that libraries are ultimately consumed by applications that define their dependencies in the same way.
Consider the scenario where you have various applications that all consume a base library.
If this library contains a lot of concrete code, it will not be very stable and it will now and then be needed or desired to make a major release. A major release is one that can contain breaking changes and therefore does not automatically get used by consumers of the library. These consumers instead need to manually update the version range of the library they are compatible with and possibly adjust their code to work with the breaking changes.
In this scenario that is not a problem. Suppose the library is at version 1.2 and that all applications use version 1.x. If breaking changes are made in the library, these will need to be released as version 2.0. Due to the versioning system the consuming applications can upgrade at their leisure. There is of course some cost to making a major release. The consumers still do need to spend some time on the upgrade, especially if they are affected by the breaking changes. Still, this is typically easy to deal with and is a normal part of the development process.
Things change drastically when we have an unstable library that is used by other libraries, even if those libraries themselves are unstable.
In such a scenario making breaking changes to the base library are very costly. Let’s assume we make a breaking change to the base library and that we want to use it in our application. What do we actually need to do to get there?
In other words, we need to make a new release of EVERY library that uses our base library and that is also used by our application. This can be very painful, and is a big waste of time if these intermediate libraries where not actually affected by the breaking change to begin with. This means that it is costly, and generally a bad idea, to have libraries depend on another library that is not very stable.
Stability can be achieved in several ways. If the package is very abstract, and we assume good design, it will be stable. Think of a package providing a logging interface, such as psr/log. It can also be approached by a combination of following the package principles and taking care to avoid breaking changes.
Keep the package principles in mind, and avoid depending on unstable libraries in your own libraries where possible.
The post Base Libraries Should be Stable appeared first on Entropy Wins.
]]>The post Clean Architecture + Bounded Contexts appeared first on Entropy Wins.
]]>At Wikimedia Deutschland we use this combination of The Clean Architecture and Bounded Contexts for our fundraising applications. In this post I describe the structure we have and the architectural rules we follow in the abstract. For the story on how we got to this point and a more concrete description, see my post Bounded Contexts in the Wikimedia Fundraising Software. In that post and at the end of this one I link you to a real-world codebase that follows the abstract rules described in this post.
If you are not yet familiar with The Clean Architecture, please first read Implementing the Clean Architecture.
Diagram by Jeroen De Dauw, Charlie Kritschmar, Jan Dittrich and Hanna Petruschat. Click image to enlarge
In the top layer of the diagram we have applications. These can be web applications, they can be console applications, they can be monoliths, they can be microservices, etc. Each application has presentation code which in bigger applications tends to reside in a decoupled presentation layer using patterns such as presenters. All applications also somehow construct the dependency graph they need, perhaps using a Dependency Injection Container or set of factories. Often this involves reading configuration from somewhere. The applications contain ALL framework binding, hence they are the place where you will find the Controllers if you are using a typical web framework.
Since the applications are in the top layer, and dependencies can only go down, no code outside of the applications is allowed to depend on code in the applications. That means there is 0 binding to mechanisms such as frameworks and presentation code outside of the applications.
In the second layer we have the Bounded Contexts. Ideally one Bounded Context per subdomain. At the core of each BC we have the Domain Model and Domain Services, containing the business logic part of the subdomain. Dependencies can only point inwards, so the Domain model which is at the center cannot depend on anything more to the outside. Around the Domain Model are the Domain Services. These include interfaces for persistence services such as Repositories. The UseCases form the final ring. They can use both the Domain Model and the Domain Services. They also form a boundary around the two, meaning that no code outside of the Bounded Context is allowed to talk to the Domain Model or Domain Services.
The Bounded Contexts include their own Persistence Layer. The Persistence Layer can use a relational database, files on the file system, a remote web API, a combination of these, etc. It has implementations of domain services such as Repositories which are used by the UseCases. These implementations are the only thing that is allowed to talk to and know about the low-level aspects of the Persistence Layer. The only things that can use these service implementations are other Domain Services and the UseCases.
The UseCases, including their Request Models and Response Models, form the public interface of the Bounded Context. This means that there is 0 binding to the persistence mechanisms outside of the Bounded Context. It also means that the code responsible for the domain logic cannot be directly accessed elsewhere, such as in the presentation layer of an application.
The applications and Bounded Contexts contain all the domain specific code. This code can make use of libraries and of course the runtime (ie PHP) itself.
As examples of Bounded Contexts following this approach, see the Donation Context and Membership Context. For an application following this architecture, see the FundraisingFrontend, which uses both the Donation Context and Membership Context. Both these contexts are also used by another application the code of which sadly enough is not currently public. You can also read the stories of how we rewrote the FundraisingFontend to use the Clean Architecture and how we refactored towards Bounded Contexts.
Sign up below to receive news on my upcoming Clean Architecture book, including a discount:
If you are not yet familiar with Bounded Contexts or how to design them well, I recommend reading Domain-Driven Design Distilled.
Blogged: Clean Architecture + Bounded Contexts https://t.co/r30eiw4OP4 #CleanCode #CleanArchitecture #DDDesign
— @JeroenDeDauw August 16, 2018
The post Clean Architecture + Bounded Contexts appeared first on Entropy Wins.
]]>