Programmers try to avoid switch statements because switches are breaking the rule of the Open-Closed-Principle. Also, continuously extending the switch statement with more clauses leads to a high maintenance effort, as you need to modify the written code every time you extend it. Maybe your working unit-tests will fail after the change. You might need to fix your business logic and your tests every time — what a nightmare…
“The Problem with such duplicate switches is that, whenever you add a clause, you have to find all the switches and update them.” (Martin Fowler, [Refactoring])
We use them because switch…
Based on my story Inheritance & Object Composition I want to show you, how you can use some Angular features for reusing code in really nice ways. I prepared some easy examples below.
Inheritance is a very simple principle in Angular. You can combine a base class with a superclass by using the extends term. The superclass inherits fields and methods from the base class.
Subscribing and unsubscribing to an observable in a Redux store is a common repeatable practise. You can DRY, when you separate this logic in a base class.
>>> base.component.tsexport class BaseComponent implements OnInit, OnDestroy…
Protecting your Angular code with tests has many advantages:
Reusing code and reducing the maintenance effort of code are more and more popular principles of modern programming because the code base of software applications increases very quickly within a short time. Feature pressure is one of many plagues of a software engineer these days.
Besides Object Composition, Inheritance is one of many principles for reusing code. The design pattern Template Method uses this to define the structure of an algorithm in a superclass but allows its subclasses to override specific steps of the algorithm without changing the structure.
In Angular the method for reading URL parameters by using the…
Early in my career, the most commonly occurring condition in TypeScript and Angular was the null
checking condition. I think other developers have the same habit of making their code more stable. Such a condition isn’t bad, but when you check the same special value every time in different places in your code, you may have redundant code and you may not be complying with the DRY principle.
A null
checking condition is nothing more than a check for data availability:
It is helpful to implement a Special Case object to handle such conditions when most or all of…
You can use the reactivity of Angular in class- or ngClass-attributes of HTML elements. With this combination you can change your stylings dynamically at runtime — which would be helpful for theme components.
First of all, you need a variable of the current theming in a controller. This variable is a good starting point for using reactivity.
>>> app.component.ts
public theme: string;
constructor() {
this.theme = 'light';
}
Secondly, you need to define the theming in the stylesheet with some rules. Here you can use CSS, SCSS, SASS or other preprocessors. …
These days for your applications, a decoupled and encapsulated software system is ever more relevant. Reusability, maintenance, care, and sustainability play a major role in our software development projects — they are getting bigger and bigger. People, who try to master these challenges, read Robert C. Martins Clean Code, Clean Coder, or Clean Architecture. Actually, some of them follow his SOLID principles:
So, why should we care about the so called “Inheritance & Object Composition”? Because they are a big part of one of the aforementioned SOLID principles: the Open-Closed-Principle.
„Software entities…
Fullstack Software Engineer | Trained Mathematical-Technical Software Developer | Employee at Energy2market