Search code examples
objectpropertiesunused-variables

Unused object properties bad?


I have a object list with some properties that dont need .I always include the ones I know I won't use anyway because "maybe I need them later" excuse. Is there a clear answer if this is bad or not? I imagine it is a tiny waste of performance, but does it really matter?


Solution

  • It depends.

    I often find that it is just as easy to add properties in later when I know for sure that I need them, and how I will use them.

    Depending on what your compiler/interpreter does with the unused properties then you might get (very negligible) performance issues[citation for C#].

    If you are just hacking together some code, then you are probably not going to hurt anything. On the other hand, if you are working with a team or even on a public API then having unused variables could be very harmful. Imagine I have some interface like this:

    interface IMyInterface {
      void DoStuff(); // implemented
      bool WarningAsError {get; set;} // just speculative - no implementation yet
    }
    

    Anybody using this interface will probably assume that they can set the WarningAsError flag and your interface will treat warnings as errors. This could waste huge amounts of time wondering why the warnings are not treated as errors.