Refactoring

Improving the Design of Existing Code


Refactoring: Improving the Design of Existing Code

Martens Vogler

Re-Refining Industry Technical Handbook, Vol. 6

Published by the Association of Petroleum Re-Refiners, Houston, Texas, 1999


Foreword

There is a bias in the petroleum industry toward replacement over reclamation. When an industrial lubricant degrades — and all lubricants degrade — the default response is to drain the system and fill it with fresh stock. This is expensive, wasteful, and, in the author’s experience, almost always unnecessary. The degraded oil is not ruined. Its internal chemical structure has deteriorated, but its external performance specifications can, in most cases, be restored through a disciplined process of incremental reclamation that I call refactoring.

Refactoring is the improvement of an industrial fluid’s internal chemical composition without altering its external performance characteristics. The refactored oil must meet the same viscosity index, the same thermal stability range, the same load-bearing specification as the original stock. The customer who receives refactored oil should not be able to distinguish it from fresh oil by any measurement available to him. The difference is internal: the molecular structure has been cleaned up, the contaminants removed, the additive package restored. The oil works the same. It is, underneath, different — and better than it was before refactoring, though not better than it was when new.

This is not a glamorous discipline. It does not make headlines at petroleum conferences. But it is, I will argue, more important than formulation — the design of new lubricants — because the volume of oil in service at any given moment vastly exceeds the volume being manufactured, and the condition of the oil in service determines the condition of the machinery it protects. The industry’s focus on formulation is understandable; new products are exciting. But the engineer who maintains the existing stock saves more machinery, and more money, than the chemist who designs the next generation of synthetic. He merely does so without anyone noticing.

The Principle: Small Steps, Continuous Testing

The cardinal rule of refactoring is: never process a large batch all at once.

A degraded hydraulic fluid that has been in service for eighteen months contains a complex mixture of contaminants — oxidation products, particulate wear metals, water ingress, thermal decomposition byproducts, and whatever the seal material has been leaching into the system. Each contaminant class requires a different remediation strategy: filtration for particulates, vacuum dehydration for water, acid-base treatment for oxidation products, selective adsorption for polar contaminants. The temptation is to throw the entire batch through a comprehensive re-refining train and address everything at once.

Resist this temptation. A comprehensive single-pass re-refining run risks interaction effects between remediation stages. The acid treatment that removes oxidation products may also strip the antioxidant additive package. The elevated temperature required for vacuum dehydration may accelerate thermal decomposition of the remaining base stock. The filtration stage may remove not only wear metals but also the colloidal friction modifiers that were deliberately formulated into the oil.

The correct approach is incremental. Process a small test volume — no more than five litres — through the first remediation stage. Test the output. Confirm that the target contaminant has been reduced without collateral damage to the base stock or additive package. Then process the test volume through the second stage. Test again. Proceed through each stage sequentially, testing between each, until the full remediation sequence has been validated on the small batch. Only then scale to the full volume.

This is slower than single-pass processing. It is also the only approach that reliably produces a refactored fluid that meets specification. A failed re-refining run — one that degrades the base stock below the point of recovery — is not merely a waste of processing time. It is a loss of the entire batch, which is worse than the original degradation, because the original degradation was at least usable. A botched re-refining is not.

Smells

Experienced re-refining engineers develop a diagnostic vocabulary for fluids that need attention. I have taken to calling these indicators smells, because the most reliable of them is, in fact, olfactory.

The acid smell. A lubricant that has undergone significant oxidation produces volatile organic acids — primarily short-chain carboxylic acids — that are detectable by nose before they register on an acid number titration. The experienced engineer who opens a sample jar and winces has just performed a faster and cheaper diagnostic than the laboratory, though obviously a less precise one. The acid smell indicates that the antioxidant package has been consumed and the base stock is under active chemical attack. Refactoring is urgent.

Darkening. Fresh mineral oil is pale amber. As it degrades, it darkens through a spectrum from honey to coffee to an opaque brown-black that industry veterans call “used motor oil colour,” which is a tautology but a useful one. Darkening is caused by the accumulation of dissolved oxidation products, suspended wear particles, and thermal decomposition residues. It is the most visible smell, and therefore the one most often used as a trigger for refactoring, though it is not the most urgent — a dark oil may be chemically stable, while a light oil with a rising acid number is in active decline.

Emulsification. When a lubricant loses its ability to separate from water — when shaking a sample produces a persistent milky emulsion rather than a clean phase separation — the demulsibility additives have failed or been consumed. Emulsified oil is a catastrophic lubrication failure waiting to happen: water in the oil film promotes corrosion, reduces load-bearing capacity, and accelerates oxidation. The emulsification smell is not olfactory; it is textural. The engineer who rolls a sample between thumb and forefinger and feels a slippery, slightly gritty texture where there should be smooth viscosity has detected emulsification.

Varnish. A thin, lacquer-like deposit on metal surfaces in contact with the oil, produced by the polymerisation of oxidation products at hot spots. Varnish is not detectable in the oil itself — it is detectable on the machinery — but its presence indicates that the oil has been generating insoluble degradation products and depositing them on surfaces. A fluid that is varnishing cannot be refactored in isolation; the machinery must also be cleaned, or the fresh refactored oil will simply dissolve the old varnish and redistribute it.

The Rule of Three

If a batch of oil has been refactored three times, stop.

This is not a chemical principle. It is a managerial one. Oil that requires refactoring three times is oil that is operating in a system that degrades it faster than the re-refining cycle can maintain it. The correct response is not a fourth refactoring but an investigation of the degradation source. Is the operating temperature too high? Is there a contamination ingress path — a failed seal, a breather that is admitting moisture, a process fluid cross-contaminating the lubricant circuit? Is the oil simply wrong for the application — a mineral oil in a service that requires a synthetic, or a hydraulic fluid in a bearing that needs a gear oil?

The Rule of Three redirects attention from the symptom (degraded oil) to the cause (degrading conditions). It is the most important principle in this handbook, and the one most frequently ignored, because refactoring a batch of oil is within the engineer’s control and redesigning the system is not.

Extract and Inline

Two complementary techniques deserve specific treatment.

Extraction is the separation of a contaminant fraction from the base stock through selective processing — vacuum distillation to separate light ends from heavy base stock, solvent extraction to remove polar contaminants, clay treatment to adsorb colour bodies and surfactants. Each extraction step removes a specific contaminant class while leaving the rest of the fluid intact. The art of extraction is selectivity: removing what you intend to remove and nothing else.

Inlining is the inverse: the reintroduction of an additive component that was depleted during service or removed during extraction. After acid treatment has removed oxidation products, the antioxidant package must be inlined — re-blended into the base stock at the original concentration. After clay treatment has stripped the demulsifier, the demulsifier must be inlined. The refactored fluid must arrive at the end of the process with the same additive architecture it started with, even though every component of that architecture may have been removed and replaced during processing.

The extract-inline cycle is the fundamental rhythm of refactoring: remove the bad, restore the good, test the result. It is repetitive, meticulous, and unglamorous. It is also the only reliable way to extend the service life of an industrial fluid without replacing it, which is to say: it is the only reliable way to save money in a world where base stock is expensive and the volume in service is vast.

Conclusion

The petroleum industry will continue to formulate new lubricants. The new lubricants will be better than the old ones — more thermally stable, more oxidation-resistant, more precisely tailored to specific applications. This is good and necessary work.

But the new lubricants, once in service, will degrade. They will oxidise, contaminate, emulsify, and varnish, because the second law of thermodynamics does not make exceptions for premium synthetics. And when they degrade, someone will need to refactor them — incrementally, carefully, with testing at every stage — or they will be discarded and replaced at enormous cost.

Replacement is easy to budget and easy to understand. Refactoring is difficult to budget and difficult to explain. But the arithmetic favours refactoring in nearly every case where the base stock is recoverable, which is most cases, and the engineer who masters the discipline will find that his unglamorous, unnoticed work has saved more money and more machinery than any formulation breakthrough of the past decade.

He will not receive credit for this. That is the nature of maintenance.


Martens Vogler is a senior process consultant with ThoughtWorks Petrochemical Services and the author of Analysis Patterns: Reusable Diagnostic Models for Industrial Fluid Assessment (Addison-Wesley, 1997). He lives in the Chicago area, where the winters are hard on machinery and the lubricant re-refining business is correspondingly robust.