An introduction to C++'s SFINAE concept: compile-time introspection of a class member

Trivia:

As a C++ enthusiast, I usually follow the annual C++ conference cppconf or at least try to keep myself up-to-date with the major events that happen there. One way to catch up, if you can't afford a plane ticket or the ticket, is to follow the youtube channel dedicated to this conference. This year, I was impressed by Louis Dionne talk entitled "C++ Metaprogramming: A Paradigm Shift". One feature called is_valid that can be found in Louis's Boost.Hana library particulary caught my attention. This genious is_valid function heavily rely on an even more "magic" C++ programming technique coined with the term SFINAE discovered at the end of the previous century. If this acronym doesn't speak to you, don't be scared, we are going to dive straight in the subject.

Note: for the sake of your sanity and the fact that errare humanum est, this article might not be 100% accurate!

Introspection in C++?

Before explaining what is SFINAE, let's explore one of its main usage: introspection. As you might be aware, C++ doesn't excel when it comes to examine the type or properties of an object at runtime. The best ability provided by default would be RTTI. Not only RTTI isn't always available, but it also gives you barely more than the current type of the manipulated object. Dynamic languages or those having reflection on the other hand are really convenient in some situations like serialization.

For instance, in Python, using reflection, one can do the following:

classA(object):# Simply overrides the 'object.__str__' method.def__str__(self):return"I am a A"classB(object):# A custom method for my custom objects that I want to serialize.defserialize(self):return"I am a B"classC(object):def__init__(self):# Oups! 'serialize' is not a method. self.serialize=0def__str__(self):return"I am a C"defserialize(obj):# Let's check if obj has an attribute called 'serialize'.ifhasattr(obj,"serialize"):# Let's check if this 'serialize' attribute is a method.ifhasattr(obj.serialize,"__call__"):returnobj.serialize()# Else we call the __str__ method.returnstr(obj)a=A()b=B()c=C()print(serialize(a))# output: I am a A.print(serialize(b))# output: I am a B.print(serialize(c))# output: I am a C.

As you can see, during serialization, it comes pretty handy to be able to check if an object has an attribute and to query the type of this attribute. In our case, it permits us to use the serialize method if available and fall back to the more generic method str otherwise. Powerful, isn't it? Well, we can do it in plain C++!

Here is the C++14 solution mentionned in Boost.Hana documentation, using is_valid:

#include<boost/hana.hpp>#include<iostream>#include<string>usingnamespacestd;namespacehana=boost::hana;// Check if a type has a serialize method.autohasSerialize=hana::is_valid([](auto&&x)->decltype(x.serialize()){});// Serialize any kind of objects.template<typenameT>std::stringserialize(Tconst&obj){returnhana::if_(hasSerialize(obj),// Serialize is selected if available![](auto&x){returnx.serialize();},[](auto&x){returnto_string(x);})(obj);}// Type A with only a to_string overload.structA{};std::stringto_string(constA&){return"I am a A!";}// Type B with a serialize method.structB{std::stringserialize()const{return"I am a B!";}};// Type C with a "wrong" serialize member (not a method) and a to_string overload.structC{std::stringserialize;};std::stringto_string(constC&){return"I am a C!";}intmain(){Aa;Bb;Cc;std::cout<<serialize(a)<<std::endl;std::cout<<serialize(b)<<std::endl;std::cout<<serialize(c)<<std::endl;}

As you can see, it only requires a bit more of boilerplate than Python, but not as much as you would expect from a language as complexe as C++. How does it work? Well if you are too lazy to read the rest, here is the simplest answer I can give you: unlike dynamically typed languages, your compiler has access a lot of static type information once fired. It makes sense that we can constraint your compiler to do a bit of work on these types! The next question that comes to your mind is "How to?". Well, right below we are going to explore the various options we have to enslave our favorite compiler for fun and profit! And we will eventually recreate our own is_valid.

The old-fashioned C++98-way:

Whether your compiler is a dinosaur, your boss refuses to pay for the latest Visual Studio license or you simply love archeology, this chapter will interest you. It's also interesting for the people stuck between C++11 and C++14. The solution in C++98 relies on 3 key concepts: overload resolution, SFINAE and the static behavior of sizeof.

Overload resolution:

A simple function call like "f(obj);"" in C++ activates a mechanism to figure out which f function shoud be called according to the argument obj. If a set of f functions could accept obj as an argument, the compiler must choose the most appropriate function, or in other words resolve the best overload! Here is a good cppreference page explaining the full process: Overload resolution. The rule of thumb in this case is the compiler picks the candidate function whose parameters match the arguments most closely is the one that is called. Nothing is better than a good example:

voidf(std::strings);// int can't be convert into a string.voidf(doubled);// int can be implicitly convert into a double, so this version could be selected, but...voidf(inti);// ... this version using the type int directly is even more close!f(1);// Call f(int i);

In C++ you also have some sink-hole functions that accept everything. First, function templates accept any kind of parameter (let's say T). But the true black-hole of your compiler, the devil variable vacuum, the oblivion of the forgotten types are the variadic functions. Yes, exactly like the horrible C printf.

std::stringf(...);// Variadic functions are so "untyped" that...template<typenameT>std::stringf(constT&t);// ...this templated function got the precedence!f(1);// Call the templated function version of f.

The fact that function templates are less generic than variadic functions is the first point you must remember!

Note: A templated function can actually be more precise than a normal function. However, in case of a draw, the normal function will have the precedence.

SFINAE:

I am already teasing you with the power for already few paragraphs and here finally comes the explanation of this not so complex acronym. SFINAE stands for Substitution Failure Is Not An Error. In rough terms, a substitution is the mechanism that tries to replace the template parameters with the provided types or values. In some cases, if the substitution leads to an invalid code, the compiler shouldn't throw a massive amount of errors but simply continue to try the other available overloads. The SFINAE concept simply guaranties such a "sane" behavior for a "sane" compiler. For instance:

/* The compiler will try this overload since it's less generic than the variadic. T will be replace by int which gives us void f(const int& t, int::iterator* b = nullptr); int doesn't have an iterator sub-type, but the compiler doesn't throw a bunch of errors. It simply tries the next overload. */template<typenameT>voidf(constT&t,typenameT::iterator*it=nullptr){}// The sink-hole.voidf(...){}f(1);// Calls void f(...) { }

All the expressions won't lead to a SFINAE. A broad rule would be to say that all the substitutions out of the function/methods body are "safes". For a better list, please take a look at this wiki page. For instance, a wrong substitution within a function body will lead to a horrible C++ template error:

// The compiler will be really unhappy when it will later discover the call to hahahaICrash. template<typenameT>voidf(Tt){t.hahahaICrash();}voidf(...){}// The sink-hole wasn't even considered.f(1);

The operator sizeof:

The sizeof operator is really a nice tool! It permits us to returns the size in bytes of a type or an expression at compilation time. sizeof is really interesting as it accurately evaluates an expression as precisely as if it were compiled.
One can for instance do:

typedefchartype_test[42];type_test&f();// In the following lines f won't even be truly called but we can still access to the size of its return type.// Thanks to the "fake evaluation" of the sizeof operator.chararrayTest[sizeof(f())];std::cout<<sizeof(f())<<std::endl;// Output 42.

But wait! If we can manipulate some compile-time integers, couldn't we do some compile-time comparison? The answer is: absolutely yes, my dear reader! Here we are:

Combining everything:

Now we have all the tools to create a solution to check the existence of a method within a type at compile time. You might even have already figured it out most of it by yourself. So let's create it:

template<classT>structhasSerialize{// For the compile time comparison.typedefcharyes[1];typedefyesno[2];// This helper struct permits us to check that serialize is truly a method.// The second argument must be of the type of the first.// For instance reallyHas<int, 10> would be substituted by reallyHas<int, int 10> and works!// reallyHas<int, &C::serialize> would be substituted by reallyHas<int, int &C::serialize> and fail!// Note: It only works with integral constants and pointers (so function pointers work).// In our case we check that &C::serialize has the same signature as the first argument!// reallyHas<std::string (C::*)(), &C::serialize> should be substituted by // reallyHas<std::string (C::*)(), std::string (C::*)() &C::serialize> and work!template<typenameU,Uu>structreallyHas;// Two overloads for yes: one for the signature of a normal method, one is for the signature of a const method.// We accept a pointer to our helper struct, in order to avoid to instantiate a real instance of this type.// std::string (C::*)() is function pointer declaration.template<typenameC>staticyes&test(reallyHas<std::string(C::*)(),&C::serialize>*/*unused*/){}template<typenameC>staticyes&test(reallyHas<std::string(C::*)()const,&C::serialize>*/*unused*/){}// The famous C++ sink-hole.// Note that sink-hole must be templated too as we are testing test<T>(0).// If the method serialize isn't available, we will end up in this method.template<typename>staticno&test(...){/* dark matter */}// The constant used as a return value for the test.// The test is actually done here, thanks to the sizeof compile-time evaluation.staticconstboolvalue=sizeof(test<T>(0))==sizeof(yes);};// Using the struct A, B, C defined in the previous hasSerialize example.std::cout<<hasSerialize<A>::value<<std::endl;std::cout<<hasSerialize<B>::value<<std::endl;std::cout<<hasSerialize<C>::value<<std::endl;

The reallyHas struct is kinda tricky but necessary to ensure that serialize is a method and not a simple member of the type. You can do a lot of test on a type using variants of this solution (test a member, a sub-type...) and I suggest you to google a bit more about SFINAE tricks. Note: if you truly want a pure compile-time constant and avoid some errors on old compilers, you can replace the last value evaluation by: "enum { value = sizeof(test(0)) == sizeof(yes) };".

You might also wonder why it doesn't work with inheritence. Inheritence in C++ and dynamic polymorphism is a concept available at runtime, or in other words, a data that the compiler won't have and can't guess! However, compile time type inspection is much more efficient (0 impact at runtime) and almost as powerful as if it were at runtime.
For instance:

// Using the previous A struct and hasSerialize helper.structD:A{std::stringserialize()const{return"I am a D!";}};template<classT>booltestHasSerialize(constT&/*t*/){returnhasSerialize<T>::value;}Dd;A&a=d;// Here we lost the type of d at compile time.std::cout<<testHasSerialize(d)<<std::endl;// Output 1.std::cout<<testHasSerialize(a)<<std::endl;// Output 0.

Last but no least, our test cover the main cases but not the tricky ones like a Functor:

The trade-off for a full coverage would be the readability. As you will see, C++11 shines in that domain!

Time to use our genius idea:

Now you would think that it will be super easy to use our hasSerialize to create a serialize function! Okay let's try it:

template<classT>std::stringserialize(constT&obj){if(hasSerialize<T>::value){returnobj.serialize();// error: no member named 'serialize' in 'A'.}else{returnto_string(obj);}}Aa;serialize(a);

It might be hard to accept, but the error raised by your compiler is absolutely normal! If you consider the code that you will obtain after substitution and compile-time evaluation:

std::stringserialize(constA&obj){if(0){// Dead branching, but the compiler will still consider it!returnobj.serialize();// error: no member named 'serialize' in 'A'.}else{returnto_string(obj);}}

Your compiler is really a good guy and won't drop any dead-branch, and obj must therefore have both a serialize method and a to_string overload in this case. The solution consists in spliting the serialize function into two different functions: one where we solely use obj.serialize() and one where we use to_string according to obj's type. We come back to an earlier problem that we already solved, how to split according to a type? SFINAE, for sure! At that point we could re-work our hasSerialize function into a serialize function and make it return a std::string instead of compile time boolean. But we won't do it that way! It's cleaner to separate the hasSerialize test from its usage serialize.

We need to find a clever SFINAE solution on the signature of "template <class T> std::string serialize(const T& obj)". I bring you the last piece of the puzzle called enable_if.

template<boolB,classT=void>// Default template version.structenable_if{};// This struct doesn't define "type" and the substitution will fail if you try to access it.template<classT>// A specialisation used if the expression is true. structenable_if<true,T>{typedefTtype;};// This struct do have a "type" and won't fail on access.// Usage:enable_if<true,int>::typet1;// Compiler happy. t's type is int.enable_if<hasSerialize<B>::value,int>::typet2;// Compiler happy. t's type is int.enable_if<false,int>::typet3;// Compiler unhappy. no type named 'type' in 'enable_if<false, int>';enable_if<hasSerialize<A>::value,int>::typet4;// no type named 'type' in 'enable_if<false, int>';

As you can see, we can trigger a substitution failure according to a compile time expression with enable_if. Now we can use this failure on the "template <class T> std::string serialize(const T& obj)" signature to dispatch to the right version. Finally, we have the true solution of our problem:

template<classT>typenameenable_if<hasSerialize<T>::value,std::string>::typeserialize(constT&obj){returnobj.serialize();}template<classT>typenameenable_if<!hasSerialize<T>::value,std::string>::typeserialize(constT&obj){returnto_string(obj);}Aa;Bb;Cc;// The following lines work like a charm!std::cout<<serialize(a)<<std::endl;std::cout<<serialize(b)<<std::endl;std::cout<<serialize(c)<<std::endl;

Two details worth being noted! Firstly we use enable_if on the return type, in order to keep the paramater deduction, otherwise we would have to specify the type explicitely "serialize<A>(a)". Second, even the version using to_string must use the enable_if, otherwise serialize(b) would have two potential overloads available and raise an ambiguity. If you want to check the full code of this C++98 version, here is a gist.
Life is much easier in C++11, so let's see the beauty of this new standard!

Note: it's also important to know that this code creates a SFINAE on an expression ("&C::serialize"). Whilst this feature wasn't required by the C++98 standard, it was already in use depending on your compiler. It trully became a safe choice in C++11.

When C++11 came to our help:

After the great century leap year in 2000, people were fairly optimistic about the coming years. Some even decided to design a new standard for the next generation of C++ coders like me! Not only this standard would ease TMP headaches (Template Meta Programming side-effects), but it would be available in the first decade, hence its code-name C++0x. Well, the standard sadly came the next decade (2011 ==> C++11), but it brought a lot of features interesting for the purpose of this article. Let's review them!

decltype, declval, auto & co:

Do you remember that the sizeof operator does a "fake evaluation" of the expression that you pass to it, and return gives you the size of the type of the expression? Well C++11 adds a new operator called decltype. decltype gives you the type of the of the expression it will evaluate. As I am kind, I won't let you google an example and give it to you directly:

declval is an utility that gives you a "fake reference" to an object of a type that couldn't be easily construct. declval is really handy for our SFINAE constructions. cppreference example is really straightforward, so here is a copy:

The auto specifierspecifies that the type of the variable that is being declared will be automatically deduced. auto is equivalent of var in C#. auto in C++11 has also a less famous but nonetheless usage for function declaration. Here is a good example:

boolf();autotest=f();// Famous usage, auto deduced that test is a boolean, hurray!// vvv t wasn't declare at that point, it will be after as a parameter!template<typenameT>decltype(t.serialize())g(constT&t){}// Compilation error// Less famous usage:// vvv auto delayed the return type specification!// vvv vvv the return type is specified here and use t!template<typenameT>autog(constT&t)->decltype(t.serialize()){}// No compilation error.

As you can see, auto permits to use the trailing return type syntax and use decltype coupled with an expression involving one of the function argument. Does it means that we can use it to test the existence of serialize with a SFINAE? Yes Dr. Watson! decltype will shine really soon, you will have to wait for the C++14 for this tricky auto usage (but since it's a C++11 feature, it ends up here).

constexpr:

C++11 also came with a new way to do compile-time computations! The new keyword constexpr is a hint for your compiler, meaning that this expression is constant and could be evaluate directly at compile time. In C++11, constexpr has a lot of rules and only a small subset of VIEs (Very Important Expression) expressions can be used (no loops...)! We still have enough for creating a compile-time factorial function:

constexprintfactorial(intn){returnn<=1?1:(n*factorial(n-1));}inti=factorial(5);// Call to a constexpr function.// Will be replace by a good compiler by:// int i = 120;

constexpr increased the usage of std::true_type & std::false_type from the STL. As their name suggest, these types encapsulate a constexpr boolean "true" and a constrexpr boolean "false". Their most important property is that a class or a struct can inherit from them. For instance:

Blending time:

First solution:

In cooking, a good recipe requires to mix all the best ingredients in the right proportions. If you don't want to have a spaghetti code dating from 1998 for dinner, let's revisit our C++98 hasSerialize and serialize functions with "fresh" ingredients from 2011. Let's start by removing the rotting reallyHas trick with a tasty decltype and bake a bit of constexpr instead of sizeof. After 15min in the oven (or fighting with a new headache), you will obtain:

template<classT>structhasSerialize{// We test if the type has serialize using decltype and declval.template<typenameC>staticconstexprdecltype(std::declval<C>().serialize(),bool())test(int/* unused */){// We can return values, thanks to constexpr instead of playing with sizeof.returntrue;}template<typenameC>staticconstexprbooltest(...){returnfalse;}// int is used to give the precedence!staticconstexprboolvalue=test<T>(int());};

You might be a bit puzzled by my usage of decltype. The C++ comma operator "," can create a chain of multiple expressions. In decltype, all the expressions will be evaluated, but only the last expression will be considered for the type. The serialize doesn't need any changes, minus the fact that the enable_if function is now provided in the STL. For your tests, here is a gist.

Second solution:

Another C++11 solution described in Boost.Hanna documentation and using std::true_type and std::false_type, would be this one:

This solution is, in my own opinion, more sneaky! It relies on a not-so-famous-property of default template parameters. But if your soul is already (stack-)corrupted, you may be aware that the default parameters are propagated in the specialisations. So when we use hasSerialize<OurType>::value, the default parameter comes into play and we are actually looking for hasSerialize<OurType, std::string>::value both on the primary template and the specialisation. In the meantime, the substitution and the evaluation of decltype are processed and our specialisation has the signature hasSerialize<OurType, std::string> if OurType has a serialize method that returns a std::string, otherwise the substitution fails. The specialisation has therefore the precedence in the good cases. One will be able to use the std::void_t C++17 helper in these cases. Anyway, here is a gist you can play with!

I told you that this second solution hides a lot of complexity, and we still have a lot of C++11 features unexploited like nullptr, lambda, r-values. No worries, we are going to use some of them in C++14!

The supremacy of C++14:

According to the Gregorian calendar in the upper-right corner of my XFCE environment, we are in 2015! I can turn on the C++14 compilation flag on my favorite compiler safely, isn't it? Well, I can with clang (is MSVC using a maya calendar?).
Once again, let's explore the new features, and use them to build something wonderful! We will even recreate an is_valid, like I promised at the beggining of this article.

auto & lambdas:

Return type inference:

Some cool features in C++14 come from the relaxed usage of the auto keyword (the one used for type inference).

Now, auto can be used on the return type of a function or a method. For instance:

A feature for functional lovers:

autol1=[](B&b){returnb.serialize();};// Return type figured-out by the return statement.autol3=[](B&b)->std::string{returnb.serialize();};// Fixed return type.autol2=[](B&b)->decltype(b.serialize()){returnb.serialize();};// Return type dependant to the B type.std::cout<<l1(b)<<std::endl;// Output: I am a B!std::cout<<l2(b)<<std::endl;// Output: I am a B!std::cout<<l3(b)<<std::endl;// Output: I am a B!

C++14 brings a small change to the lambdas but with a big impact! Lambdas accept auto parameters: the parameter type is deduced according the argument. Lambdas are implemented as an object having an newly created unnamed type, also called closure type. If a lambda has some auto parameters, its "Functor operator" operator() will be simply templated. Let's take a look:

More than the lambda itself, we are interested by the generated unnamed type: its lambda operator() can be used as a SFINAE! And as you can see, writing a lambda is less cumbersome than writing the equivalent type. It should remind you the beggining of my initial solution:

// Check if a type has a serialize method.autohasSerialize=hana::is_valid([](auto&&x)->decltype(x.serialize()){});

And the good new is that we have everything to recreate is_valid, right now!

The making-of a valid is_valid:

Now that we have a really stylish manner to generate a unnamed types with potential SFINAE properties using lambdas, we need to figure out how to use them! As you can see, hana::is_valid is a function that takes our lambda as a parameter and return a type. We will call the type returned by is_valid the container. The container will be in charge to keep the lambda's unnamed type for a later usage. Let's start by writing the is_valid function and its the containter:

template<typenameUnnamedType>structcontainer{// Remembers UnnamedType.};template<typenameUnnamedType>constexprautois_valid(constUnnamedType&t){// We used auto for the return type: it will be deduced here.returncontainer<UnnamedType>();}autotest=is_valid([](constauto&t)->decltype(t.serialize()){})// Now 'test' remembers the type of the lambda and the signature of its operator()!

The next step consists at extending container with the operator operator() such as we can call it with an argument. This argument type will be tested against the UnnamedType! In order to do a test on the argument type, we can use once again a SFINAE on a reacreated 'UnnamedType' object! It gives us this solution:

template<typenameUnnamedType>structcontainer{// Let's put the test in private.private:// We use std::declval to 'recreate' an object of 'UnnamedType'.// We use std::declval to also 'recreate' an object of type 'Param'.// We can use both of these recreated objects to test the validity!template<typenameParam>constexprautotestValidity(int/* unused */)->decltype(std::declval<UnnamedType>()(std::declval<Param>()),std::true_type()){// If substitution didn't fail, we can return a true_type.returnstd::true_type();}template<typenameParam>constexprstd::false_typetestValidity(...){// Our sink-hole returns a false_type.returnstd::false_type();}public:// A public operator() that accept the argument we wish to test onto the UnnamedType.// Notice that the return type is automatic!template<typenameParam>constexprautooperator()(constParam&p){// The argument is forwarded to one of the two overloads.// The SFINAE on the 'true_type' will come into play to dispatch.// Once again, we use the int for the precedence.returntestValidity<Param>(int());}};template<typenameUnnamedType>constexprautois_valid(constUnnamedType&t){// We used auto for the return type: it will be deduced here.returncontainer<UnnamedType>();}// Check if a type has a serialize method.autohasSerialize=is_valid([](auto&&x)->decltype(x.serialize()){});

If you are a bit lost at that point, I suggest you take your time and re-read all the previous example. You have all the weapons you need, now fight C++!

Our hasSerialize now takes an argument, we therefore need some changes for our serialize function. We can simply post-pone the return type using auto and use the argument in a decltype as we learn. Which gives us:

// Notice how I simply swapped the return type on the right?template<classT>autoserialize(T&obj)->typenamestd::enable_if<decltype(hasSerialize(obj))::value,std::string>::type{returnobj.serialize();}template<classT>autoserialize(T&obj)->typenamestd::enable_if<!decltype(hasSerialize(obj))::value,std::string>::type{returnto_string(obj);}

FINALLY!!! We do have a working is_valid and we could use it for serialization! If I were as vicious as my SFINAE tricks, I would let you copy each code pieces to recreate a fully working solution. But today, Halloween's spirit is with me and here is gist. Hey, hey! Don't close this article so fast! If you are true a warrior, you can read the last part!

For the fun:

There are few things I didn't tell you, on purpose. This article would otherwise be twice longer, I fear. I highly suggest you to google a bit more about what I am going to speak about.

Firstly, if you wish to have a solution that works with the Boost.Hana static if_, you need to change the return type of our testValidity methods by Hana's equivalents, like the following:

template<typenameUnnamedType>structcontainer{// Let's put the test in private.private:// We use std::declval to 'recreate' an object of 'UnnamedType'.// We use std::declval to also 'recreate' an object of type 'Param'.// We can use both of these recreated objects to test the validity!template<typename...Params>constexprautotest_validity(int/* unused */)->decltype(std::declval<UnnamedType>()(std::declval<Params>()...),std::true_type()){// If substitution didn't fail, we can return a true_type.returnstd::true_type();}template<typename...Params>constexprstd::false_typetest_validity(...){// Our sink-hole returns a false_type.returnstd::false_type();}public:// A public operator() that accept the argument we wish to test onto the UnnamedType.// Notice that the return type is automatic!template<typename...Params>constexprautooperator()(Params&&...){// The argument is forwarded to one of the two overloads.// The SFINAE on the 'true_type' will come into play to dispatch.returntest_validity<Params...>(int());}};template<typenameUnnamedType>constexprautois_valid(UnnamedType&&t){// We used auto for the return type: it will be deduced here.returncontainer<UnnamedType>();}

This code is working even if my types are incomplete, for instance a forward declaration, or a normal declaration but with a missing definition. What can I do? Well, you can insert a check on the size of your type either in the SFINAE construction or before calling it: "static_assert( sizeof( T ), "type is incomplete." );".

Finally, why are using the notation "&&" for the lambdas parameters? Well, these are called forwarding references. It's a really complex topic, and if you are interested, here is good article about it. You need to use "auto&&" due to the way declval is working in our is_valid implementation!

Notes:

This is my first serious article about C++ on the web and I hope you enjoyed it! I would be glad if you have any suggestions or questions and that you wish to share with me in the commentaries.

Anyway, thanks to Naav and Superboum for rereading this article and theirs suggestions. Few suggestions were also provided by the reddit community or in the commentaries of this post, thanks a lot guys!