Taking Inventory of Your Code – A Checklist Aside

Most individuals aren’t excited on the prospect of auditing code, however it’s turn out to be one in all my favourite forms of tasks. A CSS audit is de facto detective work. You begin with a website’s code and dig deeper: you have a look at what number of stylesheets are being known as, how that impacts website efficiency, and the way the CSS itself is written. Your purpose is to search for methods to enhance on what’s there—to sleuth out fixes to make your codebase higher and your website sooner.

Article Continues Beneath

I’ll share recommendations on easy methods to method your personal audit, together with some great benefits of taking a full stock of your CSS and numerous instruments.

Advantages of an audit#section2

An audit lets you set up your code and remove repetition. You don’t write any code throughout an audit; you merely take inventory of what’s there and doc suggestions to move off to a consumer or talk about together with your group. These suggestions guarantee new code received’t repeat previous errors. Let’s take a better have a look at different advantages:

  • Cut back file sizes. An entire overview of the CSS enables you to take the time to seek out methods to refactor the code: to wash it up and maybe lower down on the variety of properties. You can too hunt for any odds and ends, reminiscent of outdated variations of browser prefixes, that aren’t in use anymore. Eliminating unused or pointless code trims down the file folks must obtain after they go to your website.
  • Guarantee consistency with tips. As you audit, create documentation concerning your kinds and what’s taking place with the positioning or software. You possibly can make a proper fashion information, or you can simply write out suggestions to notice how completely different items of your code are used. No matter kind your documentation takes, it’ll save anybody coming onto your group a variety of time and bother, as they’ll simply familiarize themselves together with your website’s CSS and structure.
  • Standardize your code. Code group—which actually attracts differing opinions—is crucial to maintaining your codebase extra maintainable into the long run. As an illustration, if you happen to select to alphabetize your properties, you’ll be able to readily spot duplicates, since you’d find yourself with two units of margin properties proper subsequent to one another. Or chances are you’ll favor to group properties in keeping with their perform: positioning, field model-related, and so forth. Having a system in place helps you guard in opposition to repetition.
  • Enhance efficiency. I’ve saved the most effective for final. Auditing code, together with combining and zipping up stylesheets, results in markedly sooner website speeds. For instance, Harry Roberts, a front-end architect within the UK who conducts common audits, advised me a few website he lately labored on:

    I rebuilt Fasetto.com with a view to enhancing its efficiency; it went from 27 separate stylesheets for a single-page website (primarily UI toolkits like Bootstrap, and so forth.) all the way down to only one stylesheet (which is definitely minified and inlined, to avoid wasting on the HTTP request), which weighs in at simply 5.4 kB post-gzip.

    It is a big win, particularly for folks on slower connections—however everybody features when websites load shortly.

The best way to audit: take stock#section3

Now that audits have received you over, how do you go about doing one? I like to start out with a number of instruments that present an outline of the positioning’s present codebase. It’s possible you’ll method your personal audit in another way, primarily based in your website’s downside areas or your philosophy of the way you write code (whether or not OOCSS or BEM). The essential factor is to bear in mind what will likely be most helpful to you and your personal website.

As soon as I’ve identified my code by instruments, I look at it line by line.


The primary device I attain for is Nicole Sullivan’s invaluable Kind-o-matic, an add-on for Firebug that generates a JSON report of all the sort kinds in use throughout a website. As an added bonus, Kind-o-matic creates a visible report because it runs. By taking a look at each studies, you already know at a look when to mix kind kinds which can be too comparable, eliminating pointless kinds. I’ve discovered that the element of the JSON report makes it simple to see easy methods to create a extra reusable kind system.

Along with Kind-o-matic, I run CSS Lint, a particularly versatile device that flags a variety of potential bugs from lacking fallback colours to shorthand properties for higher efficiency. To make use of CSS Lint, click on the arrow subsequent to the phrase “Lint” and select the choices you need. I wish to test for repeated properties or too many font sizes, so I at all times run Maintainability & Duplication together with Efficiency. CSS Lint then returns suggestions for modifications; some could also be associated to recognized points that may break in older browsers and others could also be finest practices (because the device sees them). CSS Lint isn’t excellent. When you run it leaving each choice checked, you might be certain to see issues in the long run report that you could be not agree with, like warnings for IE6. That stated, this can be a fast solution to get a deal with on the general state of your CSS.

Subsequent, I search by the CSS to evaluation how typically I repeat widespread properties, like float or margin. (When you’re snug with the command line, kind grep together with directions and plug in one thing like grep "float" kinds/kinds.scss to seek out all situations of "float".) Notice any properties chances are you’ll lower or bundle into different modules. Trimming your properties is a balancing act: to scale back the variety of repeated properties, chances are you’ll want so as to add extra lessons to your HTML, in order that’s one thing you’ll have to gauge in keeping with your venture.

I like to do that step by hand, because it forces me to stroll by the CSS by myself, which in flip helps me higher perceive what’s occurring. However if you happen to’re brief on time, or if you happen to’re not but snug with the command line, instruments can easy the way in which:

  • CSS Dig is an automatic script that runs by all your code that can assist you see it visually. The same device is StyleStats, the place you kind in a url to survey its CSS.
  • CSS Colorguard is a brand-new device that runs on Node and outputs a report primarily based in your colours, so you already know if any colours are too alike. This helps restrict your coloration palette, making it simpler to keep up sooner or later.
  • Mud-Me Selectors is an add-on for Firebug in Firefox that finds unused selectors.

Line by line#section5

After you run your instruments, take the time to learn by the CSS; it’s price it to get an actual sense of what’s taking place. As an illustration, feedback within the code—that instruments miss—could clarify why some quirk persists.

One massive factor I double-check is the depth of applicability, or how far down an attribute string applies. Does your CSS depend on a variety of specificity? Are you seeing lengthy strings of selectors, both within the fashion recordsdata themselves or within the output from a preprocessor? A excessive depth of applicability means your code would require a really particular HTML construction for kinds to work. When you can scale it again, you’ll get extra reusable code and speedier efficiency.

Evaluation and advocate#section6

Now to the enjoyable half. Upon getting all of your information, you’ll be able to work out easy methods to enhance the CSS and make some suggestions.

The advice doc doesn’t must be closely designed or formatted, however it ought to be simple to learn. Splitting it into two components is a good suggestion. The primary consists of your evaluation, itemizing the belongings you’ve discovered. When you confer with the outcomes of CSS Lint or Kind-o-matic, you should definitely embrace both screenshots or the JSON report itself as an attachment. The second half incorporates your actionable suggestions to enhance the code. This may be so simple as a listing, with objects like “Consolidate kind kinds which can be carefully associated and create mixins to be used sitewide.”

As you analyze all the data you’ve collected, search for areas the place you’ll be able to:

  • Tighten code. Do you’ve gotten 4 completely different units of kinds for a call-out field, a number of comparable hyperlink kinds, or means too many exceptions to your commonplace grid? These are nice candidates for repeatable modular kinds. To make consolidation even simpler, you can use a preprocessor like Sass to show them into mixins or prolong, permitting kinds to be utilized while you name them on a category. (Simply test that the outputted code is smart too.)
  • Maintain code constant. A great audit makes positive the code adheres to its personal philosophy. In case your CSS is written primarily based on a selected method, reminiscent of BEM or OOCSS, is it constant? Or do kinds veer sometimes, and are there acceptable deviations? Be sure you doc these exceptions, so others in your group are conscious.

When you’re working with a consumer, it’s additionally essential to clarify the approaches you prefer, in order that they perceive the place you’re coming from—and what issues chances are you’ll contemplate as points with the code. For instance, I favor OOCSS, so I are likely to push for extra modularity and reusability; a number of lessons stacked up (if you happen to aren’t utilizing a preprocessor) don’t trouble me. Ensuring your consumer understands the context of your work is especially essential while you’re not on the implementation group.

Hand off to the consumer#section7

You probably did it! When you’ve written your suggestions (and brought a while to assume on them and guarantee they’re stable), you’ll be able to hand them off to the consumer—be ready for any questions they could have. If that is on your group, congratulations: get cracking in your checklist.

However wait—an audit has much more rewards. Now that you simply’ve received this prime documentation, take it a step additional: use it because the springboard to speak about easy methods to preserve your CSS going ahead. If the identical points stored popping up all through your code, doc the way you solved them, so everybody is aware of easy methods to proceed sooner or later when creating new options or sections. It’s possible you’ll flip this doc into a mode information. One other factor to think about is how typically to revisit your audit to make sure your codebase stays squeaky clear. The timing will range by group and venture, however set a practical, common schedule—this a key a part of the auditing course of.

Conducting an audit is an important first step to maintaining your CSS lean and imply. It additionally helps your documentation keep updated, permitting your group to have deal with on easy methods to transfer ahead with new options. When your code is structured properly, it’s extra performant—and everybody advantages. So discover the time, seize your finest sleuthing hat, and get began.

Leave a Comment