This ppt is based on the book "Clean Code" written by Robert C Martin. It's a wonderful book and cover all the mistakes that we do while writing bad code. I am sharing few good practices shared in the book, hope you will like it
What is clean code Is it Readable? Is it Maintainable? Is it Reusable? Is it Testable?
What is clean code Should be Readable like a well written book. Should be crisp. Contain only what is necessary. Provides one way rather than many ways of doing one thing. Does one thing well. Each class, function exposes single minded attitude.
What is clean code Easier for other people to enhance it. Smaller is better. Contains no duplication Close to expectations. No surprises
What is clean code“You need to write that minimizes the time it would take someone else to understand it – even if that someone else is you – Dustin Boswell and Trevor Foucher”
Do you know? Time spent reading versus writing is well over 10:1if you want your code to be easy to write, make it easy to read
What makes us write bad code? Were you trying to go fast? Tired of working on this program and wanted it to be over. Looked at the backlog To clean it up later. “We forget later equals never”
Why does good code rots? Requirements change Tight schedule Stupid Managers ...
Suppose if patient demands to stop hand washing because it’s taking too much time.Doctor will refuse to do that because he knows the risk involved in it. Fault is Ours. We are Unprofessional.
Meaningful NamesChoosing meaningful name takes time but saves more than it takes
Rules Name should tell you why it exists, what it does, and how it is used e.g: int elapsedTimeInDays Make meaningful distinctions. e.g: customerInfo is indistinguishable from customer Use Pronounceable names. e.g: genymdhms
Rules Cont... No magic String or magic numbers. Use searchable names e.g; MAX_CLASSES_PER_STUDENT is easily searchable rather than 7. Pick one word per concept. eg: it’s confusing to have fetch, retrieve, and get as equivalent methods for different classes.
Rules Cont... You also don’t need to prefix member variables. e.g; personName variable in Person class Class names should have noun or noun phrase names like Customer, Account, AddressParser etc. Method name should start with the verb. e.g; postPayment, save, delete etc.
Rules Cont... Use problem domain names. Variable holding collection should be plural. e.g; persons, employees If a variable is expected to have a default value, then assign it at the time of declaration
Rules Cont... Don’t be cute. Choose clarity over entertainment value. Use kill rather than whack. Say what you mean. Mean what you say.
FunctionsShould be easy to read and understand which communicates its intent
Rules Small should be smaller. Avoid more the 10 lines. Do not write more than 120 character in one line ( Line should be visible at one glance) Should be properly indented. It should not be more than 2. Blocks in if, else or loops should be only 1 line. Probably that 1 line should be a method call.
Rules Cont.. Do one thing. Should do it well. Should do it only. Avoid multiple return statements. Long descriptive name is better than long comment. Well defined parameters (with descriptive names) are better than a map as parameter.
Rules Cont... Step Down Rule – Code read from top to bottom. Function Arguments More than three (polyadic) requires very special justification—and then shouldn’t be used anyway. Passing flag argument is a terrible practice. It loudly proclaims that the function does more than one thing.
Rules Cont... Has no side effects. checkPassword method should only check whether password is correct or not, it should not initialize the user session. If we want to initialize the session then method name should be checkPasswordAndInitializeSession. Function should either change the state of object or it should return some information about the object. Doing both often leads to confusion.
Rules Cont... Prefer exception to returning error codes. Extract body of try catch blocks into method to increase the readability and structure of the code. DRY (Do not Repeat Yourself).
“Leave the campground cleaner than you found it”