Skip to content

Instantly share code, notes, and snippets.

View congyiwu's full-sized avatar

Congyi Wu congyiwu

  • GitHub Staff
  • Cary, NC
View GitHub Profile
@congyiwu
congyiwu / AtomicWriteFile.cs
Created July 8, 2020 02:04
AtomicWriteFile.cs
/// <summary>
/// Atomically create or replace file at <paramref name="path"/>, with contents provided by
/// <paramref name="write"/>.
/// </summary>
/// <remarks>
/// Although <paramref name="path"/> is atomically updated, the contents of <c>tmpPath</c> are
/// undefined if this method does not return normally.
/// </remarks>
public static void AtomicWriteFile(string path, Action<Stream> write)
{
@congyiwu
congyiwu / OrdinalVsInvariantCulture.md
Last active January 21, 2021 00:29
Ordinal vs. InvariantCulture

In .NET, when should you use Ordinal(IgnoreCase) vs InvariantCulture(IgnoreCase)?

Ordinal(IgnoreCase) does simple byte comparisions, similar to memcmp, and is faster.

InvariantCulture(IgnoreCase) applies extra linguistic rules that are English-biased, but not guaranteed to exactly match any "real" culture.

Use Ordinal(IgnoreCase) when strings are mostly non-linguistic or opaque, e.g. file names, serial numbers.

  • Note that most filesystems behave closer to Ordinal(IgnoreCase), whereas UIs like File Explorer may re-sort filenames culture-sensitively.

Use InvariantCulture(IgnoreCase) when:

@congyiwu
congyiwu / NullStringChecks.md
Last active March 6, 2023 22:44
NullStringChecks

IMO when checking if a string is "set":

  • Simply using bla is null/bla is not null is ideal
    • bla == null/bla != null is functionally equivalent for strings. For other types, "==" and "!=" could theoretically have a slow or incorrect override. is null/is not null is generally preferable since it ignores overrides.
  • string.IsNullOrWhiteSpace(bla) is generally wrong
  • In some contexts, string.IsNullOrEmpty() does make sense

Simply using bla is null/bla not null is ideal

In most code that accepts input from other code (i.e. not a human), it is more elegant only use null to indicate the absence of a value, and not empty strings or whitespace:

  • That's literally what null means
@congyiwu
congyiwu / MapCalleeToCallerParams
Created July 25, 2019 18:33
MapCalleeToCallerParams
# Builds an [ordered] containing only params that were explicitly passed in. With splatting, this
# this lets us call another script without overriding its default params.
function MapCalleeToCallerParams {
param(
# An [ordered] where the keys are param names in the script being called (the callee) and
# values are param names in this script (the caller).
[System.Collections.Specialized.OrderedDictionary][Parameter(Mandatory=$true)] $Mappings
)
$result = [ordered]@{}
@congyiwu
congyiwu / Delete
Last active September 22, 2017 03:01
SqlRaceConditionSandbox
SET XACT_ABORT ON
SET NOCOUNT ON
WHILE 1=1
BEGIN
DELETE tbl_Foo
WAITFOR DELAY '00:00:00.1'
END
@congyiwu
congyiwu / cd-and-pwd.sh
Last active August 29, 2015 14:15
bashism free wrappers for cd and pwd for git-sh-setup
pwd () {
if test -z "${PWD#/[a-z]}"
then
#e.g. '/c' -> 'c:/'
echo "${PWD#/}:/"
return
fi
pwd_after_drive="${PWD#/[a-z]/}"
if test "$pwd_after_drive" != "$PWD"
@congyiwu
congyiwu / DebuggingAssemblyAndTypeLoadErrors.md
Last active August 29, 2015 14:03
Debugging assembly and type load errors

####If you get an error like:

Could not load type '<SomeType>' from assembly '<SomeAssembly>, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1234567890abcdef'.
<followed by some stack trace>

Then ask yourself the following questions:

  1. Where is the assembly for the missing type being loaded from? Use the Modules window in the VS debugger.
  2. Was this the right location?
  3. Is the right version of the assembly in that location?