diff --git a/lectures/figures/name_and_scope.drawio.svg b/lectures/figures/name_and_scope.drawio.svg new file mode 100644 index 0000000..aa124af --- /dev/null +++ b/lectures/figures/name_and_scope.drawio.svg @@ -0,0 +1,4 @@ + + + +
function
scope
length of name
variable
\ No newline at end of file diff --git a/slides/12_clean_code.html b/slides/12_clean_code.html index 0886e9b..1577549 100644 --- a/slides/12_clean_code.html +++ b/slides/12_clean_code.html @@ -300,9 +300,9 @@

clean code

Clean Code: A Handbook of Agile Software Craftsmanship

by Robert C. Martin (2009) [2]

-
-

some recommendations are too specific to C-like languages

-
+
@@ -885,39 +885,82 @@

debug tables

-
-

use descriptive names

+
+

the inverse scope law of function names

+
+
+
+
+

The longer the scope of a function, the shorter its name +should be. Functions that are called locally from a few nearby +places should have long descriptive names, and the longest function +names should be given to those functions that are called from just one +place.

+

Robert +C. Martin

+
+
+
+

“longer scope”: more general part of a code

+
+
+

+
+
+
+
+

function arguments

+
+
+
    -
  • a function does something, so it should start with an -imperative verb (imperative) +
  • do not use more than three [2]
  • +
+
+
    -
  • e.g., increaseSpeed
  • -
-
  • should be obvious what it does
  • -
  • The Inverse Scope Law of Function Names +
  • what if you’d need more?
      -
    • -

      Robert C. Martin The -Inverse Scope Law of Function Names: The longer the scope of a -function, the shorter its name should be. Functions that are called -locally from a few nearby places should have long descriptive names, and -the longest function names should be given to those functions that are -called from just one place.

      -
    • +
    • wrap it into an object
  • -
    -
    -

    function arguments

    + +
      -
    • do not use more than three
    • -
    • -

      Flag arguments are ugly […] loudly proclaiming that this function -does more than one thing [2].

      -
    • +
    • do not use flags +
        +
      • “Flag arguments are ugly […] loudly +proclaiming that this function does more than one thing [2].”
      • +
    +
    +
    +
    def build_empty_dataframe(start, end, cols):
    +    records = []
    +    for woy in range(start, end):
    +        for dow in range(1, 8):
    +            records.append([woy, dow, 0])
    +    return pd.DataFrame.from_records(
    +        records, columns=cols
    +    )
    +
    +
    def query_progress(as_percentage: bool):
    +    res = con.execute(progress_query)
    +    progress = res.fetchone()[0]
    +
    +    if as_percentage:
    +        return progress * 100
    +    else:
    +        return progress
    +
    +
    +

    no side effects

    @@ -961,28 +1004,28 @@

    prefer exceptions to returning error codes

    denoting blocks

    -
    for (i = 0; i < 10; i++) {
    -    console.log(i);
    -}
    -
    for (i = 0; i < 10; i++)
    -    console.log(i);
    var a = 0;
    -for (i = 0; i < 10; i++)
    -    a++;
    -    console.log(i);
    -
    -
    +class="sourceCode javascript">for (i = 0; i < 10; i++) { + console.log(i); +}
    for i in range(10):
    -    print(i)
    +class="sourceCode javascript">for (i = 0; i < 10; i++) + console.log(i);
    a = 0
    -for i in range(10):
    -    a += 1
    -    print(i)
    +class="sourceCode javascript">var a = 0; +for (i = 0; i < 10; i++) + a++; + console.log(i);
    +
    +
    +
    for i in range(10):
    +    print(i)
    +
    a = 0
    +for i in range(10):
    +    a += 1
    +    print(i)
    @@ -1000,24 +1043,24 @@

    denoting blocks

    puts i } ```--> -
    package main
    - 
    -import (
    -    "fmt"
    -)
    - 
    -func main() {
    -    for i:=0; i<10; i++ {
    -        fmt.Println(i)
    -    }
    -}
    -
    fn main() {
    -    for i in 0..9 {
    -        println!("{}", i);
    -    }
    -}
    +
    package main
    + 
    +import (
    +    "fmt"
    +)
    + 
    +func main() {
    +    for i:=0; i<10; i++ {
    +        fmt.Println(i)
    +    }
    +}
    +
    fn main() {
    +    for i in 0..9 {
    +        println!("{}", i);
    +    }
    +}
    @@ -1029,23 +1072,23 @@

    what could go wrong?

    parts from sslKeyExchange.c

    -
    if ((err = ReadyHash(&SSLHashSHA1, &hashCtx)) != 0)
    -    goto fail;
    -if ((err = SSLHashSHA1.update(&hashCtx, &clientRandom)) != 0)
    -    goto fail;
    -if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0)
    -    goto fail;
    -if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
    -    goto fail;
    -    goto fail;
    -if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0)
    -    goto fail;
    -
    fail:
    -    SSLFreeBuffer(&signedHashes);
    -    SSLFreeBuffer(&hashCtx);
    -    return err;
    +
    if ((err = ReadyHash(&SSLHashSHA1, &hashCtx)) != 0)
    +    goto fail;
    +if ((err = SSLHashSHA1.update(&hashCtx, &clientRandom)) != 0)
    +    goto fail;
    +if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0)
    +    goto fail;
    +if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
    +    goto fail;
    +    goto fail;
    +if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0)
    +    goto fail;
    +
    fail:
    +    SSLFreeBuffer(&signedHashes);
    +    SSLFreeBuffer(&hashCtx);
    +    return err;

    more about Apple’s “goto fail” fiasco (2014): [6], + + +
    function
    scope
    length of name
    variable
    \ No newline at end of file