1 / 26

Keeping Your Code Clean

Keeping Your Code Clean. Atlanta Code Camp 10/24/2015. What is Clean Code?. Clean code is…. Readable (to HUMANS!) Simple, straight forward Organized Follow the language conventions Clean is NOT clever. Why is a clean code base important?. You should maintain your code because….

bradye
Download Presentation

Keeping Your Code Clean

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Keeping Your Code Clean Atlanta Code Camp 10/24/2015

  2. What is Clean Code?

  3. Clean code is… • Readable (to HUMANS!) • Simple, straight forward • Organized • Follow the language conventions • Clean is NOT clever.

  4. Why is a clean code base important?

  5. You should maintain your code because… • For the principle of it. • Kind of like housekeeping. Its not necessarily fun to do. But you don’t want to live in filth. • Saves time in the long run • Reduces confusion • It only takes marginally more time upfront.

  6. Good programs clean their code

  7. In the words of Martin Fowler… • "Any fool can write code that a computer can understand. Good programmers write code that humans can understand." Martin Fowler, Refactoring: Improving the Design of Existing Code

  8. Clean philosophy

  9. Basics Parts to Clean Code • Write it well in the first place • Fix it when you see it • Remember: it’s a gradual (and constant) process!

  10. Who made this mess??

  11. It was a team effort… • Lack of communication between team members • Incomplete understanding of code base • Software evolves • Even good code degrades • Deadlines • Inexperience (new developers)

  12. Some basic guidelines

  13. Naming • Important because it describes your code, should not need comments • Consistency in naming • I.e. “mouseID” vs “mouseIdentifier” vs “mouseCode” • Avoid acryonms • Per coding standards: • Classes use nouns • Methods are verbs • Accessorsstart with get • Mutators start with set • Predicates start with is

  14. Functions/Methods • VERY short • Does only ONE thing • Else, break into multiple methods • Or turn it into it’s own class • Should take no more than 2-3 parameters • Else, probably use an object (or throw away your code)

  15. If - Statements • Do not over nest statements • (Also inconsistent conditions)

  16. If - Statements • No overly complicated conditions

  17. Don’t Repeat Yourself (DRY) • Code Duplication =  • Arises slowly over time. • I learned this the hard way.

  18. Remove dead code • If you’re not using it, DELETE it • Don’t be a hoarder, you have version control

  19. DO NOT obfuscate your code • Make it clear

  20. From the “International Obfuscated C Code Contest”

  21. Basic Code Smells • Comments • Should know the code based on names • Comments are rarely up to date • Long methods • Single purpose • Unit test • Large Class • Long parameter list • Duplicated Code • Conditional Complexity • Dead Code

  22. My clients don’t about code cleanliness 

  23. Why there’s never time for it… • Clients and managers may not value it • It takes time from producing code • Procrastination • 2am is not the time to do it

  24. When to Refactor

  25. When to fit it in… • Slight improvements over time. • Don’t do it all at once. • Burn out • Only refactor in code/features that will be tested/reviewed • Bad code in production =  • It won’t ever be perfect, but its okay • Leave it better than you found it

  26. Visual Studio Code Analysis • There are other tools out there • But this is built-in and available • You/your company don’t have to buy anything • Use default rulesets or create custom rulesets.

More Related