问题
What does the term 'poorly factored' and 'refactoring' a program mean? Can you give a simple example to understand the basic difference ?
回答1:
Refactoring is a general technique that can refer to many tasks. It usually means cleaning up code, removing redundancy, improving code quality and readability.
A very simple example of poorly factored code:
do_task1("abc");
do_task2(123);
do_task3(7.43);
...
//100 lines later:
do_task1("abc");
do_task2(123);
do_task3(7.43);
...
//80 lines later:
do_task1("abc");
do_task2(123);
do_task3(7.43);
See how the same set of 3 lines is repeated over and over and over?
Refactoring this code might give:
procedure do_tasks1to3(x,y,z)
do_task1(x);
do_task2(y);
do_task3(z);
end
do_tasks1to3("abc",123,7.43);
...
//100 lines later:
do_tasks1to3("abc",123,7.43);
...
//80 lines later:
do_tasks1to3("abc",123,7.43);
The refactored code makes use of a procedure to perform the repetitive tasks, and if a do_task4
ever needs to be added, it only needs to be done inside the procedure, not in 4 separate places like before.
There are other ways to refactor this, and of course if you ever needed to have variance to the do_taskn
functions this might not work, but this is usually how you'd start...
回答2:
Poorly factored means containing redundancies, or organized in a way that makes core dependencies difficult to see. The term initially comes from math:
Factoring: Finding what to multiply together to get an expression.
There are many ways to factor an expression, just as there are many ways to write a program achieving the same result. As we all know form algebra, finding a suitable factoring can make the whole equation much easier to solve.
回答3:
Poorly factored code is code that has been written in such a way as to be difficult to understand, maintain, reuse, etc.
Refactoring generally means to alter the way a piece of code/module/program is written without actually changing its functionality. Some of the goals of refactoring include increased readability, decreased coupling, and easier code reuse (i.e. fixing what's wrong with poorly factored code.)
回答4:
Poorly factored means it was not well crafted. While refactoring refers to review and improve code.
回答5:
Means of refactoring
回答6:
Refactoring of Code Include:
* Clarity: the meaning of the code should be obvious to anyone reading it. Comments can be refactored as well for adding additional explaination for hairy code, but one should try to make the code itself clear enough to understand
* Eliminate redundant code: refactoring is the time to recognize patterns and factor them out of your code. Similar and redundant classes and code blocks should be combined, and unnecessary functions and variables eliminated.
* Modularity and Scalability: code should be easy to extend or modify, and be able to scale without adding complexity
* Simplicity: Reduce Unnecessary complexity. Complexity should only be considered if it considerably increases readability or modularity ( hence reducing future efforts)
回答7:
Basically making your code more elegant: removing repetition, making code more maintainable, and more readable.
However, it is possible to refactor code too far. Excess amounts of time can be spend cleaning up code, making it more readable, more robust, etc. This can be bad if you are working under tight deadlines for example a week could be spend refactoring code. With the result being a segment of code which may 'functionally' do the same thing, but is only more readable, or looks a little prettier.
I find it best to refactor as I go, the occasional renaming of variables, methods etc and doing the larger methods of refactoring when:
a) I know I'll have enough time. AND
b) By refactoring this section of code, significant functional/ performance benefits will be gained. (Outweighing the time drawback.)
Just my experience.
I have been told by my colleagues to read Clean Code by Robert Martin :- http://www.amazon.co.uk/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882/ref=sr_1_1?s=books&ie=UTF8&qid=1346277262&sr=1-1
回答8:
poorly factored means that it has not undergone enough iterative refactorings to remove bad design patterns (ie. anti-patterns).
In general it means it is just poor code.
来源:https://stackoverflow.com/questions/5600257/factoring-refactoring-a-program