Sunday 5 April 2020

Protocol oriented programming




Protocol oriented programming(POP) is a paradigm that has come into the limelight with the advent of Swift. Different languages over different periods have had some flavor of POP in them, but there are some traits unique in the POP central to Swift, which gives them a measurable advantage over OOP. That is not to say that OOP is flawed and or POP is the knight in the shining armor. POP simply extends OOP with a few new additions that help in writing better code ergo, better systems
Contents
a. Existing system example
b. Problem with Inheritance
c. Enter protocols
d. Protocol Extensions
e. Protocols & Value types

1. Existing system example

Let’s take a simple example to see how OOP and POP work on the same problem. Consider that we are contracted to build a vehicle that can be driven. The requirement is we will be asked to build many vehicles that will have different colors, number of wheels, different engine capacity, etc. Coming from the OOP world, the core of the solution would be something like this


Everything works fine. SmallCarRaceCar both can be driven. They also can have their unique traits.(noOfSeats()nitroBoosterCapacity() etc). No paradigm maps the real world as efficiently as OOP, and hence we can apply principles of real-world like inheritance in object modeling. That’s the beauty of it.
However, the real world is far from perfect, and that seeps into its derivative like OOP. Consider the same example above. The customer has now contracted you to build the ability to have wipers for the existing category of vehicles. So now Vehicle can be modified to have the ability to wipe


Courtesy, inheritance, all the categories of vehicles created hitherto can wipe its screen. RaceCar or SmallCar can simply call wipe() and startDriving() safely in the rains! The customer is now happy and since you are so awesome in building systems he now asks for introducing a new class of vehicle, A two-wheeler known as a motorcycle. The first reaction would be to subclass Motorcycle from Vehicle. In this case, Vehicle would not be useful. Why? Semantically a motorcycle is a vehicle so we should be able to use the Vehicle class. But the problem is if a Motorcycle inherits from Vehicle it would also inherit the ability to wipe(). Needless and absurd as well! So if we don’t extend Motorcycle from Vehicle, all the complex logic and business rules which are available through Vehicle namely in startDriving() will not be available to a Motorcycle. We could simply argue that ignore the ability to wipe() in a Motorcycle, but that’s knowingly introducing an association in the system which is not required. So what’s wrong with our design?

OOPs, OOP has done it again!

A subclass may not need all the features from its parent class. A child who has a rich, alcoholic father will happily choose to inherit his wealth but would keep his distance from his father's drinking vice(hopefully!). Similarly, a subclass may not require all the traits of its superclass. Since most of the languages do not support multiple inheritances we cannot break the superclass features into multiple smaller classes and have the subclasses inherit from the required ones.


OOP does not do a great job when it comes to this.

2. Problem with Inheritance

Inheritance is a great mechanism to reuse code and build software, but it is not particularly great when it comes to a selective association. The class that gets inherited is imbibed in the DNA of the subclass whether it likes it or not. It’s like a mobile data plan which comes with a host of great features that you are interested in but also with a few meh! features which you would have rather not wanted if sold individually. But since it’s part of the package you can’t escape it.

3. Enter protocols

Protocols have been in existence since OOP itself and it offers another way of designing and modeling classes. Let’s consider the above example of vehicles and how we could design the same using protocols


As we can see, with protocols we have been able to break the dependency enforced by Inheritance on the class Motorcycle when it inherited Vehicle. Now any new category of Vehicle which does not require wipers won’t be burdened with the same. It simply does not conform to it. Don’t need it, don’t ask for it and so not burdened with it! For e.g. a scooter. The protocol used in Swift generally is implemented as an abstraction (virtual class, interface so on and so forth) in many other languages. Nothing really new there. Readers would have noticed here that the need to define the logic of driving is now on the concrete class which implements Vehicle i.e both RaceCar and SmallCar now has to define how to startDriving(). This can lead to the repetition of code and logic. Besides if protocols have been available in many other languages what’s so special about POP in Swift? The answer is Protocol extensions.

4. Protocol Extensions

The cornerstone for POP is protocol extension. Swift (v2.0 onwards) allows a protocol to have a generic behavior that can be overridden as well. This generic behavior allows every implementation to “inherit” this by default. If they don’t like the generic behavior, well, simple, change it i.e., override it.
For e.g. the above example can now be written as

This is exactly like inheritance with the extra association of startWiper() now broken down to a need basis. Every implementation of Vehicle now has the complex startDriving() logic available by default i.e it has “inherited” the same from the protocol. Plus motorcycle does not have any association with Wipers. Thus protocol extensions help implementation to have the ability to “inherit” from a protocol and keep the dependency to an atomic level. i.e A protocol should contain only those contracts which an implementation or concrete object has to implement. Otherwise, it exhibits a fat interface problem. For e.g., Vehicle was a fat interface because it contained startWiper() which was not required for all subclasses.
5. Protocols & Value types
Swift advocates using value types over reference types wherever applicable. The use cases, benefits of both are well documented and won’t be covered here in this post. Value types like structenum can extend Protocols as well thus extending the benefits of designing using Protocols to even value types. This is one of the main reasons why Apple evangelizes using protocols over classes.

Bottom Line

POP extends OOP to provide another level of abstraction which helps a developer to write better code and design reusable components. Everything has its place under the sun and OOP is certainly not to be totally replaced with POP. Only where required. The decision to use POP or OOP can be context-specific

Thursday 2 April 2020

6 point design guide for Generation Z




The current generation or demography is known by many names iGeneration, Gen Wii, etc. In mainstream media, they are widely known as Generation Z, the one following Gen Y(surprise!) or millennials. Like the demography which preceded Gen Z, i.e. Gen Y, Gen X, etc., Gen Z has its unique traits, preferences, likes and dislikes, and behavioral patterns.
The millennials(Gen Y) were the first to consume digital products the way we see them today. With the advent of the Internet, the millennials adapted to services, tastes, sharing, brands favored, buying preferences, etc over the Internet in a digital platform. Whether it be booking movie tickets, ordering lunch, filing taxes, etc. digitalization is how the millennials made their lives easier.

The behavior of Gen Z

For Gen Z, the digital world is what is.
But For Gen Z, the Internet & the digital world was not about making lives easier. The digital world and its products were not luxury. They were born into this digital world where the Internet was a necessity. Experimenting and expressing with social channels & the digital world is how Gen Z communicates. As opposed to the traditional model of sales like brick and mortar setup, mom and pop stores, this is a generation that prefers buying online and learning about brands online.

How does Gen Z deal with brands

All most all brands and businesses eventually had to migrate to this digital platform to stay relevant since the purchasing power and ability of this demography is staggering. The traditional methods for branding, advertising, and sales have entirely turned on its head in the digital world. The pace of change in the digital world is fast, and brands have to adapt to keep themselves competitive. Physical channels of sales are superseded by digital channels. Methods of advertisement like flyers, print, television are rejected over likes & comments & reviews & feedback by existing users over social media. A “like” which a friend gives to a product is worth more than a plastic smile driven endorsement by a Taylor Swift or a Justin Bieber.
According to consulting firm Altitude, this new generation will account for 40% of consumers by 2020.

How to design for Gen Z




Before Gen Z, technology was used in society, but it was never as imbibed in everyday life as it is today. Consumption of services and goods was predominantly through offline modes. But thanks to technology, things have changed.
  • Constant information
  • Fluid
  • Engagement
  • Content
  • Honest, fact-filled stories
  • Options

1. Constant information

Gen Zers have grown up with constant connectivity. This is a generation that knew “swiping,” pinching, “liking,” zooming before they could walk. They search online for information that they need rather than relying on someone or something be it for education, entertainment or anything. A brand that wants to be appealing to this generation needs to have a presence that serves information about itself to Gen Zers all the time. Patience is not a virtue for this generation since there are too many things vying to catch their attention over multiple mediums. If your brand can’t provide information about itself when they require it, you are history. Designers thus need to realize that constant availability of information is not a “good to have,” but rather a “prerequisite” for the Gen Zer's attention. Build a design that is accessible all the time.

2. Fluid

Gen Zers use social media to share thoughts, ideas, and views. Geographical distances, gender specifics, location-based preferences are no longer applicable when it comes to a Gen Zers lifestyle. What is vogue in one corner of the world, is also a must-have in another part of the world. Traditional preferences based on different parameters are irrelevant to this generation. Gen Zers identify themselves as fluid when it comes to favorites, identity, likes, and dislikes. Designers need to rethink traditional concepts of branding based on geography, gender, and other variables.

3. How to engage with Gen Z

Traditional forms of marketing, like print media, television are one way. A potential customer is informed via text, songs, stories, etc. about a product, and that’s it. The way a user places his trust in the product is by trying it and then rejecting or accepting it. With the Gen Zers, feedback for a brand is available for a potential user even before he/she uses the product thanks to social media and other online channels. A review by a family user is considered more useful than a superstar who endorses the brand. Hence the designers have to continually engage with the Gen Zer and get their feedback.
  • What is it about this product they like?
  • What can be made better?
What should be removed? How can this be shipped faster? It’s mandatory to engage the Gen Zer in all of this. This reinforces their trust in the brand.

4. Content

Since branding is primarily done on digital channels, pixel-perfect designs are a no brainer. Relevant content with bright, bold colors, designs, and icons that convey the brand’s purpose should be used. Slow, lagging, delayed experiences will be swiftly rejected. Mobile-specific content catering to different sizes and screens are a must as well. In this age of information overload, the content should be loud enough to call out Gen Zer's attention while maintaining relevance.

5. Honest, fact-filled stories

Inthis digital age, when nothing is hidden from anybody, and everything is available online, brands have to be authentic and honest. Gen Zers can immediately determine when they hear corporate lies. They value honesty and reliability over everything since they were born in an age of recession, religious turmoils, terrorism, fake news, etc. This makes them value authenticity and honesty and reliability. Brands and designers have to appeal to the Gen Zer with an unmasked front. Designers have to showcase real-world use cases to connect with the sentiments of Gen Zers.

6. Options

Spoil them for choice. Period. Globalization has indeed arrived. What is locally produced is available everywhere and vice versa. Gen Zers are sticklers for what they want and like, considering their propensity for stability. Hence if your brand does not give them multiple options, it will be rejected. Options catering to their fluid interests should be made available.

Parting shot

Gen Z, in short, is a digital generation. This generation seeks, disseminates, share, express, observe, objects, likes, dislike in the digital world. Designers have to rethink traditional values, methods, techniques of branding to fit into the mobile, tablet world which is the primary mode of consumption in the digital world.