···991010CRC Card: **C**lass name, **R**esponsibilities, **C**ollaborators.
11111212-[CRC Cards](https://en.wikipedia.org/wiki/Class-responsibility-collaboration_card) are a teaching tool on how to design software. They were proposed by Kent Beck and Ward Cunningham, and, hell yeah it's useful.
1212+[CRC Cards](https://en.wikipedia.org/wiki/Class-responsibility-collaboration_card) are a teaching tool on how to design software. They were proposed by [Kent Beck](https://www.kentbeck.com) and [Ward Cunningham](https://en.wikipedia.org/wiki/Ward_Cunningham), and, hell yeah it's useful.
13131414-<CrcCard name="Component name" responsabilities={["First responsability", "second responsability"]} collaborators={["First collaborator", "second collaborator"]} />
1414+<CrcCard
1515+ name="Component name"
1616+ responsabilities={["First responsability", "second responsability"]}
1717+ collaborators={["First collaborator", "second collaborator"]}
1818+/>
15191620When I'm talking with tech leaders, my goal is to sharpen our vision about the software we are working on. When developers implemente features, I often see responsability leaks: the feature works, but the component are hard to read, hard to reuse and we pile up technical debt too quickly.
1721···19232024## The horrible `UserBookmarks` component 😨
21252222-Disclaimer: we're about to look at a very ugly code that is definetely doing too many things. The purpose of the discussion I have is to show to the tech lead that we really want to prevent this to happen and it is her mission to standardize it with her team.
2626+Disclaimer: we're about to look at a very ugly code that is definetely doing too many things, (FYI it is a simplified version of a component in one of my project). The purpose of the discussion I have is to show to the tech lead that we really want to prevent this to happen and it is her mission to standardize it with her team.
2727+2828+As the code does too many things, it can be hard to follow this post, but, I'll try my best to take you with me on this journey.
23292430Let's say we have a `UserBookmarks` component.
2531···7076 repeat: -1,
7177 stagger: 0.025,
7278 })
7373-7979+7480 return <div className="user-bookmarks">
7581 <Title title={strings['frontoffice.bookmark.user_bookmarks']} />
7682 {isLoading
···97103}
98104```
99105100100-It's a reeeeeeally long component that does many things, (FYI it is a simplified version of a component in one of my project). Most of the time, the tech lead freezes seeing how many things we need to talk to. Let's break it down piece by piece.
106106+It's a reeeeeeally long component that does many things. Most of the time, the tech lead freezes seeing how many things we need to talk to. Let's break it down piece by piece.
107107+108108+## The name
109109+110110+The easiest part: it has a name, maybe it's not perfect but `UserBookmarks` make sense for me. Let's create its CRC card knowing that the only code calling it is the main page.
111111+112112+<CrcCard name="UserBookmarks" collaborators={["Home.tsx"]} />
113113+114114+That's a start!
101115102116## Inputs and output
103117104104-The `UserBookmarks` component takes a `user` prop and return a list of styled bookmarks with the possibility to add bookmark via a modal.
118118+The `UserBookmarks` component takes a `user` prop and return a list of styled bookmarks with the possibility to add bookmark via a modal. I'll say something positive about this component, it's that it is pretty convenient for the parent calling it, just pass it the user, it'll give you all the user bookmarks and more. Maybe too much.
119119+120120+But hey! We can already note the first responsability on our CRC card.
121121+122122+<CrcCard
123123+ name="UserBookmarks"
124124+ responsabilities={["Display user bookmarks"]}
125125+ collaborators={["Home.tsx"]}
126126+/>
127127+128128+As we are talking about inputs, I want to know how the props are used. Are they necessary? Are they sufficient? Here we can see that `user` is only used for fetching data:
129129+130130+```ts
131131+...
132132+const userBookmark = await fetch(`/users/${user.id}/bookmarks`, {
133133+...
134134+```
135135+136136+Only the `user id` is necessary, why not just give the userId instead of the whole object?
137137+138138+> Here is a classic responsibility leak I frequentely see. Components don't care that user have a `user.address.line1` property when they only want their `id`.
139139+140140+That will be our first simplification.
141141+142142+```tsx
143143+interface Props {
144144+ userId: number
145145+}
146146+147147+export const UserBookmarks: FunctionComponent<Props> = ({ userID }) => {
148148+ const [bookmarks, setBookmarks] = useState<Bookmark[]>([])
149149+ const [showAddBookmarkModal, setShowAddBookmarkModal] = useState(false)
150150+ const [isLoading, setIsLoading] = useState(false)
105151106106-Cool! That's the first thing we can note on our CRC card.
152152+ useEffect(() => {
153153+ setIsLoading(true)
107154108108-<CrcCard name="UserBookmarks" responsabilities={["Display user bookmarks"]} collaborators={["Home.tsx"]} />
155155+ try {
156156+ const userBookmark = await fetch(`/users/${userId}/bookmarks`, {
157157+ method: 'GET'
158158+ })
159159+ setBookmarks(userBookmarks)
160160+ } catch (error) {
161161+ setBookmarks([])
162162+ } finally {
163163+ setIsLoading(false)
164164+ }
165165+ }, [])
166166+167167+ const addBookmarkToUser = async ({ bookmark }) => {
168168+ setIsLoading(true)
169169+ try {
170170+ const newBookmark = await fetch(`/users/${userId}/bookmarks`, {
171171+ method: 'POST',
172172+ body: JSON.stringify({ bookmark })
173173+ })
174174+ setBookmarks([...bookmarks, newBookmark])
175175+ } catch (error) {
176176+ console.warn(error);
177177+ } finally {
178178+ setIsLoading(false)
179179+ }
180180+ }
181181+182182+ const tilesAnimation = gsap.to({
183183+ duration: 0.8,
184184+ opacity: 0.35,
185185+ yoyo: true,
186186+ repeat: -1,
187187+ stagger: 0.025,
188188+ })
189189+190190+ return <div className="user-bookmarks">
191191+ <Title title={strings['frontoffice.bookmark.user_bookmarks']} />
192192+ {isLoading
193193+ ? <TilesSkeleton animation={tilesAnimation} numberOfTiles={16} />
194194+ : <BookmarkItem
195195+ key={bookmark.id}
196196+ data={bookmark}
197197+ />}
198198+199199+ <PrimaryButton
200200+ text={strings['frontoffice.bookmark.all_bookmarks']}
201201+ onClick={() => setShowAddBookmarkModal(true))}
202202+ alt={'strings['frontoffice.bookmark.add_bookmarks']'}
203203+ image={<FontAwesomeIcon icon={['fas', 'plus']} color="white" />}
204204+ />
205205+206206+ <AddBookmarkModal
207207+ visible={showAddBookmarkModal}
208208+ onClose={() => setShowAddBookmarkModal(false)}
209209+ bookmarks={bookmarks}
210210+ onBookmarkAdd={addBookmarkToUser}
211211+ />
212212+ </div>
213213+}
214214+```
215215+216216+## The secret sauce
217217+218218+Now comes where we'll definetely challenge how the component does its magic.