Clean-Coding-A-Practical-Guide11111.pptx

irfankhatib440 7 views 7 slides Aug 31, 2025
Slide 1
Slide 1 of 7
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7

About This Presentation

what is clean code in FEWD


Slide Content

Clean Coding: A Practical Guide Clean coding is more than just writing error-free code. It's about creating code that's easy to understand, modify, and maintain. Join us on a journey to learn the principles and best practices that will make you a clean code master! by Irfan Khatib

Naming Variables Like a Pro (5 min) Think of variable names as labels for a box. The label should describe what's inside so others can easily understand. Key Principles Be Descriptive: ❌ Avoid: let a = 10; ✅ Use: let userAge = 10; (“a” is too vague, but “userAge” tells us exactly what it represents.) Use Consistent Naming Conventions: Use camelCase for variables and functions (e.g., userName, calculateTotal). Use UPPERCASE for constants (e.g., PI, MAX_USERS). Avoid Shortened or Cryptic Names: ❌ Avoid: let usrNm; ✅ Use: let userName;

Writing Elegant and Efficient Functions (15 min) Functions are like recipes – they should be simple, clear, and focused on one task. Key Principles Follow the Single Responsibility Principle (SRP): A function should do one thing and do it well. ❌ Avoid:
function calculateAndLogUserDetails(user) { // Calculate logic // Logging logic } ✅ Use:

function calculateUserDetails(user) { // Calculation logic } function logUserDetails(user) { // Logging logic } Keep Functions Short: Ideally, a function should fit on one screen without scrolling. Use Descriptive Function Names: ❌ Avoid: function x(y) ✅ Use: function calculateTotalPrice(cartItems) Use Default Parameters: Avoid undefined errors by providing defaults:

function greet(name = 'Guest') { console.log(`Hello, ${name}!`); }

Formatting and Commenting Code Like a True Artist Code formatting is like good handwriting – it makes your work legible and polished. Key Principles Indentation and Spacing: Use consistent indentation (2 or 4 spaces). Add blank lines between logical sections of code.

function calculateSum(a, b) { return a + b; } function calculateDifference(a, b) { return a - b; } Add Comments Where Necessary: Don’t comment on what the code does (the code itself should tell this). Comment why a specific logic is implemented.

// Check if user is an admin to grant special access if (user.role === 'admin') { // Allow access to all resources }

Add Comments Where Necessary: Don’t comment on what the code does (the code itself should tell this). Comment why a specific logic is implemented.

// Check if user is an admin to grant special access if (user.role === 'admin') { // Allow access to all resources } Organize Code Logically: Group related variables and functions together. Use Linting Tools: Tools like ESLint ensure your code follows consistent style rules. Common Formatting Issues to Avoid Overly Long Lines: Break long lines of code for better readability.

Summary of Clean Code Clean code is a mindset, not just a set of rules. Start with the fundamentals (naming, functions, formatting), embrace automated tools, continuously refactor, and always strive for simplicity. Every line of code should be easily understood by others.

Quiz and Doubt Clarification Test your knowledge with a short quiz, followed by an open Q&A session to clarify any remaining doubts. Feel free to ask questions and participate actively in the discussion. Let's explore clean code together and become better developers!
Tags