The problem with intellisense


Today I was working with some sample code, and I came across a misspelling. Not a big deal – there was a field that was named “m_postion” rather than “m_position”.


But that got me thinking…


In the past, that sort of thing wouldn’t have happened. You would have written:


int m_postion;


but then, when you wrote your code, you would have written:


m_position = 5;


and the compiler would have complained. But with intellisense, you now just pick whatever looks right in the popup list, and the mistakes stay around a bit longer.


So, I wrote a bit of code to help with this – it reflects over an assembly, and produces a list of words.


 


 

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;

namespace IdentifierExtract
{
class GetIdentifiers
{
Dictionary m_identifiers = new Dictionary();

public GetIdentifiers()
{
}

public void Process(string assemblyFilename)
{
Assembly assembly = Assembly.LoadFrom(assemblyFilename);

foreach (Type type in assembly.GetTypes())
{
ProcessType(type);
}
}

void ProcessType(Type type)
{
BreakIntoWordsAndAdd(type.Name);

foreach (MemberInfo memberInfo in type.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static))
{
BreakIntoWordsAndAdd(memberInfo.Name);

MethodBase methodBase = memberInfo as MethodBase;

if (methodBase != null)
{
foreach (ParameterInfo parameterInfo in methodBase.GetParameters())
{
BreakIntoWordsAndAdd(parameterInfo.Name);
}
}
}
}

void BreakIntoWordsAndAdd(string identifier)
{
List words = BreakPascalCasing(identifier);

foreach (string word in words)
{
AddIdentifer(word);
}
}

List BreakPascalCasing(string identifier)
{
Regex regex = new Regex(“.[a-z]+”);

MatchCollection matches = regex.Matches(identifier);

List words = new List();
foreach (Match match in matches)
{
words.Add(match.Value);
}

return words;
}

private void AddIdentifer(string name)
{
name = name.ToLower();
if (!m_identifiers.ContainsKey(name))
{
m_identifiers.Add(name, null);
}
}

public void WriteToConsole()
{
List identifiers = new List(m_identifiers.Keys);
identifiers.Sort();

foreach (string identifier in identifiers)
{
Console.WriteLine(identifier);
}
}
}
}

Comments (4)

  1. Peter Ritchie says:

    Interesting, I get errors on Dictionary and List looking for the parameter types.  Do you have using aliases in the original code that you didn’t include, for example:

    using Dictionary = System.Collections.Generic.Dictionary<string, object>;

    using List = System.Collections.Generic.List<string>;

  2. Tim Danner says:

    Doesn’t FxCop do this, including the spell checking?

  3. The problem was in C++ if you consistently misspelled it it became painful to rename it so you end up living with the mispelling…

    These days

    Install Visual Assist X

    and Resharper

    When you find a mispelled word, rename it! either in C++ or C#

    Side Note: a bit disturbing you’re using C++ naming conventions in C# though? 🙂  No doubting your a ninja coder and I love your stuff, but seriously, bringing the m_ prefixing into C# is a bit of a "cant teach an old dog new tricks" thing.  

  4. billrob458 says:

    At least it wasn’t "m_dictIdentifiers" and "szName"