Marco Cecconi

@sklivvz

StackSort connects to StackOverflow, searches for 'sort a list', and downloads and runs code snippets until the list is sorted.

You shouldn't be wedded to any particular technology, but have a broad enough background and experience base to allow you to choose good solutions in particular situations. 

"The Pragmatic Programmer: From Journeyman to Master", Andrew Hunt & David Thomas, preface

GUESS THE

CODING STYLE?

Object oriented hierarchy

private static IEnumerable<LocalizedString> MakeTranslatedStringObvious(
    string inSourceTemplate
)
{
    var input = inSourceTemplate.Normalize();
    var sb = new StringBuilder(input.Length);

    var indicesMatching = r => r.Matches(input)
                                .OfType<Match>()
                                .SelectMany(x => Enumerable
                                    .Range(x.Index, x.Length));

    var leaveAsIs = Enumerable.Empty<int>()
        .Union(indicesMatching(ReplacementFields))
        .Union(indicesMatching(HiddenReplacementFields))
        .Union(indicesMatching(PluralFields))
        .Union(indicesMatching(HiddenPluralFields))
        .ToDictionary(x => x);

    // ...
}

Functional: lambdas, fluid syntax, enumerables...

[StackRoute("questions/tagged")]
[StackRoute("questions/tagged/{tagnames}")]
[StackRoute("tags/{tagnames}", RoutePriority.Low)]
[TrackNewsletter(NewsletterEmailClickTarget.tag)]
public ActionResult ListByTag(string tagnames, int? page = null, ...)
{
    ///
}

Aspect Oriented: runtime behaviors are defined by decorators

[ScheduledRoute("scheduled/hourly", RecurrenceSeconds = 60 * 60)]
public ActionResult Hourly()
{
    if (Current.Site.IsChildMeta)
    {
        SyncReputationWithParentSite();
        SyncProfilesWithParentSite();
    }
    BountyCloseExpired();
    BountyInformClosesSoon();
    BumpUnansweredQuestions(Current.Site.Settings.Questions.UnansweredBumpPerHour);
    ClearExpiredSessions(Current.Site.Settings.Site.SessionExpirationMinutes);
    DeleteAnswersForDeletedQuestions();
    ModeratorElection.CycleCurrentElectionState();
    FixNullDisplayNamesImpl();
    UnlockExpiredNotices();
    InvalidateReviewTaskResultsImpl();
    RejectBannedSuggestedEdits();
    RejectOrphanTagWikiSuggestedEdits();
    DeleteScheduledUsers();
    ExpirePendingUserMerges();
    MakeSelfDeleteLinksAvailableAndSendUserDeleteEmails();
    CreatingMissingSuggestedEditReviewTasks();
    ExpireSuggestedEditAuditReviewTasks();
    ExpireCommunityDownVotesOnUpVoteByOtherUser();
    NormalizeQuestionAnswerScoresImpl();
    RedisBridge.BackupOrRestoreRedisKeys(LogInstance, GetType().Assembly);
    Log($"** end hourly; took {sw.ElapsedMilliseconds:N1} ms");
    return ScheduledComplete();
}

Procedural: Batches of sequential actions are used for long running operations

// hundreds of similar lines

if (ActiveBountyQuestionsOnly)
{
    EmitMemberGet(il, nameof(QuestionValue.BountyEnd));
    il.Emit(OpCodes.Ldarg_0);
    il.Emit(OpCodes.Ldfld, now);
    il.Emit(OpCodes.Blt, exclude);
}

if (NeedAnswersOnly)
{
    EmitMemberGet(il, nameof(QuestionValue.IsLocked));
    il.Emit(OpCodes.Brtrue, exclude);
    Label shortCircuit = il.DefineLabel();
    EmitMemberGet(il, nameof(QuestionValue.AnswerCount));
    il.Emit(OpCodes.Brfalse, shortCircuit);
    EmitMemberGet(il, nameof(QuestionValue.IsAnswered));
    il.Emit(OpCodes.Brfalse, shortCircuit);
    EmitMemberGet(il, nameof(QuestionValue.BountyEnd));
    il.Emit(OpCodes.Ldarg_0);
    il.Emit(OpCodes.Ldfld, now);
    il.Emit(OpCodes.Blt, exclude);
    il.MarkLabel(shortCircuit);
}

// hundreds of similar lines

Meta-programming: code that writes code

public static readonly char[] Space = { ' ' },
                              Comma = { ',' },
                              Period = { '.' },
                              Minus = { '-' },
                              Plus = { '+' },
                              Asterisk = { '*' },
                              Percent = { '%' },
                              Ampersand = { '&' },
                              Equal = { '=' },
                              Underscore = { '_' },
                              NewLine = { '\n' },
                              SemiColon = { ';' },
                              Colon = { ':' },
                              VerticalBar = { '|' },
                              ForwardSlash = { '/' },
                              DoubleQuote = { '"' },
                              Dash = { '-' },
                              NewLine_CarriageReturn = { '\n', '\r' },
                              Comma_Space = { ',', ' ' },
                              Comma_SemiColon = { ',', ';' },
                              Comma_SemiColon_Space = { ',', ';', ' ' },
                              BackSlash_Slash_Period = { '\\', '/', '.' },
                              DoubleRightArrow = { '»' };

Weird stuff that saves millions of allocations

Premature optimization is the root of all evil

-- Donald Knuth

Fact-based optimization is the root of all awesome

-- Not Donald Knuth

miniprofiler

miniprofiler

DECLARE @p0 int = 4623;

SELECT TOP (1) 
    [t0].[Id], [t0].[UserTypeId], [t0].[Reputation], 
    [t0].[LastAccessDate], [t0].[LastDailySiteAccessDate], 
    [t0].[LastLoginDate], [t0].[LastEmailDate], [t0].[LastLoginIP], 
    [t0].[CreationDate], [t0].[Email], [t0].[DisplayName], 
    [t0].[WebsiteUrl], [t0].[RealName], [t0].[Location], 
    [t0].[Birthday], [t0].[BadgeSummary], [t0].[OptInEmail], 
    [t0].[PreferencesRaw], [t0].[HasReplies], [t0].[TimedPenaltyDate], 
    [t0].[DaysVisitedConsecutive], [t0].[DaysVisitedTotal], 
    [t0].[LastModifiedDate], [t0].[IsVeteran], [t0].[ReputationToday], 
    [t0].[ReputationWeek], [t0].[ReputationMonth], 
    [t0].[ReputationQuarter], [t0].[ReputationYear], 
    [t0].[ReputationSinceLastCheck], [t0].[AcceptRateAsked], 
    [t0].[AcceptRateAccepted], [t0].[AccountId], 
    [t0].[ProfileImageUrl], [t0].[HasAboutMeExcerpt]
FROM [dbo].[Users] AS [t0]
WHERE [t0].[AccountId] = @p0;

miniprofiler

{
  "Query": "",
  "Sort": 6,
  "Skip": 0,
  "Take": 200,
  "DualThreaded": true,
  "Parameters": {
    "Reverse": false,
    "CreationDateMin": "\/Date(-62135596800000)\/",
    "CreationDateMinRaw": -62135596800,
    "CreationDateMaxRaw": 253402300800,
    "LastActivityMinRaw": -62135596800,
    "LastActivityMaxRaw": 253402300800,
    "CreationDateMax": "\/Date(253402300799999)\/",
    "LastActivityMin": "\/Date(-62135596800000)\/",
    "LastActivityMax": "\/Date(253402300799999)\/",
    "ScoreMin": -2147483648,
    
    /* snip */

  },
}

miniprofiler

  select RouteName, 
         avg(SqlCount) as SqlNum, 
         max(SqlDurationMs) as SqlMs, 
         avg(RedisCount) as RedisNum, 
         max(RedisDurationMs) as RedisMs, 
         max(AspNetDurationMs) as AspMs, 
         count(1) as Num
    from LogsToday 
   where ClientIp = ##ClientIp## 
     and RouteName is not null
group by RouteName

Data Explorer

opserver

opserver

opserver

bosun

bosun

In computer networking, IP over Avian Carriers (IPoAC) is a humorously-intended proposal to carry Internet Protocol (IP) trafficby birds such as homing pigeons. IP over Avian Carriers was initially described in RFC 1149

pairing

Pragmatic techniques

  • Objective necessity drives architecture
  • Measure everything
  • Allow discussion across the company
  • Facts win 
  • Involve your community
  • Pair across functions
  • Teach people how to work together not how to work
  • The best ideas do not usually come from the top

Marco Cecconi

@sklivvz

Pragmatic Development at Stack Overflow

By Marco Cecconi

Pragmatic Development at Stack Overflow

  • 726