您如何从主代码中完全分离代码以实现向后兼容性? [英] How do you cleanly separate code for backwards compatibility from the main code?

查看:63
本文介绍了您如何从主代码中完全分离代码以实现向后兼容性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对人们想出什么策略来将保持向后兼容性所需的所有繁琐逻辑与应用程序的主要代码分离开来感兴趣.换句话说,可以使您更接近使代码看起来像没有向后兼容性问题的策略,只是为该任务使用了单独的隔离源文件.

I'm interested in what strategies people have come up with for separating all the crufty logic that's necessary to maintain backwards compatibility from the main code of an application. In other words, strategies that let you come closer to having your code look as if there were no backwards compatibility concerns except for separate isolated source files distinctly for that task.

例如,如果您的应用程序读取一种特定的文件格式,而不是一个巨大的鸣叫文件解析功能,则可以让您的代码首先迭代怪癖"条目/对象的列表,其中每个怪癖都会检查该文件以查看是否这是一个文件,它将应用于该文件,如果是这样,则调用它自己的解析逻辑,而不是普通的大小写逻辑.

For example, if your application reads a particular file format, instead of one giant honking file parsing function, you could have your code first iterate a list of "quirks" entries/objects, where each quirk checks the file to see if it's a file it would apply to, and if so invokes its own parsing logic instead of the normal case logic.

Quirks是一种不错的策略,但是您必须做一些工作才能在应用程序中的所有适当位置插入用于进行怪癖检查的钩子,并且对于不同的怪癖类型,检查的外观将有所不同,等等.应该有专门用于此任务的样板库.另一个问题是如何强制不将怪癖作为通用钩到应用程序任意块中的行为.

Quirks is an OK strategy but you have to do work to put hooks in for quirks checks at all the appropriate places in your app, and what the checks will look like will vary for different quirk types, etc. It almost seems like there should be libraries dedicated to the boilerplate for this task. Another issue is how to enforce that quirks aren't abused as general purpose hooks into arbitrary chunks of the app.

推荐答案

我通常的策略是单独进行一些操作,以将向后兼容性输入转换为新的实现输入,然后将新的实现代码与转换后的数据一起使用.

My usual strategy is to have something separate that will translate the backward compatibility input into the new implementation input, and then use the new implementation code with this translated data.

这篇关于您如何从主代码中完全分离代码以实现向后兼容性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆