<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: quoc187</title>
    <description>The latest articles on DEV Community by quoc187 (@nguyentrongquocuet).</description>
    <link>https://dev.to/nguyentrongquocuet</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F818630%2F7e973030-c995-4344-99b9-b9e5d78bee52.png</url>
      <title>DEV Community: quoc187</title>
      <link>https://dev.to/nguyentrongquocuet</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nguyentrongquocuet"/>
    <language>en</language>
    <item>
      <title>I don't remember the principles of OOP :D</title>
      <dc:creator>quoc187</dc:creator>
      <pubDate>Tue, 21 Feb 2023 11:45:04 +0000</pubDate>
      <link>https://dev.to/nguyentrongquocuet/i-dont-remember-the-principles-of-oop-d-1ip7</link>
      <guid>https://dev.to/nguyentrongquocuet/i-dont-remember-the-principles-of-oop-d-1ip7</guid>
      <description>&lt;p&gt;The interviewer asked me to describe the principles of Object-oriented Programming. I don't remember the answer? but should I remember all that? My opinion is I don't need to.&lt;/p&gt;

&lt;p&gt;Is there any connection between remember what the definitions are and the right way to use them? I think they are far apart. You dont need to list every principle on the paper in order to implement OOP the good way.&lt;/p&gt;

&lt;p&gt;We all know a Class can be inherited&lt;br&gt;
We all know in some cases we don't want to expose everything to the outside, that's why we use &lt;code&gt;private&lt;/code&gt; and &lt;code&gt;protected&lt;/code&gt; for some of our methods and properties&lt;br&gt;
We all know a method can be override&lt;br&gt;
We all know a method could have multiple signatures&lt;br&gt;
We all know when we have different entities but have the common behavior, we could use interface or AbstractClass...&lt;/p&gt;

&lt;p&gt;What is Encapsulation, Inheritance, Polymorphism, Abstraction. Man give me 5 minutes for searching :D. I don't remember what they are, but if you ask me what is the problem in some lines of OOP codes, I might still get the right answer.&lt;/p&gt;

&lt;p&gt;Could us in some points violate the principles? We could, but the thing is, we do the best way, not do as any principle tells us to do, the code might not be OOP, or might not be FP, but if it works and it works well, go for it.&lt;/p&gt;

&lt;p&gt;What I want to emphasize is the way of learning programming, you don't need to remember all the definitions or all the terms, expecially if it doesnt have anything to do with a realworld problem, learn how to use them, and how to use them the efficient way.&lt;/p&gt;

&lt;p&gt;Same thing happen to SOLID principles, you don't remember what S,O,L,I,D stands for but if you know how to follow those principles, that's more than enough.&lt;/p&gt;

&lt;p&gt;When learning a programming concept, instead of what they are, learn how to utilize them and why we follow them.&lt;/p&gt;

&lt;p&gt;Some might agree with me, some might say this post is just me excuses for not remembering one of the most famous set of principles in programming world.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>About redux</title>
      <dc:creator>quoc187</dc:creator>
      <pubDate>Wed, 02 Mar 2022 04:30:09 +0000</pubDate>
      <link>https://dev.to/nguyentrongquocuet/about-redux-eap</link>
      <guid>https://dev.to/nguyentrongquocuet/about-redux-eap</guid>
      <description>&lt;p&gt;I've never used redux before. Today i tried redux-core with React. It did not work, why?&lt;/p&gt;

&lt;h2&gt;
  
  
  First, why do we use redux?
&lt;/h2&gt;

&lt;p&gt;Managing state, it means redux must have such functionalities like subscribes to changes, makes the components update when state changed, right?&lt;br&gt;
Does redux-core have such things? the answer is No, it doesnt. That's why they created react-redux.&lt;/p&gt;
&lt;h2&gt;
  
  
  The reducer function.
&lt;/h2&gt;

&lt;p&gt;Simply take the previous state and the action, which is a normal object, but we usually structure this as a object with 2 fields: type(string) and payload(any), of course we can use any type but that is not recommended.&lt;/p&gt;
&lt;h2&gt;
  
  
  How redux initiated the state?
&lt;/h2&gt;

&lt;p&gt;There's 2 way to initial Redux state, arcorrding to the docs.&lt;br&gt;
The first way is passing the &lt;code&gt;preloadedState&lt;/code&gt; to &lt;code&gt;createStore&lt;/code&gt; function. Nothing to say about this way.&lt;br&gt;
The second way is by passing the reducer's default argument for &lt;code&gt;state&lt;/code&gt;, something like &lt;code&gt;const reducer = (state = defaultState, action) =&amp;gt; ...&lt;/code&gt;.&lt;br&gt;
Arcorrding to the docs, redux will dispatch abitrary action to get the &lt;code&gt;default-state&lt;/code&gt;, that action will fall in to the &lt;code&gt;default&lt;/code&gt; case we provided(if we use &lt;code&gt;switch&lt;/code&gt; to determine the type).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;createStore(combineReducers({
    test1: (state = 'YESY', action: any) =&amp;gt; { console.log('test1', state, action); return state; },
    test2: (state = 'YESY2', action: any) =&amp;gt; { console.log('test2' ,state, action); return state; },
  })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will get the store of { test1: 'YESY', test2: 'YESY2' }&lt;br&gt;
The initial state is always the default value of &lt;code&gt;state&lt;/code&gt;? No, it is not, since redux dispatches some dummy actions to get the default state, it runs our reducer and treat the return-value as a default state.&lt;br&gt;
Try this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;createStore(combineReducers({
    test1: (state = 'YESY', action: any) =&amp;gt; { console.log('test1', state, action); return 'NOT_A_DEFAULT_VAL'; },
    test2: (state = 'YESY2', action: any) =&amp;gt; { console.log('test2' ,state, action); return state; },
  })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will get the store of { test1: 'NOT_A_DEFAULT_VAL', test2: 'YESY2' }&lt;/p&gt;

&lt;h2&gt;
  
  
  We dispatch a action, which reducers will be run?
&lt;/h2&gt;

&lt;p&gt;Redux passes the action to all the reducer, if the reducer expects action, it will return new state, otherwise returns the previous state(maintained the referrence)(this depends on your implementation, it might return previous state or something else). So i guess if we have about 10000 reducers :D, our app will be so freaking slow.&lt;/p&gt;

&lt;h2&gt;
  
  
  What applyMiddleware does?
&lt;/h2&gt;

&lt;p&gt;Ive felt very confuse the first time read the docs, it said take the dispatch, return new dispatch, i cant figure out how it works fromt the docs, so i choose to read the source code :D&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FP3AtKpq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/963oxlxiiq10u3pwvqqz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FP3AtKpq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/963oxlxiiq10u3pwvqqz.png" alt="applyMiddleware" width="880" height="784"&gt;&lt;/a&gt;&lt;br&gt;
&lt;code&gt;applyMiddleware&lt;/code&gt; creates new dispatch function, redux assign this dispatcher to &lt;code&gt;store&lt;/code&gt;. The dispatch is created by calling &lt;code&gt;compose(...chain)(store.dispatch)&lt;/code&gt;&lt;br&gt;
&lt;code&gt;chain&lt;/code&gt; is created by mapping all the middleware to the result returned by calling itself with the &lt;code&gt;context&lt;/code&gt;, &lt;code&gt;context&lt;/code&gt; contains the &lt;code&gt;getState, and the default dispatcher&lt;/code&gt;, note that it map, not reduce, so every context passed to the middleware are the same and &lt;strong&gt;middlewares passed to applyMiddleware will be called from left to right&lt;/strong&gt;&lt;br&gt;
Continue on, the chain will be composed together, but what &lt;code&gt;compose&lt;/code&gt; does? &lt;code&gt;compose&lt;/code&gt; takes a list of function, then returns a functiont that take only 1 parameter, when we call the function, first the last function in compose'parameter will be called, get the value returned, call the last-1 function with that value, and so on to the first parameter. Confuse right, here is a example: &lt;code&gt;compose(fn1, fn2, fn3)(input) =&amp;gt; fn1(fn2(fn3(input))&lt;/code&gt;, &lt;strong&gt;dispatcher returned by applyMiddleware will be called from right to left&lt;/strong&gt;. It means &lt;strong&gt;whatever the chain is, the first middleware will take the original-action, the final-action will depends on what the first middleware's dispatcher returns, usually we will call the next dispatcher and get back the (modified) action, and do things with the action returned&lt;/strong&gt;.&lt;br&gt;
Back to the &lt;code&gt;applyMiddleware&lt;/code&gt;, new dispatcher is created by calling &lt;code&gt;compose&lt;/code&gt; with &lt;code&gt;chain&lt;/code&gt;, and pass in a default dispatcher, note that default dispacher and new dispacher is the same type, so &lt;code&gt;chain&lt;/code&gt; must be a list of function that take a dispatcher and return a dispatcher, arcorrding to how &lt;code&gt;compose&lt;/code&gt; works.&lt;br&gt;
Finnally we can simplify the parameter of &lt;code&gt;applyMiddleware&lt;/code&gt; like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;({ dispatch: defaultDispatch, getState }) =&amp;gt; lastDispatch =&amp;gt; newDispatch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What is the lastDispatch of the last middleware we passed? it simply the function that returns whatever i takes. &lt;code&gt;lastDispatch = (action) =&amp;gt; action&lt;/code&gt;.&lt;br&gt;
What if one middleware didnt call the lastMiddleware? The chain will stop right at that middleware, any middleware after that will not be called, and makes no effect.&lt;br&gt;
The only thing a dispatcher can do is modify the &lt;code&gt;action&lt;/code&gt; passed to it, but how we archieve the modified action? That's the question i asked myself, the answer is returning the modified version, so the dispacher that called it can get back the modified action. &lt;strong&gt;Whatever we wanna do in our middleware's dispacher, better return an action&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6g9wka5F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pt84cytu33wurwksr9xn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6g9wka5F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pt84cytu33wurwksr9xn.png" alt="How it works" width="341" height="511"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
    </item>
    <item>
      <title>I failed an easy test</title>
      <dc:creator>quoc187</dc:creator>
      <pubDate>Mon, 21 Feb 2022 13:36:29 +0000</pubDate>
      <link>https://dev.to/nguyentrongquocuet/i-failed-an-easy-test-3jnj</link>
      <guid>https://dev.to/nguyentrongquocuet/i-failed-an-easy-test-3jnj</guid>
      <description>&lt;p&gt;This is just me, blame myself and summarize my problem.&lt;br&gt;
&lt;a href="https://www.hackerrank.com/challenges/sherlock-and-valid-string"&gt;The problem here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;According to the description, a valid string is a string that has all characters appears a CONST time(i will call it the perfect string), or just one character has more than 1 time appears.&lt;/p&gt;

&lt;p&gt;But i misunderstood it, i thought we can remove one character(incase string is not perfect), and then it can possibly valid if we remove one more, and so on. It blows my brain, especially in the context of an interview, i get nervous, and then things go wrong.&lt;/p&gt;

&lt;p&gt;The solution is very simple, the only thing we should care about is the frequencies, or in fact how many frequencies of character occurs in the string.&lt;/p&gt;

&lt;p&gt;Step 1: create a mapping between character and its frequency&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YZ4QXDdE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ad3phb2ijcszutjpmzs7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YZ4QXDdE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ad3phb2ijcszutjpmzs7.png" alt="Step 1" width="826" height="676"&gt;&lt;/a&gt;&lt;br&gt;
Step 2: create a mapping between the frequency and number of times it occurs&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nb43lJT7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g2xiqet09kwqmrrpmdgx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nb43lJT7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g2xiqet09kwqmrrpmdgx.png" alt="Step2" width="826" height="410"&gt;&lt;/a&gt;&lt;br&gt;
Step 3:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Case 1: If there's more than 2 frequencies, it mean the string is not valid, no way to make a one frequency from removing a character
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R6_irc8---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4yx22mr2u3qr29hhkapa.png" alt="Case 1" width="826" height="446"&gt;
&lt;/li&gt;
&lt;li&gt;Case2: If there's just 1 frequencies, the string is just one character repeat serveral times
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fW6D6x6C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7277kocr4hho5tg485qx.png" alt="Case 2" width="826" height="410"&gt; &lt;/li&gt;
&lt;li&gt;Case 3: Now the last case is 2 frequencies case, let say &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;. We must delete one charater to make the string valid, but after deleting, &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;'s value in the frequency map will be changed, to make a string valid, the final frequency map must contains just 1 key(case 2).
First we must check if &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; occurs 1 time, if both of them occurs more than 1 time, we can't make a string valid even after deleting one character.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let say we wanna delete the character that has frequency of &lt;code&gt;a&lt;/code&gt;, after deleting, we have a new frequency that is &lt;code&gt;a-1&lt;/code&gt;, and the value of a in the frequency mapw will be reduced by 1&lt;br&gt;
so we have a new frequency map &lt;code&gt;{ b: bOccurrences, a: aOccurences-1, [a-1]: 1 }&lt;/code&gt;&lt;br&gt;
To make string valid, &lt;code&gt;aOccurrences - 1&lt;/code&gt; must be 0 and [a-1] must be &lt;code&gt;b&lt;/code&gt; or 0, or in another words: &lt;code&gt;aOcurrences&lt;/code&gt; == 1 and &lt;code&gt;(a-1 ==b || a == 1)&lt;/code&gt; (note that obviously &lt;code&gt;b&lt;/code&gt; cannot be 0)&lt;br&gt;
Same approach with b.&lt;br&gt;
If you feel confused, see the code below&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ds-rw0na--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0m57na02o2tq6yp3y5sx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ds-rw0na--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0m57na02o2tq6yp3y5sx.png" alt="Image description" width="774" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is final solution in python&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--P6xMBdzr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dbtz7df0a5jk0334gtrh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P6xMBdzr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dbtz7df0a5jk0334gtrh.png" alt="Final" width="880" height="985"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>This is for myself, dont click :D</title>
      <dc:creator>quoc187</dc:creator>
      <pubDate>Sun, 20 Feb 2022 18:28:44 +0000</pubDate>
      <link>https://dev.to/nguyentrongquocuet/this-is-for-myself-dont-click-d-2aod</link>
      <guid>https://dev.to/nguyentrongquocuet/this-is-for-myself-dont-click-d-2aod</guid>
      <description>&lt;h2&gt;
  
  
  This is just me writing myself a todo list of 2022-20xx
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Things im gonna be learned:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;English, of course :D&lt;/li&gt;
&lt;li&gt;Functional programming&lt;/li&gt;
&lt;li&gt;Progressive web app&lt;/li&gt;
&lt;li&gt;Block-chain&lt;/li&gt;
&lt;li&gt;Working with docker&lt;/li&gt;
&lt;li&gt;Cloud with Java/Nodejs&lt;/li&gt;
&lt;li&gt;Micro-frontend/Monorepo partterns&lt;/li&gt;
&lt;li&gt;Prepare for my Software Engineer feature :D&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
    </item>
  </channel>
</rss>
