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);
            }
        }
    }
}