ความอ่านง่ายสำคัญมาก

หลักการวิศวกรรมซอฟต์แวร์ใน Python

Adam Spannbauer

Machine Learning Engineer

Zen of Python

import this
The Zen of Python, by Tim Peters (abridged)

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
The complex is better than complicated.
Readability counts.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
หลักการวิศวกรรมซอฟต์แวร์ใน Python

การตั้งชื่อแบบสื่อความหมาย

ตั้งชื่อไม่ดี

def check(x, y=100):
    return x >= y

ตั้งชื่อแบบสื่อความหมาย

def is_boiling(temp, boiling_point=100):
    return temp >= boiling_point

ยาวเกินไป

def check_if_temperature_is_above_boiling_point(
        temperature_to_check, 
        celsius_water_boiling_point=100):
    return temperature_to_check >= celsius_water_boiling_point
หลักการวิศวกรรมซอฟต์แวร์ใน Python

รักษาความเรียบง่าย

The Zen of Python, by Tim Peters (abridged)

Simple is better than complex.
Complex is better than complicated.

  ชิ้นพิซซ่า

หลักการวิศวกรรมซอฟต์แวร์ใน Python

การทำพิซซ่า - แบบซับซ้อน

def make_pizza(ingredients):
    # Make dough
    dough = mix(ingredients['yeast'],
                ingredients['flour'],
                ingredients['water'],
                ingredients['salt'],
                ingredients['shortening'])

    kneaded_dough = knead(dough)
    risen_dough = prove(kneaded_dough)

    # Make sauce
    sauce_base = sautee(ingredients['onion'],
                                ingredients['garlic'],
                                ingredients['olive oil'])

    sauce_mixture = combine(sauce_base,
                            ingredients['tomato_paste'],
                            ingredients['water'],
                            ingredients['spices'])

    sauce = simmer(sauce_mixture)
    ...
หลักการวิศวกรรมซอฟต์แวร์ใน Python

การทำพิซซ่า - แบบเรียบง่าย

def make_pizza(ingredients):
    dough = make_dough(ingredients)
    sauce = make_sauce(ingredients)
    assembled_pizza = assemble_pizza(dough, sauce, ingredients)

    return bake(assembled_pizza)
หลักการวิศวกรรมซอฟต์แวร์ใน Python

เมื่อไหร่ควร Refactor

ตั้งชื่อไม่ดี

def check(x, y=100):
    return x >= y

ตั้งชื่อแบบสื่อความหมาย

def is_boiling(temp, boiling_point=100):
    return temp >= boiling_point

ยาวเกินไป

def check_if_temperature_is_above_boiling_point(
        temperature_to_check, 
        celsius_water_boiling_point=100):
    return temperature_to_check >= celsius_water_boiling_point
หลักการวิศวกรรมซอฟต์แวร์ใน Python

มาฝึกกันเถอะ!

หลักการวิศวกรรมซอฟต์แวร์ใน Python

Preparing Video For Download...