diff --git a/blog_posts/code-anatomy-performant-python.md b/blog_posts/code-anatomy-performant-python.md index f7f52a6e2..45950cf35 100644 --- a/blog_posts/code-anatomy-performant-python.md +++ b/blog_posts/code-anatomy-performant-python.md @@ -6,7 +6,7 @@ authors: maciv,chalarangelo cover: blog_images/walking-on-top.jpg excerpt: Writing short, efficient Python code is not always straightforward. Read how we optimize our list snippets to increase performance using a couple of simple tricks. firstSeen: 2020-03-15T12:50:05+02:00 -lastUpdated: 2021-06-12T19:30:41+03:00 +lastUpdated: 2021-11-07T16:34:37+03:00 --- Writing short and efficient Python code is not always easy or straightforward. However, it's often that we see a piece of code and we don't realize the thought process behind the way it was written. We will be taking a look at the [difference](/python/s/difference) snippet, which returns the difference between two iterables, in order to understand its structure. @@ -18,7 +18,7 @@ def difference(a, b): return [item for item in a if item not in b] ``` -The above implementation may work well enough, but doesn't account for duplicates in `b`, making the code take more time than necessary in cases with many duplicates in the second list. To solve this issue, we can make use of the `set()` method, which will only keep the unique values in the list: +This implementation may work well enough, but doesn't account for duplicates in `b`. This makes the code take more time than necessary in cases with many duplicates in the second list. To solve this issue, we can make use of the `set()` method, which will only keep the unique values in the list: ```py def difference(a, b): @@ -50,7 +50,7 @@ def difference(a, b): return [item for item in a if item not in _b] ``` -Another option worth mentioning when analyzing performance for this snippet is the use of a list comprehension versus using something like `filter()` and `list()`. Implementing the same code using the latter option would result in something like this: +Another option worth mentioning in terms of performance is the use of a list comprehension versus `filter()` and `list()`. Implementing the same code using the latter option would result in something like this: ```py def difference(a, b): @@ -58,6 +58,6 @@ def difference(a, b): return list(filter(lambda item: item not in _b, a)) ``` -Using `timeit` to analyze the performance of the last two code examples, it's pretty clear that using list comprehension can be up to ten times faster than the alternative, as it's a native language feature that works very similar to a simple `for` loop without the overhead of the extra function calls. This explains why we prefer it, apart from readability. +Using `timeit` to analyze the performance of the last two code examples, it's pretty clear that using list comprehension can be up to ten times faster than the alternative. This is due to it being a native language feature that works very similar to a simple `for` loop without the overhead of the extra function calls. This explains why we prefer it, apart from readability. This pretty much applies to most mathematical list operation snippets, such as [difference](/python/s/difference), [symmetric_difference](/python/s/symmetric-difference) and [intersection](/python/s/intersection). diff --git a/blog_posts/css-pseudo-classes.md b/blog_posts/css-pseudo-classes.md index 3c65bf774..08be4ee1f 100644 --- a/blog_posts/css-pseudo-classes.md +++ b/blog_posts/css-pseudo-classes.md @@ -6,7 +6,7 @@ authors: chalarangelo cover: blog_images/orange-flower.jpg excerpt: Learn how to use CSS pseudo-classes to style an element based on changes to its state. firstSeen: 2020-08-18T19:56:12+03:00 -lastUpdated: 2021-06-12T19:30:41+03:00 +lastUpdated: 2021-11-07T16:34:37+03:00 --- CSS pseudo-classes provide a way to style elements, based on changes to their state. For example, `:hover` can be used to apply additional styles to an element when the user's pointer hovers over it. @@ -15,7 +15,7 @@ Pseudo-classes let you apply styles to elements in relation to the content of th ### Commonly used pseudo-classes -Below is a list of the top 5 most commonly used pseudo-classes and their usage. This list is by no means complete; you should always refer to relevant documentation from authoritative sources, such as [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes) for more information. +Below is a list of the top 5 most commonly used pseudo-classes and their usage. This list is by no means complete. You should always refer to relevant documentation from authoritative sources, such as [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes) for more information. - `:hover`, `:focus` and `:active` are used to provide feedback for user interaction (e.g. changing a button's color on hover) - `:link` and `:visited` are useful for styling links based on navigation history (e.g. changing the color of visited links) diff --git a/blog_posts/javascript-higher-order-functions.md b/blog_posts/javascript-higher-order-functions.md index 551d05134..6653a9838 100644 --- a/blog_posts/javascript-higher-order-functions.md +++ b/blog_posts/javascript-higher-order-functions.md @@ -6,14 +6,14 @@ authors: chalarangelo cover: blog_images/rock-climbing.jpg excerpt: Learn everything you need to know about higher-order functions with this short guide and level up your programming skills. firstSeen: 2020-09-24T12:54:08+03:00 -lastUpdated: 2021-06-12T19:30:41+03:00 +lastUpdated: 2021-11-07T16:34:37+03:00 --- Higher-order functions are functions that operate on other functions, either by taking them as arguments or by returning them as their results. This allows us to create an abstraction layer over actions, not just values. -The reason we can write higher-order functions in JavaScript is due to the fact that functions are values, meaning they can be assigned to variables and passed as values. You might also often hear the term _callback_ when referring to a function that is passed as an argument, due to it being called by the higher-order function. This is particularly common in JavaScript, with event handling, asynchronous code and array operations relying heavily on the concept of callbacks. +The reason we can write higher-order functions in JavaScript is due to the fact that functions are values. This means they can be assigned to variables and passed as values. You might also often hear the term _callback_ when referring to a function that is passed as an argument. This is due to it being called by the higher-order function. Callbacks are particularly common in JavaScript, with event handling, asynchronous code and array operations relying heavily on them. -The main advantages of this technique are, as mentioned before, the abstraction layers it allows us to create as well as composition and more reusable and readable code. Most of the 30 seconds of code snippets are based on the idea of higher-order functions, as they promote small, easily digestible functions that can be easily composed to create more complex logic and are highly reusable. +The main advantages of this technique are abstraction, composition, code reusability and readability. Most of the 30 seconds of code snippets are built with higher-order functions in mind. They are small, easily digestible functions that are highly reusable and can be composed to create more complex logic. That being said, we can take a look at an example, utilizing some very simple functions: @@ -27,4 +27,4 @@ const evenValues = data.filter(isEven); // [2, 4, 6] const evenSum = data.filter(isEven).reduce(add); // 12 ``` -In the above example, we define two simple functions that we then use as callbacks in `Array.prototype.reduce()` and `Array.prototype.filter()` to get the result we want. Both of these functions are higher-order functions, allowing us to create an abstraction layer for any action we might want to perform without having to rewrite how the filtering or reduction algorithm is to be applied every single time. +In this example, we define two simple functions that we then use as callbacks in `Array.prototype.reduce()` and `Array.prototype.filter()` to get the result we want. Both of these functions are higher-order functions. This allows us to create an abstraction layer for any action we might want to perform without having to rewrite how the filtering or reduction algorithm is to be applied every single time. diff --git a/blog_posts/javascript-memoization.md b/blog_posts/javascript-memoization.md index f4aa5f3a2..4196c7c62 100644 --- a/blog_posts/javascript-memoization.md +++ b/blog_posts/javascript-memoization.md @@ -6,14 +6,14 @@ authors: chalarangelo cover: blog_images/cherry-trees.jpg excerpt: Learn different ways to memoize function calls in JavaScript as well as when to use memoization to get the best performance results. firstSeen: 2020-02-27T16:23:25+02:00 -lastUpdated: 2021-06-12T19:30:41+03:00 +lastUpdated: 2021-11-07T16:34:37+03:00 --- -Memoization is a commonly used technique that you can use to speed up your code significantly. It uses a cache to store results, so that subsequent calls of time-consuming functions do not perform the same work another time. Based on this definition, we can easily extract some criteria that can help us decide when to use memoization in our code: +Memoization is a commonly used technique that can help speed up your code significantly. This technique relies on a cache to store results for previously completed units of work. The purpose of the cache is to avoid performing the same work more than once, speeding up subsequent calls of time-consuming functions. Based on this definition, we can easily extract some criteria that can help us decide when to use memoization in our code: -- Memoization should be mainly used to speed up slow-performing, costly or time-consuming function calls -- Memoization speeds up subsequent calls, so it is best used when you anticipate multiple calls of the same function under the same circumstances -- Memoization stores results in memory, therefore it should be avoided when the same function is called multiple times under very different circumstances +- Memoization is useful mainly in speeding up slow-performing, costly or time-consuming function calls +- Memoization speeds up subsequent calls, so it's best used when you anticipate multiple calls of the same function under the same circumstances +- Memoization stores results in memory, so it should be avoided when the same function is called multiple times under very different circumstances A simple, object-oriented example of implementing memoization could be as follows: @@ -43,7 +43,7 @@ for (let i = 0; i < 100; i ++) myObject.firstNonEmptyItemMemo(); // ~70ms ``` -The above example showcases a way to implement memoization inside a class, however it makes the assumptions that the `data` structure will not be altered over the lifecycle of the object and that this is the only expensive function call we will make, so it cannot be reused. It also doesn't account for arguments being passed to the function, which would alter the result. A functional approach that would work with any given function and also account for arguments can be found in the form of the [memoize snippet](/js/s/memoize/), which uses a `Map` to store different values. +This example showcases a way to implement memoization inside a class. However, it makes a couple of assumptions. First, it's assumed that the `data` structure will not be altered over the lifecycle of the object. Seconds, it's assumed that this is the only expensive function call we will make, so the code is not reusable. The example also doesn't account for arguments being passed to the function, which would alter the result. A functional approach would work with any given function and also account for arguments. Such an approach can be seen in the form of the [memoize snippet](/js/s/memoize/), which uses a `Map` to store different values. We still recommend using that snippet as the primary way to memoize a function, however JavaScript's [Proxy object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) provides an interesting alternative via the use of the `handler.apply()` trap, which can be used for this purpose as follows: diff --git a/blog_posts/javascript-switch-object.md b/blog_posts/javascript-switch-object.md index 0d5ffa7fe..b9fdfadda 100644 --- a/blog_posts/javascript-switch-object.md +++ b/blog_posts/javascript-switch-object.md @@ -6,12 +6,12 @@ authors: chalarangelo cover: blog_images/rocky-lake.jpg excerpt: JavaScript's `switch` statement often feels hard to remember and a little bit out of place. Maybe it's time to use object literals, instead. firstSeen: 2021-04-01T12:00:00+03:00 -lastUpdated: 2021-06-12T19:30:41+03:00 +lastUpdated: 2021-11-07T16:34:37+03:00 --- JavaScript's `switch` statement is one of the few things I find hard to remember the syntax for (so glad VS Code has autocomplete). It also feels a little bit out of place syntactically, as it's the only thing that doesn't use curly braces and you need to remember to `break` for every `case`. Moreover, its performance is less than stellar as its control flow is procedural. -Luckily, JavaScript provides another alternative for most use-cases I can think of for `switch` statements - object literals. The idea is to define an object with a key for each `case` you would have in a `switch` statement, then access its value directly using the expression you would pass to the `switch` statement. +Luckily, JavaScript's object literals are a pretty good alternative for most `switch` statement use-cases I can think of. The idea is to define an object with a key for each `case` you would have in a `switch` statement. Then you can access its value directly using the expression you would pass to the `switch` statement. ```js let fruit = 'oranges'; @@ -87,7 +87,7 @@ const logFruit = { (logFruit[fruit] || logFruit['default'])(); // Logs: 'Known fruit' ``` -To wrap this all up, we can generalize and extract this logic into a simple reusable function, which we will supply with the lookup object and an optional name for the default case (we'll default to `_default` to avoid any conflicts). This function will in turn return a function with the appropriate lookup logic and we can use it to replace any `switch` statement. +To wrap this all up, we can generalize and extract this logic into a simple reusable function. We will supply it with the lookup object and an optional name for the default case (we'll default to `_default` to avoid any conflicts). This function will in turn return a function with the appropriate lookup logic and we can use it to replace any `switch` statement. ```js const switchFn = (lookupObject, defaultCase = '_default') => diff --git a/blog_posts/python-swap-variables.md b/blog_posts/python-swap-variables.md index 9a1f660f9..695ec47d7 100644 --- a/blog_posts/python-swap-variables.md +++ b/blog_posts/python-swap-variables.md @@ -6,12 +6,12 @@ authors: maciv cover: blog_images/leaves-read.jpg excerpt: Learn 3 easy ways to swap the values of two variables in Python. firstSeen: 2021-02-04T11:00:00+02:00 -lastUpdated: 2021-06-12T19:30:41+03:00 +lastUpdated: 2021-11-07T16:34:37+03:00 --- ### Using a temporary variable -The simplest way to swap the values of two variables is using a `temp` variable. The `temp` variables is used to store the value of the fist variable (`temp = a`), allowing you to swap the value of the two variables (`a = b`) and then assign the value of `temp` to the second variable. +The simplest way to swap the values of two variables is using a `temp` variable. The `temp` variables is used to store the value of the fist variable (`temp = a`). This allows you to swap the value of the two variables (`a = b`) and then assign the value of `temp` to the second variable. ```py a = 11 diff --git a/blog_posts/react-conditional-classname.md b/blog_posts/react-conditional-classname.md index 0d6f869d9..b53fedff4 100644 --- a/blog_posts/react-conditional-classname.md +++ b/blog_posts/react-conditional-classname.md @@ -6,12 +6,12 @@ authors: maciv cover: blog_images/succulent-red-light.jpg excerpt: When developing React components, you might often need to conditionally apply a className. Learn how to handle empty classNames correctly using this handy tip. firstSeen: 2020-11-06T20:17:21+02:00 -lastUpdated: 2021-06-12T19:30:41+03:00 +lastUpdated: 2021-11-07T16:34:37+03:00 --- -When developing React components, you often need to conditionally apply a `className` attribute to one or more elements. Sometimes, you will have two or more possible values depending on a condition, but there are also times that you might apply a `className` based on a condition or leave it completely empty otherwise. +When developing React components, you often need to conditionally apply a `className` attribute to one or more elements. Sometimes, you will have two or more possible values depending on a condition. But there are also times that you might apply a `className` based on a condition or leave it completely empty otherwise. -However, there is a correct way to handle a conditional empty `className` and an incorrect one and there are many examples of handling this incorrectly all around the web. Consider the following example: +There is a correct way to handle a conditional empty className and an incorrect one. Surprisingly, the incorrect way is pretty common and examples of it can be found all around the web. Consider the following code: ```jsx const MyComponent = ({ enabled }) => { @@ -31,8 +31,8 @@ ReactDOM.render( ); ``` -In the code example above, we create two very similar components, both of which conditionally set the `className` of an element based on the value of the `enabled` prop. The first one will set the `className` to an empty string if `enabled` is `false` and the second one will set it to `null`. +In this code example, we define two very similar components. Both of them conditionally set the `className` of an element based on the value of the `enabled` prop. The first one will set the `className` to an empty string if `enabled` is `false` and the second one will set it to `null`. -Both will result in a very similar output, however, if you carefully inspect the HTML, you will notice that the first one will render `