Search code examples
performanceresharperredundancy

Do redundant expressions, directives and qualifiers impact performance?


I'm working with somebody else's code right now and ReSharper is giving me a lot of warnings about redundant code, such as:

  • Unused directives
  • Array creation expressions (new string[]{xxx,xxx,xxx} instead of {xxx,xxx,xxx})
  • Explicit delegate creation (new EventHandler(xxx) instead of xxx)
  • Qualifiers (System.Windows.Forms.Form instead of Form when directives are used)

There is a lot of code to work with, so I'm not sure if it's worth going through it all and removing all redundant code.

With that in mind, I wanted to ask if these are warnings that are only there to improve code style (for aesthetics) or do those pieces of code actually perform extra operations and impact performance a little?


Solution

  • It depends.

    • Unused using directives don't matter.

    • Abbreviated array initializers I'm pretty sure compile to the same CIL

    • Explicit delegate creation I'm not so sure, but I'd expect the same performance

    • Unneeded qualifiers I'm very sure compile to the same CIL.

    However

    The computer's time is in general much less important than your time, or the time of anyone else reading the code. Take a piece of code and replace all the ints with System.Int32s and it will take longer to read and longer to understand, and those are also important 'performance' metrics.

    Programs must be written for people to read, and only incidentally for machines to execute, as a much cleverer person than me once wrote.