locked
Evil Code Monkey RRS feed

  • Question

  • Hello everyone.

    I've noticed the new features of Visual Studio will deploy "Evil Code Monkey" and there is an example in the picture below:

    I'm not understanding why the "theList[0]" is a bad practice, I've been searching in google but I can't find what kind of practice this is.

    I would appreciate your help, thanks in advance.


    Ivan Dario Ospina C#.NET, T-SQL Developer

    Friday, July 27, 2012 8:17 PM

Answers

  • You never check if "theList" is null.  Try this as the first line in your function:
     
        if (theList == null) return string.empty;

    The list could also be empty.

    Also note you may not want to return an empty string; it depends on the circumstances.  Throw an exception might be acceptable, returning null, or there could be some other default value.

    • Proposed as answer by Lisa Zhu Tuesday, July 31, 2012 8:50 AM
    • Marked as answer by Lisa Zhu Thursday, August 2, 2012 8:54 AM
    Friday, July 27, 2012 9:14 PM

All replies

  • You do know that that image comes from a website that is making a joke about Visual Studio.  I don't know how seriously look at the code in the joke.
    Friday, July 27, 2012 8:25 PM
  • it make sense, isn't it? I've just have a runtime error caused by a similar practice, I have a discusion about why should I avoid hardcode positions of the array in code, but there is no a strong explanaition of it, only speculations.

    Ivan Dario Ospina C#.NET, T-SQL Developer

    Friday, July 27, 2012 8:32 PM
  • You never check if "theList" is null.  Try this as the first line in your function:
     
        if (theList == null) return string.empty;
     
     

    --
    Mike
    Friday, July 27, 2012 8:52 PM
  • It would be the solution for the problem, but I'm looking for a strong explanation about why I should not use the hardcoded way, you are still using it after the "if" statement what solves a possible run-time exception.

    Ivan Dario Ospina C#.NET, T-SQL Developer

    Friday, July 27, 2012 9:12 PM
  • You never check if "theList" is null.  Try this as the first line in your function:
     
        if (theList == null) return string.empty;

    The list could also be empty.

    Also note you may not want to return an empty string; it depends on the circumstances.  Throw an exception might be acceptable, returning null, or there could be some other default value.

    • Proposed as answer by Lisa Zhu Tuesday, July 31, 2012 8:50 AM
    • Marked as answer by Lisa Zhu Thursday, August 2, 2012 8:54 AM
    Friday, July 27, 2012 9:14 PM
  • Hi Ivanzinho,

    I am working on to integrate Evil Code Monkey in VS2012. i found this hasn't been integrated. please let us know how could you do it.

    Tuesday, September 23, 2014 1:14 PM