question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

General refactoring

See original GitHub issue

Current Behaviour:

A lot of functions have many if-else branches and the procedures implemented by these branches have too much code in them. This makes readability and testability hard.

For example the branch in this chunk of code should be ported into a function like:

    if checkClassOp(class_path, "GET"):
        items_get_check_support() 
    abort(405)

Into a dedicated module called itemshelpers.py the above functions should be defined by incorporating the current code:

def items_get_check_support(...):
    """ Check if class_type supports GET operation
            try:
                # Try getting the Item based on ID and Class type
                response = crud.get(
                    id_,
                    class_type,
                    api_name=get_api_name(),
                    session=get_session())

                response = finalize_response(class_path, response)
                return set_response_headers(
                    jsonify(hydrafy(response, path=path)))

            except (ClassNotFound, InstanceNotFound) as e:
                error = e.get_HTTP()
                return set_response_headers(jsonify(error.generate()), status_code=error.code)

Each of these helpers functions should be tested separately.

Expected Behaviour:

All functions should perform well-scoped units of work and have their own tests. Handlers in endpoints classes should be much more concise, all the units of work for each branch should be moved into helpers functions.

Main functions should look something like this:

def post(...):
    """ Docstring """
    ... some code ...
    if condition_1:
        run_helper_1(...)
    elif condition_2:
        run_helper_2(...)
    else:
        run_helper_3(...)
    ... rest of the code ...

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
sameshlcommented, Jan 12, 2020

@Mec-iS I would love to work on this. Starting right away!

0reactions
Mec-iScommented, Sep 29, 2020

For now we are good in terms of code clarity. Let’s close this and open a new one for specific cases to refactor.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Code refactoring - Wikipedia
In computer programming and software design, code refactoring is the process of restructuring existing computer code—changing the factoring—without changing ...
Read more >
Refactoring
Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.
Read more >
Refactoring and Design Patterns
Refactoring is a systematic process of improving code without creating new functionality. Refactoring transforms a mess into clean code and simple design.
Read more >
What is Refactoring? - Agile Alliance
Refactoring consists of improving the internal structure of an existing program's source code, while preserving its external behavior.
Read more >
Your Code Is Probably Overdue for Refactoring - Built In
Refactoring is the process of improving code without making any changes to ... said refactoring techniques come down to two general ideas: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found