I go to the gym and lift weights because I like the feeling of getting stronger and better. Two months ago, I started to feel a throbbing pain in my knees, yet I continued to go to the gym.

I wanted so badly to keep getting stronger that I neglected my health and pushed myself to keep lifting weights. As you can imagine, my knee pain didn’t improve, and more and more of my body needed to recover after each grueling workout session.

As a junior developer at Rackspace (as part of the Airbrake team), at times I feel a similar drive to continue pushing out code that I think will make the product stronger. But just like my knees would complain each time I squatted, untested legacy features would buckle and sometimes break under the weight of my new features.

Eventually my physical therapist convinced me to focus on rehabilitation and prevention before continuing to lift weights. When I was healthy and had more stabilizing muscle strength, he said, I could progress faster and less painfully because I’d be less likely to hurt myself.

Taking his advice to heart, I saw a connection to my job. I felt obliged to write tests for legacy features in my product so I could progress quickly and have more confidence that I wasn’t destroying any other features along the way.

Here are some key takeaways I learned during that process:

Tools are important

I use RSpec, Capybara, FactoryGirl and Selenium for testing my Rails app. Before you write tests, be sure you research the right tools. You don’t want to spend more time wrestling with new tests than fixing broken code.

Even simple tasks like cleaning your database before and after the tests run are important to get right if you want to keep tests fast and repeatable. To do this, I use the database_cleaner gem.

If you’re the first developer on your team to catch the testing bug, it’s important to also consider that tools are important because a poor choice here can lead to others on your team neglecting to test. The whole team has to commit to testing, which means you have to make it as easy as possible to begin testing, starting with powerful, flexible tools.

Specs tell you how code should work and not how it does work

When I began testing other people’s code, I noticed I was getting in the habit of writing tests that passed first and then assessing whether the code was actually supposed to work that way. Sometimes it worked, other times I ended up writing tests that passed only because I was overfitting to my examples.

It can be tempting to take the easy way out and assume all up-to-date code works as desired and then write tests to confirm that assumption. This can lead to a strange pattern of reverse-test-driven-development: the code becomes the spec for the tests.

Focusing on the original intent of the code is a safer option, and senior developers will most likely be willing to clarify code if you can’t figure it out on your own.

You can’t test it all, so don’t try

Writing tests for an entire production app can be a full-time job. Of course, we all want 100%(+) test coverage, just like we want to have a sink free of dirty dishes. But instead of arduously cleaning the whole sink once in a while, another alternative is cleaning and putting away your dishes away as you use them. I took this strategy and now write tests to go with any new code and any legacy code I may have touched in the process.

I recommend testing as a useful procrastination for junior developers, since it forces you to learn about the code base and gets good muscle memory ingrained. But do make sure you follow these guidelines when you test.

PS: My knee feels great, thanks for asking.

What I Learned as a Junior Developer Writing Tests for Legacy Code(转载)的更多相关文章

  1. Writing Reentrant and Thread-Safe Code(译:编写可重入和线程安全的代码)

    Writing Reentrant and Thread-Safe Code 编写可重入和线程安全的代码 (http://www.ualberta.ca/dept/chemeng/AIX-43/sha ...

  2. PL/SQL Developer使用技巧、快捷键(转载)

    1.类SQL PLUS窗口:File->New->Command Window,这个类似于oracle的客户端工具sql plus,但比它好用多了. 2.设置关键字自动大写:Tools-& ...

  3. C# Note37: Writing unit tests with use of mocking

    前言 What's mocking and its benefits Mocking is an integral part of unit testing. Although you can run ...

  4. Mocks Aren't Stubs

    Mocks Aren't Stubs The term 'Mock Objects' has become a popular one to describe special case objects ...

  5. [Unit Test] Unit Test Brief Introduction

    Levels of Testing- Acceptance- Performance- Functional- Integration- Unit Why Unit Testing- Feedback ...

  6. 【Java基础】Java开发过程中的常用工具类库

    目录 Java开发过程中的常用工具类库 1. Apache Commons类库 2. Guava类库 3. Spring中的常用工具类 4. 其他工具 参考 Java开发过程中的常用工具类库 1. A ...

  7. Best practices for a new Go developer

    https://blog.rubylearning.com/best-practices-for-a-new-go-developer-8660384302fc This year I had the ...

  8. C# Development 13 Things Every C# Developer Should Know

    https://dzone.com/refcardz/csharp C#Development 13 Things Every C# Developer Should Know Written by ...

  9. 27 Best Free Eclipse Plug-ins for Java Developer to be Productive

    Eclipse offers an integrated development environment having an extensible plug-in system. This enabl ...

随机推荐

  1. java实现PV操作

    package com.jayfulmath.designpattern.command; import java.util.concurrent.Semaphore; /* P(S): ①将信号量S ...

  2. 【数据库_Postgresql】sql语句添加序号,timestamp格式时间截取日期和时间

    SELECT ROW_NUMBER() OVER (ORDER BY sr.receiptid ASC) AS 序号, sr.receiptid, sr.receiptdate, DATE(sr.re ...

  3. 求n!中因子k的个数

    思路: 求n的阶乘某个因子k的个数,如果n比较小,可以直接算出来,但是如果n很大,此时n!超出了数据的表示范围,这种直接求的方法肯定行不通.其实n!可以表示成统一的方式. n!=(km)*(m!)*a ...

  4. 各行业最受欢迎的编程语言,硬件最青睐C和C++

    近日,HackerRank发布了2018年开发技能报告,其中探讨了一些对理解开发人员环境至关重要的事情,本文将摘录编程语言排行部分 2018 开发者技能调查:不同行业中最热门的编程语言 尽管新语言经常 ...

  5. 洛谷 P2498 [SDOI2012]拯救小云公主 解题报告

    P2498 [SDOI2012]拯救小云公主 题目描述 英雄又即将踏上拯救公主的道路-- 这次的拯救目标是--爱和正义的小云公主. 英雄来到\(boss\)的洞穴门口,他一下子就懵了,因为面前不只是一 ...

  6. 【learning】矩阵树定理

    问题描述 给你一个图(有向无向都ok),求这个图的生成树个数 一些概念 度数矩阵:\(a[i][i]=degree[i]\),其他等于\(0\) 入度矩阵:\(a[i][i]=in\_degree[i ...

  7. atom插件安装引发的nodejs和npm安装血案

    最近在写前端网页,学习就要从高大上的地方开始,于是我打算装一个atom编辑器. 本来就是由github客户端的,再装个atom也算是配套了吧,其实本白也是蛮费心思的,技术不怎么地,什么神器都再努力地使 ...

  8. UESTC--1300

    原题链接:http://acm.uestc.edu.cn/problem.php?pid=1300 分析:dp,最长公共上升子列.对于两个序列num1[maxn],num2[maxn]: 如果num1 ...

  9. CentOS6.6安装heartbeat配置资源切换操作笔记实现高可用(原创)

    参考资料:http://www.centoscn.com/CentosServer/cluster/2015/0605/5604.html   背景需求: 使用heartbeat来做HA集群,并且把n ...

  10. char* & 与 char*

    原文 char*& 为指针引用,通常需要更改指针本身并返回时才这样用. char *&是指针引用char *是指针当用指针引用作为形参的时候,改变形参的指针,同时实参的指针也改变了.当 ...