Locked Rule Idea - Use Create and Not New

  • 14. července 2005 11:24
     
     
    Programming visual basic.net ISBN 0735620598 page 1307 has the following 'rule'

    When using SymmetricAlgorithm, Rijndael etc on cryto routines, you should do the following

    Dim encr as SymmetricAlgorithm = SymmetricAlgorithm.Create()

    and not use new, so your code is generic and can work with different machine level defaults.

    I don't know if this applies to other creation routines.

    I can't see such a rule in the existing list but it looks like a good one for FxCop

Všechny reakce

  • 14. července 2005 14:04
     
     Odpovědět

    I agree, this is a good rule idea, but it's not one we're likely to get to for FxCop because that tool is primarily dedicated to general correctness and design issues for reusable library.

    There is a lot of knowledge around usage of specific API sets that would be great to capture in custom rules (your suggestion is a fine example). Language compatibility is another area that's fairly ripe for new rules and which the FxCop team is unlikely to tackle.

    This is one reason why we're committed to getting a solid custom rules development SDK together as soon as possible. Current time-line for this looks to be within 6 months of shipping VS2005.

    Michael Fanning
    VSTS Development: Code Analysis

  • 14. července 2005 15:46
     
     
    The use of static creation methods is considered less usable than instance methods -- this is what I was told by the usability expert Steven Clarke. So you should avoid creating them in your own API. There is an alternative approach, a wrapper class that internally creates a machine specific object, and delegates all method calls to that object. But it is more work.
  • 14. července 2005 19:24
     
     
    I think that the API does have a machine specific object, and does as is recommended.

    As it has been pointed out, you don't necessarily want to code up API specific rules