开源日报 每天推荐一个 GitHub 优质开源项目和一篇精选英文科技或编程文章原文,坚持阅读《开源日报》,保持每日学习的好习惯。
今日推荐开源项目:《简易版本 project-x》
今日推荐英文原文:《A Framework for Overcoming Tough Bugs Without Pulling Your Hair Out》
开源日报第628期:《简易版本 project-x》
今日推荐开源项目:《简易版本 project-x》传送门:GitHub链接
推荐理由:React 和 Vue 这样的流行框架上都有在组件内保存数据后根据数据操作的功能,毕竟这玩意可是相当好用的。这个项目把流行框架上的这些功能借了过来,在你只需要这些而不需要更多功能的时候可以使用。语法方面使用了 Vue 里常见的 bind 等,而功能则包括常见的数据保存,属性设定和事件监听等这方面常用的功能,虽然是直接在 html 里使用的语法,太过于复杂会损失易读性,但是在一些小项目里不需要杀鸡用牛刀的时候可以尝试。
今日推荐英文原文:《A Framework for Overcoming Tough Bugs Without Pulling Your Hair Out》作者:Chris Geelhoed
原文链接:https://medium.com/better-programming/a-framework-for-overcoming-tough-bugs-without-pulling-your-hair-out-8da3e376c5bc
推荐理由:在调试代码过程中的小建议

A Framework for Overcoming Tough Bugs Without Pulling Your Hair Out

8 tips for breaking through roadblocks as a programmer

Programming is hard. The ability to translate an idea into code isn’t something that comes naturally to most people, and sometimes we get stuck.

Even after coding for years, you will still hit roadblocks regularly. Computer programming is an incredibly deep field, and it just isn’t possible to master everything about it.

Plus, we all have bad days sometimes.

Maybe you can’t fix a stubborn JavaScript error, or you can’t explain why your website looks completely broken in Internet Explorer, or maybe you can’t even start your development server.

Software can break in an almost unlimited number of ways, and it often does. Murphy’s law.

Setbacks in coding can’t be avoided, but they can be handled gracefully. Here are eight tips that will help you debug your code as painlessly as possible:
  • Take care of yourself first.
  • Use the tools that are available to you.
  • Speed up your debugging cycle.
  • Ask for help.
  • Look at the obvious stuff first.
  • Read your error messages.
  • Think about what changed last.
  • Take time to reflect on what you’ve learned before moving on.

1. Take Care of Yourself First

Even if you spend most of your day looking at a computer screen, you are still a human being, and it’s difficult to think clearly when you’re tired, hungry, or otherwise uncomfortable.

Try to keep tabs on yourself when you are coding. If you suddenly find that you are hitting a wall, think about what might be off. Have you gone without a break for a long period of time? Are you in an environment that makes it hard to concentrate? Did that third cup of coffee give you the jitters?

Pay attention to yourself and look for patterns like these. Small details could have a surprisingly large impact on your productivity.

2. Use the Tools That Are Available to You

Software development has come a long way since the rise of the internet and good tools now exist to make debugging easier.

Take the time to learn about the tools available in your arena. If your focus is on front-end web development, then learn how to use your browser’s built-in development tools. If you write PHP, learn about Xdebug and how breakpoints can be used in your IDE.

Whatever kind of programming you do, explore your ecosystem and try to learn how experts in your area debug their code. This kind of research is sometimes hard to justify when you have deadlines to meet, but it’s an investment in time that will pay itself back tenfold.

3. Speed Up Your Debugging Cycle

Classic programming is essentially a guess-and-check process. Your write some code then check the result. If it works, you move forward. If not, you are now debugging.

The time between writing code and seeing its result should be as short as possible. A quick feedback cycle allows you to iterate quickly and maintain your train of thought.

Do whatever you can to speed up this process. For example, if you are debugging a component that appears at the very bottom of a long page and find yourself scrolling down to it again and again to test your code, move that component to the top temporarily.

If you are writing CSS or JavaScript then take the time to set up hot reloading so that you don’t need to make a hard reload every time you change your code.

Whatever you are doing, try to notice the little steps that you are repeating and think about how they can be eliminated.

4. Ask for Help

Sometimes, you look at something for so long that you get lost in the weeds and lose sight of the bigger picture. This is called tunnel vision, and it is a problem for junior and senior engineers alike.

Instead of spending hours stressing over a problem alone, invite a colleague to look at it with you. It’s amazing what perspective an outside set of eyes can bring to the table.

We all want to be super-devs that can solve every problem alone but this isn’t a realistic goal or expectation.

A better approach is to collaborate with your team and leverage the experience of the people around you. You might be struggling with a problem that the person sitting next to you solved last week.

5. Look at the Obvious Stuff First

Most of the bugs that you encounter as a developer will be obvious, but only in hindsight.

Be self-aware as you explore solutions. Are you making assumptions without verifying them? Are you going down a rabbit hole that could be totally unrelated to your bug?

It’s a good idea to check off the little things first. Some good questions to ask yourself are:
  • Did I forget to save?
  • Is the code I’m editing even being loaded? If I print “hello world” at the top of my script do I see the output?
  • Did I forget to require that module?
  • Did I forget to call that function?
  • Did I forget to return that value?
  • Are things spelled correctly?
I can’t count the number of times I’ve pulled my hair out over a problem that ultimately came down to a simple spelling mistake.

6. Read Your Error Messages

Error messages can be intimidating when you first start coding, but they give you extremely valuable information. Take the time to read them carefully and give yourself a minute to digest them.

If the message gives you the line in the script where the error was thrown, give that line a closer look.

If the message gives you an error code without further explanation, try googling that error code — you might be able to find additional info.

Error messages can look like gibberish at first glance, but they are often the best way of understanding what went wrong. As you gain experience as a developer, they will make more and more sense to you.

7. Think About What Changed Last

This sounds a lot like the “Retrace your last steps” advice that your mom gave you when you lost something as a kid, but it’s important nonetheless.

When your code is working fine one day and suddenly breaks without explanation, you should look for what changed.

Did you upgrade your version of Node.js? Or maybe you switched from Vagrant to Docker. Things that changed recently are much more likely to cause issues than things that have already been working for a long time.

If you are fortunate enough to have the history of the project captured with a version control system like Git, then it might be worthwhile to check out the code from previous commits and see if those versions have the bug you are dealing with.

8. Take Time to Reflect Before Moving On

Fixing a bug comes as such a relief that it’s tempting to just forget about it and move on to the next task, but this is a big mistake!

Take a moment to enjoy your success, but then reflect on the problem that you just solved.

Do you understand how the bug was created in the first place, and how it was fixed? If the answer is no then you might want to dig a little deeper before moving on.

If the bug tripped you up for hours or days, it might even be worthwhile to document it somewhere — either publicly or in your own personal notes.

Write about what the bug was, how you fixed it, and what you learned. Doing this will put you in a much better position to quickly identify and fix similar bugs in the future.

Summary

Stubborn bugs are probably the worst part of programming. They aggravate us, waste our time, and sometimes make us feel like imposters.

When you find yourself in a battle with a bug, take a moment to acknowledge that this happens to everyone and then think back to the tips mentioned in this article.

They won’t give you the answer, but they will give you a solid foundation to work from and a good place to start.
下载开源日报APP:https://openingsource.org/2579/
加入我们:https://openingsource.org/about/join/
关注我们:https://openingsource.org/about/love/