DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Shame driven development

Shame driven development

David Green user avatar by
David Green
·
Dec. 27, 11 · Interview
Like (0)
Save
Tweet
Share
8.14K Views

Join the DZone community and get the full member experience.

Join For Free

i always aspire to write well-crafted code . during my day job , where all production code is paired on, i think our quality is pretty high. but it’s amazing how easy you forgive yourself and slip into bad habits while coding alone. is shame the driving force behind quality while pairing?

we have a number of archaic unit tests written using easy mock ; all our more recent unit tests use jmock . this little piece of technical debt means that if you’re changing code where the only coverage is easy mock tests you first have to decide: do you fix up the tests or, can you hold your nose and live with / tweak the existing test for your purposes? this is not only distracting, but it means doing the right thing can be substantially slower.

changing our easy mock tests to jmock is, in principle, a relatively simple task. easy mock declares mocks in a simple way:

private pricesservice prices = createmock(pricesservice.class);

these can easily be converted into jmock-style:

private mockery context = new mockery();
...
private final pricesservice prices = context.mock(pricesservice.class);

easymock has a slightly different way of declaring expectations:

prices.prefetchfor(asset);
expect(prices.for(asset)).andreturn(
    lists.newlistof("1.45", "34.74"));

these need to be translated to jmock expectations:

context.checking(new expectations() {{
    allowing(prices).prefetchfor(asset);
    allowing(prices).for(asset);
        will(returnvalue(lists.newlistof("1.45", "34.74")));
}});

this process is pretty mechanical, so as part of 10% time i started using my scripted refactoring tool – rescripter – to mechanically translate our easymock tests into jmock. rescripter let’s you run code that modifies your java source. but this is more than just simple search & replace or regular expressions: by using eclipse’s powerful syntax tree parsing, you have access to a fully parsed representation of your source file – meaning you can find references to methods, locate method calls, names, parameter lists etc. this is exactly what you need given the nature of the translation from one library to another.

this was inevitably fairly exploratory coding . i wasn’t really sure what would be possible and how complex the translation process would eventually become. but i started out with some simple examples, like those above. but, over time, the complexity grew as the many differences between the libraries made me work harder and harder to complete the translation.

after a couple of 10% days on this i’d managed to cobble together something awesome: i’d translated a couple of hundred unit tests; but, this was done by 700 lines of the most grotesque code you’ve ever had the misfortune to lay your eyes upon!

and then… and then last week, i got a pair partner for the day. he had to share this horror. having spent 10 minutes explaining the problem to him and then 15 minutes explaining why it was throwaway, one-use code so didn’t have any unit tests. i was embarrassed.

we started trying to make some small changes; but without a test framework, it was difficult to be sure what we were doing would work. to make matters worse, we needed to change core functions used in numerous places. this made me nervous, because there was no test coverage – so we couldn’t be certain we wouldn’t break what was already there.

frankly, this was an absolute nightmare. i’m so used to having test coverage and writing tests – the thought of writing code without unit tests brings me out in cold sweats. but, here i was, with a mess of untested code entirely of my own creation. why? because i’d forgiven myself for not “doing it right”. after all, it’s only throwaway code, isn’t it? it’s exploratory, more of a spike than production code. anyway, once its done and the tests migrated this code will be useless – so why make it pretty? i’ll just carry on hacking away…

it’s amazing how reasonable it all sounds. until you realise you’re being a total and utter fucktard. even if it’s one-use code, even if it has a relatively short shelf-life

the only way to go fast, is to go well

so i did what any reasonable human being would do. i spent my lunch hour fixing this state of affairs. the end result? i could now write unit tests in jasmine to verify the refactoring i was writing.

not only could i now properly test drive new code. i could write tests to cover my existing legacy code, so i could refactor it properly. amazing. and all of a sudden, the pace of progress jumped. instead of long debug cycles and trying to manually find and trigger test scenarios, i had an easy to run, repeatable, automated test suite that gave me confidence in what i was doing.

none of this is new to me: it’s what i do day-in day-out. and yet… and yet… somehow i’d forgiven myself while coding alone. the only conclusion i can draw is that we can’t be trusted to write code of any value alone. the shame of letting another human being see your sorry excuse for code is what drives up quality when pairing: if you’re not pair programming, the code you’re writing must be shameful .

from http://blog.activelylazy.co.uk/2011/09/06/shame-driven-development/

unit test

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Real-Time Analytics for IoT
  • GitLab vs Jenkins: Which Is the Best CI/CD Tool?
  • Container Security: Don't Let Your Guard Down
  • Cloud Performance Engineering

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: