0
How to improve code readability
Hi Below code works fine but I feel it is not fully readable. Get details does not communicate what details until and unless someone read through function code. C++ has pass by reference and I could have passed last to argument as id and type to make function signature more readable. But python do not have this provision. Is there any other option to make this code more readable? https://sololearn.com/compiler-playground/cPzD3ojnzq96/?ref=app
10 Réponses
+ 4
It reads quite ok to me. Maybe some comments could help?
+ 3
Your code is quite readable and quite easy to understand.
There is such a weird trend today about constantly making code more clean, but sometimes making code more clean only makes it more complex and unreadable.
You are doing just fine with your small example.
+ 3
coming from C++, you have the instinct to just mutate your data. You end up using list all over your code.
what you want is to make the intent of your data more obvious.
if you want mutable named properties, @dataclass is a decorator for classes used primarily to encapsulate data.
Other options are dictionaries, or namedtuples, if you feel lists are too generic ...
https://sololearn.com/compiler-playground/ckSWiO6Mc1qa/?ref=app
+ 3
+ 2
I think you followed the pep convention for writing source codes. The only thing that goes off is the space between : and their respective annotations
x : str (makes ugly code)
x: str (pep 8)
x:str, (frown at but okay)
+ 1
By using comments
+ 1
Comments are easy. In every programming languages we can use comments to improve code readability
0
I dont know, comment more?
0
To make your code more readable, you can start by giving your function a more descriptive name, like get_user_details instead of just get_details, to clarify what it fetches. You can also use keyword arguments in the function signature, which helps indicate the purpose of each parameter when calling the function. For example, get_details(id=123, type='employee') makes it clearer. Another option is to pass a dictionary or named tuple instead of multiple arguments, grouping related data together. For example, using a named tuple like Details = namedtuple('Details', ['id', 'type']) makes it self-explanatory. Lastly, adding a docstring inside the function can describe its purpose and parameters, which helps others understand its function without reading the entire code.
also you can add comments into your code.