Weekly Retrospective — Third Week of October

I took a trip to Busan to use up some of my remaining annual leave. The trip helped me clear my head and think through where I want to go from here. There is only one answer: keep studying. Quietly keep following my own path.

My first open-source contribution

The Open Source Contribution Academy, organized by the Ministry of Science and ICT, is nearing its end. I wanted to submit at least one PR before completing the program, so I finally tackled work I’d been putting off. Contributing to open source was simpler than I expected. fork -> commit -> pr -> approve This contribution made me realize that I could contribute too, and that it really could be this straightforward. I’ll pay attention to more open-source projects and contribute more!! Added to the backlog.

RSC components

While resolving a server-side rendering issue, I ended up learning about RSC components. Here are some characteristics I learned this time:

  • Bundlers such as webpack automatically exclude the JavaScript code of RSC components. Since they run on the server, they don’t need to run on the client. Files with a ‘server.js’ extension are automatically recognized as server components and excluded from the bundle. -> Zero-Bundle-Size Components
  • Zero-Bundle-Size Components: the client JavaScript bundle gets smaller, and the code isn’t visible in the browser developer tools’ Sources tab.

A sudden question: why did modern frontend development go from SSR to CSR and then back to SSR?

  1. Why we moved from SSR to CSR.
  • A fast user experience: CSR can offer fast initial loading and a faster experience by fetching only the data needed when navigating between pages.
  • Interactive web applications: JavaScript made it possible to update the UI immediately and handle user interactions, enabling more dynamic web applications.
  • The rise of SPAs (Single Page Applications): CSR-based SPAs can load content dynamically without full page transitions.
  1. Why we are returning from CSR to SSR: server-side rendering has recently regained attention because of several drawbacks of CSR:
  • SEO (search engine optimization): because the page renders only after JavaScript finishes, search engine crawlers may fail to recognize its content properly.
  • Initial page loading time: downloading and executing all the JavaScript needed for CSR can actually slow down the initial load.
  • Large application performance: since all UI rendering happens on the client, increasingly complex JavaScript can reduce performance.

For these reasons, frameworks such as Next.js introduced a hybrid approach combining server and client components—SSR and CSR. The server prerenders the necessary data and sends it to the client, while only the parts that need it operate with CSR. This approach can provide better SEO, faster initial page loads, and optimized performance.

Keep the inside of useEffect simple. Why?

  • I think a huge useEffect is bad for maintainability.

  • useEffect is a hook for handling side-effect logic when a component renders. If useEffect contains calculations unrelated to the component’s rendering, those become side effects.

Performing unrelated calculations directly inside a component is a poor development practice, because developers cannot control the component’s rendering. https://seokzin.tistory.com/entry/React-useEffect의-철학과-Lifecycle

Revert and Reset

In my two years as a developer, I’d never needed to revert. Following our agreed Git flow meant I rarely had to undo large changes. This time, I accidentally pushed a work-in-progress commit to the dev branch and had to revert it. I somehow managed to revert to the previous commit, but ran into trouble when trying to restore the reverted commit. Although dev now contained the code from before commit a, submitting a PR for commit a showed no changes. That led me to look more closely at revert. https://www.yagom-academy.kr/fac3e1ea-b792-420a-a1e3-d95afca093c1

https://okky.kr/questions/1080287

  • My takeaway: revert doesn’t simply erase a commit; it’s like traveling back in time to an earlier commit. The commit from before the reversal still lives in the branch. If you want to remove a commit entirely, use reset.
  • How could I restore the reverted commit to dev, which was what I wanted? Revert the revert commit.

A junior developer growing through hotfixes

Today’s bug: a URLSearchParams issue URLSearchParams

  • Returns an instance of a URLSearchParams object.
  • URLSearchParams.append: adds the specified key/value pair as a search parameter.
  • URLSearchParams.entries: returns an iterator over every key/value pair, in the same order as the query string.
  • URLSearchParams.forEach: calls the specified callback for every value in the object.
  • URLSearchParams.keys: returns an iterator over all the keys of the object’s key/value pairs.
// It returns an iterator, not an array. You cannot check its length.
// Create a test URLSearchParams object
const searchParams = new URLSearchParams("key1=value1&key2=value2");

// Display the keys
for (const key of searchParams.keys()) {
	console.log(key);
}
  • URLSearchParams.toString(): returns a query string suitable for use in a URL.
  • URLSearchParams.values: returns an iterator over all the values of the object’s key/value pairs.
  • URLSearchParams.size:
const searchParams = new URLSearchParams("c=4&a=2&b=3&a=1");
searchParams.size; // 4

Studying iterators and Array.from

  • Iterables — JavaScript Deep Dive
    • Introduced in ES6.
    • Iteration protocol: agreed-upon rules for creating traversable data collections. They can be traversed in various ways, such as for in and forEach.
      • Iterable protocol: implementing a method with Symbol.iterator as its property key, or calling a Symbol.iterator method inherited through the prototype chain, returns an iterator that follows the iterator protocol. An object that follows the iterable protocol is an iterable. Iterables can be traversed with for of and used with spread syntax and array destructuring assignment.
      • Iterator protocol: calling an iterable’s Symbol.iterator method returns an iterator. It has a next method. Calling next traverses the iterable and returns an iterator result object with value and done properties. An object that follows the iterator protocol is an iterator. An iterator acts as a pointer for exploring the elements of an iterable.

Iteration protocol diagram

const isIterable = (v) =>
	v !== null && typeof v[Symbol.iterator] === "function";

// Arrays, strings, Maps, and Sets are iterable.
isIterable([]); //true
isIterable(""); //true
isIterable(new Map()); //true
isIterable(new Set()); // true
isIterable({}); //false

const [a, ...rest] = array;
console.log(a, rest); // 1, [2, 3]
  • Plain objects: a plain object that neither implements nor inherits a Symbol.iterator method does not follow the iterable protocol and is not iterable.
  • An iterator’s next method acts as a pointer for traversing each element. Every call moves through the iterable one step at a time and returns an iterator result object representing the result.
    const array = [1, 2, 3];
    // Symbol.iterator returns an iterator. The iterator provides a next method.
    const iterator = array[Symbol.iterator]();
    // Calling next traverses the iterable and returns an iterator result object.
    // An iterator result object has value and done properties.
    console.log(interator.next()); //{value: 1, done: false}
    console.log(interator.next()); //{value: 2, done: false}
    console.log(interator.next()); //{value: 3, done: false}
    console.log(interator.next()); //{value: undefined, done: true} // Stop iterating when true.
  • Built-in iterables: JavaScript provides built-in iterables that follow the iteration protocol. The following standard built-in objects are iterable.
Array.prototype[Symbol.iterator]
String.prototype[Symbol.iterator]
Map.prototype[Symbol.iterator]
Set.prototype[Symbol.iterator]
TypedArray[Symbol.iterator]
arguments[Symbol.iterator]
...
  • for…of: traverses an iterable and assigns its elements to a variable. The syntax is as follows.
for (variableDeclaration of iterable) { ... }
for (variableDeclaration in object) { ... } // Similar syntax.

// Implementing it manually.
const iterable = [1,2,3]
const iterator = iterable[Symbol.iterator]()
for(;;) {
const res = iterator.next()
if(res.done) break
const item = res.value
console.log(item)

}
  • Iterables and array-like objects
    • Array-like objects: objects with a length property whose property values can be accessed by index, like arrays. Since they have a length property, they can be traversed with a for loop. Their keys are numeric strings representing indices, so values can be accessed by index just like an array.
    • Array-like objects are plain objects rather than iterables. Without a Symbol.iterator method, they cannot be traversed with for..of.
    • arguments, NodeList, and HTMLCollection are both array-like and iterable. Becoming iterable didn’t remove their properties or indexed access, so they are both.
    • Arrays also became iterable when iterables were introduced.
    • Not all array-like objects are iterable. An object may be array-like without being iterable. However, Array.from can easily convert it into an array. Array.from accepts an array-like object or an iterable and returns it converted into an array.
      • Array-like objects and plain objects
      • What is Array.from? Introduced in ES6, it accepts an array-like or iterable object and returns it converted into an array. With Array.from, a callback passed as the second argument can create values while filling the elements.
        Array.from({ length: 2, 0: "a", 1: "b" }); //['a', 'b']
        Array.from("Hello"); //-> ['H', 'e','l','l','o']
    // An array-like object.
    const arrayLike = {
    	0: 1,
    	1: 2,
    	2: 3,
    	length: 3,
    };
    // Array.from returns an array from an array-like object or iterable.
    const arr = Array.from(arrayLike);
    console.log(arr); // [1,2,3]
  • Why the iteration protocol is necessary: before ES6, traversable data collections each had their own structures, without a unified convention, and could be traversed through for loops, for…in, forEach, and other approaches. ES6 standardized traversable data collections as iterables that follow the iteration protocol, making them uniformly usable with for..of, spread syntax, and array destructuring assignment. Iterables serve as data providers because they are used by data consumers: for of, spread syntax, and array destructuring assignment. The protocol acts as the interface connecting data consumers and providers.

Reflection

I’ve been thinking about what makes a good approach to resolving issues. Until now, I approached problems by finding what was broken and getting that part working again, because I thought I needed to fix things as quickly as possible. But there was a flaw in that approach. It might not be the best solution I can offer. It’s a bit like patching things with tape. To build a sustainable product, I’m increasingly realizing that I need to approach issues by finding and removing their cause. Understanding the cause clearly is how we reach the best result. Let’s change perspective and find the best solution.