Extremely Serious

Category: Programming (Page 3 of 3)

Parallel Extensions

This extension calculates the most efficient way of dividing the task to the different cores available.

Note: Parallel extensions will block the calling thread.

Method Description
Parallel.For Executes for that may run in parallel.
Parallel.ForEach Executes foreach that may run in parallel.
Parallel.Invoke Executes actions that may run in parallel.

Asynchronous Programming Keywords

Async modifier

A modifier that indicates that the method can be run asynchornously.

Await keyword

Wait for a result of the asynchronous operation once the data is available without blocking the current thread. It also validates the success of the asynchronous operation. If everything is good, the execution will continue after the await keywords on its original thread.

Task.Run method

Run a task on a separate thread

ContinueWith Method

A task method that will be invoked after the running async task has returned a result.
The parameter TaskContinuationOption of this method can be use to control if the continue must be invoked if there are no exception or only with exception.

CancellationTokenSource

CancellationTokenSource is used to signal that you want to cancel an operation that utilize the token. This token be passed as a second parameter for the Task.Run method.

Dispatcher.Invoke() in WPF

Communicate with the thread that owns our UI.

ConcurrentBag Class

A thread safe collection.

Cyclomatic Complexity

Cyclomatic complexity is the count of linearly independent paths in a program's source code. With the help of control flow graph, we can use the following formula to calculate this:

CC = E - N + 2P

Variable Description
E The number of edges in the control flow graph.
N The number of nodes in the control flow graph.
P The number of connected components. This has a value of 1 if you are computing at the method (i.e. function or subroutine) level.

Related
Control Flow Graph

Reasons to Favor Composition Over Inheritance

Composition

Using some functionalities of a class but doesn't inherit (i.e. is-a relationship) it, is known as composition (i.e. has-a relationship).

Reasons to Favor Composition Over Inheritance

  • Composition offers a better testability. Imagine a class composed of another class or interface. You can easily create a mock of the composed class for the sake of testing.
  • Composition is more flexible than inheritance. It is easier to change the implemention of the composed class with better and improved version. Just create a new independent implementation (e.g. annonymously) of the composed class and change it at runtime.
  • Both composition and inheritace supports code reuse. However, inheritance breaks encapsulation. Example, if a subclass is depending on its super class behavior for its operation, it becomes fragile. Try to imagine that you change the implementation on the super class. Did you consider the effect of the change on the subclasses that overrides it but partially depends on it. In composition there's no dependencies like this. Thus, you have more robust code.
Newer posts »