Category: coding

  • What is Lorem Ipsum?

    What is Lorem Ipsum?

    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

    Why do we use it?

    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using ‘Content here, content here’, making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for ‘lorem ipsum’ will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

    Where does it come from?

    Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of “de Finibus Bonorum et Malorum” (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, “Lorem ipsum dolor sit amet..”, comes from a line in section 1.10.32.

    The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from “de Finibus Bonorum et Malorum” by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.

    Where can I get some?

    There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don’t look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn’t anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.

    paragraphswordsbyteslistsStart with ‘Lorem
    ipsum dolor sit amet…’
  • CS50: Introduction to Computer Science

    I had never taken a computer science class before, even though I’ve been learning to code for a few years. One day, I asked myself: How does a computer actually work? I started looking for a course and was surprised to find that Harvard University offers a free online course called CS50: Introduction to Computer Science. Yes, a free course from Harvard, I couldn’t believe it either.

    The course was very interesting from the start. The professor said that some topics would be hard to understand at first, but over time everything would make sense. He also explained that the ideas in this course give a foundation to program in many languages like C, Python, or JavaScript.

    CS50 starts with the basics of how computers work. We learned about zeros and ones, and how everything (images, sounds, letters) is stored as bits. It was amazing to see how computers handle so much information using simple ones and zeros.

    One of the most useful things I learned was planning a program before writing any code. The professor showed how to write a program in plain English first, step by step, before turning it into code. This makes it much easier to write correct and logical programs.

    The course also teaches how to solve problems efficiently. For example, if you want to find a number in a sorted list, instead of checking every number, you can split the list in half and focus only on the part where the number could be. This method saves a lot of time when dealing with big lists. The course also explains Big O notation, which is just a way to measure how fast or slow a program can be with larger amounts of data.

    After the basics, the course moves on to programming concepts like boolean expressions, if statements, loops, and functions. You apply these ideas in Scratch and MIT App Inventor, building small games and projects. Later weeks cover C, memory, data structures, Python, web development, and even AI basics. Each topic is explained clearly and with examples, which makes learning easier and more fun.

    I finished the whole course, and I can say it is one of the best introductions to computer science. It teaches not only how to write code, but also how to think like a programmer. The exercises and projects help you understand the ideas well, and the professor makes the course exciting and motivating.

    If you are just starting with programming, or want to strengthen your basics, CS50 is an excellent course to take.

  • Understanding useEffect() in React

    Hooks are an essential part of React, and useEffect is one of the most common ones.

    In this article, I want to explain how useEffect works and when it should be used. This is mostly based on how I understand it and how I’ve used it in my own projects.

    useEffect helps us deal with side effects in React. Side effects are things we want to do outside of rendering UI. For example, manipulating the DOM, making API calls, setting up event listeners, or working with timers.

    The main idea is simple. useEffect allows us to run some code after a component renders.

    Running an effect after every render

    If we do not pass a dependency array, the effect will run after every render.

    useEffect(() => {
      console.log("Component rendered");
    });
    

    Every state change that causes a re-render will trigger this effect. This is usually not what we want, but it can be useful in some cases.

    Running an effect only once

    If we pass an empty dependency array, the effect will run only once, when the component first renders.

    useEffect(() => {
      console.log("Component mounted");
    }, []);
    

    This is often used for things like fetching data when the page loads or setting up initial logic.

    Running an effect when values change

    If we pass values inside the dependency array, the effect will run only when one of those values changes.

    useEffect(() => {
      console.log("List changed");
    }, [list]);
    

    In this example, the effect runs only when list changes, such as when we add or remove an item. This makes useEffect more predictable and easier to reason about.

    Infinite loops (a common mistake)

    One important thing to watch out for is infinite loops. If an effect updates state, and that same state is also listed as a dependency, the effect can keep running forever.

    useEffect(() => {
      setCount(count + 1);
    }, [count]);
    

    This will cause the effect to run again and again. Spoiler alert: I’ve done this before, and it’s not fun to debug.

    Cleanup in useEffect

    When we work with events, subscriptions, or timers, we should clean them up. React gives us a way to do this by returning a function from useEffect.

    useEffect(() => {
      const handleResize = () => {
        console.log("Window resized");
      };
    
      window.addEventListener("resize", handleResize);
    
      return () => {
        window.removeEventListener("resize", handleResize);
      };
    }, []);
    

    Without cleanup, we can end up with memory leaks or performance issues, especially in larger applications.

    Do we always need useEffect?

    So far, this covers the basics of useEffect. From here, I want to make one important point. Just because useEffect exists does not mean we should always use it.

    In many cases, adding effects can make code harder to read and can introduce unexpected bugs. This idea comes from a talk by Dan Abramov, where he explains that many effects are unnecessary and that some logic can live directly in render or be handled differently.

    I haven’t deeply researched this yet, but from my own experience, this idea makes sense. Overusing useEffect can complicate things very quickly.

    This is something I plan to explore more as I continue learning and building with React.