提高 Rails 开发者编码效率的实用小贴士(Practical Rails Dev Tips to Improve the Code You Produce)

Web应用开发 William 295浏览 0评论

Most tips and tricks for leveling up as a developer focus on knowledge, experience, or communication skills. While these are definitely contributing factors they’re often too abstract for a learner to execute on effectively.

There are no quick shortcuts becoming a better developer, but here are some practices and tools which will definitely help. Here I’ll share some things I did to improve the code I produce, and thusly, the level up as a developer.

These are suggestions not doctrine; Your milage may vary.

大多数贴士和技巧,对于开发人员的重点是知识、经验或沟通技巧。虽说这些肯定是有用的因素,但是它们对于学习者能有效地执行还是太过抽象了。

成为一个更好的开发者没有捷径,但是这里一些实践和工具将肯定会有帮助。这里我将会分享一些东西来提升代码和产品,也提升开发者的水平。

这些是建议不是教条,请依据情况进行调整。

Implement and Enforce a Style guide

Ruby is a very expressive language. With this expression comes a million styles of code to accomplish a task. Unifying these styles helps unify a codebase making it easier to grok and more enjoyable to work in. Use rubocop to accomplish this. Rubocop comes with predefined styles you can tweak and refine to your taste. What your code style tastes are isn’t the goal here. Uniformity throughout your codebase is.

Oh, but you have an existing project which is all over the place in code style? Rubocop can ignore existing violations and let you fix them as you go. It will even try to fix your code style violations if you wish.

Enforcing code style can be challenging on solo and team projects. Use automated tools to enforce these styles such as guard-rubocopCodeClimateHound, or others to prevent committing mis-styled code. Avoid code smells with tools like reek.

实现和加强编码风格指南

Ruby 是一门富有表现力的语言。有了这种表现力,就会有一百万种风格的代码来完成任务。统一这些风格有助于统一一个代码库,使其更容易地理解并更愉快地在其中工作。使用 rubocop 可以实现这一点。Rubocop 具有可让你调整和细化为你的喜好的预定义风格。不过现在讨论的不是你的编码风格,而是整个代码库风格的统一性。

但是你现在的项目代码风格十分杂乱?Rubocop 可以忽略已存在的违反风格指南的地方,让你随时修复。如果你愿意,它甚至会尝试修复违反风格指南的代码风格。

加强代码风格在个人和团队项目上是非常具有挑战性的。使用自动化工具强制执行这些风格,例如 guard-rubocopCodeClimateHound 等,以防止提交错误风格的代码。为避免代码被污染,可以使用像 reek 这样的工具。

Collect Performance Metrics Early

You’ve probably read or heard this somewhere:

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.”?—?Donald Knuth

This quote gets referenced a lot, is good advice to live by. The time to optimize typically comes with little warning, so it is good to be well informed when the time comes. Having this information already is immensely helpful when performance becomes a problem, instead of trying to collect it in the heat of an outage.

Check out tools like Librato and Skylight for collecting performance metrics in production. Additionally, see the excellent rack-min-profiler gem for per-request performance metrics.

尽早收集性能指标

你可能已经在其他地方读过或者听过这句话:

“在大约 97% 的时间里,我们应该无须关心效率问题:过早的优化是所有邪恶的根源。”—?Donald Knuth

这句话被引用了很多次,是个好的谋生建议。优化的时间通常是没什么警告的,所以当那一刻到来时,最好能很好地了解当下的情况。当性能成为问题时,已经有了的这些信息是非常有用的,而不是试图在迫在眉睫的时候才收集它。

使用 Librato 和 Skylight 等检出工具来收集产品的性能指标。另外,请参阅这个优秀的 gem rack-min-profiler,用于每个请求的性能指标。

Avoid Costly Database Queries

If premature optimization is the root of all evil, ignorance of poorly performing code is it’s close relative. When writing Rails code, this usually comes in the form of poorly thought out, unused, or plainly expensive database queries.

Rails performance is usually “fast enough” for most of our webapps. Slow performance can usually be blamed on N+1 queries causing multiple round trips to your database for data. Most performance monitoring tools mentioned perviously will identify these, but there is no reason to avoid them from the get-go. Identify and eliminate these in development using the excellent bullet gem. It will analyze your page loads and tell you when you’re loading data which isn’t being used or opportunities to eliminate N+1 queries.

避免耗费大的数据库查询

如果说过早的优化是不幸的根源,那么对性能差劲的代码的无知与其有着紧密的关系。当编写 Rails 代码时,通常会出现这样的情况:思考不足、未使用或使用代价昂贵的数据库查询。

Rails 的性能对于大多数 webapp 来说,通常是 “足够快的”。差的性能通常被归咎于 N+1 查询导致的数据库的数据多次往返。大多数性能监测工具都能识别这些问题,不过我们应该从一开始就避免它们。在开发中识别和消除这些问题可以使用优秀的 bullet gem。它会分析你的页面,当你加载的数据没有被使用或者有机会消除 N+1 查询时,它就会告诉你。

Make Effective Commits

We’ve all be guilty of making commits with messages which fall short of helpful. Or have malformed code. Or has a debugger call in it. We’re human after all.

Take advantage of git’s pre-commit hooks to analyze you changes and commit message to ensure they’re up to snuff. From validating that your commit message is meaningful, to ensuring your the schema changes exists for that migration you just wrote, to validating your code is following your styleguide. Checkout configurable tools like pre-commit (ruby), and pre-commit (framework), or add your own!

做有效的提交

我们都会在提交信息时犯以下错误:缺乏帮助的消息、或者有错误的代码、或者提交中有一个调试器调用。毕竟人都会犯错。

利用 git 的 pre-commit 钩子来分析改动并提交消息,以确保它们能够达到标准。从验证你的提交信息是有意义的开始,为了确保你的方案变动存在于你刚编写的迁移中,验证你的代码是遵循你编码风格指南的。检出可配置的工具,如 pre-commit (ruby)pre-commit (framework),或自己实现一个!

Write Your Migrations with Zero Downtime in Mind

You’ll inevitably need to make a code change which alters the structure of your database. These changes might introduce downtime in your app while migrations run locking database commands.

While your apps might not be mission critical and downtime is acceptable, getting in the habit of writing your migrations with zero downtime in mind will be helpful when you need to make a schema change the day after your last maintenance window. Check out some of these great guides to learn how.

树立零停机迁移意识

不可避免地你将需要更改代码,改变数据库的结构。这些变更可能会导致你的应用在迁移时运行锁数据库的命令,从而导致宕机。

虽然你的应用没有在执行关键的任务,宕机也可以接受。但考虑到当需要在上次维护之后的第二天进行架构更改时,树立零停机意识,并养成习惯,这对你将会有所帮助。查看 这些优秀的指南,学习该怎么做。

Write Full-stack Tests

You’re writing tests, right? Unit tests are great for identifying breaking changes, but validation that all the cogs are working correctly together will help you sleep better at night. Full stack tests validate that your models, controllers, views, and front-end code are functioning exactly as your users would expect. Capybara will allow you to run your tests against real browsers like Chrome or Firefox or headless browsers like PhantomJS.

编写全栈测试代码

你在写测试代码,对吧?单元测试非常适用于识别重大的改进,但验证所有的改动是否能正确地一起运行能让你更踏实。全栈测试会验证模型、控制器、视图和前端代码是否正好与用户期望的一致。Capybara 可让你针对真正的浏览器(如 Chrome 或 Firefox)或无界面(headless browsers)浏览器(如 PhantomJS)运行测试。

转载请注明:AspxHtml学习分享网 » 提高 Rails 开发者编码效率的实用小贴士(Practical Rails Dev Tips to Improve the Code You Produce)

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址