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

8th Mar 2025, 5:33 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
10 Réponses
+ 4
It reads quite ok to me. Maybe some comments could help?
8th Mar 2025, 7:54 AM
Ausgrindtube
Ausgrindtube - avatar
+ 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.
8th Mar 2025, 9:30 AM
Jan
Jan - avatar
+ 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
8th Mar 2025, 9:39 AM
Bob_Li
Bob_Li - avatar
8th Mar 2025, 5:42 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 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)
8th Mar 2025, 11:44 AM
Badgirl MIRI
Badgirl MIRI - avatar
+ 1
By using comments
9th Mar 2025, 2:02 PM
𝑺𝒍𝒆𝒆𝒑𝒚𝑲𝒊𝒘𝒊☕︎︎
𝑺𝒍𝒆𝒆𝒑𝒚𝑲𝒊𝒘𝒊☕︎︎ - avatar
+ 1
Comments are easy. In every programming languages we can use comments to improve code readability
10th Mar 2025, 10:20 AM
𝑺𝒍𝒆𝒆𝒑𝒚𝑲𝒊𝒘𝒊☕︎︎
𝑺𝒍𝒆𝒆𝒑𝒚𝑲𝒊𝒘𝒊☕︎︎ - avatar
0
I dont know, comment more?
9th Mar 2025, 3:13 PM
Biscuits
Biscuits - avatar
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.
25th Mar 2025, 6:50 AM
james alery
james alery - avatar