LESS has Better Forms of Abstraction than CSS
Summary: LESS has obviously better forms of abstraction and combination than CSS. It has recursive style definitions, which is enough to consider it a "powerful language".
Ok, it's obvious that CSS has weak forms of combination and abstraction. But now we have a good framework for understanding why. "CSS Preprocessors", as they are called, are getting really popular now. We would be smart to analyze LESS in the same way that we analyzed CSS, if only to temper the glamor of trendiness that surrounds it. Comments are welcome.
Because LESS aims to be a superset of CSS, it has all of the primitive expressions, means of combination, and means of abstraction that come baked into CSS. I already went over those last time, so I will not go over them again. So what things are added by LESS?
Besides existing CSS properties, LESS adds two new primitive
expressions. Mixin application (
.rounded-corners(10px);) in a rule
recursively applies the primitive expressions defined in the body of
the mixin to the current rule. Mixin applications can be parameterized
with value expressions, or they can have no parameters. Extension
&:extend(.blue-button);) is similar, but instead of applying the
primitive expressions to a rule body, it adds the selector of the rule
to the rule selector of the extension. Extension is recursive as
Variables and mathematical expressions change the way primitive properties work. In CSS, primitive properties were comprised of a property name and a literal property value. In LESS, variables and math expressions, as well as literal values, can be in the value place (right hand side) of a property.
Variables can also be used in selectors.
Means of Combination
The principle means of combination are still the rule, but add to it the ability to nest rules, and things are more interesting. Nesting two rules is shorthand for writing out two rules (unnested) with a nested selector. While in the simple case it is simply a shorthand, when nested rules are applied as mixins, you gain a lot more than better syntax. Mixins with nested subrules allows you to name a nesting and refer to it later.
Means of Abstraction
CSS did not contain much in the way of abstraction. LESS focuses primarily in the realm of abstraction, probably to appease the will to power of front-end designers. Variables allow property values to be named, and naming is a form of abstraction. Variables are a good way to name values that all have the same meaning and would therefore change at the same time. For instance, a shade of green that is used throughout the styles is a perfect use for variables. Variables can be used in a similar way to name selectors.
A more powerful form of abstraction comes from the ability to define
mixins, apply mixins, and use of
:extend(). In LESS, any rule using a
single class or id selector can be used as a mixin. This is essentially
a way to name a rule---our principle form of combination. In
addition, if you put empty parentheses after the class selector in the
rule, the rule is not outputted into the generated CSS, which can save
bytes. Mixins can also have parameters (scoped variables), so they can
be abstracted over a variety of values. Extend allows a similar kind
of abstraction which promises to be more efficient.
Mixins are very powerful. In fact, this is the kind of abstraction that is needed for LESS to be powerful, as defined by Abelson and Sussman. Because you can now name a group of styles (mixin) and then use that name in another group of styles (mixin application), LESS has full-on recursive style definitions. With extension, it also has recursive selector definitions. In LESS, we can talk of "levels of abstraction" whereas in CSS there was only one.
LESS has recursion. It lets you define and name groups of properties, then refer to those groups by name in other groups of properties. We can consider LESS powerful enough to express useful abstractions. Yet though it is more powerful than CSS, it still has many of the problems of CSS (especially complex rules governing the combination of multiple rules to a single element). How can LESS be leveraged to gain its power but tame its weakness? Is there a subset of LESS that can gerrymander the good parts away from the bad parts?