Posts tagged with best practices

Using global mixins in Vue.js original

by Freek Van der Herten – 2 minute read

Recently I needed to add some global functionality to nearly all Vue components in an app. My colleague Seb told me a good way to achieve this: global mixins. In this post I'd like share that knowledge. In Vue a mixin is some functionality bundled in a file that can be added to one or more Vue…

Read more

CSS Utility Classes and "Separation of Concerns"

In my day to day work I don't write any css at all, but I still very much enjoyed this blogpost by Adam Wathan where he shares some interesting things about how css should be structured and he makes the case for using utility classes to make visual tweaks.

When you think about the relationship between HTML and CSS in terms of "separation of concerns", it's very black and white.

You either have separation of concerns (good!), or you don't (bad!). This is not the right way to think about HTML and CSS. Instead, think about dependency direction.

...

One of the biggest benefits of using small, composable utilities is that every developer on your team is always choosing values from a fixed set of options.

https://adamwathan.me/css-utility-classes-and-separation-of-concerns/

Read more

Join 9,000+ developers

Every month, I share practical tips, tutorials, and behind-the-scenes insights from maintaining 300+ open source packages.

No spam. Unsubscribe anytime. You can also follow me on X.

Precision Through Imprecision: Improving Time Objects

Ross Tuck is probably one of my favourite bloggers. He doesn't publish something often (his previous post is from 2015), but when he does it's very much worth your time.

The important takeaway here isn’t “value objects, yay, inline juggling, boo!” It’s that we were able to remove several classes of errors by reducing the precision of the DateTime we were handling. If we hadn’t done that, the value object would still be handling all of these edges cases and probably failing at some of them too.

Reducing the quality of data to get a correct answer might seem counter-intuitive but it’s actually a more realistic view of the system we’re trying to model. Our computers might run in picoseconds but our business (probably) doesn’t. Plus, the computer is probably lying anyways.

http://rosstuck.com/precision-through-imprecision-improving-time-objects

Read more

Why using Yoda conditions you should probably not be

Grégoire Paris wrote down his opinion on why he dislikes Yoda conditions.

So how do Yoda conditions work? Well it is basically a knee-jerk reaction you have to develop: whenever you write a condition, put the operand that cannot be assigned on the left. This should give you an error message if you make an assignment when you actually meant to make a comparison.

https://dev.to/greg0ire/why-using-yoda-conditions-you-should-probably-not

Personally, I'm not a big fan of Yoda conditions either. My feeling is the the cost of decreased readability is just too high for the small value that Yoda conditions bring to the table.

Read more

Typehint all the things

David Négrier, CTO of the CodingMachine, wrote a nice article on why he likes and how his team uses typehints.

As a developer consuming this function, I know how to use it. And if I’m using it wrong, I’ll know right away because PHP will crash with a nice error message when the function is called rather than with a cryptic error some time later.

https://www.thecodingmachine.com/type-hint-all-the-things/

Personally I like typehints too, because the potential readability improvement the article touches upon.

Note: (I only include this paragraph because it's mentioned in the intro of the article, don't want to stir up a discussion) the fuzz about that "Visual Debt" video was overblown. Even though I didn't agree with all of it, it was nice to hear Jeffrey's way of thinking.

Read more

A programmer's cognitive load

Brent Roose wrote down his thoughts around how things like fonts, spacing, docblock, ... can influence the cognitive load of a programmer.

As a professional programmer, I'm reading and writing code on a daily basis. I'm working on new projects, doing code reviews, working with legacy code, learning documentation etc. Based on my own experience and that of colleagues, being a programmer often involves a lot more reading than actually writing code. Whether it's your own code or that of others, when you open a file, you have to take it all in. You need to wrap your head around what's going on, before you're able to write your code. Doing this day by day, it's important to find ways to make this process easy. To try and reduce this cognitive load as much as possible. Streamlining the way you take in code, will allow you to not only work faster and better; but also improve your mental state and mood.

https://www.stitcher.io/blog/a-programmers-cognitive-load

Visual debt is real.

Read more

Using non-breakable spaces in test method names

Mattieu Napoli shows how you can use non breaking spaces to make long test function names more readable.

Yes. This article is about using non-breakable spaces to name tests. And the fact that it’s awesome. And why you should use them too.

public function test a user can add a product to a wishlist() { // ... }

The code above is valid PHP code and works. Non-breaking spaces (aka   in HTML) look like spaces in editors but are actually interpreted like any other character by PHP.

http://mnapoli.fr/using-non-breakable-spaces-in-test-method-names/

It's cool that it works, but I'm not really a fan of this. I very much prefer how test runners like jest go about this by passing the name of the test to a function so you can use spaces.

Read more

The broken windows theory or “Why some projects are just destined to suck”

In a new post on his blog, my favorite stalwart of the industry Frederick Vanbrabant, gives a explanation on why some projects turn into a big mess and how you can avoid it.

I truly believe that the broken window theory can be applied to software projects. In my personal experiences I’ve rarely seen a project start out as a total mess. If it ended up as a mess, it was gradually. I also believe that this is not necessary the fault of developers working on the project, think of it more as frogs in a pot with gradually increased temperature of water. One morning you just wake up and take a look at the project and realise that it has gotten really messy.

http://frederickvanbrabant.com/2017/06/12/broken-windows-theory.html

Read more

The status antipattern

In a new blogpost on vemv.net the author (I couldn't find his real name) argues against using a simple status field.

Dear programmer, do you ever use the name state for your variables? Like state = 42? “Hell no, that’s a terribly generic word. Better to use the domain-specific wording the variable refers to: is_invoiced, visit_count, shopping_cart, things like that.”

Sounds reasonable. But sometimes, you have a database column called status, and use it through your codebase, right? “Yes.”

Were you aware that status and state mean basically the same thing in English? “Uhm…”

https://blog.vemv.net/the-status-antipattern-479c26c7ddf7

Read more

Classes, complexity, and functional programming

In this article Kent C. Dodds clearly explains what the downsides are of using Class in JavaScript.

Classes (and prototypes) have their place in JavaScript. But they’re an optimization. They don’t make your code simpler, they make it more complex. It’s better to narrow your focus on things that are not only simple to learn but simple to understand: functions and objects.

https://medium.com/@kentcdodds/classes-complexity-and-functional-programming-a8dd86903747

Read more

PHP Generics and why we need them

On the stitcher.io blog a new post appeared that explains the benefits of generics with a practical example. Generics aren't supported in PHP, but there is an RFC.

In today's blog post we'll explore some common problems with arrays in PHP. All the problems and issues listed could be solved with a pending RFC which adds generics to PHP. We won't explore in too much detail what generics are. But at the end of this read you should have a good idea as to why they are useful, and why we really want them in PHP. So without further ado, lets dive into the subject.

https://www.stitcher.io/blog/php-generics-and-why-we-need-them

Read more

Elegant form handling in Laravel

Michael Dyrynda, co-host of both the excellent North Meets South podcast and Laravel News podcast, wrote a short article on how he pragmatically manages forms in a Laravel app.

I personally feel that I'm not gaining anything by bringing in another package just to handle generating HTML on my behalf when it's just as fast to use the tools available to me in my editor. It also means there's no implied knowledge of a now non-standard external package, should a new developer be brought on to the projects that I've worked on. HTML is HTML, after all.

https://dyrynda.com.au/blog/elegant-form-handling-in-laravel

Read more

Using destructuring assignment in for VueJS loops

Ivan Sieder offers a nice tip on how to write better v-for loops in VueJS component templates.

There is really nothing wrong with the above code, but there is a small aspect, which really bugs me. As you can see, we are repeating the product identifier inside the output twice ({{ product.name }} and {{ product.price }}).

We are modern web developers (at least I hope so) and therefore we are going to use modern ES6 functionality. The feature we are going to use is called destructuring assignment. Basically, what the destructuring assignment allows us to do, is to pull a specific property off of an object and store it in a variable.

https://simedia.tech/blog/vue-js-destructuring-assignment-for-loops/

Read more

The case for singleton objects, façades, and helper functions

Matthias Noback explains the value of singleton objects: when used properly they can make your code more developer friendly.

I'm not advocating the use of façades and helper functions per se. I assume they may have good use in specific parts of your application (just as the Laravel documentation states, by the way). My point is that, as long as you build into your libraries both options, you can offer a great developer experience, providing an out-of-the-box working solution of a useful service, which can still be used in a project that uses dependency injection everywhere.

https://php-and-symfony.matthiasnoback.nl/2017/05/the-case-for-singleton-objects-facades-and-helper-functions/

Read more

Familiarity Bias is Holding You Back: It’s Time to Embrace Arrow Functions

I don't think that less lines of code automatically means code is more readable, but I'm a big fan of ES6' arrow functions. In this article Eric Elliott dives deep into them.

I also supect that your team would become significantly more productive if you learned to embrace and favor more of the concise syntax available in ES6. While it’s true that sometimes things are easier to understand if they’re made explicit, it’s also true that as a general rule, less code is better. If less code can accomplish the same thing and communicate more, without sacrificing any meaning, it’s objectively better.

https://medium.com/javascript-scene/familiarity-bias-is-holding-you-back-its-time-to-embrace-arrow-functions-3d37e1a9bb75

Let's hope we'll soon have array functions in PHP too.

Read more

Creating strictly typed arrays and collections in PHP

You might thing that PHP is not able to automatically perform a type check on items in an array. But using variadic constructor this is possible. Bert Ramakers wrote a blogpost with some good examples on how to do this.

One of the language features announced back in PHP 5.6 was the addition of the “…” token to denote that a function or method accepts a variable length of arguments.

Something I rarely see mentioned is that it’s possible to combine this feature with type hints to essentially create typed arrays.

https://medium.com/2dotstwice-connecting-the-dots/creating-strictly-typed-arrays-and-collections-in-php-37036718c921

Read more