Nov 24, 2011

Parameters: You’re Doing It Wrong!

Having parameters for a method is perfectly fine however like anything, they can be used for evil. So let me give you a tip: If your code looks anything like this method signature (and I kid you not, this is a real method) then YOU”RE DOING IT WRONG!

SaveContentSetItem(ContentSetItem,String,String,Int32,Int32,Int32,Int32,DateTime,DateTime,DateTime,DateTime,
    DateTime,DateTime,DateTime ,DateTime,Boolean,Boolean,Boolean,Int32,Int32,Int32,Int32,Int32,Int32 ,Boolean,
    Boolean,Boolean,Boolean,Boolean,Single,Boolean,Boolean ,Boolean,Boolean,Boolean,Boolean,Boolean,Boolean,
    Boolean,Boolean ,Boolean,Boolean,Boolean,Boolean,Boolean,Boolean,Boolean,Boolean,
    FileLocation,String,Stream,String,FileDisplayFormat,Boolean,Stream)

Please, for the love of all things good, turn off your computer right now. Pack it in a box.  Put the box in a locked safe.  Put the safe in a bunker under a mountain. Seal the bunker using 40 foot thick concrete and collapse the entrance.  Place a minefield and barbed wire around the bunker, and never EVER WRITE A LINE OF CODE AGAIN!

11 comments:

  1. I found your problem. Simply replace Boolean with bool.

    You're welcome.

    ReplyDelete
  2. whoever wrote that...if they drove to work, might I suggest you take their car keys away from them?

    ReplyDelete
  3. This is how I imagine SharePoint APIs..

    ReplyDelete
  4. We have one justification for many parameters.
    We write an Excel Addin, and basically write some custom Excel "Functions" eg MYAVG(1,2,3)
    It's often necessary, in this scenario, to use 20 arguments on the end, open to the user, to add as many arguments as they need. Keeping in mind that the "API" into creating an Excel function has you creating a C# (or VB) function with the same number of arguments as the mapped Excel Function.

    This leaves us with dozens of functions with around 30 or so arguments - which map directly to Excel functions.
    Horrendous, but absolutely necessary.

    For the record, I agree with (Uncle) Bob Martin, that any more than 1 parameter is a code smell - not wrong, just a smell... that needs sniffing.

    ReplyDelete
  5. Some there is something wrong with this (used here pretty much every day)?

    public static SomeType SomeMethod(string, string, double, double, string, bool, bool, string, string, bool, int, string, string, string, string, string, double?, string, int, string, string, string, int, string, string, string, string, string, string, string, string, string, string, string, bool, bool, string, double?, int?, double, string, string, string, string, string, string, bool?, string, string, string, string, string, bool?, bool?, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, int?, string, string, string, string, string, string, string, int?, string, int?, string, string, string, string, List>, IEnumerable, string, string, string, string)

    ReplyDelete
  6. I'll leave my current role because I feel sick working on code like this :(

    Those lines means that system needs a complete rebuilding because mentality made such mistake sure did other terrible mistakes everywhere in that system.

    This is a clear indicator that system had NO *proper* design at first place.

    ReplyDelete
  7. Another example of your great sense of humour - love it.

    ReplyDelete
  8. O Dick, Where are u. Call me.

    ReplyDelete
  9. "any more than 1 parameter is a code smell"

    Correlation does not imply causation. Searching for this kind of "code smells" seems to me like you're losing the focus of a code review.

    To me, it looks like you're simply replacing one extreme case with another.

    ReplyDelete
  10. Why should you be so extreme in that? There is a problem which can be solved in a simpler way) But still I like the humor with which you write here!

    ReplyDelete