https://bugs.documentfoundation.org/show_bug.cgi?id=155628
Bug ID: 155628
Summary: Split huge complex functions into multiple functions
with less complexity
Product: LibreOffice
Version: 7.6.0.0 alpha1+ Master
Hardware: All
OS: All
Status: UNCONFIRMED
Severity: normal
Priority: medium
Component: LibreOffice
Assignee: [email protected]
Reporter: [email protected]
Description
Some of the LibreOffice modules have functions that contain hundreds of lines
of code. They are complex, hard to understand, change and maintain. This is the
task to find such complex functions, and split them into multiple functions
according to the different logical parts of work.
Rationale
Huge functions, with Line of code (LOC) > 100 are hard to work with. There are
measures of complexity beyond line of code, like having conditions, loops,
gotos, and nested functions. To make the life of the developers a little
easier, breaking the complex functions into multiple functions can be helpful.
Finding complex functions
There are many tools that can help doing this. For example, clang-tidy has a
built-in check (since 2017) that can rate functions according to a complexity
metric, and report those that are more complex than a certain threshold:
clang-tidy - readability-function-cognitive-complexity
https://clang.llvm.org/extra/clang-tidy/checks/readability/function-cognitive-complexity.html
For example, invoke this command, shows 4 functions with the complexity measure
of > 100. The most complex one is "void SwTabFrame::MakeAll(vcl::RenderContext*
pRenderContext)" with the complexity of 645.
clang-tidy --config="{Checks: '-*,
readability-function-cognitive-complexity',CheckOptions: [{key:
readability-function-cognitive-complexity.Threshold, value: 100},{key:
readability-function-cognitive-complexity.DescribeBasicIncrements, value:
false} ]}" sw/source/core/layout/tabfrm.cxx
You can use this tool via command line, or via an IDE. For example, Qt Creator
is accompanied with this tool, and it also provides a GUI tool for using it on
a certain folder of the source code. You can find the clang-tidy binary in:
/opt/qtcreator-10.0.0/libexec/qtcreator/clang/bin/clang-tidy
Changing the code
To break the function into multiple functions, the best would be doing the
refactoring in a way that:
1. One logical unit of work is done in each function.
2. Sane names are used for each function that describe the their functionality
3. The parameter passing is minimized
4. Clean coding principles are met
5. The resulting functions should be short (less than ~50-100 LOC), with much
lower complexity
Important notes:
1. You have to make sure that the new code has the same functionality as the
old one, and it does not cause performance impacts. Of course you are free to
fix some bugs later. :-)
2. Work on only part of a single module, because there can be many results.
Running the above command on sw/source/core/layout/*.cxx gives multiple
results.
3. Changing the largest functions can be the most useful thing to do, but it
will not be easy. So, you may start from the somehow less complex ones.
--
You are receiving this mail because:
You are the assignee for the bug.