Is Your C# Optimized?<br />Woody Pewitt<br />@woodyp<br />woody@pewitt.org<br />
What the heck is optimized C#?<br />
Optimized For<br />Speed<br />Maintainability<br />Flexibility<br />etc…<br />
You Want Speed?<br />Visual Studio Profiler<br />Third-party profilers<br />System.Diagnostics<br />
You Want Maintainability?<br />Get a coding standard!<br />Use A code metric<br />Refactor RefactorRefactor!<br />
Coding Standards<br />Where do you get them?<br />
Cyclomaticcomplexity<br />The complexity M is then defined as:<br />M = E - N + 2P<br />Where<br />E = the number of edges...
Code refactoring<br />Code refactoring is "disciplined technique for restructuring an existing body of code, altering its ...
DXCore<br />DevExpress<br />http://www.devexpress.com/Products/Visual_Studio_Add-in/DXCore/<br />CodeRush and DXCore Commu...
Exceptions<br />Are they good?<br />Try<br />{<br />conn.Close();<br />}<br />catch (InvalidOperationException ex)<br />{<...
Guidelines: Exceptions<br />Don’t<br />Write unnecessary try-catch code<br />Behavior is correct by default<br />Rethrow u...
What’s Wrong?<br />public void TransmitResponse(ArrayList responses, <br />StreamWriterstreamWriter)<br />{<br />foreach (...
GC.Collect<br />public void TransmitResponse(ArrayList responses, <br />StreamWriterstreamWriter)<br />{<br />foreach (Dat...
Calling GC.Collect<br />Bad because...<br />Each collection has overhead<br />Overhead not really related to # of “dead ob...
What’s Wrong?<br />public override string ToString()<br />{<br />   string fullString = "";<br />   foreach (string s in s...
Allocation in Loop<br />public override string ToString()<br />{<br />   string fullString = "";<br />   foreach (string s...
A better way<br />public string ToString()<br />{<br />   StringBuilder fullString = new StringBuilder();<br />   foreach ...
What’s Wrong?<br />enum ResponseType { ReturnValues, InvalidInput }<br />string CreateWebText(string userInput, ResponseTy...
Checking – Method #1<br />string CreateWebText(string userInput, ResponseType operation) {<br />   if (!Enum.IsDefined(typ...
Checking – preferred<br />string CreateWebText(string userInput, ResponseType operation) {<br />   switch (operation) {<br...
Guidelines: enums<br />Don’t<br />Assume that enums can only have defined values<br />Assume that the set of defined value...
Questions?<br />Woody Pewitt<br />@woodyp<br />woody@pewitt.org<br />
Upcoming SlideShare
Loading in...5
×

Is your C# optimized

1,209

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,209
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Demo VS 2010 Profiler…
  • http://duckduckgo.com/?q=.net%20coding%20standards&amp;kp=-1&amp;kn=1http://msdn.microsoft.com/en-us/library/czefa0ke(v=VS.71).aspx
  • http://en.wikipedia.org/wiki/Code_refactoringhttp://c2.com/cgi/fullSearch - Ward Cunningham
  • Is your C# optimized

    1. 1. Is Your C# Optimized?<br />Woody Pewitt<br />@woodyp<br />woody@pewitt.org<br />
    2. 2.
    3. 3. What the heck is optimized C#?<br />
    4. 4. Optimized For<br />Speed<br />Maintainability<br />Flexibility<br />etc…<br />
    5. 5. You Want Speed?<br />Visual Studio Profiler<br />Third-party profilers<br />System.Diagnostics<br />
    6. 6. You Want Maintainability?<br />Get a coding standard!<br />Use A code metric<br />Refactor RefactorRefactor!<br />
    7. 7. Coding Standards<br />Where do you get them?<br />
    8. 8. Cyclomaticcomplexity<br />The complexity M is then defined as:<br />M = E - N + 2P<br />Where<br />E = the number of edges of the graph<br />N = the number of nodes of the graph<br />P = the number of connected components<br />
    9. 9. Code refactoring<br />Code refactoring is "disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior"<br />
    10. 10. DXCore<br />DevExpress<br />http://www.devexpress.com/Products/Visual_Studio_Add-in/DXCore/<br />CodeRush and DXCore Community Plugins<br />http://code.google.com/p/dxcorecommunityplugins/<br />
    11. 11. Exceptions<br />Are they good?<br />Try<br />{<br />conn.Close();<br />}<br />catch (InvalidOperationException ex)<br />{<br />Console.WriteLine(ex.GetType().FullName);<br />Console.WriteLine(ex.Message);<br />}<br />if (conn.State != ConnectionState.Closed)<br />{<br />conn.Close();<br />} <br />
    12. 12. Guidelines: Exceptions<br />Don’t<br />Write unnecessary try-catch code<br />Behavior is correct by default<br />Rethrow using “throw e” <br />Loses stack frame information<br />Wrap in larger exception<br />Unless you’re sure nobody would ever want to “look inside”<br />Require an exception in a common path<br />
    13. 13. What’s Wrong?<br />public void TransmitResponse(ArrayList responses, <br />StreamWriterstreamWriter)<br />{<br />foreach (DataResponse response in responses)<br /> {<br />NetworkResponsenetworkResponse = response.GetNetwork();<br />networkResponse.Send(streamWriter);<br /> }<br />GC.Collect(); // clean up temporary objects<br />}<br />10<br />
    14. 14. GC.Collect<br />public void TransmitResponse(ArrayList responses, <br />StreamWriterstreamWriter)<br />{<br />foreach (DataResponse response in responses)<br /> {<br />NetworkResponsenetworkResponse = response.GetNetwork();<br />networkResponse.Send(streamWriter);<br /> }<br />GC.Collect(); // clean up temporary objects<br />}<br />
    15. 15. Calling GC.Collect<br />Bad because...<br />Each collection has overhead<br />Overhead not really related to # of “dead objects”<br />Collecting one object is as expensive (roughly) as collecting one thousand<br />Especially bad because...<br />GC.Collect() does the most expensive collection<br />
    16. 16. What’s Wrong?<br />public override string ToString()<br />{<br /> string fullString = "";<br /> foreach (string s in strings)<br /> {<br /> fullString += s + " : ";<br /> }<br /> return fullString;<br />}<br />10<br />
    17. 17. Allocation in Loop<br />public override string ToString()<br />{<br /> string fullString = "";<br /> foreach (string s in strings)<br /> {<br />fullString += s + " : ";<br /> }<br /> return fullString;<br />}<br />
    18. 18. A better way<br />public string ToString()<br />{<br /> StringBuilder fullString = new StringBuilder();<br /> foreach (string s in strings)<br />{<br />fullString.Append(s);<br />fullString.Append(“:”);<br /> }<br /> return fullString.ToString();<br />}<br />
    19. 19. What’s Wrong?<br />enum ResponseType { ReturnValues, InvalidInput }<br />string CreateWebText(string userInput, ResponseType operation) {<br /> switch (operation) {<br /> case ResponseType.ReturnValues:<br /> userInput = "<h1>Values</h1>" + FilterOutBadStuff(userInput);<br /> break;<br /> case ResponseType.InvalidInput:<br /> userInput = "<h1>Invalid</h1>" + FilterOutBadStuff(userInput);<br /> break;<br /> }<br /> return userInput;<br />}<br />s = CreateWebText(s, (ResponseType) 133); <br />10<br />
    20. 20. Checking – Method #1<br />string CreateWebText(string userInput, ResponseType operation) {<br /> if (!Enum.IsDefined(typeof(ResponseType), operation)<br /> throw new InvalidArgumentException(...);<br /> switch (operation) {<br /> case ResponseType.ReturnValues:<br />userInput = "<h1>Values</h1>" + FilterOutBadStuff(userInput);<br /> break;<br /> case ResponseType.InvalidInput:<br />userInput = "<h1>Invalid</h1>" + FilterOutBadStuff(userInput);<br /> break;<br /> }<br /> return userInput;<br />}<br />enumResponseType { <br />ReturnValues, <br />InvalidInput,<br />DumpValues,<br />}<br />
    21. 21. Checking – preferred<br />string CreateWebText(string userInput, ResponseType operation) {<br /> switch (operation) {<br /> case ResponseType.ReturnValues:<br />userInput = "<h1>Values</h1>" + FilterOutBadStuff(userInput);<br /> break;<br /> case ResponseType.InvalidInput:<br />userInput = "<h1>Invalid</h1>" + FilterOutBadStuff(userInput);<br /> break;<br /> default:<br /> throw new InvalidArgumentException(...);<br /> }<br /> return userInput;<br />}<br />
    22. 22. Guidelines: enums<br />Don’t<br />Assume that enums can only have defined values<br />Assume that the set of defined values will never change<br />
    23. 23. Questions?<br />Woody Pewitt<br />@woodyp<br />woody@pewitt.org<br />

    ×