top 5 ways to improve your code
DESCRIPTION
My TechDay 2011 presentation (ARC305)TRANSCRIPT
![Page 1: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/1.jpg)
Top 5 Ways to Improve Your CodeEric De CarufelAgile Coach
http://blog.decarufel.nethttp://pyxis-tech.com
![Page 2: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/2.jpg)
Introduction• Legacy code is code
without tests• Michael Feather in Working
effectively with legacy code• Without continuous
maintenance the code will degrade rapidly
• We need to detect and remove code smell
![Page 3: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/3.jpg)
Reduce class
hierarchy
Increase class collaboration
Reduce coupling
Increase cohesionUse one abstraction level per method
Use layer architectureExtract cross cutting concerns
1. Simplify conditionals
2. Improve code documentation
3. Improve method calls
4. Manage scope
5. Remove dead code
My Top 5 Improvements
![Page 4: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/4.jpg)
1. Simplify conditionals – Why?• Reduced complexity• Better readability• Better maintainability• Better reusability
![Page 5: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/5.jpg)
1. Simplify conditionals –When?• There is more than one condition (and / or)• There is too much code in the body• The condition is based on type• There are nested if statements• There are many decisions based on the
same info (if else if chain / switch case)
![Page 6: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/6.jpg)
1. Simplify conditionals – How?• Refactor conditional statements
• Decompose conditional code• Consolidate conditional expressions• Consolidate duplicate conditional fragments• Introduce null objects• Flatten nested if• Don't use negative conditions• Keep conditional statements lean
• Avoid conditional statements• Replace conditional code with polymorphism• Replace conditional logic with strategy• Replace conditional dispatchers with commands
![Page 7: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/7.jpg)
Decompose Conditional Code
![Page 8: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/8.jpg)
Consolidate Conditional Expressions
![Page 9: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/9.jpg)
Consolidate Duplicate Conditional Fragments
![Page 10: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/10.jpg)
Introduce Null Objects
![Page 11: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/11.jpg)
Flatten Nested if
![Page 12: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/12.jpg)
Don't Use Negative Conditions
![Page 13: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/13.jpg)
Keep Conditional Statements Lean• Do your best to have only one condition
• Use methods to combine multiple conditions• Invert your conditions if most (or all) the
code of your method is inside the "true" block• Make sure to avoid double negation
![Page 14: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/14.jpg)
Replace Conditional with Polymorphism
![Page 15: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/15.jpg)
Replace Conditional Logic with Strategy
![Page 16: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/16.jpg)
Replace Conditional Dispatcher with Command
![Page 17: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/17.jpg)
2. Delete comments – Why?• Better readability• Better maintainability• Fewer obsolete comments
![Page 18: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/18.jpg)
2. Delete comments – When?• Every time there is a comment that is not (useful)
information, intention, clarification, warning, TODO or amplification.
• Comments are the only content of a code section• Empty catch statements
• Your comments describe line by line what you are trying to do• Example:
// Getting connection string from configuration// Opening connection// Retrieving data// Closing connection
![Page 19: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/19.jpg)
2. Delete comments – How?• Replace comments with good naming
• Extract methods• Use meaningful names
• Write useful comments• Naming Conventions (MSDN: Guidelines for names)
• Properties• Enums• Events• Methods
![Page 20: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/20.jpg)
Extract Methods
![Page 21: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/21.jpg)
Use Meaningful Names• Use Intention-Revealing names• Avoid disinformation• Make meaningful distinctions• Use pronounceable names• Avoid encoding• Avoid mental mapping• Class names -> nouns not verbs• Method names -> verbs• Don't be cute. Use standard names• Solution Domain Names vs Problem Domain Names
![Page 22: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/22.jpg)
Properties• Do use PascalCase naming• Do name properties with a noun, noun phrase or
an adjective• Do Not use names that match Get method• Do name booleans with Can, Is or Has prefix
![Page 23: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/23.jpg)
Enums• Consider the first element to be the
default value• Do use PascalCasing naming• Single choice enumerations should
be singular• Bit fields enumerations should be
plural and have Flags attibute• Value of bit fields enumeration
should be coherent (Read & Write == ReadWrite)
![Page 24: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/24.jpg)
Events• Do use PascalCase naming• Do name event using a verb, present progressive for pre-event and past
for post-event
• Do provide a virtual version of the event to override
• Do provide a way to cancel pre-event
![Page 25: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/25.jpg)
Methods• Do give methods names that are verbs or
verb phrases• ProcessPayment
• Do express the return value in method name • CreateCustomer• GetInvoice
• Use consistent naming (Get, Fetch or Retrieve but not all in same context)
![Page 26: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/26.jpg)
3. Clarify contracts – Why?• Better performance• Better readability• Better reusability
![Page 27: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/27.jpg)
3. Clarify contracts – When?• There are too many parameters (how
many is too many?)• A method does more than one thing• A method uses out parameters• You need default values
![Page 28: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/28.jpg)
3. Clarify contracts – How?• Reduce number of parameters
• Introduce parameter object• Create overloads with less parameters• Use default values
• Function output• Return type value• Use out parameters
• Overload methods in proper order
![Page 29: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/29.jpg)
Introduce Parameter Object
![Page 30: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/30.jpg)
Create Overloads with Fewer Parameters
![Page 31: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/31.jpg)
Use Default Values
![Page 32: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/32.jpg)
4. Reduce scope – Why?• Avoid side effects• Better reusability• Better maintainability
![Page 33: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/33.jpg)
4. Reduce scope – When?• A field is used in only a few methods• Public members expose class behavior
![Page 34: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/34.jpg)
4. Reduce scope – How?• Reduce visibility
• Use protected• Use private• Use Internal
• Reduce scope• Move fields to method• Split class• Move local variable close to its use
• Reduce lifetime• Initialize late• Reduce references• Free early
![Page 35: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/35.jpg)
5. Remove Dead Code – Why?• Because you must• Better maintainability• Better performance• Better readability• 100% test coverage
![Page 36: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/36.jpg)
5.Remove Dead Code – When?• You know the code is dead• You think the code is dead• You want that code to be dead
![Page 37: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/37.jpg)
5. Remove Dead Code – How?• Identify and remove dead code
• Delete the code• Compile• Run the tests
• What is dead code?• Commented code.• Any line of code that is not covered by unit tests.
• Tools• There are tools that delete all code not covered by unit
tests.
![Page 38: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/38.jpg)
Next Steps• Improve your conditional code• Improve your documentation• Improve your method calls
![Page 39: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/39.jpg)
Resources• Refactoring – Improving the design of existing code
• Author: Martin Fowler• Edition: Addison Wesley• ISBN: 978-0-201-48567-7
• Refactoring to patterns (Martin Fowler signature)• Author: Joshua Kerievsky• Edition: Adison Wesley• ISBN: 978-0-321-21335-1
• Clean code – a handbook of agile software craftsmanship• Author: Robert C. Martin• Edition: Prentice Hall• ISBN: 978-0-132-35088-4
• Working effectively with legacy code• Author: Michael C. Feather• Edition: Prentice Hall• ISBN: 978-0-13-117705-5
![Page 40: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/40.jpg)
Related sessions• Day 2 – 08h45 to 10h00
• DEV373 – Testing with Microsoft Visuals Studio Test Professional and the new Team Lab 2010by Etienne Tremblay
• Day 2 – 10h30 to 11h45• ARC301 – Design by Contract (DbC) and Code
Contracts in Visual Studio 2010by Joel Hebert
![Page 41: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/41.jpg)
Remember To Complete Your Evaluations!
You could WIN a Samsung Focus Windows Phone 7!Let us know what you liked & disliked!Remember, 1=Bad, 5=Good Please provide comments!No purchase necessary. The contest is open to residents of Canada (excluding government employees). The Toronto Tech·Days evaluation form contest begins on October 25 th, 2011 and ends on October 26th, 2011. The Vancouver Tech·Days evaluation form contest begins on November 15 th, 2011 and ends on November 16th, 2011. The Montreal Tech·Days evaluation form contest begins on November 29th, 2011 and ends on November 30th, 2011. Participants can enter the contest in one of two ways: (1) complete and submit an evaluation form by the contest close date; or (2) provide contact information by the contest close date. The draw for Toronto will take place on October 31 st, 2011. The draw for Vancouver will take place on November 21st, 2011. The draw for Montreal will take place on December 5th, 2011. The chances of being selected depend upon the number of eligible entries. Selected participants will be contacted by phone and/or e-mail and will be required to answer correctly a time-limited skill-testing question. There are three (3) prizes available to be won. One (1) prize will be given away for each Tech·Days event in Toronto (October 25-26 2011), Vancouver (November 15-16 2011) and Montreal (November 29-30 2011). The prize consists of a Samsung Focus Windows Phone 7 (handset only; voice and/or data plan not included) (approximate retail value of $499 CAD). The prize will be delivered to the shipping address designated by the winner within 6-8 weeks. The winner may be required to sign a declaration and release form. For full contest rules, please see a Microsoft Tech·Days representative.
You can email any additional comments directly to [email protected] at any time.
![Page 42: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/42.jpg)
Q & A
![Page 43: Top 5 ways to improve your code](https://reader033.vdocument.in/reader033/viewer/2022051411/547ba1f25806b5db3f8b4642/html5/thumbnails/43.jpg)
© 2011 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.
The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this
presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.