<?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>Forem: Senichi</title>
    <description>The latest articles on Forem by Senichi (@senichimaro).</description>
    <link>https://forem.com/senichimaro</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%2F708148%2F0d75edcd-05ac-40b9-b826-34cab834426e.jpeg</url>
      <title>Forem: Senichi</title>
      <link>https://forem.com/senichimaro</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/senichimaro"/>
    <language>en</language>
    <item>
      <title>Object is possibly 'undefined'.ts(2532)</title>
      <dc:creator>Senichi</dc:creator>
      <pubDate>Fri, 08 Jul 2022 08:00:44 +0000</pubDate>
      <link>https://forem.com/senichimaro/object-is-possibly-undefinedts2532-k3a</link>
      <guid>https://forem.com/senichimaro/object-is-possibly-undefinedts2532-k3a</guid>
      <description>&lt;p&gt;Let me tell you now, there is no solution, but there is a method of deal with the error.&lt;/p&gt;

&lt;p&gt;Microsoft team member for Typescript said in a related trend that &lt;a href="https://github.com/microsoft/TypeScript/issues/16928"&gt;&lt;em&gt;"This is working as intended"&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Many developers are talking about this from different use cases as arrays, function callbacks, string values, html elements inside a React components (my case), and the list grows and grows...&lt;/p&gt;

&lt;p&gt;I could found suggested solution that it doesn't worked, as background is that &lt;em&gt;"The type system would effectively have to make a distinction between empty and non-empty arrays which isn't something we track"&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So, a few links where this is discussed:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare/"&gt;Null References: The Billion Dollar Mistake&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/54884488/how-can-i-solve-the-error-ts2532-object-is-possibly-undefined"&gt;How can I solve the error 'TS2532: Object is possibly 'undefined'?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/Microsoft/TypeScript/issues/12176"&gt;False "Property does not exist on type 'never'" when changing value inside callback with strictNullChecks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/44147937/property-does-not-exist-on-type-never"&gt;'Property does not exist on type 'never'&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/microsoft/TypeScript/issues/16928"&gt;TypeScript does not expect variable change in forEach&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/49431880/ts2531-object-is-possibly-null"&gt;TS2531: Object is possibly 'null'&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/40349987/how-to-suppress-error-ts2533-object-is-possibly-null-or-undefined"&gt;How to suppress "error TS2533: Object is possibly 'null' or 'undefined'"?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.angularfix.com/2021/10/getting-object-is-possibly-in.html"&gt;Getting Object is possibly 'undefined' in typescript react&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/microsoft/TypeScript/issues/11498"&gt;Annotate immediately-invoked functions for inlining flow control analysis&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Suppressing errors
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;the deal&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-6.html#suppress-errors-in-ts-files-using--ts-ignore-comments"&gt;Suppress errors in .ts files using ’// @ts-ignore’ comments&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A // @ts-ignore comment suppresses all errors that originate on the following line. &lt;strong&gt;It is recommended practice to have the remainder of the comment following @ts-ignore explain which error is being suppressed.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Please note that this comment only suppresses the error reporting, and we recommend you use this comments very sparingly.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I solve this
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;"the right combination of types and assertion"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, TypeScript also has a special syntax for removing null and undefined from a type without doing any explicit checking. Writing &lt;code&gt;!&lt;/code&gt; after any expression is effectively a type assertion that the value isn’t null or undefined:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# case
const textElementRef = useRef();
// this provokes the error in the next line
const text = textElementRef.current;
// Object is possibly 'undefined'.ts(2532)'
const textWidth = text.offsetWidth;

# what I was trying
const textElementRef = useRef();
const text = textElementRef.current; 
const textWidth = text!.offsetWidth; &amp;lt;-- type assertion
// causing : Property 'offsetWidth' does not exist on type 'never'.ts(2339)

# then I have to change the useRef definition too
const textElementRef = useRef&amp;lt;HTMLSpanElement&amp;gt;(null);
const text = textElementRef.current!; &amp;lt;-- type assertion
const textWidth = text.offsetWidth;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>How-to find specific information within huge Git Log</title>
      <dc:creator>Senichi</dc:creator>
      <pubDate>Wed, 01 Jun 2022 10:54:06 +0000</pubDate>
      <link>https://forem.com/senichimaro/how-to-find-specific-information-into-git-log-4c1c</link>
      <guid>https://forem.com/senichimaro/how-to-find-specific-information-into-git-log-4c1c</guid>
      <description>&lt;p&gt;This is a hack (not even as much xD), not feature. A simple solution to achieve a complex task powered by the command-line.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;redirect the output of &lt;code&gt;git log&lt;/code&gt; to a file as &lt;code&gt;git log &amp;gt; .tmp.gitlog.txt&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;use vim to open that file and search for the query

&lt;ul&gt;
&lt;li&gt;use &lt;code&gt;/&lt;/code&gt; to activate search&lt;/li&gt;
&lt;li&gt;introduce the query to be found&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hope it helps.&lt;/p&gt;

</description>
      <category>git</category>
    </item>
    <item>
      <title>Kubernetes &amp; Docker -&gt; Deploy a Flask Application - Manually (Simple Steps)</title>
      <dc:creator>Senichi</dc:creator>
      <pubDate>Wed, 27 Oct 2021 20:16:34 +0000</pubDate>
      <link>https://forem.com/senichimaro/deploy-a-flask-application-manually-simple-steps-iph</link>
      <guid>https://forem.com/senichimaro/deploy-a-flask-application-manually-simple-steps-iph</guid>
      <description>&lt;h2&gt;
  
  
  1. Create a named Kubernetes cluster
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;eksctl create cluster --name [cluster-name]  --profile [profile-name]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Image registry (get or create)
&lt;/h2&gt;

&lt;p&gt;Create/Build a Docker Image and push it to their Docker Hub repository.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Docker images are loaded from the container registry into Kubernetes pods. Access to the pods are exposed to consumers through a service.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  3. Deployment
&lt;/h2&gt;

&lt;p&gt;The manual &lt;strong&gt;deployment needs a YAML file&lt;/strong&gt; that will describe things like number of replicas, deployment strategy, Docker image name, and port on which the application can be accessed.&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Deployment mockup yaml file
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: apps/v1
kind: Deployment
metadata:
  name: simple-flask-deployment
  labels:
    app: simple-flask
spec:
  replicas: 3
  selector:
    matchLabels:
      app: simple-flask
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 2
      maxSurge: 2
  template:
    metadata:
      labels:
        app: simple-flask
    spec:
      containers:
      - name: simple-flask
        image: [docker-username]/[image-name]
        securityContext:
          privileged: false
          readOnlyRootFilesystem: false
          allowPrivilegeEscalation: false
        ports:
          - containerPort: 8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. In your terminal
&lt;/h4&gt;

&lt;p&gt;Navigate to &lt;a href="//deployment.yaml%20for%20short"&gt;deployment-mockup-yaml-file&lt;/a&gt;, and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl apply -f deployment.yml

# It will show the message as :
# deployment.apps/simple-flask-deployment created
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  3. Other useful commands are:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Verify the deployment
kubectl get deployments

# Check the rollout status
kubectl rollout status deployment/simple-flask-deployment

# Show the pods in the cluster
kubectl get pods

# Show the services in the cluster
kubectl describe services

# Display information about the cluster
kubectl cluster-info
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  4. Troubleshoot:
&lt;/h4&gt;

&lt;p&gt;If your pods do not show up as "Ready" while running the &lt;code&gt;kubectl get nodes1&lt;/code&gt; command, use the following troubleshooting tips:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# List all namespaces, all pods
kubectl get all -A

# Show all events
kubectl get events -w

# Show component status
kubectl get componentstatuses
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  5. Clean up
&lt;/h4&gt;

&lt;p&gt;Let's delete the deployment as well the Kubernetes cluster:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Delete your deployment
kubectl delete deployments/simple-flask-deployment

# Tear down your cluster
eksctl delete cluster eksctl-demo --profile &amp;lt;profile-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://kubernetes.io/docs/reference/kubectl/cheatsheet/"&gt;Kubernetes Cheatsheet&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>aws</category>
      <category>kubernetes</category>
      <category>docker</category>
    </item>
    <item>
      <title>What-is Flask decorator</title>
      <dc:creator>Senichi</dc:creator>
      <pubDate>Sat, 09 Oct 2021 16:55:47 +0000</pubDate>
      <link>https://forem.com/senichimaro/flask-decorator-something-57gd</link>
      <guid>https://forem.com/senichimaro/flask-decorator-something-57gd</guid>
      <description>&lt;p&gt;A function that takes another function as a parameter and returns another function.&lt;/p&gt;

&lt;p&gt;A function can be used as a parameter, a return value, and can be assigned to a variable.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Definition : &lt;br&gt;
A decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.&lt;/p&gt;

&lt;p&gt;Mechanism :&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@decorator
def f(argument):
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;f&lt;/code&gt; is replaced by &lt;code&gt;decorator(f)&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;f(argument)&lt;/code&gt; is equivalent &lt;code&gt;decorator(f)(argument)&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  @wraps as a decorator
&lt;/h2&gt;

&lt;p&gt;Decorators work as wrappers. &lt;/p&gt;

&lt;p&gt;Modifying the behavior of the code, before and after a target function execution. &lt;/p&gt;

&lt;p&gt;Allow to augmenting the original functionality, without the need to modify the function itself.&lt;/p&gt;

&lt;p&gt;Thus decorating it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Composition of Decorators
&lt;/h2&gt;

&lt;p&gt;Function decorators are simply wrappers to existing functions.&lt;/p&gt;

&lt;p&gt;Let's consider a function that wraps the string output of another function by p tags.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def get_text(name):
   return "lorem ipsum, {0} dolor sit amet".format(name)

def p_decorate(func):
   def func_wrapper(name):
       return "&amp;lt;p&amp;gt;{0}&amp;lt;/p&amp;gt;".format(func(name))
   return func_wrapper

my_get_text = p_decorate(get_text)

print(my_get_text("John"))

# &amp;lt;p&amp;gt;Outputs lorem ipsum, John dolor sit amet&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Decorator Composition : syntactic sugar
&lt;/h2&gt;

&lt;p&gt;There is a neat shortcut, mention the name of the decorating function before the function to be decorated. The name of the decorator should be perpended with an @ symbol.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def p_decorate(func):
   def func_wrapper(name):
       return "&amp;lt;p&amp;gt;{0}&amp;lt;/p&amp;gt;".format(func(name))
   return func_wrapper

@p_decorate
def get_text(name):
   return "lorem ipsum, {0} dolor sit amet".format(name)

print(get_text("John"))

# Outputs &amp;lt;p&amp;gt;lorem ipsum, John dolor sit amet&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;This is a short resume of this coders awesome articles&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://medium.com/@nguyenkims/python-decorator-and-flask-3954dd186cda"&gt;https://medium.com/@nguyenkims/python-decorator-and-flask-3954dd186cda&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.thecodeship.com/patterns/guide-to-python-function-decorators/"&gt;https://www.thecodeship.com/patterns/guide-to-python-function-decorators/&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  common @wrap example
&lt;/h2&gt;

&lt;p&gt;This example show how to implement jwt check.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from functools import wraps

def get_token_auth_header():
  if 'Authorization' not in request.headers:
    abort(401)
  auth_header = request.headers['Authorization']
  header_parts = auth_header.split(' ')
  if len(header_parts) != 2:
    abort(401)
  elif header_parts[0].lower() != 'bearer':
    abort(401)
  return header_parts[1]

def requires_auth(f):
  @wraps(f)
  def wrapper(*args, **kwargs):
    jwt = get_token_auth_header()
    return f(jwt, *args, **kwargs)

@app.route('/taget')
@requires_auth
def target():
  print(jwt)
  return 'welcome to target'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
    </item>
    <item>
      <title>How-To check a pattern into a string &amp; retrieve it</title>
      <dc:creator>Senichi</dc:creator>
      <pubDate>Fri, 01 Oct 2021 08:27:31 +0000</pubDate>
      <link>https://forem.com/senichimaro/how-to-check-a-pattern-into-a-string-retrieve-it-4pao</link>
      <guid>https://forem.com/senichimaro/how-to-check-a-pattern-into-a-string-retrieve-it-4pao</guid>
      <description>&lt;p&gt;This is an interview test so, there are no unique questions, even correct or incorrect. Because it was meant to see how a developer think and develop itself with their knowledge.&lt;/p&gt;

&lt;p&gt;This was my answer...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const string = 'qweaawq23,4ñmñleaaaaa23rt-.y90_4gsdn_klaaaaa23-4nper-etrç.mn4wetaaaaaaaaasx+';
const patt = /a+/g;

const result = string.match(patt)
// what catch the regex
console.log(result)
const arrSizes = result.map( item =&amp;gt; item.length )
let bigger = []
let longest = 0
result.forEach( (item, index) =&amp;gt; {
  if( item.length &amp;gt; longest ){
    bigger = [index, item, item.length]
    longest = parseInt(item.length)
  }
})
// what's the result of the loop
console.log(bigger)
// checking correlation
console.log(result[3])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why do this ?
&lt;/h2&gt;

&lt;p&gt;Checking a pattern it's not easy, even less into a string because there are no marks or flags that tell us by them self when something start and end. So, get that coordinates is the developer task.&lt;/p&gt;

&lt;p&gt;My original idea was to extract all the patterns encountered in the string, get them into an array and compare each other to find the longest, maybe with a kind of loop.&lt;/p&gt;

&lt;p&gt;So, regex was my first choice: but why? Because Regex are very "smart" or very efficient looking for things, they are in many situations the best option to find certain kinds of patterns: like strings.&lt;/p&gt;

&lt;p&gt;But, that was my firts and easiest option so I decide to try before with a &lt;code&gt;for loop&lt;/code&gt;. Result: &lt;strong&gt;DISASTER&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The &lt;code&gt;for loop&lt;/code&gt; disaster: a horror experience based in a real history.
&lt;/h3&gt;

&lt;p&gt;Trying a &lt;code&gt;for loop&lt;/code&gt; was very very cubersome... Because that from before: &lt;em&gt;there are no marks or flags that tell us by them self when something start and end&lt;/em&gt;. So trying to set this marks was veeeeeeeeeeeeeeeeeeeeery borring and not efficient from my perspective, even more, this reinforced the idea about use regex.&lt;/p&gt;

&lt;p&gt;Some problems encountered was:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;build a stop mark for : pattern comparison&lt;/li&gt;
&lt;li&gt;build a stop mark for : length counter between candidates&lt;/li&gt;
&lt;li&gt;exponentiation of variables needed&lt;/li&gt;
&lt;li&gt;etc...&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  regex : a happy ending
&lt;/h2&gt;

&lt;p&gt;The proper expression let me fit the requirements to a logic, like "recognize one or more coincidences of 'a' (&lt;code&gt;/a+/&lt;/code&gt;) in the string (&lt;code&gt;g&lt;/code&gt;)" : &lt;code&gt;/a+/g&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;From that point the logic flows into more or less basic structure.&lt;/p&gt;

&lt;p&gt;So, to apply &lt;code&gt;.match&lt;/code&gt; method to the string gives me the array of patterns I dream at the first look.&lt;/p&gt;

&lt;p&gt;Then, &lt;code&gt;.map&lt;/code&gt; (that return a new array) provides the longest length but I thought that It was not useful to retrieve the longest pattern if we dont know where is it or we cant have it, so...&lt;/p&gt;

&lt;p&gt;From my personal point of view to achieve this, an iteration over each of the items could be a good option, in this case where time or memory consumption it wasn't a must.&lt;/p&gt;

&lt;p&gt;Finally, in the sake of keep all simple I use an array variable called &lt;em&gt;bigger&lt;/em&gt; to save the bigger pattern and their index to have a reference of their position in the result array to be retrieved.&lt;/p&gt;

&lt;p&gt;A numeric variable with the longest value, because this logic is inspired in a binary algoritmic search so, my desition was to compare the first item length with zero and whatever value it was this allow to continue the comparisions in a progresive manner.&lt;/p&gt;

&lt;p&gt;Why is inspired in a binary search? Because comparisons increase in an exponential manner. This means that 4 items would give six posibilities and 10 items would give 45. So, in a binary search inspiration logic one value would be compared with one another value that result in one possibility, keeping things flat.&lt;/p&gt;

&lt;p&gt;By last, a if statement check an absolute length that belongs to the longest pattern with the actual length. If true, an array was choosen to keep the pattern and their index.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Awesome super fun test, the result is a logic that return &lt;em&gt;the index of the longest pattern, the pattern, and their length&lt;/em&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>testdev</category>
    </item>
    <item>
      <title>unsafe react hooks</title>
      <dc:creator>Senichi</dc:creator>
      <pubDate>Mon, 20 Sep 2021 23:01:20 +0000</pubDate>
      <link>https://forem.com/senichimaro/unsafe-react-hooks-3p1b</link>
      <guid>https://forem.com/senichimaro/unsafe-react-hooks-3p1b</guid>
      <description>&lt;p&gt;One of the biggest driving forces behind React 17 is asynchronous rendering which aims to improve both the user and developer experience.&lt;/p&gt;

&lt;p&gt;In React v16.3 the React team introduced few new lifecycles and deprecated some of the old ones. &lt;/p&gt;

&lt;p&gt;Not due to security, these lifecycles will be more likely to have bugs in future versions of React, especially once async rendering is enabled.&lt;/p&gt;

&lt;p&gt;To allow asynchronous rendering, there has to be a change in the component lifecycle and this involves deprecation of lifecycle methods which were introduced without async rendering in mind.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;componentWillMount → UNSAFE_componentWillMount&lt;/li&gt;
&lt;li&gt;componentWillReceiveProps → UNSAFE_componentWillReceiveProps&lt;/li&gt;
&lt;li&gt;componentWillUpdate → UNSAFE_componentWillUpdate&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;React 16.9 did not contain breaking changes, and the old names continue to work in this release. But you will now see a warning when using any of the old names. These warnings provide better alternatives to these lifecycle methods.&lt;/p&gt;

&lt;h4&gt;
  
  
  Easily Renaming UNSAFE Methods
&lt;/h4&gt;

&lt;p&gt;To allow easy renaming for projects where one may not have adequate time to migrate to the recommended lifecycle methods, the React team recommended a script from &lt;a href="https://medium.com/@cpojer/effective-javascript-codemods-5a6686bb46fb"&gt;codemod&lt;/a&gt; that automates this process.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd your_project
npx react-codemod rename-unsafe-lifecycles
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Asynchronous rendering is driving into version changes. To prevent bugs in future once async rendering is enabled.&lt;/p&gt;

&lt;p&gt;To allow asynchronous rendering involves deprecation of lifecycle methods without async rendering in mind.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;componentWillMount → UNSAFE_componentWillMount&lt;/li&gt;
&lt;li&gt;componentWillReceiveProps → UNSAFE_componentWillReceiveProps&lt;/li&gt;
&lt;li&gt;componentWillUpdate → UNSAFE_componentWillUpdate&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now warnings provide better alternatives to these lifecycle methods.&lt;/p&gt;

&lt;p&gt;Few new lifecycles was introduced too.&lt;/p&gt;

&lt;p&gt;React Team has introduced one new lifecycle method named &lt;code&gt;getDerivedStateFromProps()&lt;/code&gt; to achieve the same functionality as &lt;code&gt;UNSAFE_componentWillReceiveProps&lt;/code&gt; will provide.&lt;/p&gt;

&lt;p&gt;Replacement in Functional Component is no complete 1:1  but still we can achieve or use different Hooks at different scenarios to achieve the same result. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;useMemo()&lt;/code&gt; React hook specify all the props in an array that are dependent, it will run before everything whenever the props change. &lt;/p&gt;

&lt;p&gt;Together &lt;code&gt;getSnapshotBeforeUpdate(prevProps, prevState)&lt;/code&gt; with &lt;code&gt;componentDidUpdate&lt;/code&gt;, this new lifecycle should cover all use cases for the legacy componentWillUpdate.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Nodejs takeaway</title>
      <dc:creator>Senichi</dc:creator>
      <pubDate>Mon, 20 Sep 2021 22:07:32 +0000</pubDate>
      <link>https://forem.com/senichimaro/nodejs-takeaway-5bck</link>
      <guid>https://forem.com/senichimaro/nodejs-takeaway-5bck</guid>
      <description>&lt;ul&gt;
&lt;li&gt;1. What is Node.js and how it works?&lt;/li&gt;
&lt;li&gt;2. How is Node.js better than other frameworks most popularly used?&lt;/li&gt;
&lt;li&gt;3. How do you manage packages in your node.js project?&lt;/li&gt;
&lt;li&gt;4. What is the Event Loop?&lt;/li&gt;
&lt;li&gt;5. What is fork in node JS?&lt;/li&gt;
&lt;li&gt;6. How many types of API functions are there in Node.js?&lt;/li&gt;
&lt;li&gt;7. What is REPL?&lt;/li&gt;
&lt;li&gt;8. How does Node.js overcome the problem of blocking of I/O operations?&lt;/li&gt;
&lt;li&gt;9. What are node.js buffers?&lt;/li&gt;
&lt;li&gt;10. What is node.js streams?&lt;/li&gt;
&lt;li&gt;11. Why should you separate Express app and server?&lt;/li&gt;
&lt;li&gt;12. Why V8 engine?&lt;/li&gt;
&lt;li&gt;13. Exit codes of Node.js&lt;/li&gt;
&lt;li&gt;14. What is an Event Emitter in Node.js?&lt;/li&gt;
&lt;li&gt;15. Clustering&lt;/li&gt;
&lt;li&gt;16. What is a thread pool and which library handles?&lt;/li&gt;
&lt;li&gt;17. How are worker threads different from clusters?&lt;/li&gt;
&lt;li&gt;18. How to measure the duration of async operations?&lt;/li&gt;
&lt;li&gt;19 How to measure the performance of async operations?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  1. What is Node.js and how it works?
&lt;/h2&gt;

&lt;p&gt;Node.js is a virtual machine that uses JavaScript as its scripting language and &lt;strong&gt;runs Chrome’s V8 JavaScript engine&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;It's based on an &lt;strong&gt;asynchronously event-driven non-blocking architecture&lt;/strong&gt; where I/O making it lightweight and efficient.&lt;/p&gt;

&lt;p&gt;It &lt;strong&gt;provides API to access OS-level features&lt;/strong&gt; such as file system, network, etc..., being used even in developing desktop applications with electron.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. How is Node.js better than other
&lt;/h2&gt;

&lt;p&gt;Other frameworks where developers have to use thread management.&lt;/p&gt;

&lt;p&gt;Provides simplicity in development because of its non-blocking I/O.&lt;/p&gt;

&lt;p&gt;And event-based model results in short response time and concurrent processing.&lt;/p&gt;

&lt;p&gt;Also since we will use Javascript in both the frontend and backend the development will be much faster. &lt;/p&gt;

&lt;p&gt;And at last, there are ample libraries so that we don't need to reinvent the wheel.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. How do you manage packages in your node.js project?
&lt;/h2&gt;

&lt;p&gt;Mostly used are npm or yarn. Both provide almost all libraries of javascript with extended features of controlling environment-specific configurations.&lt;/p&gt;

&lt;p&gt;To maintain versions of libs being installed in a project we use package.json and package-lock.json so that there is no issue in porting that app to a different environment.&lt;/p&gt;

&lt;p&gt;But It can be managed by a number of package installers and their configuration file accordingly. &lt;/p&gt;

&lt;h2&gt;
  
  
  4. What is the Event Loop?
&lt;/h2&gt;

&lt;p&gt;In computer science, the event loop is a programming construct or design pattern that waits for and dispatches events or messages in a program. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The event loop works by making a request to some internal or external "event provider"&lt;/strong&gt; (that generally blocks the request until an event has arrived), &lt;strong&gt;then calls the relevant event handler&lt;/strong&gt; ("dispatches the event"). The event loop is also sometimes referred to as the message dispatcher, message loop, message pump, or run loop.&lt;/p&gt;

&lt;p&gt;We could think of event loop as a queue (First in first out operation), where list of events are registered, and the code associated with that event is executed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Node.js JavaScript Event Loop run initialization code and callbacks&lt;/strong&gt;. Offers a Worker Pool to handle expensive tasks like file I/O.&lt;/p&gt;

&lt;p&gt;It uses a small number of threads to handle many clients. Because It has only a few threads, you must structure your application to use them wisely.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Applications first complete an initialization phase, &lt;code&gt;require&lt;/code&gt;'ing modules and registering callbacks for events&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Then enter the Event Loop to respond incoming client requests by executing the appropriate callback&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This callback executes synchronously&lt;/strong&gt;, and may register asynchronous requests to continue processing after it completes. &lt;/p&gt;

&lt;p&gt;The callbacks for these asynchronous requests will also be executed on the Event Loop.&lt;/p&gt;

&lt;p&gt;The Event Loop will also fulfill the non-blocking asynchronous requests made by its callbacks, e.g., network I/O.&lt;/p&gt;

&lt;p&gt;In summary, the Event Loop executes the JavaScript callbacks registered for events, and is also responsible for fulfilling non-blocking asynchronous requests like network I/O.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. What is fork in node JS?
&lt;/h2&gt;

&lt;p&gt;A fork in general is used to spawn child processes. In node it is used to create a new instance of v8 engine to run multiple workers to execute the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. How many types of API functions are there in Node.js?
&lt;/h2&gt;

&lt;p&gt;There are two types of API functions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Asynchronous, non-blocking functions - mostly I/O operations which can be fork out of the main loop.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Synchronous, blocking functions - mostly operations that influence the process running in the main loop&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  7. What is REPL?
&lt;/h2&gt;

&lt;p&gt;REPL in Node.js stands for Read, Eval, Print, and Loop, which further means evaluating code on the go.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. How does Node.js overcome the problem of blocking of I/O operations?
&lt;/h2&gt;

&lt;p&gt;Nodejs has an event loop that can be used to handle all the I/O operations in an asynchronous manner without blocking the main function.&lt;/p&gt;

&lt;p&gt;if some network call needs to happen it will be scheduled in the event loop instead of the main thread (single thread). &lt;/p&gt;

&lt;p&gt;If there were multiple I/O calls each one will be queued accordingly to be executed separately (other than the main thread). &lt;/p&gt;

&lt;p&gt;Even though we have single-threaded JS, I/O ops are handled in a nonblocking way.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. What are node.js buffers?
&lt;/h2&gt;

&lt;p&gt;In general, buffers is a temporary memory that is mainly used by stream to hold on to some data until consumed. &lt;/p&gt;

&lt;p&gt;Buffers are introduced with additional use cases than JavaScript’s Unit8Array and are mainly used to represent a fixed-length sequence of bytes. &lt;/p&gt;

&lt;p&gt;This also supports legacy encodings like ASCII, utf-8, etc. It is a fixed (non-resizable) allocated memory outside the v8.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. What is node.js streams?
&lt;/h2&gt;

&lt;p&gt;Streams are instances of EventEmitter which can be used to work with streaming data in Nodejs. They can be used for handling and manipulating streaming large files(videos, mp3, etc) over the network. They use buffers as their temporary storage.&lt;/p&gt;

&lt;p&gt;There are mainly four types of the stream:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Writable: streams to which data can be written (for example, &lt;code&gt;fs.createWriteStream()&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Readable: streams from which data can be read (for example, &lt;code&gt;fs.createReadStream()&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Duplex: streams that are both Readable and Writable (for example, &lt;code&gt;net.Socket&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Transform: Duplex streams that can modify or transform the data as it is written and read (for example, &lt;code&gt;zlib.createDeflate()&lt;/code&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  11. Why should you separate Express app and server?
&lt;/h2&gt;

&lt;p&gt;This ensures that the business logic is encapsulated and decoupled from the application logic which makes the project more readable and maintainable.&lt;/p&gt;

&lt;p&gt;The server is responsible for initializing the routes, middleware, and other &lt;strong&gt;application logic whereas&lt;/strong&gt; the app has all the &lt;strong&gt;business logic&lt;/strong&gt; which will be served by the routes initiated by the server. &lt;/p&gt;

&lt;h2&gt;
  
  
  12. Why V8 engine?
&lt;/h2&gt;

&lt;p&gt;Google's v8 is the open-source most evolved, by a huge community helping in developing features and fixing bugs. &lt;/p&gt;

&lt;p&gt;The fastest as a JavaScript and WebAssembly engine till now, since it's written in c++.&lt;/p&gt;

&lt;p&gt;And it is portable to almost every machine known&lt;/p&gt;

&lt;h2&gt;
  
  
  13. Exit codes of Node.js
&lt;/h2&gt;

&lt;p&gt;Exit codes give us an idea of how a process got terminated or the reason behind termination. &lt;/p&gt;

&lt;p&gt;A few of them are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Uncaught fatal exception - (code - 1)&lt;br&gt;
There has been an exception that is not handled&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Unused - (code - 2)&lt;br&gt;
This is reserved by bash&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fatal Error - (code - 5)&lt;br&gt;
There has been an error in V8 with stderr output of the description&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Internal Exception handler Run-time failure - (code - 7)&lt;br&gt;
There has been an exception when bootstrapping function was called&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Internal JavaScript Evaluation Failure - (code - 4)&lt;br&gt;
There has been an exception when the bootstrapping process failed to return function value when evaluated&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  14. What is an Event Emitter in Node.js?
&lt;/h2&gt;

&lt;p&gt;EventEmitter is a Node.js class that includes all the objects that are basically capable of emitting events. &lt;/p&gt;

&lt;p&gt;This can be done by attaching named events that are emitted by the object using an eventEmitter.on() function. &lt;/p&gt;

&lt;p&gt;Thus whenever this object throws an even the attached functions are invoked synchronously.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
myEmitter.on('event', () =&amp;gt; {
 console.log('an event occurred!');
});
myEmitter.emit('event')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  15. Clustering.
&lt;/h2&gt;

&lt;p&gt;Node.js applications run on a single processor, which means that by default they don’t take advantage of a multiple-core system. &lt;/p&gt;

&lt;p&gt;Cluster mode is used to start up multiple node.js processes thereby having multiple instances of the event loop. &lt;/p&gt;

&lt;p&gt;When we start using cluster behind the scene multiple processes are created but there is also a parent process called the cluster manager which is responsible for monitoring the health of the individual instances of our application.&lt;/p&gt;

&lt;h2&gt;
  
  
  16. What is a thread pool and which library handles it?
&lt;/h2&gt;

&lt;p&gt;The Thread pool is handled by the libuv library. &lt;/p&gt;

&lt;p&gt;libuv is a multi-platform C library that provides support for asynchronous I/O-based operations such as file systems, networking, and concurrency.&lt;/p&gt;

&lt;h2&gt;
  
  
  17. How are worker threads different from clusters?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Cluster: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There is one process on each CPU with an IPC to communicate.&lt;/li&gt;
&lt;li&gt;In case we want to have multiple servers accepting HTTP requests via a single port, clusters can be helpful. &lt;/li&gt;
&lt;li&gt;The processes are spawned in each CPU thus will have separate memory and node instance which further will lead to memory issues.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Worker threads:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There is only one process in total with multiple threads. &lt;/li&gt;
&lt;li&gt;Each thread has one Node instance (one event loop, one JS engine) with most of the APIs accessible.&lt;/li&gt;
&lt;li&gt;Shares memory with other threads (e.g. SharedArrayBuffer).&lt;/li&gt;
&lt;li&gt;This can be used for CPU-intensive tasks like processing data or accessing the file system since NodeJS is single-threaded, synchronous tasks can be made more efficient leveraging the worker's threads.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  18. How to measure the duration of async operations?
&lt;/h2&gt;

&lt;p&gt;Performance API provides us with tools to figure out the necessary performance metrics. A simple example would be using async_hooks and perf_hooks. This would give us the exact time it took to execute the callback.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'use strict';
const async_hooks = require('async_hooks');
const {
 performance,
 PerformanceObserver
} = require('perf_hooks');
const set = new Set();
const hook = async_hooks.createHook({
 init(id, type) {
if (type === 'Timeout') {
  performance.mark(`Timeout-${id}-Init`);
  set.add(id);
}
 },
 destroy(id) {
if (set.has(id)) {
  set.delete(id);
  performance.mark(`Timeout-${id}-Destroy`);
  performance.measure(`Timeout-${id}`,
                      `Timeout-${id}-Init`,
                      `Timeout-${id}-Destroy`);
}
 }
});
hook.enable();
const obs = new PerformanceObserver((list, observer) =&amp;gt; {
 console.log(list.getEntries()[0]);
 performance.clearMarks();
 observer.disconnect();
});
obs.observe({ entryTypes: ['measure'], buffered: true });
setTimeout(() =&amp;gt; {}, 1000);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  19 How to measure the performance of async operations?
&lt;/h2&gt;

&lt;p&gt;Performance API provides us with tools to figure out the necessary performance metrics. &lt;/p&gt;

&lt;p&gt;A simple example would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { PerformanceObserver, performance } = require('perf_hooks');
const obs = new PerformanceObserver((items) =&amp;gt; {
 console.log(items.getEntries()[0].duration);
 performance.clearMarks();
});
obs.observe({ entryTypes: ['measure'] });
performance.measure('Start to Now');
performance.mark('A');
doSomeLongRunningProcess(() =&amp;gt; {
 performance.measure('A to Now', 'A');
 performance.mark('B');
 performance.measure('A to B', 'A', 'B');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>"use strict" is Strict Mode</title>
      <dc:creator>Senichi</dc:creator>
      <pubDate>Mon, 20 Sep 2021 17:47:44 +0000</pubDate>
      <link>https://forem.com/senichimaro/use-strict-is-strict-mode-3059</link>
      <guid>https://forem.com/senichimaro/use-strict-is-strict-mode-3059</guid>
      <description>&lt;p&gt;Inside native ECMAScript modules (with import and export statements) and ES6 classes, strict mode is always enabled and cannot be disabled.&lt;/p&gt;

&lt;p&gt;So, the first thing to know: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1. &lt;strong&gt;Nowadays we use Strict Mode everyday.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Strict Mode is now supported by all major browsers.&lt;/p&gt;

&lt;p&gt;The syntax, for declaring strict mode, was designed to be compatible with older versions of JavaScript to has no side effects. So &lt;code&gt;"use strict";&lt;/code&gt; only matters to new compilers that "understand" the meaning of it. &lt;/p&gt;

&lt;p&gt;Strict mode code and non-strict mode code can coexist, so scripts can opt into strict mode incrementally.&lt;/p&gt;

&lt;p&gt;So, the second thing to know: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;2. &lt;strong&gt;&lt;code&gt;"use strict";&lt;/code&gt; is ignored by earlier versions of JavaScript.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;"use strict"&lt;/code&gt; directive was new in ECMAScript version 5.&lt;/p&gt;

&lt;p&gt;ECMAScript 2009, also known as ES5, was the first major revision to JavaScript.&lt;/p&gt;

&lt;p&gt;Strict mode isn't just a subset: it intentionally has different semantics from normal code.&lt;/p&gt;

&lt;p&gt;Strict mode changes both syntax and runtime behavior. &lt;/p&gt;

&lt;p&gt;JavaScript was designed to be easy for novice developers, and sometimes it solves operations which should be semantic errors into non-error operations. Sometimes this fixes the immediate problem, but sometimes this creates worse problems in the future.&lt;/p&gt;

&lt;p&gt;Strict mode treats these mistakes as errors so that they're discovered and promptly fixed.&lt;/p&gt;

&lt;p&gt;So, the third thing to know: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;3. &lt;strong&gt;Strict Mode is the beginning of modernity.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It states the before and after into javascript paradigm. It is the base of the current javascript programming basics.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Changes converted mistakes into errors (as syntax errors or at runtime).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Changes simplifyed how the particular variable for a given use of a name is computed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Changes make it easier to write "secure" JavaScript.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Changes anticipating future ECMAScript evolution.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Time travel to the past
&lt;/h2&gt;

&lt;p&gt;There was a time when such amazing things were possible, like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you could use undeclared variables (wtf! 😳)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// set a variable without the 'var' keyword
x = 3.14;

function myFunction() {
  y = 3.14;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Mistyping a variable name creates a new global variable&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;not unique function parameter names (wtf! 🤨)&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// available through 
arguments[i]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;declare functions inside control structures
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (true) {
  function f() { }
  f();
}

for (var i = 0; i &amp;lt; 5; i++) {
  function f2()
  f2();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;eval() were allowed to create variables
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;eval ("var x = 2");
alert (x);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In normal mode &lt;code&gt;this&lt;/code&gt; would return the global object (window):
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myFunction() {
  alert(this);
}
myFunction();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;delete&lt;/code&gt; keyword usuallly deletes
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// variable/object/function/property
var x = 3.14;
delete x;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Back to the Future
&lt;/h2&gt;

&lt;p&gt;In strict mode a non-existing variable/object, any assignment to a non-writable property and etc... will throw an error.&lt;/p&gt;

&lt;p&gt;Strict mode changes previously accepted "bad syntax" into real errors.&lt;/p&gt;

&lt;p&gt;Strict mode makes it easier to write "secure" JavaScript.&lt;/p&gt;

&lt;p&gt;So, the third fourth to know: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;4. &lt;strong&gt;Strict Mode move the language to a new programming style.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In general, the changes enabled by strict mode are all for the better.&lt;/p&gt;

&lt;p&gt;Improving its resiliency make it easier to understand what's going on when there are problems.&lt;/p&gt;

&lt;p&gt;JavaScript did not have exception handling until ECMAScript 3.&lt;/p&gt;

&lt;p&gt;JavaScript’s creator, Brendan Eich, had no choice but to create the language very quickly from several programming languages: Java (syntax, primitive values versus objects), Scheme and AWK (first-class functions), Self (prototypal inheritance), and Perl and Python (strings, arrays, and regular expressions).&lt;/p&gt;

&lt;p&gt;On one hand, JavaScript had quirks missing quite a bit of functionality. On the other hand, it had several powerful features that allowed you to work around these problems.&lt;/p&gt;

&lt;p&gt;Given its influences, JavaScript enables a programming style that is a mixture of functional programming (higher-order functions; built-in map, reduce, etc.) and object-oriented programming (objects, inheritance).&lt;/p&gt;

&lt;p&gt;So, the fiveth and last to know: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;4. &lt;strong&gt;Strict Mode breaks from the initial scheme and possibilities into more formal OOP.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can use strict mode in all your programs. The "use strict" directive defines that the JavaScript code should be executed in "strict mode". &lt;code&gt;"use strict"&lt;/code&gt; is just a string expression. Old browsers will not throw an error if they don't understand it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Declaring Strict Mode
&lt;/h3&gt;

&lt;p&gt;Strict mode is declared by adding &lt;code&gt;"use strict";&lt;/code&gt; to the beginning of a script or a function.&lt;/p&gt;

&lt;p&gt;Declared at the beginning of a script, it has global scope (all code in the script will execute in strict mode):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"use strict";
x = 3.14;  // throw error
var x = 3.14;  // OK
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Declared inside a function, it has local scope (only the code inside the function is in strict mode):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myFunction() {
  "use strict";
  y = 3.14;   // throw error
  var y = 3.14;   // OK
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Declare local or global &lt;code&gt;"use strict"&lt;/code&gt; will throw an error feedback by any not legal assignment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Nowadays we use Strict Mode everyday because inside native ECMAScript modules (with import and export statements) and ES6 classes, strict mode is always enabled and cannot be disabled.&lt;/p&gt;

&lt;p&gt;Strict mode code and non-strict mode code can coexist. In general, the changes enabled by strict mode are all for the better.&lt;/p&gt;

&lt;p&gt;Strict mode changes both syntax and runtime behavior and previously accepted "bad syntax" changes into real errors.&lt;/p&gt;

&lt;p&gt;Strict mode treats these mistakes as errors or "bad syntax" to they're discovered and promptly fixed.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"use strict"&lt;/code&gt; is just a string expression. Old browsers will not throw an error if they don't understand it.&lt;/p&gt;

&lt;p&gt;The syntax, for declaring strict mode, was designed to be compatible with older versions of JavaScript to has no side effects. So &lt;code&gt;"use strict";&lt;/code&gt; only matters to new compilers that "understand" the meaning of it.&lt;/p&gt;

&lt;p&gt;Declared at the beginning of a script (global scope)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// global scope - (on top)
"use strict";

// local scope - inside block
function myFunction() {
  "use strict";
  [...]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>strictmode</category>
      <category>beforees6</category>
    </item>
    <item>
      <title>What are the different types of errors in JavaScript?</title>
      <dc:creator>Senichi</dc:creator>
      <pubDate>Sun, 19 Sep 2021 16:28:42 +0000</pubDate>
      <link>https://forem.com/senichimaro/what-are-the-different-types-of-errors-in-javascript-2k6j</link>
      <guid>https://forem.com/senichimaro/what-are-the-different-types-of-errors-in-javascript-2k6j</guid>
      <description>&lt;p&gt;Errors are statements which don't let the program run properly. &lt;strong&gt;There are three main types of errors that can occur while compiling&lt;/strong&gt; a JavaScript program. These errors include syntax errors, runtime errors, and logical errors.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1. Syntax Errors (most common)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Incorrect syntax raises parsing errors and occur at the interpretation time. Simple example is to introduce a semicolon where a double-colon it's needed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let exObj = { name; 'Rick' }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;ul&gt;
&lt;li&gt;2. Runtime Errors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Occur after the compiler interpret the code, when the code ran. Also known as exceptions. It can occur by calling a function that It wasn't declared.&lt;/p&gt;

&lt;p&gt;The syntax is correct but the function is not present.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// calling not declared function
doSomeStuff()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;ul&gt;
&lt;li&gt;3. Logical Errors (most difficult to find)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Error in the flow of data. The logical elements are well structured, the syntax is correct, but the confluence of data lands improperly.&lt;/p&gt;

&lt;p&gt;Consider the script "There is a movie called Terminator 2". Consider the happy ending statement path: ''John Connor kills Terminator.''&lt;/p&gt;

&lt;p&gt;Now consider a logical error: ''Terminator kills John Connor.'' &lt;/p&gt;

&lt;p&gt;These kind of errors change how your program should work from the result we would expect.&lt;/p&gt;




&lt;h3&gt;
  
  
  Synthesis
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;syntax errors : while interpretation. Simple example: misspelling.&lt;/li&gt;
&lt;li&gt;runtime errors: while execution. Simple example: due to misuse.&lt;/li&gt;
&lt;li&gt;logical errors: scrambled logical perform. Simple example: ''Terminator kills John Connor.''&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>theory</category>
      <category>errors</category>
    </item>
  </channel>
</rss>
