Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
## Original Way string myUrl = "blah"; if (AllowPostToWordpress(myUrl)) { // do stuff } private static bool AllowPostToWordpress(bool siteUrlOverrideExists) { // this is verbose (the code could have been written in many fewer lines) // but i want it to be clear what's going on here. there are two levels of protection // against accidental posts to wordpress which are here for our dev/staging environments. // first, we have a key that simply says whether or not we can post to production. // next, we have the ability to override the xmlrpc endpoint in the database // finally, we have the ability to require that the endpoint is overridden bool doPost = false; bool enablePostingToWordpress = Config.GetAppSetting<bool>("EnablePostingToWordpress", false); bool requireWordpressOverride = Config.GetAppSetting<bool>("RequireWordpressOverride", true); if (enablePostingToWordpress) { if (!requireWordpressOverride) { doPost = true; } else { if (siteUrlOverrideExists) { doPost = true; } } } return doPost; } ## Testable Way bool enablePostingToWordpress = Config.GetAppSetting<bool>("EnablePostingToWordpress", false); bool requireWordpressOverride = Config.GetAppSetting<bool>("RequireWordpressOverride", true); string myUrl = "blah"; if (AllowPostToWordpress(enablePostingToWordpress, requireWordpressOverride, myUrl)) { // do stuff } private static bool AllowPostToWordpress(bool enablePostingToWordpress, bool requireWordpressOverride, bool siteUrlOverrideExists) { // this is verbose (the code could have been written in many fewer lines) // but i want it to be clear what's going on here. there are two levels of protection // against accidental posts to wordpress which are here for our dev/staging environments. // first, we have a key that simply says whether or not we can post to production. // next, we have the ability to override the xmlrpc endpoint in the database // finally, we have the ability to require that the endpoint is overridden bool doPost = false; if (enablePostingToWordpress) { if (!requireWordpressOverride) { doPost = true; } else { if (siteUrlOverrideExists) { doPost = true; } } } return doPost; }
This paste will be private.
From the Design Piracy series on my blog: