Clean Code Principles

Here is a quiz about “Do you write good code ?”.

At the end of the quiz, you’ll be see your results, and replay false answer to discover the good one.

Warning

If your score is below average, call SED immediately ! 😆. Seriously, please come and talk with us to avoid doing major mistakes.

The button provides some help !

--- primary_color: steelblue --- ## Goal of Clean Code Select goals of applying clean code to a development project in this list - [x] code easier to read / understand - [x] allow fast responds to changes - [ ] Coding faster > Not directly. it take often more time to have a clean code. - [x] maintain the technical debt as lower as possible - [x] minimize risks to introduce a defect - [ ] optimized and fast-running code > 4 responses to find... :wink: ## DRY What is the DRY principle? 1. [ ] Do not drink alcohol when coding > It is mandatory ! 1. [ ] Do not run yourself 1. [x] Do not repeat yourself 1. [ ] Do read yourself ## KISS What is the KISS principle? 1. [ ] Kiss your co-workers when arriving > Not me please. 1. [x] Keep it simple, stupid 1. [ ] Keep it solid, secret 1. [ ] Know I'm shy but serious ## Dependency Inversion What is the Dependency Inversion Principle (DIP) ? 1. [x] A class should depend on abstractions rather than the detail 1. [ ] A class should depend on the detail rather than abstractions > When a coder don't know what to do, he develop an abstraction layer... ## Implement Options What is the cleaner way to implement a choice between 5 options in a class. 1. [ ] FIVE if nested conditions 1. [ ] FIVE if conditions (flat) 1. [ ] ONE switch case 1. [x] polymorphism ## Comments Chose the good answer. Are method comments useful? 1. [ ] always: each method should have its comment > No. Code, variables must be self-comprehensive. 1. [ ] never: I should be able to understand a method without comment > Never is to strong. 1. [ ] most of the time: no comment for accessors but comments for other methods 1. [x] never but in some specific cases: informative comment, explanation of intent, warning > For non-API methode of course ## Arguments What is the best ? 1. [ ] Pass a lot or arguments to a method/function to have every elements > A lot of args is a source of mistakes. 1. [x] Limit arguments number to 4 maximum, and use objects for more ## Boy Scout Rule What does mean the Boy scout rule "Leave the campground cleaner than you found it." in software development? 1. [x] improve the code each time you have the occasion to do it 1. [ ] have a clean development environment 1. [ ] delete code each time you touch to some code ## Magic Numbers Select good definitions of a "magic number" - [x] A number directly in code, like ```if ( a > 12) ...```, and it is bad. - [x] A string directly in code, like ```if ( name == "Alex") ...```, and it is bad. - [ ] A specific number to pass options to the compiler > No this is a **compiler directive** - [ ] A variable name containing a number, like ```a12 = ...``` > 2 good definitions... :wink: ## CamelCase convention Select good variables names in CamelCase - [x] relativePositionInSpace - [ ] relativepositioninspace - [ ] relative_Position_In_Space > This is Snake case - [x] numberOfElementsInBasket - [ ] Numberoffelementsinbasket > Camel case is the practice of writing phrases without spaces or punctuation. The format indicates the separation of words with a single capitalized letter, and the first word starting with either case. ## Conventions Select good conventions for clean code in this list - [ ] Never user Snake case - [ ] Use CamelCase or SnakeCase when you want - [x] Choose beetwen CamelCase and Snake case and never change in the project. - [x] Prefer english language for comment, variables, class... - [x] variables for array, list are in plurial - [x] One file per class - [ ] Most of the time, prefer embedded loops rather than methodes (for performance) > 4 good answers... :wink: ## Good Naming Variable What would be the best name (with respect to clean code) for `int d; // elapsed time in days`? 1. [ ] days > not enought meaningful. 1. [ ] elapsedTime > Can be acceptable if every time unit in the software is in days. 1. [x] elapsedTimeInDays > Right it's a descriptive naming and the comment is useless now. 1. [ ] duration > it is a wrong meaning 1. [ ] durationInDays > it is a wrong meaning > **Meaningful Names** > > - A meaningful name is one of the most important conventions. Always use a meaningful name for variables, functions, and others. Choose a name that expresses the meaning of your purpose. > > - If we need a function that will get the user's bank information, then we must not use a name like getUserInfo or something like that. We should use getUserBankInfo to be more specific. ## Basic Java Clean Code Select the `Java` code snippets which follow principles of clean code - [ ] ```java if (student.classes.length < 7) { // Do something } ``` > `7` is a *magic number* - [ ] ```java const yyyymmdstr = moment().format("YYYY/MM/DD"); ``` > variable name is not meaningful - [x] ```java public Booking book (Customer aCustomer, boolean isPremium) { if(isPremium) // logic for premium book else // logic for regular booking } ``` - [ ] ```java int nusers; // number of users. ``` > Variable name is too short and not meaningful. > **Good Practices for Clean Code** > 1. No Magic Numbers > 2. No Deep Nesting > 3. Missing Comments > 4. Avoid Large Functions > 5. Avoid Code Repetition > 6. Camel Case Variable Naming > 7. Meaningful Names > 8. Descriptive Naming Over Concise > 9. Use Consistent Verbs per Concept > 10. Use Nouns and Pascal Case for Class Name > 11. Capitalize Constant Values > 12. Avoid One-Letter Variable Names ## Clean Javascript Is this code clean (despite it is JS :)) ? ```js class CarouselRightArrow extends Component{ render() { return ( ); } }; ``` 1. [x] YES 1. [ ] NO ## Clean C Is this code clean ? ```Java extern int array_a[]; extern int array_b[]; int sum_a = 0; const NB_ELEMENTS = 4; for (int i = 0; i < NB_ELEMENTS; i++) sum_a += array_a[i]; int average_a = sum_a / NB_ELEMENTS; int sum_b = 0; for (int i = 0; i < NB_ELEMENTS; i++) sum_b += array_b[i]; int average_b = sum_b / NB_ELEMENTS; ``` 1. [x] YES > One letter variables for iterators is allowed 1. [ ] NO ## Java Is it clean ? ```java public class UserValidator { private Cryptographer cryptographer; public boolean checkPassword(String userName, String password) { User user = UserGateway.findByName(userName); if (user != User.NULL) { String codedPhrase = user.getPhraseEncodedByPassword(); String phrase = cryptographer.decrypt(codedPhrase, password); if ("Valid Password".equals(phrase)) { Session.initialize(); return true; } } return false; } } ``` 1. [ ] YES > - "Valid Password" is a *magic number*. > - The test ```if (user != User.NULL)``` must be non negative. 1. [x] NO > - "Valid Password" is a *magic number*. > - The test ```if (user != User.NULL)``` must be non negative.

The quiz only runs in your browser. Even if your score is bad ! 😉

No personnal datas about you or your score are kept. Only an anonymous ratio is store to help us to build future tutorials.