Today I learned

Unicode & Character Sets
> ##### The Single Most Important Fact About Encodings : > ##### It does not make sense to have a string without knowing what encoding it uses. You can no longer stick your head in the sand and pretend that "plain" text is ASCII. > ##### If you have a string, in memory, in a file, or in an email message, you have to know what encoding it is in or you cannot interpret it or display it to users correctly. ###### Reference: [What Every Software Developer Must Know About Unicode ...](http://www.joelonsoftware.com/articles/Unicode.html)
Mosh : SSH for intermittent connectivity
- Stays connected when network disconnects & reconnects - Doesn't lag over slow network (unlike SSH) [Source](https://mosh.org/)
Spaces in digital media
HTML provides 4 breaking spaces (width variation): - `<space>`, ` `, ` `, ` ` and 1 non-breaking fixed-width space: - ` ` ###### **Reference** : [Character Codes](https://dev.w3.org/html5/html-author/charref) , [Sentence spacing](https://en.wikipedia.org/wiki/Sentence_spacing_in_digital_media) *** Unicode provides a lot more variations on space width and breakability. ###### **Reference** : [Unicode spaces](https://www.cs.tut.fi/~jkorpela/chars/spaces.html)
Interesting Javascript comparisons
Prefer `===` over `==`. Latter leads to ambiguous comparisons. | | | | :- | - | '' == '0' | false 0 == '' | true 0 == '0' | true false == 'false' | false false == '0' | true false == undefined | false false == null | false null == undefined | true ' ' == 0 | true ##### Source: Javascript: The Good Parts - Douglas Crockford
Handy Google search operators
Op | Meaning | Example - | - | - - | skip the word or site | `jaguar -car -site:example.com` "" | search exact phrase | `"imagine all the people"` * | wildcard | `a * saved is a * earned` .. | range | `camera $50..$100` site | restrict to site | `olympics site:nbc.com` cache | google's cached page | `cache:washington.edu` ###### [Reference](https://support.google.com/websearch/answer/2466433?hl=en)
Why is it dangerous to rewrite your entire application
> ##### Netscape made the "single worst strategic mistake that any software company can make" by deciding to rewrite their code from scratch > ##### Is there an alternative? The consensus seems to be that the old Netscape code base was really bad. Well, it might have been bad, but, you know what? It worked pretty darn well on an awful lot of real world computer systems. > > ##### The sheer volume of bugs, it seems, proves that rewriting code from scratch does not make for a better code base, it makes it worse. Old code doesn't rust, it gets better, as bugs are fixed > ##### When you throw away code and start from scratch, you are throwing away all that knowledge. All those collected bug fixes. Years of programming work. > > ##### You are throwing away your market leadership. You are giving a gift of two or three years to your competitors, and believe me, that is a long time in software years. - [Things You Should Never Do](http://www.joelonsoftware.com/articles/fog0000000069.html) - [Netscape Goes Bonkers](http://www.joelonsoftware.com/articles/fog0000000027.html) - [When is a BIG Rewrite the answer ?](http://programmers.stackexchange.com/a/6303) - [Have you ever been involved in a BIG Rewrite ?](http://programmers.stackexchange.com/a/6315)
Javascript RegExp is stateful
Javascript `RegExp` object is stateful i.e. if you re-use a `RegExp` object (with `'g'` flag set), subsequent matches would start from the end of the last match. This specified by the property `lastIndex`. ``` r = new RegExp('abc', 'g'); r.lastIndex // 0 r.exec('abcdef') // ["abc"] r.lastIndex // 3 r.exec('abcdef') // null ``` To avoid this, follow either of these: - Don't use the 'g' flag - Create a new `RegExp` object for each match - Manually set `lastIndex` to 0 after each match Reference: - [Why RegExp with global flag gives wrong results](http://stackoverflow.com/questions/1520800/why-regexp-with-global-flag-in-javascript-give-wrong-results) - [regexp.lastIndex](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex)
Floating point arithmetic
```js foo = 0.15 + 0.15 bar = 0.1 + 0.2 foo == bar // can be false foo >= bar // can also be false ``` ###### 1. [Floating point comparison](http://floating-point-gui.de/errors/comparison/) ###### 2. [Is floating point math broken?](http://stackoverflow.com/a/588014) ###### 3. [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html)
Restart an unresponsive ubuntu machine
Hold `Alt` + `SysRq` (Print Screen) & type the letters `REISUB` *(mnemonic : **R**eboot **E**ven **I**f **S**ystem **U**tterly **B**roken)* ###### P.S. `Ctrl` + `Alt` + `Backspace` restarts X server
Count in PostgreSQL
`#` of rows ```sql COUNT(*) ``` `#` of rows where `val` is not `null` ```sql COUNT(val) ``` `#` of rows where `val` is distinct & not `null` ```sql COUNT(DISTINCT val) ```
MacOS : clipboard from command line
Copy to system clipboard by piping the text to `pbcopy` ``` echo "#TIL to pbcopy to clipboard" | pbcopy ``` Create a file from clipboard contents with `pbpaste` ``` pbpaste > clipboard_contents.txt ```
Unicode : UTF-8 vs UTF-16 vs UTF-32
| UTF-8[^utf8] | UTF-16[^utf16] | UTF-32[^utf32] | | ----- | ----- | ------ | | Variable-width | Variable-width | Fixed-width | | 1 - 4 bytes | 2 or 4 bytes | 4 bytes | | compatibile with ASCII | - | - | | No byte order issue | - | - | | Used in > 87% web pages[^stats1] | less than 0.1% | less than 0.1% | --- Tip: Use `iconv` to convert file from one encoding to another. Example: `iconv -f UTF-16LE -t UTF-8` [^stats1]: [Usage of character encodings](https://w3techs.com/technologies/overview/character_encoding/all) [^utf8]: [UTF-8](https://en.wikipedia.org/wiki/UTF-8) [^utf16]: [UTF-16](https://en.wikipedia.org/wiki/UTF-16) [^utf32]: [UTF-32](https://en.wikipedia.org/wiki/UTF-32)
Javascript : null vs undefined
`null` : A primitive value that represents intentional absence of any object value. To check for null, use: ```js x === null // true ``` *** `undefined` : The global property represents the primitive value undefined. A variable that has not been assigned a value is of type undefined. To check for undefined, use either: ```js typeof x === 'undefined' // true x === undefined // true ``` Former works even if `x` has not been declared. *** Differences between `null` and `undefined` ```js typeof null // 'object' * typeof undefined // 'undefined' null === undefined // false null == undefined // true ``` \* bug in ECMAScript, should be null Reference : [null](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/null) & [undefined](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/undefined)
There's no "JSON object"
Javascript objects look similar to JSON hence developers often use them interchangeably. However, JSON is a language independent data interchange specification (like XML) ```js // JSON String. var json = '{ "prop": "val" }'; // JS Object literal var bar = { "prop": "val" }; ``` JSON has a strict syntax and all property names & string values must be in **double** quotes. ###### - [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) ###### - [Differences between JSON & JS objects](http://stackoverflow.com/a/2904181/)
Javscript + operator
Can be used to add or concatenate: ``` '3' - 1 = 2 '3' + 1 = '31' '3' + (-1) = '3-1' ``` > ###### If either operand is an empty string, it produces the other operand converted to a string. If both operands are numbers, it produces the sum. Otherwise, it converts both operands to strings and concatenates them. > ###### If you intend + to add, make sure that both operands are numbers. > ###### **Source**: Javascript: The Good Parts - Douglas Crockford
Search in PostgreSQL
PostgreSQL includes a capable & robust full text search solution. It's fast for a small data set (10s of milliseconds for 400k+ rows of catorogies & items). On the plus side, you don't need a separate data store / search service and it's easy to setup (though the syntax is a bit arcane). The [trigram](https://www.postgresql.org/docs/devel/static/pgtrgm.html) module for searching similar words or misspellings is useful. However it has its limitations as you scale up or want a more versatile solution. Some of the weak points are: - faceted search - tokenization not configurable - partial word & phrase search - customizing ranking functions - multilingual support - [more](https://www.postgresql.org/docs/devel/static/textsearch-limitations.html) Tip: GIN index is faster to search than a GiST index, but slower to build or update. So GIN is better suited for static data and GiST for often-updated data.^[[GIN vs GiST](https://www.postgresql.org/docs/9.1/static/textsearch-indexes.html)] Mainstream options : Elasticsearch, SOLR, Lucene, Sphinx References [^one] [^two] [^three] [^one]: [PostgreSQL full text search doc](https://www.postgresql.org/docs/devel/static/textsearch.html) [^two]: [Postgresql : a full text search engine](http://shisaa.jp/postset/postgresql-full-text-search-part-1.html) [^three]: [Postgres full-text search is Good Enough](http://rachbelaid.com/postgres-full-text-search-is-good-enough/)
Default arguments in Python functions
``` def fun(x=[]): x.append(len(x)) return x >>> fun() [0] >>> fun() [0, 1] ``` `x` is initialized when the function is defined. Subsequent calls do **not** re-initialize it. Reference : http://effbot.org/zone/default-values.htm
Frequently changing passwords may be a bad idea
According to [Bruce Schneier](https://www.schneier.com/blog/archives/2016/08/frequent_passwo.html) > Researchers said if people have to change their passwords every 90 days, they tend to use a pattern [...] They take their old passwords, they change it in some small way, and they come up with a new password. > The researchers used the transformations they uncovered to develop algorithms that were able to predict changes with great accuracy

Features

Log your Today I Learned moments - #TIL

Learn
Spot something new & interesting ?
Write it down for future reference
Tag
Organize your #TIL with tags,
Follow relevant tags
Markdown
Simple & beautiful layout,
CommonMark support
Remind
Forget new concepts often ?
Set alerts to revisit them
Private
Not everything is meant to be shared !
You can create private #TILs too
Customize
Want your #TIL to stand out ?
Spice 'em up with custom backgrounds
Backup
Want an offline copy of your data ?
Easily export it from the dashboard
Just a moment, Your account is being updated

Pricing

Because learning should be free
FREE
$0
per month
Create unlimited posts
Organize with Tags
Markdown Support?