Jump to content
php.lv forumi

Atrasts internetā


briedis

Recommended Posts

  • Replies 546
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted Images

https://www.sitepoint.com/self-documenting-javascript/

Ieteikumi galīgi neattiecas tikai uz JS, tā kā recommended read for everyone.

 

Vienīgais punkts, kuram ne pārāk piekrītu, ir "Avoid boolean flags". Ir lietas, kurām nekas cits neder, turklāt izmantojot IDEs, tas ir moot point - uzhovero uz funkcijas un uzreiz redzi, kas ir tas boolean parametrs (ja vien tur nav 10 boolean pēc kārtas, in which case there's different problem you need to address first).

Edited by jurchiks
Link to comment
Share on other sites

Boolean flagi argumentos sux smagi, pilnīgi nelasāmi. Labāk izveidot vnk atsevišķu funkciju, kas izsauc konkrēto funkciju ar konkrēto boolean flagu, bet funkcijai vismaz ir lasā nosaukums.

 

saveOrder($asDraft = false);

 

saveOrderAsDraft(){

    return saveOrder(true);

}

 

Internāli klasēs lietot tādas metodes vēl ok, bet uz ārpuse nerullē.

Link to comment
Share on other sites

Boolean flagi argumentos sux smagi, pilnīgi nelasāmi. Labāk izveidot vnk atsevišķu funkciju, kas izsauc konkrēto funkciju ar konkrēto boolean flagu, bet funkcijai vismaz ir lasā nosaukums.

 

saveOrder($asDraft = false);

 

saveOrderAsDraft(){

    return saveOrder(true);

}

 

Internāli klasēs lietot tādas metodes vēl ok, bet uz ārpuse nerullē.

Arī šis piemērs nav īsti pareizs, jo tas, vai flagi ir slikti vai nē, ir atkarīgs no tā, vai tu modificē behavior vai arī entity. Tādam "saveOrderAsDraft" vispār nevajadzētu eksistēt, jo draft ir Order īpašība, un ar persistance layer šim steitam diez vai būs kāds sakars.

 

Lūk:

class Order {
    private $isDraft = false;

    public setDraft($draft) {
        $this->isDraft = $draft;
    }

    public isDraft() {
        return $this->isDraft();
    }
}

// ...

$order->setDraft(true);
$db->save($order);

Taču oriģinālajā rakstā autoram ir pilnīga taisnība - no šādas inline konfigurācijas aļa flagiem vajadzētu izvairīties cik vien iespējams. Ne vienmēr tas ir praktiski un reāli izdarāms, taču, jā, tā tam vajadzētu būt.

 

Ps. Jurčik, tas, ko Tu te proponē saucas parameter hell. Un risinājums tam ir konfigurācijas konstrukti. 

// WTF?
function doStuff($relative, $secondOnly = false, $merge = false, $thing = false, $whatever = null, $oneMore = null, $booHoo = false) {
    //...
}

$x = doStuff(true, false, false, null, false, null, false);
// WTF?

// vai arī...

function doStuff(DoStuffOptions $options) {
    //..
}

$options = new DoStuffOptions()
    ->withRelative(true)
    ->secondOnly()
    ->doAMerge()
    ->addThing($thing)
    ->withWhatever($whatever)
    ->addMore($more)
    ->doABooHoo()
;

$x = doStuff($options);
Edited by F3llony
Link to comment
Share on other sites

>Ps. Jurčik, tas, ko Tu te proponē saucas parameter hell. Un risinājums tam ir konfigurācijas konstrukti.

Tu gribi teikt, ka manā koda piemērā man jātaisa speciāla klase DIVIEM boolean parametriem? Kaut gan jebkurš normāls IDEs un arī dažu advanced text editoru lietotājs var apskatīties, ko tie parametri nozīmē, sekundes laikā?

Edited by jurchiks
Link to comment
Share on other sites

Pliks boolean ir tāds pats ļaunums kā random skaitļi parametros vai aprēķinos (piemērs par meaning of life = 42). Ja izsaukums toties izskatās "doShit($with_this_stuff, YES_MORE, NOT_SURE, YES_PLS)", nevis "doStuff($with_this_stuff, true, false, true)", tad viss ir ok.

Link to comment
Share on other sites

Javā var darīt šādi:

public class Foo {
    public static Baz bar() {
        // Well, how cool is this, eh?
        return new Baz() {{
            isFoo = true;
            isBaz = true;
            method = () -> "Hello World!";
        }};
    }

    private static class Baz {
        boolean isFoo = true;
        boolean isBaz = true;
        ReturnValue method;
    }

    private interface ReturnValue {
        String thing();
    }
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...