Monday, 11 February 2019

Find where an object is unintentionally being converted to a string

I've been applying the Replace Primitive with Object pattern to a code base - changing what used to be strings into a custom type (which not only makes the code more readable, but now ensures through type safety that you can't accidentally pass in any old random values to methods that used to just take strings.

The code has tests, and after applying the refactoring, I have a failing test - which hints that somewhere there's an implicit conversion from the new strongly typed object back to a string. The test's failing assertion says it received "MyNamespace.TypedThing" (which is what the default implementation of ToString() returns), rather than the wrapped string value that TypedThing encapsulates.

My initial suspicion is that there's probably code similar to this that's causing the problem:

TypedThing thing = new TypedThing("thingy"); 
string s = $"{thing}";

ReSharper has a cool utility - "Structural Search and Replace". Unfortunately it doesn't work for single expressions like "{thing}".

If I was cluey, I might be able to write a Roslyn tool to search the code and find instances like that, but that's going to take a bit more effort than I want.

What about this: temporarily override the ToString() method on my custom type, and make it throw an exception!

It's a bit of a sledgehammer, but it worked!

As it turns out my suspicion was not quite correct. The offending code was actually assigning the custom type to an Object type (which explains the lack of compiler type warnings), which later on must be converted to a string.

Now that I could see an example, I could use ReSharper's SSR to confirm that was the only instance of that kind of assignment (SSR can be used as I'm searching for an assignment statement, not just a single expression). Just for good measure, I'll also re-run the entire test suite to make sure there aren't any other similar problems still hiding.

Sunday, 3 February 2019

Choco list -localonly (Feb 2019 Edition)

What software / applications am I using on my laptop (February 2019 Edition) according to Chocolatey? Here's an edited list of the output from choco list -localonly:

PackageVersionComments
7zip18.6
7zip.commandline16.02.0.20170209
audacity2.3.0Audio editor
becyicongrabber2.30.0.20161027Icon extractor (for creating Chocolatey packages)
beyondcompare4.2.9.23626My favourite file comparison tool
beyondcompare-integration1.0.1Configure Beyond Compare for TortoiseGit/Svn
dellcommandupdate-uwp3.0.0Dell's driver update app
dns-benchmark1.3.6668.0Useful DNS checker
docker-for-windows99.0.0.0
dotnetdeveloperbundle2.3.0.2563RedGate's .NET tools
dropbox41.4.80
fiddler5.0.20182.28034
FiraCode1.206Nice developer font
Firefox57.0.4
git2.20.1
GoogleChrome63.0.3239.132
iTunes12.9.3.3
keepass2.41Password manager
microsoft-teams1.1.00.2251
mousewithoutborders2.1.8.105Share mouse across laptop and desktop PCs
msbuild-structured-log-viewer2.0.61
nodejs11.9.0
notepadplusplus7.6.3Using this less now compared to VS Code
nuget.commandline4.9.2
obs-studio22.0.2Video / screen recording
OctopusTools5.2.0
Office365ProPlus2016.20170321
paint.net4.1.5
PDFXchangeEditor7.0.328.2My favourite
Pester4.4.1PowerShell unit tests
pingplotter5.8.10Useful visual ping network status
powershell-core6.1.2
procmon3.50SysInternals Process Monitor
resharper-clt.portable2018.3.2ReSharper's free command-line tools
resharper-ultimate-all2018.3.2
screentogif2.16Handy screen recorder
skype8.38.0.138
snagit2019.1.0Screen grabber
sql-server-201714.0.1000
sql-server-management-studio14.0.17289.1
tailblazer0.9.0.536Text/log file viewer
tortoisegit2.7.0.0
tortoisesvn1.11.1.28492
ubiquiti-unifi-controller5.9.29Software for managing UniFi wireless access points
vagrant2.2.3Manage virtual machines
visualstudio2017enterprise15.2.26430.20170605
visualstudiocode1.19.3
vsts-cli0.1.4.20190126Command line tool for managing Azure DevOps
vswhere2.6.7
wifiinfoview2.42
windirstat1.1.2.20161210Where's all that disk space being used?
wireshark2.6.5
x-lite5.4.0.94388VoIP client
yarn1.13.0
zoomit4.50.0.20160210Great for presentations

This is also a good basis for refreshing my Boxstarter scripts.