larry@garfieldtech.comByte-sized functional programming: Pure functions make testing easy (14.7.2020, 14:28 UTC)
Byte-sized functional programming: Pure functions make testing easy

When testing stateful code, you have to first set up all of the explicit and implicit state that a given piece of code depends on. If the object you're testing has many dependencies that could be complex, or references global variables, or calls many other routines with their own dependencies, you may be looking at a lot of complex setup that makes testing hard to do effectively.

Pure functions greatly reduce that problem.

A pure function is a function that:

  • has no inputs other than those explicitly specified;
  • has no effect on any value other than the value it returns.

That means there is, by definition, only one place to inject dependencies: the call itself. And there's only one effect to validate: the return value of the function. No need to call a function and check what the effect was on some other value or piece of code: by definition, the only effect is the return value.

If one of the parameters you pass in is itself complex, that may make the test complex. But if the parameter is complex, that can serve as an impetus to simplify it. Rather than passing in an object, for example, just pass in a single function. If that function in practice has more complexity behind it, fine, but it makes passing a mock function trivial. Just... make a new (anonymous) function for the one test.

When your code has fewer sneaky interactions, there's less effort involved in testing as well as fewer things to test.

---

Want to know more about functional programming and PHP? Read the whole book on the topic: Thinking Functionally in PHP.


Thinking Functionally in PHP
Larry 14 July 2020 - 9:28am
Link
Voices of the ElePHPantInterview with Kim Cottrell (14.7.2020, 11:30 UTC)

Show Notes

This episode is sponsored by
Ring Central Developers

The post Interview with Kim Cottrell appeared first on Voices of the ElePHPant.

Link
Derick RethansPHP Internals News: Episode 61: Stable Sorting (9.7.2020, 08:24 UTC)

PHP Internals News: Episode 61: Stable Sorting

In this episode of "PHP Internals News" I chat with Nikita Popov (Twitter, GitHub, Website) about his Stable Sorting RFC.

The RSS feed for this podcast is https://derickrethans.nl/feed-phpinternalsnews.xml, you can download this episode's MP3 file, and it's available on Spotify and iTunes. There is a dedicated website: https://phpinternals.news

Transcript

Derick Rethans 0:18

Hi, I'm Derick, and this is PHP internals news, a weekly podcast dedicated to demystifying the development of the PHP language. This is Episode 61. Today I'm talking with Nikita Popov about a rather small RFC that he's proposing called stable sorting. Hello Nikita, how are you this morning?

Nikita 0:36

Hey, Derick, I'm great. How are you?

Derick Rethans 0:38

Not too bad myself. Let's jump straight in here. The title of the RFC is stable sorting, what does that mean, what is stable sorting, or what is sorting stability?

Nikita 0:48

Sorting stability refers to the behaviour of the sort when it comes to equal elements. And equal share means that we sort comparison function. For example, the one you pass to usort says the elements are equal, but there is still some way to distinguish them. For example, if you're sorting some objects, to take the example from the RFC, we have an array with users, and users have an age, and we use usort to only sort the users by age. Then according to the comparison callback all users with the same age are equal. But of course, the user also has other fields on which we can distinguish it. And the question is now in what order will equal elements appear. If we have a stable sort, then they will appear in the order they were originally in. So it's something not going to change.

Derick Rethans 1:41

And that is not what PHP sorting mechanism currently does?

Nikita 1:44

Right. PHP currently uses an unstable sort, which means that the order is simply unspecified. It will be deterministic. I mean if you take the same input array and sort it, then every time we will get the same result. But there is no well specified order or relative order of elements. There's just some order. The reason why we have this behaviour is that well there are, I would say, two, the only two sorting algorithms. There is merge sort. Which is a guaranteed n log n sort that the stable, but has the disadvantage that that requires additional memory to perform the merge step. The other side there is a quicksort, which is an average case n log n sorting algorithm and is unstable, but does not require any additional memory. And in practice, everyone uses one of these algorithms, usually with a couple of extensions on sort of merge sort. Nowadays we use timsort, but which is still based on the same underlying principle, and for quicksort, we have sort which is better than quicksort, which tries to avoid some of the bad worst case performance which quicksort can have. PHP currently uses us a quicksort, which means that our sorting results are unstable.

Derick Rethans 3:07

Okay, and this RFC suggesting to change that. How would you do that? How would you modify quicksort to make it stable?

Nikita 3:15

Two ways. One is to just change the sorting algorithm. So as I mentioned, the really popular stable sorting is timsort, which is used by Python by Java and probably lots of other languages at this point. And the other possibility is to stick with an unstable source. So to stick with quicksort, but to artificially enforce that the comparison function does not have, does not report equal elements that a

Truncated by Planet PHP, read more at the original (another 5853 bytes)

Link
PHP: Hypertext PreprocessorPHP 8.0.0 Alpha 2 available for testing (9.7.2020, 00:00 UTC)
The PHP team is pleased to announce the second testing release of PHP 8.0.0, Alpha 2. This continues the PHP 8.0 release cycle, the rough outline of which is specified in the PHP Wiki. For source downloads of PHP 8.0.0 Alpha 2 please visit the download page.Please carefully test this version and report any issues found in the bug reporting system.Please DO NOT use this version in production, it is an early test version. For more information on the new features and other changes, you can read the NEWS file, or the UPGRADING file for a complete list of upgrading notes. These files can also be found in the release archive. The next release will be Alpha 3, planned for 23 Jul 2020.The signatures for the release can be found in the manifest or on the QA site.Thank you for helping us make PHP better.
Link
platform.shBest practices for deploying web apps have evolved (9.7.2020, 00:00 UTC)
[This post is heavily inspired by Christopher Skene's 2017 "What is best practice in web application deployment." Three years is a long time for best practices in devops land, however, so it's time to again update our thinking.] “Best practices” are often talked about in the tech world, but rarely is it acknowledged that they can and do change over time. All “best practices” are nothing more than a distillation of the challenges of a given situation to a usually optimal set of trade-offs.
Link
Matthias NobackRelease of the Advanced Web Application Architecture book (8.7.2020, 07:25 UTC)

Book cover

100% done

I'm happy to announce that my latest book "Advanced Web Application Architecture" is now complete. With ~390 pages it has become a well-rounded book full of useful design patterns and architectural principles built around the notion of object-pure code, that will help you create decoupled applications that are testable by definition, and support a domain-first approach to software development.

Use this link to get 10% off the price: https://leanpub.com/web-application-architecture/c/RELEASE_DAY

"This book helped me to improve myself and I recommend it to any developer who wants to learn how to solve problems in a better way." -- Iosif Chiriluta

"Matthias has a knack for distilling decades of experience into simplified rules of thumb. Explained with practical examples, the rules in this book are accessible to developers of any level of experience. I expect to recommend this book for many years to come." -- Ramon de la Fuente

A second round of book club sessions

Read with the author

In the past few months we've been having some really interesting sessions with early-access readers of the book. With the book fully finished, I decided to plan another round of sessions starting in September. To join these sessions make sure to select the package "The Book + 6 Book club sessions" when buying your copy of the book.

"This book is a most welcome simplification of modern web application architecture concepts, making the topic accessible for a wide audience. By guiding the reader through classic web application scenarios and applying a couple of simple rules and principles while staying very pragmatic, the author achieves what most others don't - making the core idea so simple that it is near impossible for the reader to get it wrong. The only architecture book I would recommend even to my junior colleagues." -- Ondřej Bouda

Print edition

For those who prefer a paper book there's also a print edition available via Lulu. This includes a free copy of the e-book. You'll find instructions for obtaining it inside the book.

The making of

Back in April 2019 I started working on this project. It all started with the family visiting friends in France while I stayed at home, alone, with a bunch of post-it's.

Post-it's

From this first brainstorm session I created a preliminary table of contents:

Original TOC part 1Original TOC part 2

The rough outline was there, but I started working on the setup first.

So far I've only used Leanpub's Markdown-based publishing process. They first convert the Markdown manuscript to LaTeX, then to PDF, HTML, EPUB and MOBI. I thought it would be nice to have more control over all the build artifacts, so I worked on my own LaTeX-based setup. This was a silly decision. I didn't keep track of all the hours that went into this, but it has probably taken me about 50 hours to get it right. Of course, I keep running into little things that don't work. But I do have a fully automated local book writing process now. Being able to add all kinds of other generated artifacts is really helpful. For instance, I can edit the book cover in Gimp and it will be automatically resized for the different e-book formats, and also flattened for the print cover. The same goes for the diagrams: if I modify them using the Draw.io desktop editor, they will be automatically prepared for inclusion in the manuscript. Cool! But also: very costly.

With the technical stuff out of the way, the writing process was great in the beginning, but seemed to last for ages in the end. I've spent a total of 208 hours of actual writing (including writing the code samples and creating the diagrams). I generally aim to get everything right the first time (of course), but revising

Truncated by Planet PHP, read more at the original (another 962 bytes)

Link
Derick RethansXdebug Update: June 2020 (7.7.2020, 08:32 UTC)

Xdebug Update: June 2020

Another monthly update where I explain what happened with Xdebug development in this past month. These will be published on the first Tuesday after the 5th of each month. Patreon supporters will get it earlier, on the first of each month. You can become a patron to support my work on Xdebug. If you are leading a team or company, then it is also possible to support Xdebug through a subscription.

In June, I worked on Xdebug for about 100 hours, with funding being around 70 hours. I worked mostly on the following things:

Xdebug 3

I spent most of my time this month on Xdebug 3. In the first week I made a few changes around the way how you enable specific features. In Xdebug 2, each feature can be turned on at the same time (such as through xdebug.remote_enable, and xdebug.profiler_enable). In Xdebug 3 this has now changed to a single setting xdebug.mode which for step debugging and profiling would have the values debug and profile respectively.

I am also in the process of renaming things to have a more logical meaning. For example I renamed the filter constants XDEBUG_NAMESPACE_BLACKLIST with XDEBUG_NAMESPACE_EXCLUDE. I will continue to rename things on the way towards Xdebug 3, with things such as the xdebug.remote_host likely to become something like xdebug.ide_hostname. I am also hoping to reduce the amount of settings in general.

By separating out the modes and being able to turn them on with one setting, it becomes clearer that it is best not to have all the functionality turned on by default. During the rest of the month I've split out Xdebug's internals so that it is much easier to enable specific information gathering only when it is actually necessary. This should improve performance drastically.

Because Xdebug has lots of old code in it, some of it is still optimised for PHP 5. A new, and more performant, way of handling strings was introduced in PHP 7, but Xdebug has not yet switched over to that new data type. This is what I am currently trying to finalise. Once that is done, I hope to see a performance boost already. Stay tuned for some benchmarks on this.

The upgrade guide has more information about all the changes that are coming to Xdebug 3, but please be aware that these changes are preliminary, and could still change.

Xdebug Cloud

I worked on Xdebug Cloud only a little. I have also been in contact with JetBrains to evaluate how PhpStorm could connect to Xdebug Cloud without requiring a separate (local) proxy service. If you've signed up to the Xdebug Cloud mailinglist I'll let you know as soon as something can be tried-out.

Business Supporter Scheme and Funding

In June, no new supporters signed up.

If you, or your company, would also like to support Xdebug, head over to the support page!

Besides business support, I also maintain a Patreon page and a profile on GitHub sponsors.

Become a Patron!
Link
larry@garfieldtech.comByte-sized functional programming: Pure functions encourage small code (6.7.2020, 22:13 UTC)
Byte-sized functional programming: Pure functions encourage small code

One of the many pieces of advice for a long-term sustainable code base is to keep code small. The larger a code base is, the more effort it takes to understand all the moving parts. Your brain can only keep so much mental model of your code in it at once, and if the code you're looking at is too large then what you can fit in your own "active memory" at once then you will have an increasingly hard time understanding it.

Most useful applications tend to grow larger than what the typical human can fit in their active memory, however, so you need a way to break up your code so you can load a relevant piece into your brain at once to understand and debug it. Usually that takes the form of encapsulation, coupling, cohesion, and other common object-oriented vernacular.

But what about just a pure function?

A pure function is a function that:

  • has no inputs other than those explicitly specified;
  • has no effect on any value other than the value it returns.

That has a number of advantages, such as being idempotent (calling it a second time with the same values is guaranteed to return the same result) and referential transparency (a function and its parameters is synonymous with its result, which can let you optimize the function away entirely in some cases). But perhaps the biggest advantage of pure functions doesn't have a fancy name: It's really easy to fit in your brain.

If you're trying to understand a given piece of code, a pure function will always be the easiest to understand because there is no need for context. There are some explicit inputs, which you can see; There is an explicit output, which you can see; And there's nothing else to care about. Reading the function (and the data definition of its parameters) is all you need to think about, because there's nothing else to think about. Every function becomes a natural "small enough to fit in your brain" unit.

A pure function will often call other pure functions, but that only creates slightly more overhead. Once you know a function is pure, it's easy to mentally "unload" as a black box that you can deal with separately. Go read that code first, then put it out of your brain and focus on the next function.

Moreover, functional programming-style code tends to favor function composition over direct function calls. That is, rather than function A calling function B which calls function C, you call function A and pass its return value to function B, then pass B's return value to C. That whole process can be wrapped up into another function if necessary. That makes it even easier to focus on only one function at a time, which is virtually guaranteed to fit in your brain at once.

---

Want to know more about functional programming and PHP? Read the whole book on the topic: Thinking Functionally in PHP.


Thinking Functionally in PHP
Larry 6 July 2020 - 5:13pm
Link
larry@garfieldtech.comA Major Event in PHP ebook now available (6.7.2020, 22:12 UTC)
A Major Event in PHP ebook now available

In early 2019, the PHP Framework Interoperability Group (PHP-FIG) released PSR-14, the Event Dispatcher specification. At the time I posted a long series of blog posts detailing PSR-14 in all its glory.

After discussing with a few other FIG folks, I've decided to release that blog series as a small ebook. Mainly that is to provide an easy single-point-of-reference for those who want to really understand PSR-14. Also, it serves as a simple fundraiser.

The book itself is available completely free, as the original blog posts were. However, you can also purchase a copy if you want to help support my work on the Framework Interoperability Group and Open Sourcing Mental Illness. OSMI is a non-profit organization that works to raise awareness of and research information about mental health challenges in the tech community. 50% of all royalties for this book are automatically donated to OSMI to support their vital work.

If you just want to grab a copy for free, go for it. If you are able to, though, I would encourage you to pay what you're comfortable with to support both my Open Source efforts and OSMI.

(This announcement is a bit late due to a publishing goof on my blog. If you're seeing this for the second time, my apologies.)

Larry 6 July 2020 - 5:12pm
Link
Voices of the ElePHPantInterview with Michelangelo van Dam (3.7.2020, 10:07 UTC)
Link
LinksRSS 0.92   RDF 1.
Atom Feed   100% Popoon
PHP5 powered   PEAR
ButtonsPlanet PHP   Planet PHP
Planet PHP