投稿時間:2023-01-08 07:09:05 RSSフィード2023-01-08 07:00 分まとめ(8件)
カテゴリー等 | サイト名等 | 記事タイトル・トレンドワード等 | リンクURL | 頻出ワード・要約等/検索ボリューム | 登録日 |
---|---|---|---|---|---|
Git | Gitタグが付けられた新着投稿 - Qiita | Gitの使い方 | https://qiita.com/Mizut452/items/323c49f93b721a0ea77e | 記録 | 2023-01-08 06:10:05 |
海外TECH | MakeUseOf | How to Remote Access Linux From a Windows Computer | https://www.makeuseof.com/tag/remote-control-linux-windows/ | linux | 2023-01-07 21:30:15 |
海外TECH | DEV Community | No to Imperative Code! Because Debugging Spaghetti is No Fun | https://dev.to/noriller/no-to-imperative-code-because-debugging-spaghetti-is-no-fun-4hle | No to Imperative Code Because Debugging Spaghetti is No FunIt s tempting to write imperative code because it can be easier to understand and write especially for those who are new to programming But let s be real who wants to be a newbie forever Here are some reasons why you should avoid imperative code even when using classes object oriented programming OOP or functional paradigm Just because you re writing in hipster functional languages or old school OOP languages people will always find a way to add imperative spaghetti code It s kind of amazing really Imperative Code is Harder to Maintain and DebugImperative code often relies on a lot of explicit details and state changes which can make it difficult to understand the overall flow of the code This leads to more harder to fix bugs Debugging imperative code is like trying to find a needle in a haystack except the haystack is on fire and the needle is actually a piece of spaghetti Good luck with that Imperative Code is Less Flexible and ReusableImperative code often focuses on how to accomplish a specific task rather than on the overall logic and abstractions This can make it harder to adapt the code for different use cases or to integrate it with other code Trying to reuse imperative code is like trying to fit a square peg in a round hole it might work for a little bit but eventually it s going to cause more problems than it solves A Better Approach Declarative CodeDeclarative code focuses on what the code should do rather than on how to do it Declarative code is often easier to understand maintain and reuse because it relies on abstractions and high level logic Hey let s be real the problem is not really using imperative code sometimes the brute force solution is the best first solution After it works you then refactor it to make it better And maybe probably before that you would test to make sure you re not breaking anything For example rather than writing code that defines every step in detail mutating variables and states you can use higher order functions and immutable data structures to define the logic of your code Here are some examples of imperative and declarative code using TypeScript Imperative code function findMax arr number number let max arr for let i i lt arr length i if arr i gt max max arr i return max console log findMax Declarative code function findMax arr number number return arr reduce acc cur gt cur gt acc cur acc Infinity console log findMax Let s compareAnd I ll first start with the declarative one and please do disregard the small detail of it being only one line If you understand what reduce does then you know it starts with the lowest number possible minus infinity and then you check which of the accumulator or current is larger and return the larger one Yes you need to know a lot more beforehand it s an upfront investment and then you can easily read understand and debug the code And yes you could simply use Math max but where would be the fun in that Meanwhile the imperative one You start with a number you loop through all of them if it is bigger you change the max variable and when you are finished you return max It s straightforward it works but wait why did the loop start at index Go read it back I m waiting Ah We already started with index at the max variable See Even in this short example you have to pay double the attention Now check your most recent push for this type of spaghetti and beat yourself with your keyboard if you find any You can also pester your pasta chef colleagues for the same I hear mechanical keyboards make a nice sound when hitting heads Yes I tested and I know you will too Please send videos I can t stress enough how much I hate imperative codeIn conclusion you write code once and read it every time after that Not only that I might be the one reading it and I don t really want to see what was in your head at that moment just to understand that piece of pasta code So let s do us all a favor and just not use imperative statements okay Just remember that declarative code is like a well oiled machine It may take a little more effort to set up initially but it will pay off in the end Cover Photo by Christine Sandu on Unsplash | 2023-01-07 21:14:03 |
海外TECH | DEV Community | What is destructuring in JavaScript? | https://dev.to/ezinne_anne/what-is-destructuring-in-javascript-1aae | What is destructuring in JavaScript Destructuring in JavaScript means copying the values of an array or the properties of an object to a variable Destructuring does not imply destruction It is not the case because the array or object you could copy the values from is not modified This is a JavaScript expression that was introduced in ECMAScript In this article you will learn how to destructure values in an array properties in an object and other uses of destructuring Basic destructuring in arraysDestructuring makes it easier to access the values of an array For example If you want to access values in an array without destructuring const array deer greet heyy const arr array const arr array console log arr deerconsole log arr greetIn this example you assigned the values by using their index But with destructuring this is how it will be const array deer greet heyy const arr arr arr array console log arr deerconsole log arr greetSo you could assign the values of the array variable to the new variables all at the same time Swapping valuesIn arrays you could swap the values in a variable by switching places Usually you would need a third variable to achieve this Here s an example let arr deer let arr greet console log arr deerconsole log arr greetlet newArr arr assigning the value greet to a new variable newArr console log newArr greetarr arr assigning the value deer to the variable arr arr newArr assigning the value greet to the variable arr console log arr deerconsole log arr greetBut with destructuring you can do that without a third variable const array deer greet heyy let arr arr arr array console log arr deerconsole log arr greet arr arr arr arr using destructuring to swap the values console log arr greetconsole log arr deer Skipping ValuesYou could also skip values when assigning values to a variable You do that by adding an extra comma to the array you are destructuring the values to The comma indicates the index of the value In this example a comma at the assignment s beginning indicates the array s first value const array deer greet heyy const arr arr array skipping the first value and using only the second and thirdconsole log arr greetconsole log arr heyy Using array destructuring with iteratorsArray destructuring works on all iterable values You can array destructure any iterable value Examples of such iterables are strings sets and so on For example using array destructuring on a string const a b abc console log a aconsole log b bexample of using array destructuring on a set const array deer greet heyy const exampleSet new Set array const arr arr arr exampleSet console log arr deerconsole log arr greetconsole log arr heyyyArray destructuring works on iterable values only Non iterable values would give an error Examples of non iterables are null and undefined Also javascript would return undefined if the array you are destructuring is empty for example const arr arr console log arr undefinedconsole log arr undefinedHere is another scenario const array const arr arr array console log arr undefinedconsole log arr undefined Default values in arraysWhen writing code there are cases where you may be unsure of the values you are assigning If that value is undefined you would need a default value Destructuring makes it easier for you to assign default values to an array See this example const array deer greet heyy const arr arr arr arr array console log arr wordconsole log arr fangconsole log arr greyconsole log arr undefinedTo assign a default value to arrconst array deer greet heyy To assign a default value to arrconst arr arr arr arr mirrors array console log arr mirrorsWith that you can assign a default value to a variable while destructuring Using the rest syntaxDestructuring in Javascript also uses the rest syntax It is called the rest syntax as it covers the rest of the items in an array The syntax is then the name of the array created The name rest that appears after the three dots is optional You could use another name after the three dots Syntax rest When using the rest syntax always make sure it comes last after other items as it will give an error if you do otherwise Here s an example of rest in an array Using the previous example const array deer greet heyy const arr …newArr array console log arr deerconsole log newArr greet heyy As you can see the newArr array was created from the old array Object destructuringWith object destructuring you can retrieve property values from an object Objects take a different syntax from arrays when assigning values to variables To assign values without destructuring would be const obj word dataTypes num bool true bigInt example of the object To access the object propertyconst words obj word const boolean obj bool console log words dataTypesconsole log boolean trueThis is how to assign values to objects using destructuring const obj word dataTypes num bool true bigInt example of the object To access the object propertyconst word bool obj console log word dataTypesconsole log bool true This makes value assignments easier Note The order does not matter in objects and you should use the same name This would result in the following error const obj word dataTypes num bool true bigInt const words boolean obj console log words console log boolean ReferenceError obj is not definedThis is because JavaScript does not store the words and boolean variables as references for the object hence the error However you can assign different names to the values while destructuring Assigning different names to the object propertiesYou can assign different names to the object properties to access them const obj word dataTypes num bool true bigInt const word words bool boolean obj console log words dataTypesconsole log boolean trueUsing object destructuring on arraysYou can use the index of an array as a property when you apply object destructuring to arrays const arr arr arr deer greet heyy console log arr deerconsole log arr heyyInitial declaration of variableIn objects you could declare a variable initially before you assigned a value to it let word const obj word dataTypes num bool true bigInt word obj console log word dataTypes Note during an assignment use parentheses or JavaScript would see it as a block function and return a syntax error Using the rest syntax in objectsYou could use the rest syntax in an object Like in arrays the rest syntax comes at the end and covers the remaining properties in an object Here s an example const obj word dataTypes num bool true bigInt example of the object To access the object propertyconst word words others obj console log words dataTypesconsole log others num bool true bigInt Note Like in arrays javascript would return undefined when it is set to destructure an empty object Object destructuring will return a type error when the values are undefined and null Default values in objectsIn an object you could assign default values to variables when destructuring If a property was not in the object you could create a variable and assign a default value to it For example const obj word dataTypes num bool true bigInt example of the object To access the object propertyconst word bool num str street obj console log word dataTypesconsole log str streetIn this example the default value street was assigned to the str variable while destructuring Summary That sums up the main features of destructuring in JavaScript I hope you learned something from this If you liked this piece you could share it with a friend or leave a comment Thanks for reading Need more information Check out this guide for more on destructuring in javascript | 2023-01-07 21:08:42 |
海外科学 | NYT > Science | ‘Consciousness’ in Robots Was Once Taboo. Now It’s the Last Word. | https://www.nytimes.com/2023/01/06/science/robots-artificial-intelligence-consciousness.html | difficult | 2023-01-07 21:46:42 |
ニュース | @日本経済新聞 電子版 | 芥川賞・直木賞19日決定 候補10作、あなたへの一冊 https://t.co/3ftzOEWz5W | https://twitter.com/nikkei/statuses/1611838528140476416 | 芥川賞 | 2023-01-07 21:33:46 |
ニュース | BBC News - Home | Prince Harry says he cried once after Diana death | https://www.bbc.co.uk/news/uk-64197679?at_medium=RSS&at_campaign=KARANGA | diana | 2023-01-07 21:00:58 |
ニュース | BBC News - Home | Sheffield Wednesday 2-1 Newcastle United: Josh Windass double gives League One side win | https://www.bbc.co.uk/sport/football/64167995?at_medium=RSS&at_campaign=KARANGA | newcastle | 2023-01-07 21:28:01 |
コメント
コメントを投稿