└── README.mdx /README.mdx: -------------------------------------------------------------------------------- 1 | # 5 Clean Code Principles Every Software Engineer Should Know 2 | 3 | Writing clean code is essential for creating maintainable, readable, and professional software. Whether you're preparing for a coding interview or working on a large-scale project, adhering to clean code principles can make a significant difference. In this blog post, we'll explore **five clean code principles** with practical examples in **C**, **C++**, **Rust**, **Java**, **PHP**, **JavaScript**, and **Python**. 4 | 5 | --- 6 | 7 | ## 1. Reduce Unnecessary Nesting 8 | 9 | Deeply nested code is hard to read and maintain. Flatten your code by using early exits (`continue`, `return`, or `break`) to simplify logic. 10 | 11 | ### **Example** 12 | 13 | #### C 14 | 15 | ```c 16 | // Bad: Nested if statements 17 | if (condition1) { 18 | if (condition2) { 19 | if (condition3) { 20 | do_something(); 21 | } 22 | } 23 | } 24 | 25 | // Good: Flattened with early exit 26 | if (!condition1) return; 27 | if (!condition2) return; 28 | if (!condition3) return; 29 | do_something(); 30 | ``` 31 | 32 | #### C++ 33 | 34 | ```cpp 35 | // Bad: Nested if statements 36 | if (condition1) { 37 | if (condition2) { 38 | if (condition3) { 39 | doSomething(); 40 | } 41 | } 42 | } 43 | 44 | // Good: Flattened with early exit 45 | if (!condition1) return; 46 | if (!condition2) return; 47 | if (!condition3) return; 48 | doSomething(); 49 | ``` 50 | 51 | #### Rust 52 | 53 | ```rust 54 | // Bad: Nested if statements 55 | if condition1 { 56 | if condition2 { 57 | if condition3 { 58 | do_something(); 59 | } 60 | } 61 | } 62 | 63 | // Good: Flattened with early exit 64 | if !condition1 { return; } 65 | if !condition2 { return; } 66 | if !condition3 { return; } 67 | do_something(); 68 | ``` 69 | 70 | #### Java 71 | 72 | ```java 73 | // Bad: Nested if statements 74 | if (condition1) { 75 | if (condition2) { 76 | if (condition3) { 77 | doSomething(); 78 | } 79 | } 80 | } 81 | 82 | // Good: Flattened with early exit 83 | if (!condition1) return; 84 | if (!condition2) return; 85 | if (!condition3) return; 86 | doSomething(); 87 | ``` 88 | 89 | #### PHP 90 | 91 | ```php 92 | // Bad: Nested if statements 93 | if ($condition1) { 94 | if ($condition2) { 95 | if ($condition3) { 96 | doSomething(); 97 | } 98 | } 99 | } 100 | 101 | // Good: Flattened with early exit 102 | if (!$condition1) return; 103 | if (!$condition2) return; 104 | if (!$condition3) return; 105 | doSomething(); 106 | ``` 107 | 108 | #### JavaScript 109 | 110 | ```javascript 111 | // Bad: Nested if statements 112 | if (condition1) { 113 | if (condition2) { 114 | if (condition3) { 115 | doSomething(); 116 | } 117 | } 118 | } 119 | 120 | // Good: Flattened with early exit 121 | if (!condition1) return; 122 | if (!condition2) return; 123 | if (!condition3) return; 124 | doSomething(); 125 | ``` 126 | 127 | #### Python 128 | 129 | ```python 130 | # Bad: Nested if statements 131 | if condition1: 132 | if condition2: 133 | if condition3: 134 | do_something() 135 | 136 | # Good: Flattened with early exit 137 | if not condition1: 138 | return 139 | if not condition2: 140 | return 141 | if not condition3: 142 | return 143 | do_something() 144 | ``` 145 | 146 | --- 147 | 148 | ## 2. Use Descriptive Variable Names 149 | 150 | Clear and descriptive variable names make your code self-explanatory. Avoid ambiguous names like `i`, `temp`, or `x`. 151 | 152 | ### **Example** 153 | 154 | #### C 155 | 156 | ```c 157 | // Bad: Ambiguous variable names 158 | int x = 10; 159 | int y = 5; 160 | int z = x + y; 161 | 162 | // Good: Descriptive variable names 163 | int total_price = 10; 164 | int discount = 5; 165 | int final_price = total_price - discount; 166 | ``` 167 | 168 | #### C++ 169 | 170 | ```cpp 171 | // Bad: Ambiguous variable names 172 | int x = 10; 173 | int y = 5; 174 | int z = x + y; 175 | 176 | // Good: Descriptive variable names 177 | int totalPrice = 10; 178 | int discount = 5; 179 | int finalPrice = totalPrice - discount; 180 | ``` 181 | 182 | #### Rust 183 | 184 | ```rust 185 | // Bad: Ambiguous variable names 186 | let x = 10; 187 | let y = 5; 188 | let z = x + y; 189 | 190 | // Good: Descriptive variable names 191 | let total_price = 10; 192 | let discount = 5; 193 | let final_price = total_price - discount; 194 | ``` 195 | 196 | #### Java 197 | 198 | ```java 199 | // Bad: Ambiguous variable names 200 | int x = 10; 201 | int y = 5; 202 | int z = x + y; 203 | 204 | // Good: Descriptive variable names 205 | int totalPrice = 10; 206 | int discount = 5; 207 | int finalPrice = totalPrice - discount; 208 | ``` 209 | 210 | #### PHP 211 | 212 | ```php 213 | // Bad: Ambiguous variable names 214 | $x = 10; 215 | $y = 5; 216 | $z = $x + $y; 217 | 218 | // Good: Descriptive variable names 219 | $total_price = 10; 220 | $discount = 5; 221 | $final_price = $total_price - $discount; 222 | ``` 223 | 224 | #### JavaScript 225 | 226 | ```javascript 227 | // Bad: Ambiguous variable names 228 | let x = 10; 229 | let y = 5; 230 | let z = x + y; 231 | 232 | // Good: Descriptive variable names 233 | let totalPrice = 10; 234 | let discount = 5; 235 | let finalPrice = totalPrice - discount; 236 | ``` 237 | 238 | #### Python 239 | 240 | ```python 241 | # Bad: Ambiguous variable names 242 | x = 10 243 | y = 5 244 | z = x + y 245 | 246 | # Good: Descriptive variable names 247 | total_price = 10 248 | discount = 5 249 | final_price = total_price - discount 250 | ``` 251 | 252 | --- 253 | 254 | ## 3. Single Responsibility Principle (SRP) 255 | 256 | Each function or class should have a single responsibility. Break down large functions into smaller, focused ones. 257 | 258 | ### **Example** 259 | 260 | #### C 261 | 262 | ```c 263 | // Bad: Monolithic function 264 | void process_order(Order order) { 265 | if (!order.is_valid()) return; 266 | update_inventory(order); 267 | generate_receipt(order); 268 | send_notification(order); 269 | } 270 | 271 | // Good: Split into smaller functions 272 | int is_order_valid(Order order) { 273 | return order.is_valid(); 274 | } 275 | 276 | void process_order(Order order) { 277 | if (!is_order_valid(order)) return; 278 | update_inventory(order); 279 | generate_receipt(order); 280 | send_notification(order); 281 | } 282 | ``` 283 | 284 | #### C++ 285 | 286 | ```cpp 287 | // Bad: Monolithic function 288 | void processOrder(Order order) { 289 | if (!order.isValid()) return; 290 | updateInventory(order); 291 | generateReceipt(order); 292 | sendNotification(order); 293 | } 294 | 295 | // Good: Split into smaller functions 296 | bool isOrderValid(Order order) { 297 | return order.isValid(); 298 | } 299 | 300 | void processOrder(Order order) { 301 | if (!isOrderValid(order)) return; 302 | updateInventory(order); 303 | generateReceipt(order); 304 | sendNotification(order); 305 | } 306 | ``` 307 | 308 | #### Rust 309 | 310 | ```rust 311 | // Bad: Monolithic function 312 | fn process_order(order: Order) { 313 | if !order.is_valid() { return; } 314 | update_inventory(order); 315 | generate_receipt(order); 316 | send_notification(order); 317 | } 318 | 319 | // Good: Split into smaller functions 320 | fn is_order_valid(order: &Order) -> bool { 321 | order.is_valid() 322 | } 323 | 324 | fn process_order(order: Order) { 325 | if !is_order_valid(&order) { return; } 326 | update_inventory(order); 327 | generate_receipt(order); 328 | send_notification(order); 329 | } 330 | ``` 331 | 332 | #### Java 333 | 334 | ```java 335 | // Bad: Monolithic function 336 | public void processOrder(Order order) { 337 | if (!order.isValid()) return; 338 | updateInventory(order); 339 | generateReceipt(order); 340 | sendNotification(order); 341 | } 342 | 343 | // Good: Split into smaller functions 344 | public boolean isOrderValid(Order order) { 345 | return order.isValid(); 346 | } 347 | 348 | public void processOrder(Order order) { 349 | if (!isOrderValid(order)) return; 350 | updateInventory(order); 351 | generateReceipt(order); 352 | sendNotification(order); 353 | } 354 | ``` 355 | 356 | #### PHP 357 | 358 | ```php 359 | // Bad: Monolithic function 360 | function processOrder($order) { 361 | if (!$order->isValid()) return; 362 | updateInventory($order); 363 | generateReceipt($order); 364 | sendNotification($order); 365 | } 366 | 367 | // Good: Split into smaller functions 368 | function isOrderValid($order) { 369 | return $order->isValid(); 370 | } 371 | 372 | function processOrder($order) { 373 | if (!isOrderValid($order)) return; 374 | updateInventory($order); 375 | generateReceipt($order); 376 | sendNotification($order); 377 | } 378 | ``` 379 | 380 | #### JavaScript 381 | 382 | ```javascript 383 | // Bad: Monolithic function 384 | function processOrder(order) { 385 | if (!order.isValid()) return; 386 | updateInventory(order); 387 | generateReceipt(order); 388 | sendNotification(order); 389 | } 390 | 391 | // Good: Split into smaller functions 392 | function isOrderValid(order) { 393 | return order.isValid(); 394 | } 395 | 396 | function processOrder(order) { 397 | if (!isOrderValid(order)) return; 398 | updateInventory(order); 399 | generateReceipt(order); 400 | sendNotification(order); 401 | } 402 | ``` 403 | 404 | #### Python 405 | 406 | ```python 407 | # Bad: Monolithic function 408 | def process_order(order): 409 | if not order.is_valid(): 410 | return 411 | update_inventory(order) 412 | generate_receipt(order) 413 | send_notification(order) 414 | 415 | # Good: Split into smaller functions 416 | def is_order_valid(order): 417 | return order.is_valid() 418 | 419 | def process_order(order): 420 | if not is_order_valid(order): 421 | return 422 | update_inventory(order) 423 | generate_receipt(order) 424 | send_notification(order) 425 | ``` 426 | 427 | --- 428 | 429 | ## 4. Avoid Magic Numbers 430 | 431 | Magic numbers are hardcoded values without context. Replace them with named constants. 432 | 433 | ### **Example** 434 | 435 | #### C 436 | 437 | ```c 438 | // Bad: Magic numbers 439 | int shipping_cost = weight * 5; 440 | 441 | // Good: Named constants 442 | #define SHIPPING_RATE 5 443 | int shipping_cost = weight * SHIPPING_RATE; 444 | ``` 445 | 446 | #### C++ 447 | 448 | ```cpp 449 | // Bad: Magic numbers 450 | int shippingCost = weight * 5; 451 | 452 | // Good: Named constants 453 | const int SHIPPING_RATE = 5; 454 | int shippingCost = weight * SHIPPING_RATE; 455 | ``` 456 | 457 | #### Rust 458 | 459 | ```rust 460 | // Bad: Magic numbers 461 | let shipping_cost = weight * 5; 462 | 463 | // Good: Named constants 464 | const SHIPPING_RATE: i32 = 5; 465 | let shipping_cost = weight * SHIPPING_RATE; 466 | ``` 467 | 468 | #### Java 469 | 470 | ```java 471 | // Bad: Magic numbers 472 | int shippingCost = weight * 5; 473 | 474 | // Good: Named constants 475 | final int SHIPPING_RATE = 5; 476 | int shippingCost = weight * SHIPPING_RATE; 477 | ``` 478 | 479 | #### PHP 480 | 481 | ```php 482 | // Bad: Magic numbers 483 | $shipping_cost = $weight * 5; 484 | 485 | // Good: Named constants 486 | define('SHIPPING_RATE', 5); 487 | $shipping_cost = $weight * SHIPPING_RATE; 488 | ``` 489 | 490 | #### JavaScript 491 | 492 | ```javascript 493 | // Bad: Magic numbers 494 | let shippingCost = weight * 5; 495 | 496 | // Good: Named constants 497 | const SHIPPING_RATE = 5; 498 | let shippingCost = weight * SHIPPING_RATE; 499 | ``` 500 | 501 | #### Python 502 | 503 | ```python 504 | # Bad: Magic numbers 505 | shipping_cost = weight * 5 506 | 507 | # Good: Named constants 508 | SHIPPING_RATE = 5 509 | shipping_cost = weight * SHIPPING_RATE 510 | ``` 511 | 512 | --- 513 | 514 | ## 5. Minimize Comments 515 | 516 | Write self-documenting code to reduce the need for comments. Use comments only for complex or non-obvious logic. 517 | 518 | ### **Example** 519 | 520 | #### C 521 | 522 | ```c 523 | // Bad: Excessive comments 524 | // Check if the user is eligible for a discount 525 | if (user.age > 65) { // Senior citizen discount 526 | apply_discount(user); 527 | } 528 | 529 | // Good: Self-documenting code 530 | #define SENIOR_CITIZEN_AGE 65 531 | if (user.age > SENIOR_CITIZEN_AGE) { 532 | apply_discount(user); 533 | } 534 | ``` 535 | 536 | #### C++ 537 | 538 | ```cpp 539 | // Bad: Excessive comments 540 | // Check if the user is eligible for a discount 541 | if (user.age > 65) { // Senior citizen discount 542 | applyDiscount(user); 543 | } 544 | 545 | // Good: Self-documenting code 546 | const int SENIOR_CITIZEN_AGE = 65; 547 | if (user.age > SENIOR_CITIZEN_AGE) { 548 | applyDiscount(user); 549 | } 550 | ``` 551 | 552 | #### Rust 553 | 554 | ```rust 555 | // Bad: Excessive comments 556 | // Check if the user is eligible for a discount 557 | if user.age > 65 { // Senior citizen discount 558 | apply_discount(user); 559 | } 560 | 561 | // Good: Self-documenting code 562 | const SENIOR_CITIZEN_AGE: i32 = 65; 563 | if user.age > SENIOR_CITIZEN_AGE { 564 | apply_discount(user); 565 | } 566 | ``` 567 | 568 | #### Java 569 | 570 | ```java 571 | // Bad: Excessive comments 572 | // Check if the user is eligible for a discount 573 | if (user.getAge() > 65) { // Senior citizen discount 574 | applyDiscount(user); 575 | } 576 | 577 | // Good: Self-documenting code 578 | final int SENIOR_CITIZEN_AGE = 65; 579 | if (user.getAge() > SENIOR_CITIZEN_AGE) { 580 | applyDiscount(user); 581 | } 582 | ``` 583 | 584 | #### PHP 585 | 586 | ```php 587 | // Bad: Excessive comments 588 | // Check if the user is eligible for a discount 589 | if ($user->age > 65) { // Senior citizen discount 590 | applyDiscount($user); 591 | } 592 | 593 | // Good: Self-documenting code 594 | define('SENIOR_CITIZEN_AGE', 65); 595 | if ($user->age > SENIOR_CITIZEN_AGE) { 596 | applyDiscount($user); 597 | } 598 | ``` 599 | 600 | #### JavaScript 601 | 602 | ```javascript 603 | // Bad: Excessive comments 604 | // Check if the user is eligible for a discount 605 | if (user.age > 65) { 606 | // Senior citizen discount 607 | applyDiscount(user); 608 | } 609 | 610 | // Good: Self-documenting code 611 | const SENIOR_CITIZEN_AGE = 65; 612 | if (user.age > SENIOR_CITIZEN_AGE) { 613 | applyDiscount(user); 614 | } 615 | ``` 616 | 617 | #### Python 618 | 619 | ```python 620 | # Bad: Excessive comments 621 | # Check if the user is eligible for a discount 622 | if user.age > 65: # Senior citizen discount 623 | apply_discount(user) 624 | 625 | # Good: Self-documenting code 626 | SENIOR_CITIZEN_AGE = 65 627 | if user.age > SENIOR_CITIZEN_AGE: 628 | apply_discount(user) 629 | ``` 630 | 631 | --- 632 | 633 | ## Conclusion 634 | 635 | By following these **five clean code principles**, you can write code that is: 636 | 637 | - **Readable**: Easy for others (and your future self) to understand. 638 | - **Maintainable**: Simplifies debugging, testing, and updating. 639 | - **Professional**: Demonstrates good coding practices, especially in interviews. 640 | 641 | Remember, clean code is not just about functionality—it's about creating a positive experience for everyone who interacts with your code. Start applying these principles today, and watch your coding skills improve! 642 | 643 | Happy coding! 🚀 644 | --------------------------------------------------------------------------------