無瑕的程式碼 Clean Code 心得分享

  • Published on
    12-Apr-2017

  • View
    18.017

  • Download
    0

Embed Size (px)

Transcript

  • Clean Code PIXNET

    PIXNET @ Win 17/03/01

  • () L O

    accountList

    (hypotenuse) : hp

    int a = 1;

    if (O == l) a = O1;else l = 01;

  • (a1, a2, , aN)

    ProductInfo vs ProductData

    source, destination

    public static void copyCharts(char a1[], char a2[]) { for (int i = 0; i < a1.length; i++) { a2[i] = a1[i]; }}

  • genymdhms() timestamp

  • bug

    5 WORK_DAYS_PER_WEEK

  • https://zh.wikipedia.org/wiki/

    lAccountNum"l"arru8NumberList8"arru8"szNamesz")

    m_dsc = name;

    https://zh.wikipedia.org/wiki/%E5%8C%88%E7%89%99%E5%88%A9%E5%91%BD%E5%90%8D%E6%B3%95

  • Customer

    Data, Info, Manager, Processor

  • postPayment

    Javabean

    (accessors): get

    (mutators): set

    (predicates): is

  • DeleteItems HolyHandGrenade ()

    EatMyShort() abort

  • IDE

  • Switch

    (SRP)

    (OCP)

    1 public Money caluculatePay(Employee e) throws InvalidEmployeeType { 2 switch (e.type) { 3 case COMMISSIONED: 4 return calculateCommissionedPay(e); 5 case HOURLY: 6 return calculateHourlyPay(e); 7 default: 8 throw new InvalidEmployeeType(e.type); 9 }10 }

  • Switch

    Switch (Abstract Factory)

    1 public Money caluculatePay(Employee e) throws InvalidEmployeeType { 2 switch (e.type) { 3 case COMMISSIONED: 4 return calculateCommissionedPay(e); 5 case HOURLY: 6 return calculateHourlyPay(e); 7 default: 8 throw new InvalidEmployeeType(e.type); 9 }10 }

  • Clean Code

  • (:niladic)

    (:monadic)

    (:dyadic)

    (:triadic)(:polyadic)

  • True: doSomething, False: SaySomething

  • writeField(name) vs writeField(outputStream, name)

    bug

    assertEquals(expected, actual)

    expected, actual

  • assertEquals(message, expected, actual)

    message expected

  • try/catch try catch

    1 public void delete(Page page) {2 try {3 deletePageAndAllReferences(pages);4 } catch (Exception e) {5 logError(e);6 }7 }

  • DRY (Dont Repeat Yourself.)

  • Guard Clause

  • // Copyright (c) 2003, 2004, 2005 by Object Mentor, Inc. All rights reserved.// Released under the terms of GNU General Public License version 2 or late.

  • TODO () TODO

  • -

  • -

    1 // Utility method that returns when this.closed is true. Throws an exception 2 // if the timeout is reached. 3 public synchronized void waitForClose(final long timeoutMillis) 4 throws Exception 5 { 6 if (!closed) { 7 wait(timeoutMillis); 8 if(!closed) 9 throw new Exception ("MockResponseSender could not be closed");10 }11 }

  • -

    1 /** 2 * 3 * @param title The title of the CD 4 * @param author The author of the CD 5 * @param tracks The number of tracks on the CD 6 * @param durationInMinutes The duration of the CD in munutes 7 */ 8 public void addCD(String title, String author, int tracks, int durationInMinutes) 9 {10 CD cd = new CD();11 cd.title = title;12 cd.author = author;13 cd.tracks = tracks;14 cd.duration = durationInMinutes;15 cdList.add(cd);16 }

  • -

    1 /* 2 * Default constructor. 3 */ 4 protected AnnualDateRule() {} 5 6 /* The day of the month. */ 7 private int dayOfMonth; 8 9 /*10 * Return the day of the month.11 *12 * @return the day of the month.13 */14 public int getDatofMonth()15 {16 return dayOfMonth;17 }

  • 1 // does the module from the global list depend on the2 // subsystem we are part of?3 if (semodule.getDependSubSystems().contains(subSysMod.getSubSystem())4 5 ArrayList moduleDpendees = smodule.getDependSubSystems();6 String outSubSystem = subSysMod.getSubSystem();7 if (moduleDpendees.contains(ourSubSystem))

  • (e.g. GIT, SVN)

    /* Added by Rick */

  • !!

  • 1 private static final Pattern pattern = Pattern.compile("'''(.+?)'''", Pattern.MULTILINE); 2 3 public BoldWidget(ParentWidge parent, String text) throws Exception { 4 super(parent); 5 6 Matcher match = pattern.matcher(text); 7 match.find(); 8 9 addChildWidgets(match.group(1));10 }

  • 1 public class ReporterConfig {2 private String m_className;3 private List m_properties = new ArrayList();4 5 public void addProperty($Property property) {6 m_properties.add(property);7 }8 }

  • -

    1 public int countTestCase() {2 int count = 0;3 for (Test each: tests)4 count += each.countTestCased();5 return count;6 }

  • -

  • -

  • -

  • -

    (assignment operators)

  • 1 public int FitNesseExpediter(Scoket s, 2 FitNesseContext context) throws Exception 3 { 4 this.context = context; 5 scocket = s; 6 input = s.getInputStream(); 7 output = s.getOutputStream(); 8 reguestParsingTimeLimit = 1000, 9 10 }

  • Try-Catch-Finally

    try-catch-finally

  • catch

  • null ()

    null null

    null SPECIAL CASE

  • null

    null null

    null null

  • Clean Code

  • (Public function)

    (Private function)

    (Stepdown rule)

  • (private)

    1. protected() 2. public

  • (SRP)

  • 1:

    (SRP)

  • 2~4:

  • ()

  • Thank you.

Recommended

View more >