General refactoring
See original GitHub issueCurrent 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:
- Created 4 years ago
- Reactions:2
- Comments:8 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Mec-iS I would love to work on this. Starting right away!
For now we are good in terms of code clarity. Let’s close this and open a new one for specific cases to refactor.