惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Securelist
C
CERT Recently Published Vulnerability Notes
Forbes - Security
Forbes - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
L
LINUX DO - 最新话题
The Hacker News
The Hacker News
Google Online Security Blog
Google Online Security Blog
SecWiki News
SecWiki News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The Last Watchdog
The Last Watchdog
S
Schneier on Security
T
Troy Hunt's Blog
N
News | PayPal Newsroom
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Schneier on Security
Schneier on Security
P
Privacy & Cybersecurity Law Blog
T
Tor Project blog
T
Threatpost
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
A
Arctic Wolf
S
Secure Thoughts
P
Proofpoint News Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Security Latest
Security Latest
Scott Helme
Scott Helme
Security Archives - TechRepublic
Security Archives - TechRepublic
Latest news
Latest news
PCI Perspectives
PCI Perspectives
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
G
GRAHAM CLULEY
V2EX - 技术
V2EX - 技术
Google DeepMind News
Google DeepMind News
Project Zero
Project Zero
V
Vulnerabilities – Threatpost
T
Threat Research - Cisco Blogs
Webroot Blog
Webroot Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
大猫的无限游戏
大猫的无限游戏
T
Tenable Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Visual Studio Blog
H
Hacker News: Front Page
Simon Willison's Weblog
Simon Willison's Weblog
AWS News Blog
AWS News Blog

CSS Wizardry

Front-End’s Missing Metric: The TBT Window Meet Your Users Where They Are with Obs.js Better Browser Caching with No-Vary-Search font-family Doesn’t Fall Back the Way You Think What Is CSS Containment and How Can I Use It? When All You Can Do Is All or Nothing, Do Nothing Obs.js: Context-Aware Web Performance for Everyone Low- and Mid-Tier Mobile for the Real World (2025) The Fastest Site in the Tour de France Making Sense of the Performance Extensibility API Why Do We Have a Cache-Control Request Header? HTML Is Not a Programming Language… Build for the Web, Build on the Web, Build with the Web Licensing Code on CSS Wizardry A Layered Approach to Speculation Rules Designing (and Evolving) a New Web Performance Score Core Web Vitals Colours The Ultimate Contract Templates for Tech Consultants: Protect Your Business and Get Paid Optimising for High Latency Environments Cache Grab: How Much Are You Leaving on the Table? blocking=render: Why would you do that?! Correctly Configure (Pre) Connections The Three Cs: 🤝 Concatenate, 🗜️ Compress, 🗳️ Cache What Is the Maximum max-age? How to Clear Cache and Cookies on a Customer’s Device The Ultimate Low-Quality Image Placeholder Technique Core Web Vitals for Search Engine Optimisation: What Do We Need to Know? The HTTP/1-liness of HTTP/2 In Defence of DOM­Content­Loaded Site-Speed Topography Remapped Why Not document.write()? Speeding Up Async Snippets Critical CSS? Not So Fast! Measure What You Impact, Not What You Influence Optimising Largest Contentful Paint Measuring Web Performance in Mobile Safari Site-Speed Topography Speed Up Google Fonts Real-World Effectiveness of Brotli Performance Budgets, Pragmatically Lazy Pre-Browsing with Prefetch Making Cloud.typography Fast(er) Time to First Byte: What It Is and How to Improve It Self-Host Your Static Assets Tips for Technical Interviews Cache-Control for Civilians Bandwidth or Latency: When to Optimise for Which ITCSS × Skillshare What If? CSS and Network Performance The Three Types of Performance Testing Image Inconsistencies: How and When Browsers Download Images Identifying, Auditing, and Discussing Third Parties My Digital Music Setup Measuring the Hard-to-Measure Finding Dead CSS The Fallacies of Distributed Computing (Applied to Front-End Performance) Ten Years Old Relative Requirements Airplanes and Ashtrays Performance and Resilience: Stress-Testing Third Parties Refactoring Tunnels Little Things I Like to Do with Git Writing Tidy Code Configuring Git and Vim Base64 Encoding & Performance, Part 2: Gathering Data Base64 Encoding & Performance, Part 1: What’s Up with Base64? Code Smells in CSS Revisited Typography for Developers Moving CSS Wizardry onto HTTPS and HTTP/2 Ack for CSS Developers A New Year, a New Focus Preparing Vim for Apple’s Touch Bar Choosing the Correct Average CSS Shorthand Syntax Considered an Anti-Pattern CSS Wizardry Newsletter Nesting Your BEM? Improving Perceived Performance with Multiple Background Images Continue Normalising Your CSS Pure CSS Content Filter Pragmatic, Practical, and Progressive Theming with Custom Properties Refactoring CSS: The Three I’s Speaker’s Checklist: Before and After Your Talk Improving Your CSS with Parker The Importance of !important: Forcing Immutability in CSS Mixins Better for Performance Managing Typography on Large Apps White October Events Workshop Partnership BEMIT: Taking the BEM Naming Convention a Step Further Travelling Like You Want to, When You Have To Contextual Styling: UI Components, Nesting, and Implementation Detail Subtleties with Self-Chained Classes Cyclomatic Complexity: Logic in CSS Immutable CSS Can CSS Be Too Modular? More Transparent UI Code with Namespaces When to use @extend; when to use a mixin The Specificity Graph CSS Wizardry Ltd.: Year 1 in review CSS Guidelines 2.0.0
Getting to Know a Legacy Codebase
Harry Roberts · 2018-07-21 · via CSS Wizardry

Written by on CSS Wizardry.

Table of Contents

Independent writing is brought to you via my wonderful Supporters.

  1. See What Breaks
  2. Identify Your Entry Points
  3. Get an Idea of What’s Been Changed
  4. Identify (Roughly) What is Used
  5. Learn the Codebase Organically
  6. Set Up Regression Tests
  7. Learn How to Use Parker, ack, and ctags
  8. Git on Top of Things
  9. Reach Out to People
  10. What Next?

The other day, Brad dropped me a message asking me about the topic of getting to know a brand new (specifically CSS) codebase. The kind of codebase that no one person truly understands any more; the kind of codebase that’s had a dozen different contributors over just as many years; the kind of codebase that’s never had a full-scale refactor or overhaul, but that’s grown organically over time and changed with new techniques, styles, and trends.

(Un)fortunately, I’ve dealt with a number of these types of project over time, so I have a few tips about getting to know a codebase like this, and how to begin making sense of it.

See What Breaks

Honestly, to start with, I just identify a part of the codebase (it could be a large or important feature, a frequently-occurring component, something pertinent to current workstreams, etc.) and play around with it: bend it, break it, change it, see what happens to it.

Make a note of anything odd or unusual; if that thing happens again when you play around elsewhere in the codebase, there could be some significance to it.

Identify Your Entry Points

Try as much as you can to build up an idea of which files import which other files; who passes what onto who; what inherits from where; what does your dependency tree look like?

Once I can work out where files are bundled up and imported from, I can build a much better mental model of the topology of the codebase.

Get an Idea of What’s Been Changed

It’s often a good idea to see what files were changed last: any files that haven’t been altered in over a year are probably less important to current work. One (rather cumbersome, I admit) way to see that is with the following:

$ git log --name-only --since="365 days" | sort -u | awk '/\.(le|c|sa|sc)ss$/{print}' | nl

N.B. This output will not be listed chronologically.

This will log your codebase, list the filenames associated with any commits made in the last year, it will then pass them off into sort where we leave only unique entries, before passing it into Awk and printing only .less, .sass, .scss, or .css filepaths, then finally passing it all into nl to number the lines of output.

This should give you a list of all the stylesheets changed in the last year.

Identify (Roughly) What is Used

Chrome’s Coverage tool is a great way to see at a very high-level glance what CSS is being used by the current page. This very non-scientific method of checking code usage should give you an approximate idea of what might be dead code, or what you might simply be able to ignore.

For a slightly more robust, but still imperfect method of identifying dead code, try this technique that I wrote up earlier in 2018.

Although very vague data, it should give some idea as to where to start looking.

Learn the Codebase Organically

Don’t go looking for trouble. Work around learning the features you need to work on immediately. If you never need to touch the help and support section, there’s no point going out of your way to research it. Try to remain focused and learn about the parts of the codebase that are most important and timely.

This is general good advice with any tech debt or refactoring problem: don’t increase your (cognitive) workload unnecessarily.

Set Up Regression Tests

You need to make sure you don’t inadvertently mess anything up while you’re looking around. There are two methods I would use here: the first is very low-fidelity and a little coarse; the second is far more involved.

If I’m playing around with Sass, or any other precompiled assets, I take a checksum of the compiled CSS file before I start playing, and then a checksum after I made any changes. If the two fingerprints differ, I know what whatever changes I just made wound up in the compiled assets. This kind of light-touch smoketest allows me to crudely figure out inputs and outputs. It can also help me to figure out whether a particular variable or mixin might be being used in the project: if I change a variable’s value, but the compiled CSS is identical, I could make a note that that variable might be unused.

harryroberts in ~/Sites/csswizardry.com on (master)
» shasum _includes/css/csswizardry.min.css
7142689d6f79927f192577002f181aef5721d1bc  _includes/css/csswizardry.min.css

harryroberts in ~/Sites/csswizardry.com on (master)
» vim css/_settings.colors.scss

harryroberts in ~/Sites/csswizardry.com on (master)
» shasum _includes/css/csswizardry.min.css
ae2a4c94c945d61f1c009d4fb14c40fa19f20ddd  _includes/css/csswizardry.min.css

Note that the two checksums differ: this means that whatever I changed in css/_settings.colors.scss did make its way into the complied CSS. Although I might not know exactly what or how far reaching that change was, I do at last know that the thing I altered will affect the project.

For longer-term refactoring or auditing, it can be useful to set up a formal regression testing tool such as BackstopJS. This will alert me if I do any severe, customer-facing damage to the UI. Ideally, we’d have a regression testing suite in-place permanently, so this is as good a time as any to start.

Parker is a wonderful static analysis tool from my buddy Katie. You can use it to grab useful information about the overall make-up and health of your CSS: how many IDs, mean specificity, how many !importants, etc. Learn more about Parker with my post all about demystifying its output.

ack is an incredible little command line utility that serves as a more useful, sensible, and user-friendly alternative to grep. You can use it to quickly search entire codebases for specific strings using regex. As an example:

This will search every CSS file for pixel values that contain decimals (e.g. 24.1px). ack is incredibly useful, so I’d recommend reading my Ack for CSS Developers post for some more practical examples.

If you’re feeling adventurous, install ctags. Although a little old school, ctags can be incredibly powerful on all manner of projects. Wikipedia defines ctags as:

…a programming tool that generates an index (or tag) file of names found in source and header files of various programming languages. Depending on the language, functions, variables, class members, macros and so on may be indexed. These tags allow definitions to be quickly and easily located by a text editor or other utility.

Basically, ctags will create a kind of of database of all of the function, variable, mixin, ID, and class definitions for your project. Text editors that support ctags can then use this information to begin linking up disparate parts of the codebase for you, showing you where particular code is originally defined.

For me, as a Vim user, I generate my project’s tags file from the project’s root like so:

This will -Recursively look through my project and make the database of its references. Then, once in my text editor, hitting Ctrl-] with my cursor over—for example—a Sass variable will jump me to the exact place that variable was defined.

Git on Top of Things

Learn some advanced Git logging, showing, and blaming. The entire project’s history is contained here, and if you know how to interrogate it effectively, you’ll be able to build up a really great context of how things came together.

Reach Out to People

Your extensive detective work in Git will no doubt yield a number of different names: if these people are still in the organisation, use them. Ask them to help clarify things; see if they remember the events or decisions surrounding certain features, etc.

What Next?

None of these techniques will allow you to learn the codebase in an afternoon, but if you keep referring back to them, you should find that navigating your way around an existing, sprawling project becomes a little easier.