Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Depth of composition
Mike Skoe
Mike Skoe

Posted on • Edited on

Depth of composition

When developing applications, it is common to split a large amount of code into smaller units. However, one of the difficulties is how to compose these parts together. In this post, we will emphasize the "depth" of a composition. This refers to how nested composition differs from flat composition in different contexts.

Image description

Class

// -- nested --classLow{}classMiddle{privatelow=newLow();}classHigh{privatemiddle=newMiddle();}// usageconsthigh=newHigh();// -- flat --classLow{}classMiddle{constructor(privatelow:Low,){}}classHigh{constructor(privatemiddle:Middle,){}}// usageconsthigh=newHigh(newMiddle(newLow()));
Enter fullscreen modeExit fullscreen mode

React component

// -- nested --constLow=()=><low/>;constMiddle=()=><middle><Low/></middle>;constHigh=()=><high><Middle/></high>;// usage<High/>// -- flat --constLow=()=><low/>;constMiddle=({children})=><middle>{children}</middle>;constHigh=({children})=><high>{children}</high>;// usage<High><Middle><Low/></Middle></High>
Enter fullscreen modeExit fullscreen mode

Function

// -- nested --functionlow(){}functionmiddle(){low();}functionhigh(){middle();}// usagehigh();// -- flat --functionlow(){}functionmiddle(low:()=>void):()=>void{return()=>{low();}}functionhigh(middle:()=>void):()=>void{return()=>{middle();}}// usagehigh(middle(low))();
Enter fullscreen modeExit fullscreen mode

Interface

// -- nested --interfaceLow{}interfaceMiddleextendsLow{}interfaceHighextendsMiddle{}// -- flat --interfaceLow{}interfaceMiddle{}interfaceHighextendsMiddle,Low{}
Enter fullscreen modeExit fullscreen mode

Why flat?

  • Easier to navigate between layers
  • More control over each layer of the code
  • Easier to manage and modify
  • It facilitates mocking
  • It encourages reliance on interfaces

Why nested?

  • More intuitive structure
  • Easier to implement and use
  • It hides inner details from the API
  • It provides a simpler interface for users

By considering these factors, developers can choose the most suitable approach for their specific needs and context.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Work
    Frontend, backend, ts developer at Sibirix
  • Joined

More fromMike Skoe

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp