260 likes | 280 Views
Understand the significance of clean code in software development and learn the basic principles to maintain a clean code base. Discover why clean code is essential, how to avoid common pitfalls, and when to refactor for better code quality. Improve code readability, organization, and efficiency through simple guidelines and tools like Visual Studio Code Analysis.
E N D
Keeping Your Code Clean Atlanta Code Camp 10/24/2015
Clean code is… • Readable (to HUMANS!) • Simple, straight forward • Organized • Follow the language conventions • Clean is NOT clever.
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.
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
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!
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)
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
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)
If - Statements • Do not over nest statements • (Also inconsistent conditions)
If - Statements • No overly complicated conditions
Don’t Repeat Yourself (DRY) • Code Duplication = • Arises slowly over time. • I learned this the hard way.
Remove dead code • If you’re not using it, DELETE it • Don’t be a hoarder, you have version control
DO NOT obfuscate your code • Make it clear
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
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
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
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.