[Check Abnormal net]-20160401-remove the code odor

Source: Internet
Author: User

Why do we need quality code?

    • Agile development methods are used to respond to feedback that requires code to make a lot of changes.
    • If the program does not express in a good way, the program will be difficult to read, difficult to maintain, difficult to modify.

  What is code odor?

    • Code odor is a bad writing code caused by a smelly feeling, a program where there will be a problem feeling
    • Odor is more from a intuition than a standard that can be checked, and when you see the interesting code, you "feel" it.
    • If you do not remove the odor, you will soon get used to the smell, no longer aware of it
    • You can write smelly code in any language: even in the simplest and most secure language, you can make a fool out of genius:)
    • We often don't realize that we're writing very smelly code, and we often need outsiders to point this out to us.
      • Note : If you do not want to deliberately criticize someone's program, do not say "too stupid", to say "Oh, this is very interesting ...." There's a better way to do it, you know?

  Duplicate code

    • Causes the same error in many places in the program.
    • Indian boy : Every two months we'll make the same mistake again.
    • Venkat: Have you removed the duplicated code?
    • Indian boy : You said this method is good!

  Unnecessarily complex

    • Programmers are essentially happy to deal with complex problems
    • The most complex and terrifying

  Exception handling

    • Q: What's worse than an empty exception capture code?
      • try{.} catch (Exception e) {}
    • A: An empty exception capture code with comments!
      • try{.} catch (Exception e) {//Is this required?}
    • Java exception check: Good or bad?
    • If you don't want to handle an exception, pass it down.
    • If you want to catch two exceptions, use two catch codes, do not write only one and use the IF condition to handle

  Switch statement & judging by type criteria

    • Switch statements and conditional judgments by type can often be used in place of polymorphism

  Long method

    • You can't see the whole method on one screen.
    • This usually means a way to take on this multi-tasking
    • Difficult to debug
    • Not tested
    • Difficult to reuse, causing programmers to copy and paste duplicated code from other parts of the method
    • Complex conditional Statements challenge the logical analysis capabilities of the brain
    • Method Length: Organization induction level is more important than control code line number

  Method composition Pattern

    • All statements in a method must be at the same inductive level

  Useless annotations

    • Make code self-confessed
    • Label why this, not how it is
    • Describing the performance of a method equals repeating performance
    • Such comments are equivalent to repeating the code again
      • i + = 1//increment
    • A comment in the long method that describes how this method has a different function
      • Extract the function fragments into small methods & Delete comments
    • IDE excrement: The IDE automatically generates comment blank placeholders
    • Bad annotations are usually generated in tdd*
      • * (Tdd:threat Driven development, scare-driven development)-you should write a comment for the representation of the method, you should write a comment for the long method, etc.
    • Comments in the product:
      • God bless, I really don't know what that means.

  Variable name

    • Use a name that can be ideographic
    • Do not name with a single letter
    • Do not use a name that is too long

  Inherited

    • Inherited more is being abused
    • Combinations are usually better than inheritance
    • Use inheritance in a one-to-one relationship to meet the Liskov substitution principle
    • do not use inheritance to implement method reuse
    • When reusing a method, the delegate is a better choice

  The language of the sticky hand

    • This language is more likely to lead to mistakes

  The most smelly code

    • Lengthy classes
    • Duplicate code
    • Methods of elimination
    • Non-essential molding (CAST)
    • Over-use design patterns

  Code in addition to flavor

    • Code Review!
      • Write it out as soon as possible
      • To be incrementally
      • To review test cases
    • Can be programmed with pairing
      • But keep a constant change to your partner, or you'll get used to your scent and no longer be aware.
      • Pair of partners one or two days exchange

  Some design principles

    • High polymerization
    • Low coupling
    • Demeter law [Don't tell me, I'll let you know]
    • Liskov substitution principle
    • Let it run, let it go again, let it go fast.
    • Development/Closure Principles
    • Reverse dependency
    • Single Responsibility Principle

  Some reference books

    • Code neat Way (clean code)
    • Code Complete 2
    • The path of Programmer's cultivation (the pragmatic Programmer)
    • The path of agile development (practices of an agile Developer)
    • Smalltalk Best Practice Patterns
    • Implementation mode (Implementation Patterns) (from @protoiyer)

  Ask and Answer

    • about using code detection tools, such as PMD: Such a tool is very useful, it allows you to catch a very straightforward problem, so that your code review work focused on high-level design principles issues
    • about the tools attached to the IDE : Don't run them yourself. Let these tools run automatically (or intelligently) in the background
    • Need refactoring in a dynamic language : There aren't too many automated refactoring tools in a dynamic language, but programmers should still refactor manually
    • Design Patterns for dynamic languages : Each language has its own patterns and features. Example: Smalltalk's execute around method mode
    • About mastering multiple languages
      • You should know how to deal with a problem in multiple paradigms, multiple styles and multiple ways
      • The characteristic method that one language has learned to apply to other languages
      • Know the individual risks of different ways
    • about programming language trends : Strong interest in functional programming and mobile device programming
    • about writing: a long time to think about the topics in the book, do more discussion of this topic, absorb the essence. When you start writing, you're ready to write in 2 weeks.
    • about Thinking literature: It's useful to think about literature, but you should also look at critical thinking articles about how you think (double loop learning?).
    • about Learning : Working with other people in the user group to communicate and discuss. You can't learn everything, but try to narrow your own "you don't know what you don't know" and make it "you know you don't know"

[Check Abnormal net]-20160401-remove the code odor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.