PageSourceSearch

https://compose.diamonds/assets/js/4cb83456.965333ac.js

js compose.diamonds collected 2026-10-03 23:56:06 UTC 4,872 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkcompose_documentation=self.webpackChunkcompose_documentation||[]).push([["9139"],{82098(e,t,n){n.r(t),n.d(t,{metadata:()=>o,default:()=>p,frontMatter:()=>s,contentTitle:()=>r,toc:()=>d,assets:()=>c});var o=JSON.parse('{"id":"design/repeat-yourself","title":"Repeat Yourself \u2014 Intentional Code Duplication","description":"Repeat yourself when it makes your code easier to read and understand.","source":"@site/docs/design/repeat-yourself.mdx","sourceDirName":"design","slug":"/design/repeat-yourself","permalink":"/docs/design/repeat-yourself","draft":false,"unlisted":false,"editUrl":"https://github.com/Perfect-Abstractions/Compose/tree/main/website/docs/design/repeat-yourself.mdx","tags":[],"version":"current","sidebarPosition":2,"frontMatter":{"sidebar_position":2,"title":"Repeat Yourself \u2014 Intentional Code Duplication","sidebar_label":"Repeat Yourself","description":"Repeat yourself when it makes your code easier to read and understand."},"sidebar":"mainSidebar","previous":{"title":"Readability","permalink":"/docs/design/written-to-be-read"},"next":{"title":"Composition","permalink":"/docs/design/design-for-composition"}}'),a=n(62540),i=n(43023),l=n(92550);let s={sidebar_position:2,title:"Repeat Yourself \u2014 Intentional Code Duplication",sidebar_label:"Repeat Yourself",description:"Repeat yourself when it makes your code easier to read and understand."},r="Repeat Yourself",c={},d=[];function u(e){let t={code:"code",em:"em",h1:"h1",header:"header",p:"p",strong:"strong",...(0,i.R)(),...e.components};return(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(t.header,{children:(0,a.jsx)(t.h1,{id:"repeat-yourself",children:"Repeat Yourself"})}),"\n",(0,a.jsxs)(t.p,{children:["The DRY principle \u2014 ",(0,a.jsx)(t.em,{children:"Don't Repeat Yourself"})," \u2014 is a well-known rule in software development. We ",(0,a.jsx)(t.strong,{children:"intentionally"})," break that rule."]}),"\n",(0,a.jsxs)(t.p,{children:["In traditional software, DRY reduces duplication and makes it easier to update multiple parts of a program by changing one section of code. But the source code of deployed smart contracts is small and ",(0,a.jsx)(t.strong,{children:"doesn't change"}),"."]}),"\n",(0,a.jsx)(t.p,{children:"DRY can actually reduce clarity. Every internal function adds another indirection that developers must trace through, and those functions sometimes introduce extra logic for different cases."}),"\n",(0,a.jsx)(t.p,{children:"Repetition can make smart contracts easier to read and reason about. Instead of using an internal function for a bit of code, just use the bit of code. Even if it is used in multiple places."}),"\n",(0,a.jsxs)(t.p,{children:["However, DRY still has its place. For example, when a large block of code performs a complete, self-contained action and is used identically in multiple locations, moving it into an internal function can improve readability. For example, Compose's ERC-721 implementation uses an ",(0,a.jsx)(t.code,{children:"internalTransferFrom"})," function to eliminate duplication while keeping the code easy to read and understand."]}),"\n",(0,a.jsx)(l.A,{type:"tip",title:"Guideline",children:(0,a.jsx)(t.p,{children:"Repeat yourself when it makes your code easier to read and understand. Use DRY sparingly and only to make code more readable."})})]})}function p(e={}){let{wrapper:t}={...(0,i.R)(),...e.components};return t?(0,a.jsx)(t,{...e,children:(0,a.jsx)(u,{...e})}):u(e)}},92550(e,t,n){n.d(t,{A:()=>s});var o=n(62540);n(63696);var a=n(11750),i=n(21737);let l={callout:"callout_aeEX",calloutHeader:"calloutHeader_fXny",calloutIcon:"calloutIcon_vX9X",calloutTitle:"calloutTitle_kaV8",calloutContent:"calloutContent_Hl52","callout--info":"callout--info_TgTd","callout--warning":"callout--warning_ioY5","callout--danger":"callout--danger_OAha","callout--success":"callout--success_rrx_","callout--tip":"callout--tip_iqbU","callout--note":"callout--note_gJ54"};function s({type:e="info",title:t,children:n,icon:r=!0}){let c={info:"lightbulb",warning:"warning",danger:"no-entry",success:"checkmark",tip:"lightbulb",note:"memo"};return(0,o.jsxs)("div",{className:(0,a.A)(l.callout,l[`callout--${e}`]),children:[(0,o.jsxs)("div",{className:l.calloutHeader,children:[r&&(0,o.jsx)("span",{className:l.calloutIcon,children:(0,o.jsx)(i.A,{name:c[e]||c.info,size:24})}),t&&(0,o.jsx)("span",{className:l.calloutTitle,children:t})]}),(0,o.jsx)("div",{className:l.calloutContent,children:n})]})}},43023(e,t,n){n.d(t,{R:()=>l,x:()=>s});var o=n(63696);let a={},i=o.createContext(a);function l(e){let t=o.useContext(i);return o.useMemo(function(){return"function"==typeof e?e(t):{...t,...e}},[t,e])}function s(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(a):e.components||a:l(e.components),o.createElement(i.Provider,{value:t},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.