TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

How to Prepare for Refactoring in Python?

4 点作者 matjazdrolc将近 4 年前
Refactoring is inevitable in any sufficiently complex piece of software.<p>Type system in Java, C++ or C# helps the process immensely. Automated tooling can do most of the heavy lifting there.<p>In Python type hints are optional. Additionally, many libraries are written such that typing is hard (example: Flask&#x27;s g object, or SQLAlchemy&#x27;s models).<p>How to anticipate refactoring in Python and write code that is easy to refactor?<p>Things that worked well so far:<p>- verbose names. Longer variable or function name is less likely to collide with others within a project. Helps to quickly find all places in the project where the variable or function is used.<p>- ending variable names with _type. Example: `current_user`. Searching for `user.last_login_date` will surface `current_user.last_login_date` too.

2 条评论

afarrell将近 4 年前
Automated tests.<p>The books by Harry Percival on this are good.<p><a href="https:&#x2F;&#x2F;www.obeythetestinggoat.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.obeythetestinggoat.com&#x2F;</a>
rajacombinator将近 4 年前
Big fan of the two you mentioned. A couple more to add would be:<p>- Use black to format your code. No exceptions.<p>- Very modular code.<p>- Tests, although this costs time of course. Focus on meaningful rather than trivial ones if needed.<p>- Write comments explaining reasoning for particularly dense pieces of logic.<p>If you practice most of the above, hard to see where you can go wrong.