Image by Dmitry Popov

To BEM or not to BEM : Christoph Reinartz

“BEM was not designed to be pretty but to scale. In general I think it is quite bulletproof. And it helped us a lot.”

Date: 06 May 2016

Q. Hi Christoph, thank you for accepting my invite for this interview.

A. You are very welcome. Happy to chat about my experiences with BEM.

Q. This is a side project, called “To BEM or not to BEM”, and as you will guess, it’s about the BEM methodology, but not only.

Everything started when one evening, a few weeks ago, I read your article on Medium, in which I found this phrase: “In the meantime, to fight the CSS structure problem we found that ITCSS is the way to go.” after having told that in the beginning you were using BEM as methodology.

So, let’s start from the beginning. Can you tell me your role in trivago? how comes that you started to work on the project? what is your role?

A. I am currently leading the UI/UX Engineering team at trivago and I would describe myself a Front-End Developer. The company I am working for grew a lot the last years. When I started we were around 150 people and last week we celebrated employee Nr.1000. In such a fast growing environment we had issues with keeping consistency in code and design. In the beginning of 2015 we started creating a Design System and introduced a pattern library. After we redesigned our company pages we had to somehow bring in the new architecture into our core product, the trivago hotel search.

Q. When you started to work on the project, as far as I understand, it’s one year ago. How was built the CSS codebase? Less, Sass, “pure” CSS, other?

A. Yes correct one year ago we started with revising our coding guidelines and the way we built our UI patterns. Our CSS codebase was built with Sass but our coding guidelines didn’t allow us to scale up our Front-End which let us rethink the way we built our CSS.

Q. What was the reason for “not being able to scale”? What do you mean?

A. We started to repeat ourselves too often. we reinvented the wheel way all the time because re-usability was very low. This slowed us down in maintainability and in creating new features

Q. And how BEM enter in all of this? Which were the reasons that made you choose to adopt it? Who decided, you on your own or was a collective/team decision?

A. I had a very deep look into BEM and was very convinced that this will help building more modular CSS. I think it is important that not one person decides but the team is comfortable with it. So I proposed it and asked my colleagues to try it out. At first people where very unlucky with the amount of classes BEM can add to the markup, but once they understood the structure and the benefits they felt comfortable with applying it. BEM was not designed to be pretty but to scale. Although we still often discuss about too many classnames, but in nearly every instance you can proof that moving the “problem” to CSS and try to save a classname with e.g. only using modifiers classes for block elements does not scale but will blow up your CSS.

Q. Do you follow strictly the BEM dictations? e.g. “everything is a class and nothing is nested”. Do you nest, for example? I have seen the worst things, caused by this “Thou shalt not nest!” :) Do you have multiple components/blocks under the same file, or there is always a one-to-one relation?

A. No this might work in an ideal world. But e.g. in some cases you might not have access to the markup, because it is coming from a database. So we don’t strictly apply BEM but in some cases like our new re-designed item element we use it extensively. The element is also very, very complex and so in some cases we nest, but in general the nesting level is max 2, way less than it was before applying BEM. It depends on the use-case. Also we combine BEM with OOCSS and Atomic CSS, which adds even more classes but keeps the CSS more DRY.

Q. But if everything is in the root scope, how do you avoid to end up in clashing names (e.g. someone decides to use a class name, that doesn’t know is already used somewhere else) or in spending time and mental energies trying to find names to the things?

I mean, once someone before me decided to use “.form” for something (where “form-new” is the block part, I mean), and maybe is not really a global “form” style across all the website/application, that name is taken and I will not be able to use it. so I have seen things like “.form-new” “.form-v2” and “.form-new-v2”, because of that

A. We try to use BEM on components level as much as possible, so the form would be a component for us. But we sometimes have issues with our Atomic CSS / helper classes, e.g. something like “pull-left”. It happened recently that we had different class names for this and I removed the deprecated one, which was the one everybody used :) In general knowledge sharing with workshops and code reviews is highly important for us. So it is important that people also understand the big picture of the CSS codebase. If they just would do BEM everywhere there might be e.g. a lot of color-definitions which you could also add with a atomic CSS class.

Q. So you say BEM is working well for your project, and within your team. So why did you say that “ITCSS is the way to go”? What happened? Did you started to do BEMIT (as Harry Roberts calls it)?

A. OK we see ITCSS more of a structural approach for our files and it nicely goes along / combines with our BEM approach. Beginning from the components level we only use BEM but only a very rare cases on the layers below, e.g. objects. So this allows us that we always have our base CSS loaded on pageload and then beginning from the components layer we can conditionally load the components. I am still a big fan of having a global CSS but like also to have CSS chunks for your components (which rely on that the base CSS is loaded)

We had a look into BEMIT, but as our structure is working so good for us we didn’t yet apply it.

Q. So you are doing BEM + OOCSS + Atomic + ITCSS ? :)

A. Yes exactly! And the results are convincing us.

Q. Let’s talk about the future: I see two big “architectural” shifts on the CSS panorama: CSS Modules in React (and web components in general) and Atomic CSS.

With the first one, do you still see the need for BEM? You just said “We try to use BEM on components level as much as possible”, but if the component is really “isolated” like in the case of a React component, and the CSS is “imported” like here:

import styles from ‘./buttons.css’
-- or --
import styles from ‘./forms.css’

at that point, why do you need the extra complexity of the B-E-M naming, when there’s non risk of name clashing or property overriding/inheritance?

A. We try to built up a system / base which works for all our applications, some of them already use approaches of web components (we e.g. use already webpack to load and our CSS asynchronously) while others e.g. use Wordpress with a way more simple approach to render a site and load their assets. It is important for us that we consider all ways to include our CSS so there will be most likely always a benefit of BEM for us.

Q. OK, and what about Atomic CSS? At the end, it’s BEM taken to the extremes, no? Every class is an “helper” (in some way a “*__* — “ modifier), everything is in the root scope but because of the naming convention there’s no risk of clashes, all the styles are declaratively applied in the HTML itself, no need to create “structure” in the CSS.

A. I see Atomic CSS only as a useful addition but I would never see the benefit of putting everything into the root scope. Your CSS would become completely unstructured. Also I think this would not make it maintainable anymore nor would it be encapsulated.

Q. I agree.

What sometimes struggles me is that I don’t find so many examples of “real world usage” of BEM as I would expect, given the fact that apparently everybody uses BEM. Do you have some preferred Git repository/project that you use as example/reference/inspiration? Or other sources of “real world use cases” that you can share?

What I mean is, of “good use” of BEM that goes beyond the “hello world” examples that you can find on every article or tutorial on BEM. Some large-scale project built using BEM consistently across all the codebase.

A. Ahaha yes. It is like with the Todo-Apps in the JS-area, there are so many articles and tutorials, but none of them helps you to apply BEM on large scale. We are thinking of making at least a part of our new CSS base public on GitHub. Also because we are curious about the feedback. I like what the e.g. The Guardian is doing, they use BEM a lot. I even think they recently started using BEMIT.

Q. You are so enthusiast of BEM. Is there something that you find doesn’t work well, of this methodology? Or you simply have “adapted” it to your own taste (see the integration with OOCSS, Atomic, etc.)

Which is what in some way happened with the Agile methodologies: everybody “does Agile” but everyone has chosen his own flavour or pick of them.

“We do Agile but” and is something similar to what I generally hear of BEM: “We use BEM but”, “Yes, we use BEM. Kind of”.

A. In general I think it is quite bulletproof. And it helped us a lot. But as already said in some cases you might not have full access to the markup or content writers will add some text with a text editor which doesn’t automatically add your element classes. So in this case you could write your own / adapt the editor or you just apply BEM where it works for you. And if some Atomic CSS classes make you develop fast and keep your CSS size smaller then we decide to do so.

Q. Ok, make sense. very pragmatic (and smart).

So, last question, To BEM or not to BEM?

If someone should start a large-scale greenfield project today, would you advice her/him to use BEM? or yourself, would you still use BEM or you would go for {name your other choice here}?

A. Yes definitely! I would advise using BEM and ITCSS and make use of Atomic CSS + OOCSS if needed/suitable.

Q. Good.

That’s it, thank you so much for your time!

A. You are very very welcome! Was a pleasure. And I liked your questions

Q. I know that in the next weeks you are going to present your experience (the so called Project Ironman) at many conferences across Europe. Can you mention them?

A. Official are Codemotion Amsterdam, Frontend United and ScotlandCSS. A few are pending and not official yet — I already had the pleasure at JSunconf Hamburg

Q. Wow, great!

Thank you so much, thank you for sharing your experiences not only here with me, but also through your posts and your talks. Sharing is caring :)

Hope to see you soon, and thank you again!

Christoph Reinartz is Front-End Developer & Teamlead UI/UX Engineering at trivago. He is working on scaling and improving the User Interface of the trivago core product on a large scale since more than 3 years now.

He worked in several agencies before. Loves to code things that people like to look at and want to interact with. Always worried about performance of websites and the scalability of frontends.

Links: website // twitter // github

This is the first of a series of ten interviews, originally published at www.didoo.net/to-bem-or-not-to-bem.

--

--

--

A series of interviews around the BEM methodology (and CSS in general)

Recommended from Medium

Find the best Software Development company?

software development companies

Effective Kotlin: Consider inline modifier for higher-order functions

Learn about HandyPick_1

10 free UI Libraries for making Web Applications

DRYing out Procore’s Kubernetes Configuration

How to succeed in AWS Certified Solutions Architect — Associate exam

HW#18 Practice of Code Writing

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cristiano Rastelli

Cristiano Rastelli

Design System Lead at HashiCorp. In love with Atomic Design, Design Systems & CSS architecture. Attendee/speaker/organiser of tech conferences and meetups.

More from Medium

Don’t use CSS classes in your tests!

How to synchronize the color contrast between the background and the text with javascript?

The default look of the selected filters

Adding a Chart to Your Website With Chart.js

Create a CSS-only Word Clock