简体   繁体   English

如何快速分析程序变更的影响?

[英]How to quickly analyse the impact of a program change?

Lately I need to do an impact analysis on changing a DB column definition of a widely used table (like PRODUCT, USER, etc). 最近我需要对更改广泛使用的表(如PRODUCT,USER等)的DB列定义进行影响分析。 I find it is a very time consuming, boring and difficult task. 我发现这是一个非常耗时,乏味和困难的任务。 I would like to ask if there is any known methodology to do so? 我想问一下是否有任何已知的方法可以这样做?

The question also apply to changes on application, file system, search engine, etc. At first, I thought this kind of functional relationship should be pre-documented or some how keep tracked, but then I realize that everything can have changes, it would be impossible to do so. 这个问题也适用于应用程序,文件系统,搜索引擎等的变化。起初,我认为这种功能关系应该预先记录下来或者一些如何跟踪,但后来我意识到一切都可以有变化,它会不可能这样做。

I don't even know what should be tagged to this question, please help. 我甚至不知道该问题应该贴上什么,请帮忙。

Sorry for my poor English. 抱歉我的英语不好。

Sure. 当然。 One can technically at least know what code touches the DB column (reads or writes it), by determining program slices . 通过确定程序切片 ,技术上至少可以知道哪些代码接触DB列(读取或写入)。

Methodology: Find all SQL code elements in your sources. 方法:查找源中的所有SQL代码元素。 Determine which ones touch the column in question. 确定哪些触摸相关列。 (Careful: SELECT ALL may touch your column, so you need to know the schema). (小心:SELECT ALL可能会触及您的列,因此您需要了解架构)。 Determine which variables read or write that column. 确定读取或写入该列的变量。 Follow those variables wherever they go, and determine the code and variables they affect; 随时随地跟踪这些变量,并确定它们影响的代码和变量; follow all those variables too. 也遵循所有这些变量。 (This amounts to computing a forward slice). (这相当于计算前向切片)。 Likewise, find the sources of the variables used to fill the column; 同样,找到用于填充列的变量的来源; follow them back to their code and sources, and follow those variables too. 跟随他们回到他们的代码和来源,并遵循这些变量。 (This amounts to computing a backward slice). (这相当于计算后向切片)。

All the elements of the slice are potentially affecting/affected by a change. 切片的所有元素都可能受到更改的影响/影响。 There may be conditions in the slice-selected code that are clearly outside the conditions expected by your new use case, and you can eliminate that code from consideration. 切片选择的代码中可能存在明显超出新用例预期条件的条件,您可以从中考虑消除该代码。 Everything else in the slices you may have inspect/modify to make your change. 切片中的其他所有内容都可以检查/修改以进行更改。

Now, your change may affect some other code (eg, a new place to use the DB column, or combine the value from the DB column with some other value). 现在,您的更改可能会影响其他一些代码(例如,使用DB列的新位置,或者将DB列中的值与其他值组合)。 You'll want to inspect up and downstream slices on the code you change too. 您还需要检查更改的代码的上行和下游切片。

You can apply this process for any change you might make to the code base, not just DB columns. 您可以将此过程应用于您可能对代码库进行的任何更改,而不仅仅是数据库列。

Manually this is not easy to do in a big code base, and it certainly isn't quick. 手动这在大型代码库中并不容易,而且肯定不是很快。 There is some automation to do for C and C++ code, but not much for other languages. 对C和C ++代码有一些自动化,但对其他语言则不多。

You can get a bad approximation by running test cases that involve you desired variable or action, and inspecting the test coverage. 您可以通过运行涉及所需变量或操作的测试用例并检查测试覆盖率来获得不正确的近似值。 (Your approximation gets better if you run test cases you are sure does NOT cover your desired variable or action, and eliminating all the code it covers). (如果运行测试用例,您的近似值会更好,您确定不会覆盖所需的变量或操作,并且会消除它所涵盖的所有代码)。

Eventually this task cannot be automated or reduced to an algorithm, otherwise there would be a tool to preview refactored changes. 最终,此任务无法自动执行或缩减为算法,否则将会有一个工具来预览重构的更改。 The better you wrote code in the beginning, the easier the task. 你在开始编写代码越好,任务就越容易。

Let me explain how to reach the answer: isolation is the key . 让我解释一下如何找到答案: 隔离是关键 Mapping everything to object properties can help you automate your review. 将所有内容映射到对象属性可以帮助您自动执行审阅。

I can give you an example. 我可以举个例子。 If you can manage to map your specific case to the below, it will save your life. 如果您能设法将您的具体案例映射到下面,它将挽救您的生命。

The OR/M change pattern OR / M改变模式

Like Hibernate or Entity Framework... 像Hibernate或Entity Framework一样......

A change to a database column may be simply previewed by analysing what code uses a certain object's property. 可以通过分析哪些代码使用某个对象的属性来简单地预览对数据库列的更改。 Since all DB columns are mapped to object properties, and assuming no code uses pure SQL , you are good to go for your estimations 由于所有数据库列都映射到对象属性, 并且假设没有代码使用纯SQL ,因此您最好进行估算


This is a very simple pattern for change management. 这是一个非常简单的变更管理模式。

In order to reduce a file system/network or data file issue to the above pattern you need other software patterns implemented . 为了将文件系统/网络或数据文件问题减少到上述模式,您需要实现其他软件模式 I mean, if you can reduce a complex scenario to a change in your objects' properties, you can leverage your IDE to detect the changes for you, including code that needs a slight modification to compile or needs to be rewritten at all. 我的意思是,如果您可以将复杂场景减少到对象属性的更改,您可以利用IDE来检测更改,包括需要稍微修改才能编译或需要重写的代码。

  • If you want to manage a change in a remote service when you initially write your software, wrap that service in an interface . 如果要在最初编写软件时管理远程服务中的更改,请将该服务包装在界面中 So you will only have to modify its implementation 所以你只需要修改它的实现
  • If you want to manage a possible change in a data file format (eg length of field change in positional format, column reordering), write a service that maps that file to object (like using BeanIO parser) 如果要管理数据文件格式的可能更改(例如,位置格式的字段更改长度,列重新排序),请编写将该文件映射到对象的服务(如使用BeanIO解析器)
  • If you want to manage a possible change in file system paths, design your application to use more runtime variables 如果要管理文件系统路径中的可能更改,请将应用程序设计为使用更多运行时变量
  • If you want to manage a possible change in cryptography algorithms, wrap them in services (eg HashService, CryptoService, SignService) 如果要管理加密算法中可能的更改,请将它们包装在服务中(例如HashService,CryptoService,SignService)

If you do the above, your manual requirements review will be easier. 如果您执行上述操作,则您的手动需求审核将变得更加容易。 Because the overall task is manual, but can be aided with automated tools. 因为整体任务是手动的,但可以使用自动化工具辅助。 You can try to change the name of a class's property and see its side effects in the compiler 您可以尝试更改类属性的名称,并在编译器中查看其副作用

Worst case 最差的情况

Obviously if you need to change the name, type and length of a specific column in a database in a software with plain SQL hardcoded and shattered in multiple places around the code, and worse many tables present similar column namings, plus without project documentation (did I write worst case, right?) of a total of 10000+ classes, you have no other way than manually exploring your project, using find tools but not relying on them. 显然,如果您需要更改软件中数据库中特定列的名称,类型和长度,并且在代码周围的多个位置使用纯SQL进行硬编码和破碎,更糟糕的是许多表提供类似的列命名,加上没有项目文档(确实如此)我写了最糟糕的情况,对吧?)总共10000多个类,除了手动探索你的项目之外别无他法,使用查找工具但不依赖它们。

And if you don't have a test plan , which is the document from which you can hope to originate a software test suite , it will be time to make one. 如果您没有测试计划 ,这是您希望从中创建软件测试套件的文档,那么现在是时候制作一个。

Just adding my 2 cents. 加我2美分。 I'm assuming you're working in a production environment so there's got to be some form of unit tests, integration tests and system tests already written. 我假设你在生产环境中工作,所以必须进行某种形式的单元测试,集成测试和系统测试。

If yes, then a good way to validate your changes is to run all these tests again and create any new tests which might be necessary. 如果是,那么验证更改的一种好方法是再次运行所有这些测试并创建可能需要的任何新测试。

And to state the obvious, do not integrate your code changes into the main production code base without running these tests. 显而易见,不要在不运行这些测试的情况下将代码更改集成到主生产代码库中。

Yet again changes which worked fine in a test environment may not work in a production environment. 然而,在测试环境中工作正常的更改可能无法在生产环境中起作用。 Have some form of source code configuration management system like Subversion, GitHub, CVS etc. This enables you to roll back your changes 有一些形式的源代码配置管理系统,如Subversion,GitHub,CVS等。这使您可以回滚您的更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM