SlideShare a Scribd company logo
1 of 21
CLEAN CODING
AHMED HELMY | SENIOR SOFTWARE DEVELOPER
AGENDA
 What Are Code Reviews?
 What Is It?
 Why Do It?
 How to Do It??
 How to Not Do It?
 Lessons for C#
 Lessons for Classes
 Lessons for ASP.NET
 Lessons for Architecture
 References and Discussion
WHAT ARE CODE REVIEWS?
 What Is It?
 Read the code
 Learn by looking at your own codebase
 Search for patterns and anti-patterns
 Quantify the quality of your code
 Not exhaustive… spot check
WHAT ARE CODE REVIEWS?
 Why Do Code Review?
The job of a code review is to determine
what is being done well as well as what can
be done better. It is not a witch hunt.
Sharing the findings of a code review
makes all developers involved better.
WHAT ARE CODE REVIEWS?
 How to Review Code?
 Taking your experience to bear…
 Find things to laud and things to fix
 Look of common problems, not one-off issues
 Review 200-400 lines at a time
 Review the whole code-base is time consuming
 No code is perfect code
 Maintainable code is a feature!
WHAT ARE CODE REVIEWS?
 How to Not Review Code?
 There are patterns to reviewing code – there are anti-patterns too
 Don’t get emotional
 Don’t focus on blame
 Don’t redesign the code
 Don’t judge based on how you would have code it
 Don’t include one-off problems
 Trying to find patterns to fix, not single bugs
LESSONS FOR C#
 Magic Numbers and Strings
 Replace with strongly typed variables
 Large Method Bodies
 More than one-page span
 Avoid comments if there is another way to describe your code (self-descriptive)
 const vs. readonly
 If
LESSONS FOR C#
 String Concatenation
 string.Concat()
 string.Format()
 StringBuilder
LESSONS FOR C#
 Bad IDisposable Usage
 Use using scope statement
LESSONS FOR CLASSES
 Interfaces and Abstract Classes
 You can only have one base class, but you can implement multiple interfaces
 “Is A” relationship for abstract class
 “I Can” relationship for interfaces
 Abstract class can have default implementation
 Excess Derivation
 Class inheritance trees
 Static classes and Singletons
 Instead of implementing Singleton, you can use static classes instead as it’s a singleton, it will reduce code to implement singleton
and easier to use
 Nested class usage
 Using nested class reduce simplicity and maintainability
 Hiding class members with ‘new’
 Auto Properties vs. Private Members
 If no business needed to be done in setting and getting, use auto properties instead of backing fields
LESSONS FOR ASP.NET
 Using Encoding in Markup Files
 Making sure you are helping browsers by telling them what encoding you’re using
 Make sure you are not only using the meta tag
 But you also make sure file> advanced save options use same character set
LESSONS FOR ASP.NET
 Edge Versions of Browsers
 If you are using HTML5 websites, make sure the browsers know you want to use edge rendering
 If your app using older browser maybe the tag is ignored
 The tag ensure you are using latest version of browser you are using
LESSONS FOR ASP.NET
 Embedding JS/CSS on a Page
 Eliminating embedded JavaScript and Style Sheets helps the browser improve
caching of your code
 Lose page caching as it’s imported every time it’s called
 Instead move it to files
LESSONS FOR ASP.NET
 Content Delivery Networks
 Use Content Delivery Networks so that browser caching can improve startup time for new users
 Bundles.
 Concatenating CSS
 Reduce round-trips with fewer CSS files means you’re less likely to run into browser limits of CSS
LESSONS FOR ASP.NET
 Razor Lessons
 The less code inside Razor file the better
 Remove C# code from view
 Also notice using statement in view
LESSONS FOR ASP.NET
 Caching
 Creating cached objects inside per-request classes isn’t necessary and adds complexity
 Overuse of Web API attributed Routing
 Routing tables are still a valid way of doing routing for much of your code
 Use Route Prefix
 Use Routing config
LESSONS FOR ARCHITECTURE
 Not Invented Here Syndrome
 Deciding that you are smarter than the community of the framework will lead to trouble
 Collective intelligence will always be smarter than one developer or even a small team
 Don’t build it unless you’re doing something very special
 That’s why communities exist
 Examples
 Custom data caching layers, instead of relying on caching systems
 In-house ORM/Data Access
 MVC Filters with Business Logic, It’s hard to be tested, instead put it in your business layer
 Don’t to build a great piece of code to prove you are worth (you want be the lead architect or head of
development)
 Buy, borrow, key parts of your system and focus on the hard problems, domain specific problems
LESSONS FOR ARCHITECTURE
 Too Many Layers
 Creating layers of abstraction for their own sake is seldom a good idea
 As Developers, we like architecture diagrams, we like to see the big picture of how system works
 We can get caught in the idea that more layers and abstraction layers are better… THEY ARE NOT, it will add
complexity too
 If system will change from using SQL server to use MongoDB or any other engine, change is not that easy as you change your
abstraction layer for data access, it’s a huge architectural change and the smallest part you will be concerned with is your data
access layer (schema, stored procedures, storage, relations, business logic, etc…)
 A lot of DTOs (validation, same classes)
 Hops
 You need to maintain these layers
LESSONS FOR ARCHITECTURE
 Integration Problems
 Skipping continuous integration or unit testing is going to affect code quality
 Lack of unit tests
 You can’t determine code quality without tests
 Manual testing is good, but doesn’t scale
 If you are not running tests, your users are your testers
 Writing tests for client-side and server-side code
 When they are different (e.g. web)
 Most code smell originated in lack of testing
 Unit tests are more valuable for regression than creation
 Continuous integration
 Building code frequently improves code quality
 Integration problems are surfaced earlier
 Running automated unit tests helps find regression bugs
 Breaking the build should be enforced by shame, not reprimands
 On large teams, this becomes even more important
 You should know the state of your code at all times
LESSONS FOR ARCHITECTURE
 Dependency Injection
 Using Dependency Injection is great, but defeating with hard problems isn’t any better than none
 Circular Dependencies
 Logger with Repository
 Lazy Loading Problems in EF
 Understanding that your Entity Framework code is executing on the server is important
 Joins
 Large Resultsets in EF
 Paging
 Big DMX Problems in EF
 Sub diagrams
REFERENCES & DISCUSSION
 Lessons from Real World .NET Code Reviews by Shawn Wildermuth

More Related Content

Recently uploaded

Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile EnvironmentVictorSzoltysek
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 

Recently uploaded (20)

Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 

Featured

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 

Featured (20)

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 

Clean coding with Code Review

  • 1. CLEAN CODING AHMED HELMY | SENIOR SOFTWARE DEVELOPER
  • 2. AGENDA  What Are Code Reviews?  What Is It?  Why Do It?  How to Do It??  How to Not Do It?  Lessons for C#  Lessons for Classes  Lessons for ASP.NET  Lessons for Architecture  References and Discussion
  • 3. WHAT ARE CODE REVIEWS?  What Is It?  Read the code  Learn by looking at your own codebase  Search for patterns and anti-patterns  Quantify the quality of your code  Not exhaustive… spot check
  • 4. WHAT ARE CODE REVIEWS?  Why Do Code Review? The job of a code review is to determine what is being done well as well as what can be done better. It is not a witch hunt. Sharing the findings of a code review makes all developers involved better.
  • 5. WHAT ARE CODE REVIEWS?  How to Review Code?  Taking your experience to bear…  Find things to laud and things to fix  Look of common problems, not one-off issues  Review 200-400 lines at a time  Review the whole code-base is time consuming  No code is perfect code  Maintainable code is a feature!
  • 6. WHAT ARE CODE REVIEWS?  How to Not Review Code?  There are patterns to reviewing code – there are anti-patterns too  Don’t get emotional  Don’t focus on blame  Don’t redesign the code  Don’t judge based on how you would have code it  Don’t include one-off problems  Trying to find patterns to fix, not single bugs
  • 7. LESSONS FOR C#  Magic Numbers and Strings  Replace with strongly typed variables  Large Method Bodies  More than one-page span  Avoid comments if there is another way to describe your code (self-descriptive)  const vs. readonly  If
  • 8. LESSONS FOR C#  String Concatenation  string.Concat()  string.Format()  StringBuilder
  • 9. LESSONS FOR C#  Bad IDisposable Usage  Use using scope statement
  • 10. LESSONS FOR CLASSES  Interfaces and Abstract Classes  You can only have one base class, but you can implement multiple interfaces  “Is A” relationship for abstract class  “I Can” relationship for interfaces  Abstract class can have default implementation  Excess Derivation  Class inheritance trees  Static classes and Singletons  Instead of implementing Singleton, you can use static classes instead as it’s a singleton, it will reduce code to implement singleton and easier to use  Nested class usage  Using nested class reduce simplicity and maintainability  Hiding class members with ‘new’  Auto Properties vs. Private Members  If no business needed to be done in setting and getting, use auto properties instead of backing fields
  • 11. LESSONS FOR ASP.NET  Using Encoding in Markup Files  Making sure you are helping browsers by telling them what encoding you’re using  Make sure you are not only using the meta tag  But you also make sure file> advanced save options use same character set
  • 12. LESSONS FOR ASP.NET  Edge Versions of Browsers  If you are using HTML5 websites, make sure the browsers know you want to use edge rendering  If your app using older browser maybe the tag is ignored  The tag ensure you are using latest version of browser you are using
  • 13. LESSONS FOR ASP.NET  Embedding JS/CSS on a Page  Eliminating embedded JavaScript and Style Sheets helps the browser improve caching of your code  Lose page caching as it’s imported every time it’s called  Instead move it to files
  • 14. LESSONS FOR ASP.NET  Content Delivery Networks  Use Content Delivery Networks so that browser caching can improve startup time for new users  Bundles.  Concatenating CSS  Reduce round-trips with fewer CSS files means you’re less likely to run into browser limits of CSS
  • 15. LESSONS FOR ASP.NET  Razor Lessons  The less code inside Razor file the better  Remove C# code from view  Also notice using statement in view
  • 16. LESSONS FOR ASP.NET  Caching  Creating cached objects inside per-request classes isn’t necessary and adds complexity  Overuse of Web API attributed Routing  Routing tables are still a valid way of doing routing for much of your code  Use Route Prefix  Use Routing config
  • 17. LESSONS FOR ARCHITECTURE  Not Invented Here Syndrome  Deciding that you are smarter than the community of the framework will lead to trouble  Collective intelligence will always be smarter than one developer or even a small team  Don’t build it unless you’re doing something very special  That’s why communities exist  Examples  Custom data caching layers, instead of relying on caching systems  In-house ORM/Data Access  MVC Filters with Business Logic, It’s hard to be tested, instead put it in your business layer  Don’t to build a great piece of code to prove you are worth (you want be the lead architect or head of development)  Buy, borrow, key parts of your system and focus on the hard problems, domain specific problems
  • 18. LESSONS FOR ARCHITECTURE  Too Many Layers  Creating layers of abstraction for their own sake is seldom a good idea  As Developers, we like architecture diagrams, we like to see the big picture of how system works  We can get caught in the idea that more layers and abstraction layers are better… THEY ARE NOT, it will add complexity too  If system will change from using SQL server to use MongoDB or any other engine, change is not that easy as you change your abstraction layer for data access, it’s a huge architectural change and the smallest part you will be concerned with is your data access layer (schema, stored procedures, storage, relations, business logic, etc…)  A lot of DTOs (validation, same classes)  Hops  You need to maintain these layers
  • 19. LESSONS FOR ARCHITECTURE  Integration Problems  Skipping continuous integration or unit testing is going to affect code quality  Lack of unit tests  You can’t determine code quality without tests  Manual testing is good, but doesn’t scale  If you are not running tests, your users are your testers  Writing tests for client-side and server-side code  When they are different (e.g. web)  Most code smell originated in lack of testing  Unit tests are more valuable for regression than creation  Continuous integration  Building code frequently improves code quality  Integration problems are surfaced earlier  Running automated unit tests helps find regression bugs  Breaking the build should be enforced by shame, not reprimands  On large teams, this becomes even more important  You should know the state of your code at all times
  • 20. LESSONS FOR ARCHITECTURE  Dependency Injection  Using Dependency Injection is great, but defeating with hard problems isn’t any better than none  Circular Dependencies  Logger with Repository  Lazy Loading Problems in EF  Understanding that your Entity Framework code is executing on the server is important  Joins  Large Resultsets in EF  Paging  Big DMX Problems in EF  Sub diagrams
  • 21. REFERENCES & DISCUSSION  Lessons from Real World .NET Code Reviews by Shawn Wildermuth