Public pages of John’s personal knowledge base
(home)
This was written in , much of this is obsolete. ___ would be filled in with the name of the company or organization that owns the code.
___.
and in namespaces starting ___.
, followed in either case by the name of the product.javascript:
or wiring event handlers in attributes and avoid style=""
attributes
</body>
tag unless there’s a necessity to put them higher or in the header, and should always be of the form <script type="text/javascript" src=""></script>
instead of <script type="text/javascript">/* JS code here */</script>
modernizr.js
only works inside <head>
<link rel="stylesheet" href="theme.css"/>
(which must be inside the <head>
) instead of <style>/* CSS code here */</style>
Dispose()
on all objects that implement IDisposable
, that are either instantiated or returned from function calls, unless it’s being returned from that function. In C# this is typically done with a using
block.System.Data
outside data access classes and namespace System.Web
outside of UI-specific classes, in both cases this includes child namespaces
readonly
out
parameters, create an object instead (in SQL, use an additional result set)throw;
, never use throw ex;
to avoid resetting the stack trace. Keep in mind that catching always unwinds the stack, so do that only if you are taking meaningful actiontry
–catch
and let the exception bubble since the result is the sameInnerException
to the original exceptionI
for interfacesv
for database views which are not updatablenew
, should be PascalCasedbin
and obj
directories should never exist in source control, for binaries use NuGet if possible, otherwise create a lib
folder
.refresh
files must be checked in since there is no project file, and they live in the bin
folder// TODO:
comments, checked in code should be “done”/NameLabel
instead of lblName
), this allows similar controls to be grouped in IntelliSense and, when using WebForms, it allows writing selectors as [id$='\_NameLabel']
instead of needing to change ClientIdMode
to make #NameLabel
work
[id$='txtName']
would match anything ending in txtName
, i.e. both txtName
and reqtxtName
, leading to hard-to-find bugs; one more reason to avoid type prefixes… another way to avoid the potential conflict is qualify the selector with the type, i.e. input[id$='\_txtName']
[id|='\_someID_']
\
optional prior to the _
, some older browsers, including IE6, ignore CSS selectors that begin with _
, escaping avoids the issueSYSUTCDATETIME()
not GETDATE()
. To display a time to the user, convert it to the appropriate time zone in the UI tier.string.Equals()
or string.Compare()
instead of operators
switch
), always convert to upper case (via string.ToUpperInvariant()
or string.ToUpper()
in .NET or string.toUpperCase()
in JavaScript), never to lower case (see “Turkish i problem” in your favorite search engine)StringComparison
, always pass one
CultureInfo
and CompareOptions
(if it takes one) insteadIFormatProvider
(and a format string) to a method that takes them (i.e., ToString()
or string.Format()
)
CultureInfo.CurrentCulture
, DateTimeFormatInfo.CurrentInfo
, or NumberFormatInfo.CurrentInfo
CultureInfo.InvariantCulture
, DateTimeFormatInfo.InvariantInfo
, or NumberFormatInfo.InvariantInfo
IFormatProvider
is deprecated or obsolete (e.g., Enum.ToString()
), don’t pass onedbo
as their default schema, this can also cause plan cache bloat as different users can get different query plansWITH (NOEXPAND)
on the view, as without that hint, lower editions of SQL Server will ignore the indices and do a table scanEXEC sys.sp_executesql @sql;
never with EXEC(@sql);
AddWithValue
infer it, because it will always guess the exact length of the value passed, and different parameter lengths will get different query plans, bloating the plan cachedatetime2
, date
, or time
; never use datetime
, smalldatetime
, or datetimeoffset
time
or datetime2
, only use datetimeoffset
in the UI tier
tempuri.com
none
or an empty string, instead add and remove a class defined in your style sheet, i.e. .hidden { display: none !important; visibility: hidden; }
(bootstrap already defines this), to avoid issues around different behavior for block v. inline elements, etc.===
and !==
, never use ==
and !=
, the former compare type and value, the later type casts one of the objects to the type of the other, often changing the value (or at least the trueness/undefined
to get the only value of type undefined
, use the void
operator instead (i.e. void null
or void 0
). ECMA versions 3 & 4 allow the global property to be redefined, although this redefining is disallowed by ECMA version 5, many implementations still allow it.(function moduleIIFE() {
"use strict";
var window, ___, module;
window = this; // in a browser, should be window
___ = window.___ = window.___ || {};
module = ___.module = ___.module || {};
// code goes here, including $(window.document).ready or window.pageLoad
}).call(this);
pageLoad
and pageUnload
, always check if it’s already defined to ensure the original functionality isn’t removed, i.e.// this example assumes the code is inside an IIFE as shown above
module.pageLoad = function (sender, args) {
// code goes here
}
if (window.pageLoad) {
module.pageLoadExisting = window.pageLoad;
window.pageLoad = function (sender, args) {
module.pageLoadExisting(sender, args);
module.pageLoad(sender, args);
};
} else {
window.pageLoad = module.pageLoad;
}
where module is the name of whatever module is defined in that script
window.pageLoad = function (sender, args) {
/// <summary>Wrap AjaxControlToolkit pageLoad in a jQuery event, so multiple handlers can be added.</summary>
/// <param name="sender" type="Sys._Application">Application object the event is acting upon.</param>
/// <param name="args" type="Sys.ApplicationLoadEventArgs">Application load event arguments.</param>
/// <returns type="undefined" />
$(window.document).trigger("ajaxToolkit-pageLoad", sender, args);
};
$(window.document).on("ajaxToolkit-pageLoad", function (event, sender, args) {
/// <summary>AjaxControlToolkit pageLoad.</summary>
/// <param name="event" type="jQuery.Event">jQuery event object.</param>
/// <param name="sender" type="Sys._Application">Application object the event is acting upon.</param>
/// <param name="args" type="Sys.ApplicationLoadEventArgs">Application load event arguments.</param>
/// <returns type="undefined" />
// code goes here
});