First 250 days of software dev - Part 14
Day 131
The ambiguities of yesterday's work have been cleared up a little bit today. Yesterday's problem: Suddenly encountering different technologies (many terminal commands, strapi, postman, react native-{component, context, ...other topics}-, trying to use asynchronous function correctly, getting the syntax right for using nested components) and a fatal error with a person who couldn't figure out which one to understand and couldn't explain to himself what was going on.Let's talk about today. It was incomparably better than yesterday. Because rather than trying to do something about things I don't know, I tried to understand them, to get the concept in my head. Let's go in order:
- First of all, I looked at what a context is all over again. In a tree, a prop normally has to pass through every part and knot in the trunk to get from the root to the branch at the end. But having to go through all of this data (prop) can increase the complexity considerably. Context provides us with a global data (state) that can be accessed by consumer components accessing the provider.
- Component is basically like a JavaScript function and prop is input. State is similar to prop, but it is private and controlled by component. When the state is updated, re-rendering occurs. Components are like a gift that React has given us. They are the building blocks that enable code reusability. Let's imagine we are writing a button component. On every page where a button is used, we can use the same component instead of writing code for the button over and over again. We can also create different variations of the same component. By using different variations of the same button component, we get a consistent design (Unrelated and overlapping buttons on each page will provide a dirty and inconsistent look. This should not be the unique design of the application that stays in the memory).
- Afterwards, with your advice, just like creating the architecture of a website project, readings were made on how the react project should have an architecture, folder directory, design pattern. As with web project architecture, there is no single right or wrong here, and everyone's best practice was their own approach. Two designs in particular were frequently mentioned: There was a project architecture where components were built with an atomic structure (atom-molecule-organism-template-page). Here atoms were the smallest building block components that could not be broken down into smaller groups. Molecules were a group of atoms that did one thing. It can be thought of as a form field. Organism was a structure made up of molecules that made up the more complex part of the app. Template, as the name suggests, was the layout, the template of a page (Illustration: The template of an object is the class. The page is the instance of the template). Another common design was the Container/Component concept. The logic was to make components as stateless as possible and see them as "dump". Container was acting as both a controller and the page itself. There have been many disadvantages of this issue in the past and now there are optimized solutions for it (applying command pattern from design patterns, developing with the help of hooks, etc.), but since there are different applications and it's late, I think I'll talk about it later. But in the reference sources where atomic design is explained, issues such as the container/component concept is not scalable, it is not clear what it does (is it a page or a statful component acting like a controller) are mentioned and it is said that there is no need to use it. On the gray side, both of them have a lot of users and it would be useful to create a design according to which is most useful for us and which will cause less problems in the future. The options can be not only two but a completely different structure.
A resource that doesn't go into these detailed nomenclatures and explains React quite well, frankly, caught my attention more: React project architecture.
Honestly, I personally liked the containerization method and I realized that it was similar to what is generally recommended on the web, except that it was closer to the container/component concept. There are a lot of ideas on this topic and my idea is to replicate the web concept here. And I think there is a similar one in the link above.On a different topic, I understand the reason for the hook's existence a little better. Components increase resusability, but let's imagine that there is a structure used by different components. Here we can create a custom hook ourselves.
SRP translated to react native: "A component or a hook must do one thing, do it well, and do it only."
Again referring to the link above, I take the following sentence directly: "You should create a separate context structure for each module that cannot communicate directly with each other but are dependent on each other in terms of content. In addition, the context updates each component it is connected to on any state update it holds. The components that listen to the context must already need that context. If you find any state you call unnecessary, either that state has no use for the context or you have built the context incorrectly. "
An article about the differences between using redux and context api made me understand the subject better.
I have a better understanding of reducer, provider, context, state, useState and useReducer, dispatch, action. Although I don't know the difference between useReducer and useState yet, react's documentation recommends me to use useReducer instead of useState if I use multiple states in the same context. I will try to understand better.I think I will look at the data retrieval from API, context usage, re-rendering again tomorrow morning. Seeing the comment of one of the authors of both redux and react , I had the feeling of reading Alan Key's thoughts while studying OOP.
I feel like the smoke screen has been lifted a little bit. Of course, there will be many, many more things to learn, mistakes to make, developments to be happy about. All I know is that there has been progress compared to yesterday.Day 132
At the beginning of the day, I read about the general concept again. As you said, no matter how much I try to acquire theoretical knowledge, it doesn't make much sense unless I put it into practice. There is a lot of confusion. It is of course useful to read other people's problems and the solutions developed, but it may not be easy to understand that problem without having a problem myself (it can be thought of as empathizing: "hmm yes, I have faced this problem too, and the solutions were/are like this").Then, as you said, I clicked on an image and it took me to a page about it, but it took a while. The reasons why it took a long time: I didn't know how to use navigation at all. I downloaded that library, read the documentation as much as I could, tried to solve my own problem using similar code structures. One of the problems that took me a while: since scrollView loads all the resources on the page directly, I avoided using it and adopted Flatlist (since it does lazy initialization on the web by default). But I couldn't create the scroll due to a problem I can't figure out where it originated from. I'll look at it again. Then I was encountering an error in parameter passing during navigation. I encountered such a problem because I didn't know object destructuring well and I used copy-paste code incorrectly. I fixed it with your help and now the code seems to be working normally. But the components don't use any state (it is used only in the navigation related part, but it is a property of the library's own object). Since the state is not kept, naturally there is no need for a context. After doing the basic operations, I think I can also make improvements. Especially JavaScript can be mastered more and debug capabilities can be more. It will be done.
Day 133
I thought I would take what I learned today, most of the things I thought I would do. But because there were so many interconnected components, the code got complicated. Of course, it was possible to get out of it, but the structure was like a shanty. Then I saw an example from a site where photos were taken and displayed through an api forever as you scroll the page. I just tried to understand it. Everything was exactly the same with the same folder structures, the same dependencies. I tried to understand it because I thought it was written with both context, pulling data from api, lots of callbacks, and a nice logic that looks simple. Honestly, I understood what was done in that code in general, but there were some parts that were not understood. Tomorrow morning, I will look at the parts that are not understood in that example (especially useCallback and useEffect are not fully understood, I will understand it). In React Native, I used to think that it is only a fronend because there is only the Css JavaScript side, but as far as I understand, it also includes the controller part. As far as I understand, navigation, data transmission (state management), style are the most general areas that are dealt with. In general terms, when I think about it, I think what's wrong with it, but things may not be that much. I should not succumb to my ego and continue to look at the work with the childish curiosity and passion that comes with being a beginner. I will (Of course, words do not make the ship of cheese, I try to do my best in practice).Day 134
The meaning and significance of the day for me is that I wrote an answer to a question on Stack Overflow for the first time. It was actually one of those simple questions that was asked in many places but not answered properly (although the answer was given on Github around 2015-16, that was also added). Of course, it may not be right to write about something I have never mastered and just started, but I wanted to write because I thought I understood the cause of the problem encountered. If it is wrong, I will delete or update it, of course, I think there is nothing to be ashamed of.
Apart from that, thank you for the information you gave about rendering. I fully understood the use of useEffect after you explained it.If you ask what I did differently, I typed "react native examples" into Google, and I either took the small examples I came across directly as copy paste and interpreted them or tried to write them myself. That's all for today.
Day 135
Although I've been looking at React for a week, I came across the structure you used today for the first time. As far as I understand, in order to pass some properties to the children of a component, it is necessary to 'bestow' that property to each child one by one. It doesn't look like a very nice structure, but it provides the solution. It's a different perspective. I don't think that what we do is magic, but sometimes it feels magical. I don't think this is wrong. Apart from that, it is fun to create and use components, but it can be difficult for me to establish relationships between them, to use asynchronous structures (when it happens). I will be happy if it becomes more difficult because we have just started.Day 136
I got stuck somewhere today, and towards the end of the day I found what I needed to do to get the desired solution:@react-navigation/stack
@react-navigation/native-stack
These two were different. I was using the bottom library, and there were two differences between the two in general: The first library was much more customizable. The second one was not so open to customization, but it was faster than the first one in terms of performance. In the first one, the navigation pattern in the given task was provided directly (it worked in the example I tried). In the second one, it overwrites every new state in the navigation state. That's why I could never reach the screens in between. All day long I was looking for a way to fix this. I tried all sorts of things but I couldn't find it. Honestly, I wondered if the dispatch method in the bottom tab library could do the desired thing, I tried, but I couldn't reach the result. Tomorrow morning I will continue where I left off and try to solve that problem.Day 137
This morning I watched what you did carefully because I acted with the motto "let your eye be a thief, not your hand" that my father learned from cooking and often told me. Afterwards, when I tried to do the drawer navigation, I encountered an error with a library due to a version error. When I Googled the problem, I found different solution methods. I was able to reach a solution by making changes in some codes in the files and removing the relevant library and adding a different version. Although my hand trembled at first with the thought that I would break it even more, then I reached the solution with the motto "those who are afraid have no children" and with caution. Then there was the problem of converting pages into file and folder structure. When I did research on this topic on the web, it was generally advised to establish a file-folder organization by classifying by feature (group by feature, or group by archetype) rather than by archetype. Although I thought of the same principle on the mobile side, since I saw the screen-component structure most frequently in the examples, it was a solution that converged to ten situations. I will continue with the last task tomorrow from where I left off.Day 138
I struggled with something today and it took me a long time to solve it. The problem was due to my inability to use an asynchronous function. I knew the source of the problem. I was trying to use the response of a function that runs asynchronously in a synchronous reducer function and receives a response from the server in a synchronous structure. You explained it to me well and corrected my incorrect code on the same topic before. The reason why I had difficulty in reaching the solution today was because I didn't think my way to the solution thoroughly and systematically. I was too busy thinking about how to make the reducer asynchronous or how to use an asynchronous function in the reducer, whereas if I had given the dispatch function that triggers the reducer as a callback to the asynchronous function I was using, the problem would have been fixed. Even though it was too late, I was able to find this solution and I was very happy when I did. Although it is still difficult to use asynchronous structure, I can find a solution with the help of you.As a result, depending on the user input, the login state can be accessed and changed globally from all pages. I can log out the user tomorrow. When the user logs in and out, I can make a change in the display of components or pages according to the state. I think it looks like I will solve this very well.
Day 139
Today I made some improvements in the code I wrote. First of all, I put the part about server communication in a separate class. In this way, I could provide my needs for api communication directly from there. I had put a function component at first, but I could create an object and call http methods with functions such as post, get that can be added to the object. So I returned the function component to the class. I thought it would be useful.I also did the creation of the user token on the front end side, but then I completed it on the server side. First I took the unique id obtained from the user's phone. Then I used one of the examples in the following answer that I came across while researching how to create a unique id. Although I wonder how the algorithm works, I plan to look at it tomorrow after work. Because I have not yet understood how a unique id can be created with a string expression with a finite number of lengths. In summary, I created a token by combining the unique id received on the user's phone and a unique value I created. Then, when the user contacts the server, he can prove his existence by sending this token in the header. Even if the user deletes the application, since the unique id from his phone can remain with us, his information can be reused and remembered.
Then last week, while reading and researching, I came across this issue in practice: when I had an object in state and I changed the value of an element in the object, the change was not detected. Therefore, there was no re-rendering. I found the solution as you said, and the way I understand it now is that we are not updating the object. We are creating a new object.Even though I thought you might ask me if I was still looking at it afterwards, I still looked at call-by-value-reference a little bit. This time I found something different. I learned that object is neither pass-by-value nor pass-by-reference. Obviously, although php, javascript and many modern languages use pass by value by default, call(pass)-by-sharing was an evaluation strategy for object.
As I understand it, objects are neither cloned nor copied. Objects are shared. Yes, sharing is mentioned. Let's go with an example (taken from (the first answer ):):
function changeStuff(a, b, c)
{
a = a * 10;
b.item = "changed";
c = {item: "changed"};
}
var num = 10;
var obj1 = {item: "unchanged"};
var obj2 = {item: "unchanged"};
changeStuff(num, obj1, obj2);
console.log(num); //10
console.log(obj1.item); //changed
console.log(obj2.item); //unchanged
The only thing we know is that by default everything is pass-by-value. The variable "num" is unchanged because it is passed by copying (taking its value). It is no longer the one being processed inside the function. Let's read the pass-by of object obj1 as described in wikipedia. obj1 is a box. I can change the contents of the box. But the box is still the same box. Everything I read, see and know is just the box itself. If it contains shoes today, cables or broken electronic devices tomorrow, it is none of my business. So I can change the contents of the box. Someone else can also change the contents of the box. So the box is actually something that is shared. So in the simplest sense, thinking in the old way, everything looks like pass by reference (call by sharing, alias, etc.). But it is not. Let's come to why it is not: if it was pass by reference, obj2 should have changed. But there was no change. Why is that? We shared the box with the function. Here you can do whatever you want with this box. And what did it do? It didn't use the box we gave it at all. Instead it created a different object and told the variable that referenced our box, "Now you reference this new object, leave the box that came to us". So there was no change in obj2. Honestly, I like this box example. Although the subject is understood, sometimes there can be confusion. It makes it a little better understood and exemplified.
I still can't say that I know exactly how JavaScript works. But I will try to understand and learn it better whenever possible. I know that everything is an object or behaves like an object. Considering the above example, why didn't 10 change? 10 was a box. When we multiplied it by ten (assignment), the value we equalized was not itself. It was a different box. It was 100. Maybe this idea of mine is not correct at all (in JavaScript, which also treats primitive values as objects). I could just say pass-by-value, but it's fun and satisfying to think about it and try to find different perspectives.
Apart from that, towards the end of the day, I looked at the problems in the mobile site, the differences that I could say would be nice. I also looked at the applications of different e-commerce sites. I looked at their navigation, routing, design. I think there is a lot to do. I think this is great. I think I can contribute a lot.
Day 140
Today only the errors and problematic places in the mobile application were examined. In fact, the mobile application was examined completely. Places that repeat each other and that are thought to look aesthetically ugly were also added to the report. The parts that put the application directly into fatal error were also mentioned. I think there is a lot to do. I can find many things I can do for myself, like the saying "turn a crisis into an opportunity." I hope I will. The work I do doesn't feel like an obligation. If it did, I probably wouldn't be doing it. Not only the work itself, but also the environment in which the work is done is very effective. Fortunately, the environment is a precious environment that encourages me, not hinders me. There are so many things to do, so many things to learn about the subjects we work on... I think that if I can become a good software developer, software will not be the only thing I will do (you can also live on a boat and fish at the age of 45, of course, ideas like "I will be a CEO, I will own a company, I will go down in history like Newton, Einstein, Galileo, I will save the world (from what?)" should not come to mind directly). But I think being a software developer will be a great contribution to me.By the way, I wanted to mention that the "diary" folders on one drive consisted of the reports I sent you. There was even a folder called "xxx" which contained the JavaScript files (clickbait file names sound good for some reason :)). Again, it was a more than technical report. But since I tried to be a bit more technical in the pdf I sent today, I don't think this will be a problem.