逃避大型功能 [英] escaping a large function

查看:54
本文介绍了逃避大型功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大型程序,该程序包含在do ... while循环中.它取决于某个变量是否小于一定数量.

这就是要抓住的地方:变量位于do ... while循环内的for循环内的for循环内.

是的,也许我应该更改布局(不完全确定如何更改).

为了救我,有人知道我可以访问此变量或以某种方式转义整个序列的方法吗?最终,该程序只是另一个程序的开始...所以我不能说停止一切".

如果有办法,非常感谢,否则为您的忙碌而感到抱歉!祝您有美好的一天:-)

I have a large program which is encompassed by a do... while loop. It is dependent on a variable going under a certain number.

Here''s the catch: The variable is found inside a for loop inside a for loop inside the do...while loop.

yeah, maybe I should change my layout (not entirely sure how).

To save me, does anybody know of a way I could access this variable or somehow escape the entire sequence? eventually this program will be just the start of another... so I can''t say "stop everything".

Many thanks if there is a way, else sorry for the hastle! Have a nice day :-)

推荐答案

您的问题可以通过多种方式解决.首先,您应该注意到您的编码风格并不正确,您可能正在编写意大利面条代码 [ ^ ].这不是问题,特别是如果您是初学者,因为我敢肯定每个人都从意大利面开始.问题在于您是否保持这种风格,因为有些人倾向于这样做是因为愚蠢或懒惰.意大利面条通常是错误的代码设计的结果,而错误的代码设计是错误的或没有问题的结果.关注点分离 [http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html [
Your problem can be solved in several ways. The first you should notice that your coding style isn''t alright, you are probably writing Spaghetti code[^]. That''s not a problem especially if you are a beginner because I''m pretty sure everyone starts with spaghetti. The problem is if you keep that style because some people tend to do so either becuase of stupidity or lazyness. Spaghetti is usually a result of bad code design that is the result of bad or no Separation of concerns[^]. You are trying to implement of lot of things (or everything) inside one function instead of breaking that stuff into several functions or classes. In general, if your function spans vertically more than what you can read on your monitor on "one page" then your code is spaghetti and needs splitting into several functions or classes. The solution to the problem of breaking out more than one loop is missing from C/C++, in some other languages its present, for example in java. If you take a look at the following page and search for the "search:" label on it then you will see a java snippet that breaks out from 2 loops: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html[^]. However in C/C++ you can not do that but you can achieve similar behaviour. One of them is using an exception what I don''t recommend in C++ and is not available in C. The other solution is putting the whole spaghetti code with all loops into a separated function and then instead of breaking out from the loop you return from the function call where you want to break from several loops. This way your program doesn''t exit, just returns from a function. Putting things to a separate function call is a good solution to a lot of problems in C.

Example:
// an ugly spaghetti solution of breaking out from 2 loops but sometimes it does the job
bool stop = false;
for (int i=0; i<5; ++i)
{
    for (int j=0; j<5; ++j)
    {
        printf("%d, %d\n", i, j);
        if (i==3 && j==3)
        {
            stop = true;
            break;
        }
        if (stop)
            break;
    }
}

// the function + return solution
void perform_loops()
{
    for (int i=0; i<5; ++i)
    {
        for (int j=0; j<5; ++j)
        {
            printf("%d, %d\n", i, j);
            if (i==3 && j==3)
                return;
        }
    }
}

// calling the function
perform_loops();



如果您有一个丑陋的函数,并且带有很多长代码的局部变量,那么最好将以下步骤作为重构的第一步:
1.创建一个空类,并将函数的局部变量作为成员变量放入空类中. 2.将整个原始函数代码放入类中,以便使用类的成员变量而不是局部变量.
3.将类中的方法分为几个方法,这很容易,因为类的每个方法都可以访问该类的每个成员变量.
4.现在,您在类中有几个通过成员变量相互依赖的方法(变量初始化顺序,调用顺序),因此您的下一个任务是使该依赖关系更清洁-比起您最初遇到的原始问题,解决起来要容易得多(色情/怪兽功能).在此期间,您可能会发现某些方法仅使用该类的某些成员变量,因此您可以使用所有使用这些成员变量的方法将这些成员变量拆分为另一个类,然后可以使用第一个中的第二个类.

现在您有了新的一个或多个类,原始的丑陋函数如下所示:



If you have a big ugly function with a lot of local variables with long code then its the best to do the following as a first step of refactorization:
1. Create an empty class and put the local variables of the function into the empty class as member variables.
2. Put the whole original function code into the class so that it uses the member variables of the class instead of local variables.
3. Split the method in the class into several methods, its quite easy since every method of the class can access every member variable of the class.
4. Now you have several methods in the class that depend on each other via member variables (variable initialization order, call order) so your next task is to make that dependency cleaner - this is much easier to solve than the original problem you started from (spaghetty/monster func). During this you might find out that some methods use only some of the member variables of the class so you can split those member variables into another class with all the methods that use those member variables and then you can use the second class from the first one.

Now that you have your new class (or classes) your original ugly function looks like the following:

int previously_ugly_func(params)
{
    MyNewPrettyClass c;
    return c.DoWork(params);
}


您可以直接使用新的类来解决与原始的丑陋函数相同的问题,而不必调用原始的丑陋函数.


Instead of calling the original ugly func you can directly use the new class that solves the same problem you original ugly func solved.


简单,请使用另一个变量,该变量可以存储所需变量的值.
在您的特定点上使用标签,并使用goto statemnt到达那里

easy, use another variable which can store the value of your desired variable.
use label on your specific point and use goto statemnt to get there

int x;
for (;;)
for(;;)
for(;;)
{
 int y=123;
 if(y==123){ x=y; goto mylabel;}
}

mylabel:
cout << x;



goto不是一个好方法,但是有它的优点,当您不想使用return语句并且想要进行跳转时,只是goto可以在函数中的任何地方跳转,这使其变得很危险....

ps:如果您不想离开序列,而只想查看变量,则可以从if子句中删除goto语句.如果它不仅是您想要的值,还可以使用指针类型来访问变量.



goto is not a good approach but has its advantages, when you dont want to use return statement and do want make a jump, just that goto can jump anywhere in a function which makes it dangerous....

ps: if you dont want to leave your sequence but just want to see your variable than remove goto statement from if clause. if it is not just a value you want, you can also use a pointer type to access your variable.


您的编程风格是错误的.学习最佳编程实践.您需要采用良好的编程风格.如果您的程序变得过于庞大而难以维护,则意味着您的程序不可调用.需要重构.您必须在单独的函数中移动一些代码,您将立即避开许多问题.使用调试器查看您的程序在做什么,变量在做什么.仅仅移动和分离功能是不够的.在这种情况下,您将需要OOP.
Your programming style is wrong. Study best programming practices. You need to adopt a good programming style. If your program is hard to maintain as it becomes overgrown, this means your program is not scallable. Need to refactor. You have to move some piece of code in separate functions, and you instantly will escape many of your problems. Use debugger to see what your program is doing, and what your variables are. Even moving and separating functionalties is not enough. You will need OOP in this case.


这篇关于逃避大型功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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