A
A
Alexey Nikolaev2019-11-13 20:56:37
Vue.js
Alexey Nikolaev, 2019-11-13 20:56:37

Which version of the component is objectively better?

Good evening.
I made an accordion component using named slots (don't say that there is a better one, it's just that on my current gallery they basically saw their bikes). In my opinion, it turned out very convenient and beautiful. I’ll omit the implementation code (it doesn’t matter within the framework of the question), the use looks like this:

<v-accordion
  :items="addresses"
>
  <template v-slot:item="{ item }">Item {{ item.id }}</template>

  <template v-slot:content>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit
  </template>
</v-accordion>

Everything is simple and transparent. We pass an array of elements, we have the ability to set an arbitrary element and its content. However, the team leader took it sharply with hostility and offered his own version.
<v-accordion>
  <v-accordion-item v-for="item in items">
    <div v-slot="title">
      {{ item.title }}
    </div>
    
    <div>{{ item.content }}</div>
  </v-accordion-item>
</v-accordion>

At the same time, I don’t see any pluses in his decision at point-blank range, but there are a lot of minuses:
- extra import (v-accordion-item in every place where the accordion will be used, and there’s no way around it), in my version v-accordion-item is encapsulated in the slot
- follows from point 1, extra lines in the components of the component (we do not register components globally)
- you will constantly have to write v-for, and in general, specifying the body of the accordion explicitly - isn't it superfluous?
- for certain there will be a problem of stylization of content. Since there is this poker "div item.content div" inside the default slot, this means that we will either have to access this div through nth-child (we will depend on the DOM structure, not good), or sculpt the necessary class each time (code repetition , not good), or start v-accordion-content and import the third component as well. Three components, Carl, instead of one! Is it good?
In general, I want to understand why solution 2 is objectively better than solution 1, and is it better? Because so far, the campaign is confirming the theory that the team leader !== is a good programmer. The arguments of the team leader himself sound like this: "Well, you can do it this way, and it works, right? Why bother with something?" and "I've always done that, we're so used to it."
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rustam Azizov, 2019-11-13
@UnformedVoid

The arguments of your team leader are unprofessional and not supported by rational argumentation. Your approach maintains the component structure of the application. While what your team leader suggested makes you remember too much about how to code, increases the amount of repetitive code, generally makes it more imperative, which is not compatible with the ideas behind Vue. In general, I advise you to insist on your own and describe why your approach is better. “I have always done this, we are so used to it” is not an argument. Your team leader clearly does not see the abstract side and does not understand the work of the framework well, or maybe, as you say, he is not very professional.
UPD.
I researched the comments here, thought and came to additional conclusions, which I will describe here:
1) Your approach has a minus - it is redundant, since it contains two slots, which makes the logic unclear. The fact is that you do not understand what the second content slot refers to. The reader might think that this is the content that the accordion itself outputs - once, and not for each element. I am especially familiar with such logic from the WPF framework. Everything works there, but judging by your example, this slot belongs to the item slot, that is, it is used not for the accordion, but for its elements, which is wrong, as it violates encapsulation. This is a minus of your approach, but it is easily corrected.
2) The disadvantages of your team lead's approach remain the same - verbosity, imperativeness. It violates the DRY principle.
So, an approach is required that will not violate best practices - a hybrid approach. First, we need to separate the concepts of an accordion and an accordion element - now these are two loosely coupled components. We should be able to change the accordion element component to a new one without touching the accordion itself. This is solved very simply. We just take your approach and leave only one item slot in it. This slot will allow the user of the accordion to create the markup of their choice. We remove the content slot as unnecessary. In the future, we can add more slots to the accordion and they will change its structure, for example, add a title to it or something else, but at the same time not touching how the interior of its elements is displayed (remember about encapsulation)! This should be done by the items slot. Now, we have the ability to create the markup of its elements. We can do this with simple HTML tags if we have the accordion in one place and forget about the next (remember the principles of YAGNI and KISS). If we have an accordion in several places, and, moreover, the markup of its elements is the same each time, then it is worth thinking about the component of the accordion element. Its structure is not important, so I won't go into details. Next, we simply create another component that will wrap our accordion and substitute the corresponding component of the accordion element into its item slot (and, possibly, add some more functionality, markup) - like creating a subclass. This way we do not write v-for every time, that is, we do not repeat ourselves (remember about DRY), and we have full control over what happens in the application. If you need to change something for everyone, we'll just go in and change that in our final wrapper component. If we need to implement something new, then we will create a new wrapper or even use our basic accordion on the desired page (remember YAGNI). Some may find this to be redundant. It is, and it must be used wisely. If you have one accordion for the entire application, then thinking about reusing it is pointless. If the component is required in several places, then the approach I have given is perfect, as it is based on the best practices taken from OOP. If you have one accordion for the entire application, then thinking about reusing it is pointless. If the component is required in several places, then the approach I have given is perfect, as it is based on the best practices taken from OOP. If you have one accordion for the entire application, then thinking about reusing it is pointless. If the component is required in several places, then the approach I have given is perfect, as it is based on the best practices taken from OOP.
PS With the wrapper approach, we don't even need to write separate components for the accordion elements, since, in principle, we don't need the accordion elements separately from it. All you have to do is create a child accordion with the desired markup in the item slot.

P
Philipp, 2019-11-14
@zoonman

Isn't it possible to write it like a human being?
I don’t know Vue, but from the options offered, I imagine it somehow like this

<accordion :items="items">
  <!-- add your content layout,
  in our case this is div  -->
  <div>{{item.content}}</div>
</accordion>

I have no idea, but from my point of view it is absolutely logical to use some kind of public interface that strictly declares the items structure.
The main child element becomes the template for rendering the content inside the accordion.
The team lead template is absolutely clear to me without any knowledge of Vue. Your template looks like some kind of voodoo magic. It is necessary to climb into the documentation and understand what's what.
I can offer an alternative option with direct declaration of structural elements.
<v-accordion :items="items">
  <v-title>{{title}}</v-title>
  <v-content>{{content}}</v-content>
</v-accordion>

I don't know if it's possible to do without all that garbage, but its presence annoys me.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question