Passa al contingut principal

Judging CSS / Spotting Bad Code

Judging CSS / Spotting Bad Code:
In a recent Q&A article on Smashing Magazine, a question was asked about how to tell if a developer has written bad CSS. Specifically:
What are the signs that the CSS is sub-optimal, or that the developer hasn’t done a good job? What do you look for in CSS to determine how good or bad it is?
I thought that was an interesting question and I'd expound upon my answer a bit.




Why?

Perhaps you've hired someone to write CSS for you and you want to have a gauge of how good they did. Perhaps you're looking through someone's CSS that you may potentially hire. Perhaps you want to gauge your own CSS somehow.

The Obvious Test

Look at the website. If it looks all messed up, then they did a bad job.
Taking that a step further, check it against the browsers you agreed on would be supported. The design should be workable in all of them.
If the design is supposed to be responsive, resize your browser window to ensure the design works no matter what width or height it is.
If everything looks good, that's a good (and required) first step. But it's not absolute proof the CSS is good.

The Formatting Test

Take a look through the authored CSS file. Remember that it's best practice to serve the live website minified CSS (all non-important whitespace removed), so don't look at that.

That's not intended to be read by humans. Look at the file that they actually created.

If you have an established style guide that was expected to be followed, was it followed?
If not, does it look consistent - as if they had a style guide of their own that they adhere to? Or is it a bit sloppy? Sloppy meaning sometimes there is one space after selectors and sometimes there is none. Some blocks of code are indented and others aren't. Single line CSS is mixed with multi-line CSS with no rhyme or reason.
Clean code is the sign of a respectful developer. One who has a respect for the craft and the work they do.

From Nicolas Gallagher's Idomatic CSS

If these first two test pass, that's great. But still not quite proof the CSS is good.

The Selector Test

Those first two tests could be done by just about anybody, but from here out you'll need to have familiarity with CSS yourself. Start reading the CSS and see if what you're seeing makes sense to you.
Do the selectors look rational? If you see a selector like
.article #comments ul > li > a.button {
  /* Crazy town */
}
I'd worry. That is a developer fighting themselves with specificity problems, something good CSS doesn't do.
How are the class names? Understandable? Hopefully you don't find anything like "bigGray" or "left50" as the accuracy of those will be surely be short lived making for confusing future development.
How repetitive is it? For example, if you see the exact same box-shadow applied in 20 disparate places that's probably a sign of lack of refactoring. Good CSS developers sense patterns like that and accomodate them better.

The Size Test

How is the file size of the deployed CSS? 100k would be absolutely enormous for a CSS file. Small is good. Even (especially) on complex sites. Huge files is often a sign of lack of consistency.

The Editing Test

Come up with a style you'd like to change on the site and attempt to change it yourself. For instance, swap the all the fonts in use with other fonts. Were you able to familiarize yourself with the code quickly? Did it feel like there was a plan for how fonts were handled in place? How quickly were you able to do it? Faster or slower than that kind of thing normally takes you?

How do you do it?

Have you ever been in the position to judge other's CSS? Did you use simliar tests? Did you have more defined metrics?
Judging CSS / Spotting Bad Code is a post from CSS-Tricks

Comentaris

Entrades populars d'aquest blog

Learn Composition from the Photography of Henri Cartier-Bresson

“Do you see it?” This question is a photographic mantra. Myron Barnstone , my mentor, repeats this question every day with the hopes that we do “see it.” This obvious question reminds me that even though I have seen Cartier-Bresson’s prints and read his books, there are major parts of his work which remain hidden from public view. Beneath the surface of perfectly timed snap shots is a design sensibility that is rarely challenged by contemporary photographers. Henri Cartier-Bresson. © Martine Franck Words To Know 1:1.5 Ratio: The 35mm negative measures 36mm x 24mm. Mathematically it can be reduced to a 3:2 ratio. Reduced even further it will be referred to as the 1:1.5 Ratio or the 1.5 Rectangle. Eyes: The frame of an image is created by two vertical lines and two horizontal lines. The intersection of these lines is called an eye. The four corners of a negative can be called the “eyes.” This is extremely important because the diagonals connecting these lines will form the breakdown ...

El meu editor de codi preferit el 2024, que això ja se sap que va canviant 😄

Visual Code Visual Code és un editor de codi font lleuger, però potent que s’executa al teu escriptori i està disponible per a Windows, macOS i Linux. Compta amb suport integrat per a JavaScript, TypeScript i Node.js i té un ric ecosistema d’extensions per a altres llenguatges i entorns d’execució (com C++, C#, Java, Python, PHP, Go, .NET).  És una eina ideal per a desenvolupar i depurar aplicacions web i en el núvol. Per què Visual Code? Visual Code té molts avantatges com a editor de codi font, com per exemple: És gratuït, ràpid i fàcil d’instal·lar i actualitzar. Té un ampli ecosistema d’extensions que et permeten afegir funcionalitats i personalitzar la teva experiència de desenvolupament. Té un suport integrat per a molts llenguatges i entorns d’execució, i et permet depurar i executar el teu codi des del mateix editor. Té una interfície senzilla i elegant, amb diferents temes i modes de visualització. Té un sistema de sincronització de configuracions que et permet guardar les...

Las Mejores Aplicaciones Gratis para iPad de 2012

Las Mejores Aplicaciones Gratis para iPad de 2012 : ¿No tienes ni un duro? No te preocupes, pues hoy os traemos una extensa selección de las mejores apps gratuitas que puedes conseguir en la App Store para que llenes tu iPad de calidad, sin gastar nada de nada.   ¿Estás buscando juegos o apps gratis para tu iPad? En la App Store hay más de 500,000 apps y juegos, y una gran cantidad de ellos está disponible de forma totalmente gratuita. Aquí vamos con la selección de las mejores Apps gratis para iPad (todos los modelos), organizada por categoría. ¿Estás preparado? Las Mejores Apps Gratis de Redes Sociales para iPad Nombre Facebook Gratis Categoría Redes sociales Facebook es la red social más famosa del mundo , con casi mil millones de usuarios. Su app para iPad ha tardado, pero aquí está. Nombre Twitter Gratis Categoría Redes sociales Twitter es la red de microblogging por excelencia. La forma más rápida y directa de informar y mantenerse informado de las cosa...