CSS Selector Notes and Guidance

Selector

Always keep the CSS selector short and efficient.

Selectors positioned by page element location are not ideal. A selector such as .sidebar h3 span{} is positioning too dependent on relative positions, making it difficult to maintain its style when moving the span outside h3 and sidebar.

Complexly structured selectors can affect performance. The more complex the selector structure (e.g. .sidebar h3 span is three layers,.content ul p a is four layers), the greater the browser’s overhead.

Try to keep the style from relying on its positioning, and try to keep the selector simple and clear.

As a whole, the selector should be as short as possible (for example, only one layer of structure), but class names should not be too brief, for example, .user-avatar is much better than .usr-avt

Keep in mind that class doesn’t matter whether it’s semantic; Don’t emphasize that class names are semantically appropriate, but instead focus on using names that are reasonable and not out of date.

  best blockchain books 2020 

 

Over-decorated selectors

As mentioned earlier, over-decorated selectors are not ideal.

Over-decorated selectors refer to things like div.promo It’s likely that you’ll get the same effect with .promo Of course, you may occasionally need to decorate class with element types (for example, you wrote a .error and wanted it to look different in different element types, such as .error { color: red; } div.error { padding: 14px; } . . 14px; ), but most of the time, you should try to avoid it.

Another example of an over-decorated selector,ul.nav li a{} As mentioned earlier, we can delete ul right away because we know .nav is a list, and then we can find that a must be in li so we can rewrite this selector to .nav a{}

> Django Web Hosting 2020

> Free code learning website 2020

Selector performance

Despite the increasing browser performance and the faster the rendering of CSS, you should be concerned about efficiency. Avoid  the increasingly complex CSS3 new selectors with a short, nested selector and without using a global selector as a core selector.* {}

Core Selector: The browser parses the selector in right-to-left order, and the right-most element is the element in which the style takes effect, and is the core selector. 

Why Flask is better than Django !!

Flask lets you build web apps with Python. It lets you build both simple and complex web apps (what is Flask).

I like it over Django because you are in full control of the architecture choices. But of course, you should make your own choices. Perhaps you prefer Tornado

Django makes a lot of choices for you, like that you have to use an ORM (Object-relational mapping). In general I prefer having raw SQL queries than using an ORM.

Flask was made several years after Django and therefore learned from the Python community’s reactions.

Why Flask

There are many reasons to choose Flask. For one it’s a microframework, so you can learn it quite fast. Some other reasons to choice Flask are:

  • Flask is actively maintained and developed
  • It is extensible while maintaining a simple core
  • The documentation is great
  • It’s easy to get started with Flask as a beginner. There is a tiny boilerplate code for getting a simple app up and running.
  • It saves you development time (Easy to deploy Flask apps)

flask meme

What is TypeScript?

TypeScript is a language on top of JavaScript. Everything possible in JavaScript is available in TypeScript – it is a superset of JavaScript. It provides two strong advantages. The first one is that it transpiles TypeScript into JavaScript so the advanced ECMAScript features not available to all web browsers can be used by providing a polyfill. It acts as a combination of other static checkers combined with Babel. The second advantage of TypeScipt is that it can enforce static typing to catch potential issues earlier in the lifecycle of development. It reduces the need for some unit tests and can analyze the code to find runtime errors at design time.

 

TypeScript was made public since 1 October 2012, but its inception started at Redmond two years prior as an internal product (2010) at Microsoft. The project is open-source, hosted on Github.com under an Apache2 license.

TypeScript’s popularity increases year after year. It has 317 branches, 228 contributors, 48 releases, 188 pull requests monthly created with over 3 600 forks and 25 000 stars. The consistent cadence of a new version releases every two months and the great ecosystem of people contributing make it a stellar open source project. It evolves rapidly to sustain developers’ needs, and it follows the rapid pace of web technologies.

TypeScript is far from being used only by its creator, Microsoft. While it’s true that many Microsoft products use TypeScript, such as Microsoft Teams, Visual Studio Team Services (VSTS), the online version of Office, and Visual Studio Code (VSCode), to say the least, other technology giants have been borrowing the advantages of static typing. Google has been using TypeScript since Angular 2, Slack has migrated their JavasScript code base to TypeScript as well, and other companies like Ubisoft, Asana, Ericsson, Upwork, Bloomberg, and Lyft are following suit. The reason I mention these systems that use TypeScript is to highlight the investment of thousands of developers and the millions of lines in these technologies. Most of them come from different backgrounds. Aside from the tremendous amount of work and knowledge poured across the industry, it illustrates that many people before you had to decide whether to go with TypeScript or not—and chose to do so. While most of the examples provided are big projects, they all do have one thing in common, i.e. some people must write and maintain the code. Static typing shines in these areas, from small to big projects.

Here is a quick note about who is leading TypeScript. Anders Hejlsberg manages TypeScript. He is the creator of Turbo Pascal, Delphi, C# and now TypeScript. Hejlsberg graduated in the ‘80s, has received many awards in his career, and is the person who made the most commits on GitHub on that project. Having such a prolific engineer at the head of TypeScript boosts the maturity of the language.

The TypeScript community is beyond the repository where it resides. StackOverflow’s members are strong, with more than 35 000 questions. There are plenty of examples with TypeScript code with many different frameworks. It’s not difficult to find Angular or React examples written in TypeScript or popular projects like the famous TodoMVC.

TypeScript is all about having a strongly typed language at design time, but the code produced doesn’t contain any type. The type gets erased because Typescript removes types, interfaces, aliases at transpilation and ends up with a common JavaScript file. The removal may sound logical but the lack of type at runtime results in a design that must not rely on the type dynamically at runtime. We will see examples of this when working on type comparisons to figure out the interface used. Keep in mind that interface and type are not available at runtime, hence how can this check be performed when the code executed is a challenge?

Mutable and Immutable Arrays

In this lesson you will learn about two different syntaxes to define an array.

Arrays in TypeScript are exactly like in JavaScript in terms of features. The difference is that TypeScript assigns a type to the list.

The syntax, as shown below, utilizes square brackets [] with the actual type before the brackets and after the colon.

It is also possible to initialize values during the declaration. In the following example, the two arrays are typed. TypeScript infers that the first one is number and the second is a string. You can move your cursor over the two variables to see the type.

Using multiple types will require you to evaluate what the type of each value is before using an individual item of the array. This is because the variable’s operations are type-dependent. There is an equivalent syntax that uses the generic Array<T>. Both are the same.

You can initialize an empty list by setting the variable equal to empty square brackets. An array can use one or many unions to allow multiple types. Union will be discussed later. At that stage, remember that with multiple types, you must to wrap the union with parentheses.

The preference to explicitly declare type or not depends on your style of coding. For maintainability, it is good practice to be explicit. With a peek at the code above may notice, you know that the array takes 2 types without having to read further. It also forces future additions to be constrained by the expected type. Without explicitly typing the variable, someone could add a string and suddenly the variable allows a number, boolean, and string.

Before moving on, it is important to note that you can also instantiate a strongly typed object array. This is equivalent to creating a new array without assigning any values.

Immutable arrays

While the two syntaxes presented above refer to mutable collections, there is also the possibility to desire a list that is immutable. The ReadonlyArray is a generic array that allows only to read from the array once it’s constructed. As with the mutable array, there are two ways to write a read-only collection.

The first approach is to use ReadonlyArray<T> instead of Array<T>.

Control flow analysis for array construction

Control Flow Analysis is how TypeScript figure out dynamically what type it should infer. Arrays can be tricky since they accept many values which can be of many types. For example, the following code accepts a list of numbers or strings.

The last question may surprise you. The empty array is an evolving type that will be analysed during the “flow” of the code, meaning depending of what happen with the following operations. Functions like pushshiftunshift or setting directly to an index a value myArray[index] = value will transform the type. The type is finally attributed once it stops changing, hence the question #4 gets to its real type at the end of the code, not before which could be anything.

In this lesson, we saw two different syntaxes to write an array in TypeScript. We also illustrated that it is possible to have the content of an array to be immutable with readonly. We also described the difference between a constant and a read-only list.

Read more: