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
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. Bitten by yield return

Bitten by yield return

Anders Abel user avatar by
Anders Abel
·
Jun. 06, 12 · Interview
Like (0)
Save
Tweet
Share
4.72K Views

Join the DZone community and get the full member experience.

Join For Free

I like the simplicity of creating enumerations with iterator blocks through yield return. Even though I regularly do it, I was bitten by a nasty bug a few weeks ago and it was entirely my own fault.

I had been working hard on my bar management system, which includes a service to book a bar stool in advance. In my initial design, a booking had to be submitted for each day. During the first user tests, the regulars complained that it was far too complicated. They wanted to book their favorite bar stool with date intervals. The solution I came up with was a helper method that created a series of bookings between two dates.

public static IEnumerable<SubmitSeries>(BarStoolBooking bookingInfo,
    DateTime firstDate, DateTime lastDate)
{
    for (DateTime date = firstDate; date <= lastDate; date = date.AddDays(1))
    {
        bookingInfo.Date = date;
        yield return bookingInfo.Submit();
    }
}

 There’s a nasty bug in there. Can you spot it? I didn’t. I was bitten by it later on, when I wrote the presentation layer.

The presentation layer for this is simple, it prints out a header, the dates and then a footer.

var bookings = BarStoolBooking.SubmitSeries(baseBooking,
    DateTime.Now.Date, DateTime.Now.Date.AddDays(1));
 
PrintHeader(bookings.First());
foreach (var b in bookings)
{
    Debug.WriteLine(b.Date.ToShortDateString());
}
PrintFooter(bookings.First());

I still didn’t see the catastrophic error I had made. Can you spot it?

I found out when I saw the generated order numbers.

42 is booked for Arthur Dent (ref no 1) the following dates:
2012-05-28
2012-05-29
End of dates (ref no 4)

 

The header uses ref no 1. The footer uses ref no 4, but they should be the same number…

The problem is the lazy evaluation of the iterator block produced with yield return. It will be evaluated three times, although two of them only will evaluate the first object.

  1. When PrintHeader(bookings.First()) is called (just the first element)
  2. By the foreach statement (entire sequence)
  3. When PrintHeader(bookings.First() is called (just the first element)

On each evaluation, a new booking is submitted.

I was bitten by the lazy evaluation of yield return. The lesson learned? Never use yield return if there are side effects (such as submitting a booking…)

Evaluation Blocks Book Element Design Testing Fault (technology) Object (computer science)

Published at DZone with permission of Anders Abel, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Are the Benefits of Java Module With Example
  • Real-Time Analytics for IoT
  • How To Build a Spring Boot GraalVM Image
  • Master Spring Boot 3 With GraalVM Native Image

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: