禁用部分代码的简单方法 [英] Simple ways to disable parts of code
问题描述
这不是解决特定问题的典型问题,而是脑力锻炼,但我想知道是否有人有解决方案.
This isn't a typical question to solve a specific problem, it's rather a brain exercise, but I wonder if anyone has a solution.
在开发中,我们通常需要禁用或切换代码的某些部分以检查不同的方法.为此,我们使用注释或 #defines
,但我最喜欢的是:
In development we often need to disable or switch some parts of code to check different approaches. To do this we use comments or #defines
, but my favorite is:
//*
[code here]
//*/
现在,仅删除第一个斜杠时,代码将被注释掉.
Now when you remove just the first slash the code will become commented-out.
问题:有什么方法可以实现类似的 if-else 代码切换?我试图找到它,但是我总是遇到一些问题,找不到可行的解决方案.
The question: is there any way to implement similar if-else code switch? I tried to find it but I always had some problem and couldn't find a working solution.
也许您知道任何类似的把戏吗?
And maybe do you know any similar tricks?
推荐答案
我不确定是否应该发布此内容,因为它不是我认为的好代码",但是我承认使用了以下技术当我刚签出某项内容时,一种可以在两个小代码段之间快速切换的快速n肮脏的方式:
I'm not sure I should post this because it's not something I think is 'good code', but I'll admit to having used the following technique as a quick-n-dirty way to be able to quickly switch between two small snippets of code when I'm just checking something out:
// in the following , foo() is active:
/**/ foo(); /*/ bar(); /**/
现在只需删除前面的星号之一
Now just remove one of the asterisks at the front:
// now bar() is active:
/*/ foo(); /*/ bar(); /**/
当然,这绝不应该超过仅仅检查一下内容"阶段...
Of course, this should never make it past the "just checking things out" phase...
这篇关于禁用部分代码的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!