Good question! The years of experience calculation is not especially clever - it just looks at the experience entries in your Dev Story and adds up their lengths, while accounting for overlaps and gaps. (If you had two jobs concurrently in 2016, that should still count for only one year of experience. Likewise, if you took a career break that shouldn't count towards your years of experience.)
One way to do this would be to take the total length of time you've been in the workforce - the time since your oldest experience entry - and then subtract any gaps, but in fact the code actually does it the other way round, by totting up all the ranges and accounting for overlaps. (The choice is immaterial; the two approaches should ultimately yield the same results.)
Aside: Note that the years of experience calculation is unrelated to a candidate's inferred seniority (used in the seniority filter on the left-hand side in the main search page), which is a much more sophisticated algorithm taking into account things like the text of job descriptions. Because many of the dev stories in the system don't have a lot of content, the seniority engine only succeeds in figuring out how senior a candidate is about half the time. Years of experience on the other hand is much easier to calculate, so that's what we surface in the results view.
Here's the code, in case you're interested:
public static int CalculateYearsOfExperience(this CandidateSearchCv profile, DateTime utcNow)
{
IEnumerable<DateRange> MergeOverlaps(IEnumerable<DateRange> dateRanges)
{
// The idea is to find contiguous clusters of overlapping date ranges and merge them into a single range.
//
// 2009 2010 2011 2012 2013 2014 2015 2016
// |-----------| |-----------| |-----|
// |-----------------|
// |
// |
// V
// |-----------------------------| |-----|
//
// This implementation is probably far from optimal but I don't expect dateRanges to ever be large:
// even the most prolific Dev Story authors will only have a few dozen experience entries.
var result = new List<DateRange>();
foreach (var range in dateRanges)
{
var (overlapping, nonOverlapping) = result.Partition(range.Overlaps, (t, f) => (t.ToList(), f.ToList()));
overlapping.Add(range); // a range always overlaps itself
nonOverlapping.Add(new DateRange(overlapping.Min(r => r.Start), overlapping.Max(r => r.End)));
result = nonOverlapping;
}
return result;
}
var experienceDateRanges = profile
.Experience
.Where(e => e.ExperienceStartDate.HasValue) // if they didn't give a start date, we can't reasonably figure out when that was
.Select(e =>
{
var endDate = e.ExperienceEndDate ?? utcNow; // if they didn't give an end date, assume they still work there
var startDate = e.ExperienceStartDate.Value;
// sometimes people have swapped date ranges in their dev story for some reason
return new DateRange(DateExtensions.Min(startDate, endDate), DateExtensions.Max(endDate, startDate));
});
var actualExperienceTime = MergeOverlaps(experienceDateRanges).Select(r => r.Length).Sum();
// I'd like to write TimeSpan.TotalYears, but it doesn't exist so I have to do this instead https://stackoverflow.com/a/4127396/1523776
var zeroTime = new DateTime(1, 1, 1);
return (zeroTime + actualExperienceTime).RoundToYear().Year - 1;
}
(Of course this is subject to change and should not be considered definitive.) It's been a few months since I wrote this, so I'm pleasantly surprised to note that it's actually quite readable! MergeOverlaps
is a bit fiddly but I think my comment explains it fairly well. Good job, past-Benjamin!
years_of_experience = randint(0,age-19)
(I'm interested in what the real answer is though, +1)