zedr / clean-code-python Goto Github PK
View Code? Open in Web Editor NEW:bathtub: Clean Code concepts adapted for Python
License: MIT License
:bathtub: Clean Code concepts adapted for Python
License: MIT License
The following links are not working and the concepts are also not available. Is it under progress?
Great repo! Very helpful resources here. Can we have a french translation for this?
Hello @zedr!, I learned a lot from this repo, so I translated this repo to korean.
Can you please add my translated repo link to your repo?
Thank you for making a great document😊
class Menu: def __init__(self, config: dict): title = config["title"] body = config["body"] # ... menu = Menu( { "title": "My Menu", "body": "Something about my menu", "button_text": "OK", "cancellable": False } )
From my experience, using a dictionary as a structure for holding data can lead to a big mess, when you have several complex structures like that. It is not typed checked and can't be annotated. I would use TypedDict instead.
Also, I think, it is not completely clear, how it solves the stated problem. The function still has the same amount of parameters. I think the more proper solution is to refactor the function/class into several smaller functions/classes and compose them (e.g. make_menu_content, make_button, etc.).
After studying this repository, I decided to translate it into Persian, is there any problem?
I am surprised to see this example listed as "Good".
class MenuConfig:
"""A configuration for the Menu.
Attributes:
title: The title of the Menu.
body: The body of the Menu.
button_text: The text for the button label.
cancellable: Can it be cancelled?
"""
title: str
body: str
button_text: str
cancellable: bool = False
def create_menu(config: MenuConfig):
title = config.title
body = config.body
# ...
config = MenuConfig
config.title = "My delicious menu"
config.body = "A description of the various items on the menu"
config.button_text = "Order now!"
# The instance attribute overrides the default class attribute.
config.cancellable = True
create_menu(config)
I think there might be some misunderstand of how classes work in Python or there's perhaps a typo yielding a confusion on instance vs class.
Any key:type
annotation on a class gets added to __annotations__
. If the key:type
has a default then it will be set as the default class variable. Any setter
on the class will set any value (e.g., config.dragons = 34
). I believe this practice should be discouraged.
Here's an example:
In [13]: class Record:
...: alpha:int
...: beta:int
...: gamma:int = 1234
...:
...:
In [14]: r = Record
In [15]: r.alpha
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-15-b43492ebc57e> in <module>()
----> 1 r.alpha
AttributeError: type object 'Record' has no attribute 'alpha'
In [16]: r.__annotations__
Out[16]: {'alpha': int, 'beta': int, 'gamma': int}
In [17]: r.gamma
Out[17]: 1234
In [18]: r.dragons = 27
In [19]: r.more_dragons = 48
I would suggest that this Menu example should use an instance, not class, or the example should be marked as "Bad" in the text.
Hi! Thanks so much for your efforts, well done, excited for more!
One thought...
you have used the Text
typing instead of str
in your functions. Take a look at this link.
I think it's better to use str
instead of Text
unless we want backward compatibility for python2 code.
hi,
I would have expected, that in the ‘good’ example, the function will take a parameter of type ‘EchoIf’
You could have showed that there is an ‘Echo’ class with the actual implementation, and an ‘EchoIf’ interface class that abstracts the ‘write’ method.
this is basically the whole point, your function depends on an abstraction. it is clear that in order for that function to work, it is required an object that implements the ‘write’ method.
as it is done now, the function hides a dependency on the Echo class. you are building the object inside the function, instead of delegating this responsibility to a different function.
Also, same function builds ‘rows’ object with a default value, a thing that you have said in previous example that should be done using parameters with default values.
Hi guys! How about translating this project to other languages? I believe I can contribute with the PT-BR translation 👍
Greatly appreciate your documents here !
Are there any plans for completing the rest of the article , i.e. https://github.com/zedr/clean-code-python#objects-and-data-structures and https://github.com/zedr/clean-code-python#classes ?
Thank you in advance!
In the section "Function arguments (2 or fewer ideally)" it says:
"Zero arguments is the ideal case. One or two arguments is ok, and three should be avoided."
However, I think it's typically best to have at least 1 argument, as most functions with zero arguments aren't pure functions and aren't easily testable
Personally I prefer the wording in Clean Code JavaScript:
"One or two arguments is the ideal case, and three should be avoided if possible" (https://github.com/ryanmcdermott/clean-code-javascript#function-arguments-2-or-fewer-ideally)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.