<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Embold Technologies</title>
    <description>The latest articles on DEV Community by Embold Technologies (@embold_io).</description>
    <link>https://dev.to/embold_io</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F396019%2Fb004265f-472c-4c59-98a9-b42af293f9af.jpg</url>
      <title>DEV Community: Embold Technologies</title>
      <link>https://dev.to/embold_io</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/embold_io"/>
    <language>en</language>
    <item>
      <title>Taking a Look at REST API Design Patterns</title>
      <dc:creator>Embold Technologies</dc:creator>
      <pubDate>Wed, 19 Aug 2020 12:36:39 +0000</pubDate>
      <link>https://dev.to/embold_io/taking-a-look-at-rest-api-design-patterns-o5o</link>
      <guid>https://dev.to/embold_io/taking-a-look-at-rest-api-design-patterns-o5o</guid>
      <description>&lt;p&gt;We know what webservices are, and that there exist multiple types of webservices. But the main type is REST—although it is most often used in the context of HTTP, REST is an architectural design pattern, not a communication protocol.&lt;/p&gt;

&lt;p&gt;In this article, we talk a bit about useful and intuitive design patterns in RestFul webservice API architecture. In general, design patterns are formalised best practices that a programmer can use to solve common problems when designing an application or system. Below are different elements of design patterns for REST architecture.&lt;/p&gt;

&lt;p&gt;REST architecture style constraints: There are design rules that are applied to establish the different characteristics of the REST architectural style, which are referred to as REST constraints:&lt;/p&gt;

&lt;p&gt;Client-server&lt;br&gt;
Statelessness&lt;br&gt;
Cacheable&lt;br&gt;
Uniform interface&lt;br&gt;
Layered systems&lt;/p&gt;

&lt;p&gt;Goals of RESTful API design: Restful APIs should be straightforward, unambiguous, easy to consume, well-structured, and most importantly, accessible with well-known and standardized HTTP methods.&lt;/p&gt;

&lt;p&gt;API design goals:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Affordance:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Affordance is the possibility of an action on an object or environment. An API with clear perceived affordance allows the developer to understand its purpose and to use it seamlessly inside the Cybernetic Environment it was designed for.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Loosely coupled:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In Restful APIs, multiple clients are connected to the same backend server. So when internal representation of a server is changed, it should not affect API consumption at the client side. In a loosely coupled design, APIs are independent, and modifications in one won't impact the operation of consumers. Within an API, the components get added, modified, or replaced. However, the loose coupling approach offers clients better flexibility and reusability of APIs while its elements are added, replaced, or changed. Well-designed APIs exhibit loose coupling and well-composed functionalities across service boundaries to maximize scalability factors.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Leverage existing web architecture:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;RESTful APIs should use HTTP as a transport layer since the infrastructure, server and client libraries for HTTP are widely available already. RESTful APIs should take advantage of HTTP methods, or verbs, such as GET, PUT and POST.&lt;/p&gt;

&lt;p&gt;RESTful API Design Patterns: API design patterns provide a description or templates to solve specific, recurring API design problems that any software architects and API designers would like to adopt in their API designs. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Statelessness:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Communication between client and server should be stateless, means that every client request contains all the information necessary for the server to process the request. So there is no global state thereby reducing the complexity of the server. &lt;/p&gt;

&lt;p&gt;The good news is that some Restful web frameworks provide out-of-the-box implementation for Statelessness. For example: Spring Boot's REST API framework. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Content negotiation:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Content-negotiation is a mechanism or process that services and clients can select as their resources representation format for their communication and handshakes during their usual course of communication.&lt;/p&gt;

&lt;p&gt;HTTP specification comes up with a set of standard headers, through which the client can get information about a requested resource and carry the messages that indicate its representations.&lt;/p&gt;

&lt;p&gt;So, for content negotiation, REST services need to use HTTP headers; that is, when the client makes requests, it includes the accepts header, the list of file types that the client and server can handle with no additional steps to the client requests, the server processes, and replies.&lt;/p&gt;

&lt;p&gt;E.g. In Java we can use produces property in @GetMapping annotation &lt;/p&gt;

&lt;p&gt;@GetMapping(path="/investors/{investorId}/stocks/{symbol}", produces={MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;URI Templates&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It is always the case that the client may need to include some additional information in their request, and how the server lets the client include that information about resources in the URIs. Server-side developers require the ability to describe the layout of the URIs that their services will respond to. So we can use URI template. URI templates provide a way to describe a set of resources as variables. &lt;/p&gt;

&lt;p&gt;E.g. If there are multiple resources,&lt;/p&gt;

&lt;p&gt;People: &lt;a href="https://swapi.co/api/people/"&gt;https://swapi.co/api/people/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Planets: &lt;a href="https://swapi.co/api/planets/"&gt;https://swapi.co/api/planets/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Films: &lt;a href="https://swapi.co/api/films/"&gt;https://swapi.co/api/films/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Species: &lt;a href="https://swapi.co/api/species/"&gt;https://swapi.co/api/species/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can use simply &lt;a href="https://swapi.co/api/%7Bresource_id%7D/"&gt;https://swapi.co/api/{resource_id}/&lt;/a&gt; as a URI template&lt;/p&gt;

&lt;p&gt;The @PathVariable annotation provided by Spring Boot helps us implement the URI template pattern in our code seamlessly. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Design for Intent&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Design for intent is a method that expresses the different relationships between objects so that changes to one object automatically propagates changes to others.&lt;/p&gt;

&lt;p&gt;In a RESTful API world, the API should be developed to ensure they meet the requirements of the desired use cases provided and faced by users, but without exposing the internal business objects. Design for intent is a strategic design pattern that's intended to influence or result in specific and additional user behaviours.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pagination&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When there are multiple rows of data available then APIs should give the requested data in batch wise (Pagination). &lt;/p&gt;

&lt;p&gt;Pagination is a concept that helps in serving only part of the data as a response, however, with information about how to access all the data from the server, page by page, without much load and high computation for the server to serve the whole data.&lt;/p&gt;

&lt;p&gt;E.g. xxx.api.com/students?page=2.&lt;/p&gt;

&lt;p&gt;There are three variants of resource representation ways of pagination:&lt;/p&gt;

&lt;p&gt;Offset-based&lt;br&gt;
Time-based&lt;br&gt;
Cursor-based&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Discoverability:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Discoverability is very important factor in API designing, helping developers figure out programmatically whether the site that's being accessed has an API enabled or not will be the most critical responsibility of the API.&lt;/p&gt;

&lt;p&gt;Using following two ways we can ensure discoverability of API for developers,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;By valid HTTP methods:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When clients call REST services with invalid HTTP methods, the response of that request should end up in the 405 HTTP error code; that is, 405 Method Not Allowed. In addition to the error code, the response header should provide flexibility to the client to find the supported methods that allow headers in its response. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;By providing the URI of the newly created resource:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Including the URI as part of the location header as the response to the newly created resource is another method of discoverability. The returned URI as a location will be available through GET. &lt;/p&gt;

&lt;p&gt;In Sprint boot it gives out-of-the box solution for discoverability E.g. Using @GetMapping annotation&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Unicode:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A simple yet powerful way to make API support multiple languages is to enable the API to support Unicode.&lt;/p&gt;

&lt;p&gt;Unicode is an encoding standard that support an international character set. It has a unique number for every character across multiple languages including Chinese, Korean, and Arabic and their scripts. The unique number makes almost all characters identifiable and accessible across platforms, programs, and devices.&lt;/p&gt;

&lt;p&gt;API design principles:&lt;/p&gt;

&lt;p&gt;In general, the following standard guidelines should be followed while designing high-quality Restful APIs.&lt;/p&gt;

&lt;p&gt;Ubiquitous web standards&lt;br&gt;
Flexibility&lt;br&gt;
Standardization&lt;br&gt;
Optimization&lt;br&gt;
Granularity&lt;br&gt;
Sandbox or playground&lt;/p&gt;

&lt;p&gt;This should be an excellent starting point for anyone who wants to get their hands into RESTful services, with not just the basics, but essential patterns as well. The enemy of design patterns are anti-patterns, which seem sounds but are counter-productive when executed. Unfortunately, anti-patterns are hard to detect. Read more about how a free static analyser like Embold detects up to 30 structural design issues in Java programming. You can check out my other article on anti-patterns as well. &lt;/p&gt;

&lt;p&gt;Did you find this useful? Please comment. &lt;/p&gt;

</description>
      <category>java</category>
      <category>design</category>
      <category>architecture</category>
      <category>codequality</category>
    </item>
    <item>
      <title>The Silent Villains of the Coding Universe: A Review of Common Java Anti-Patterns.</title>
      <dc:creator>Embold Technologies</dc:creator>
      <pubDate>Tue, 11 Aug 2020 16:33:59 +0000</pubDate>
      <link>https://dev.to/embold_io/the-silent-villains-of-the-coding-universe-a-review-of-common-java-anti-patterns-17j9</link>
      <guid>https://dev.to/embold_io/the-silent-villains-of-the-coding-universe-a-review-of-common-java-anti-patterns-17j9</guid>
      <description>&lt;p&gt;Anti-Patterns seem harmless but lead to error-prone solutions and make your code unmaintainable in the long-run. &lt;/p&gt;

&lt;p&gt;For those who aren't familiar with the term ‘anti-pattern,’ it may sound like frivolous jargon or a throwaway catchphrase, but for those in the know, it’s a little more sinister than that. &lt;/p&gt;

&lt;p&gt;First coined by famed American programmer and prolific author in the programming literature space – Andrew Koenig – anti-pattern is the antithesis of the popular and positive ‘design pattern,’ created by programmers as a reusable solution that can be deployed to solve recurrent problems. &lt;/p&gt;

&lt;p&gt;Today, anti-patterns represent a solution to a programming issue that seems to work when taken at face value but causes problems when executed. Simply put, anti-patterns are patterns that look sound but are counter-intuitive in practice—they result in unmaintainable and error-prone solutions. &lt;/p&gt;

&lt;p&gt;Usually, anti-patterns emerge over a period of time when new functionality is added incrementally without a focus on continuous refactoring. Maintenance of code by different developers over a period of time often compounds the problem.&lt;/p&gt;

&lt;p&gt;Here are some examples of anti-patterns that tend to crop up in Java-based programming: &lt;/p&gt;

&lt;p&gt;God Class: A god class is a class of code that executes multiple functionalities that are typically unrelated to each other. This creates needless complexity in the methods being run and results in a class that becomes too critical to the overall functionality of the program. Because this one class affects so many others, you have to be exceedingly careful when it comes to refactoring this class. Since a god class doesn’t follow one of the most basic tenets of programming whereby a big problem is deconstructed into several smaller problems and then solved individually, it results in a code where any small change could have a domino effect on the entire system. A single class with too many functions, excessive amounts of data, and too many methods is a classic example of a god class. &lt;/p&gt;

&lt;p&gt;Global Breakable: Typically a structural component with an excess amount of outgoing dependencies is referred to as a global breakable. This type of anti-pattern is in constant need of maintenance as any change made to the components it is dependent on will result in a need for a change to this component as well. This is a sign of weak code and a system that is missing modularity.&lt;/p&gt;

&lt;p&gt;Global Butterfly: On the opposite spectrum of the global breakable is the global butterfly. This structural pattern denotes an object that has a number of dependents that are much higher than is advisable. Whether these dependents come in the form of classes, inner classes, interfaces, or packages, all of these result in a global butterfly situation. The drawback of having a global butterfly as part of your code is that it becomes impossible to change this code outright, and even small alterations to this component become a delicate operation that could have a wider knock-on effect. &lt;/p&gt;

&lt;p&gt;Shotgun Surgery: If you have seen a class or method that has a disproportionately high number of incoming couplings, then that is a classic example of what is known as the shotgun surgery anti-pattern. A single change in this class will affect all the methods that call into it. This means that everything from testing this class to refactoring and maintaining it becomes needlessly complex. This class becomes critical as so many methods are dependent on it, even though it may not have as imperative a role to play in isolation. &lt;/p&gt;

&lt;p&gt;Feature Envy: Logically, it is prudent to package data together with the processes that require it. Feature Envy is an anti-pattern that works against this approach. In Feature Envy, the method is linked with a class outside of the package it is a part of. This results in a situation where the importance of a class suffering from Feature Envy is simply reduced to that of a Data Class. Or worse, the method in question may use data from both its own class and from an external class as well. In this case, its functionality becomes even more complex causing the need for extra care during refactoring. You need to put the data and the code that references it together in one package, without any external links to ensure that you avoid feature envy. &lt;/p&gt;

&lt;p&gt;Dispersed Coupling: If a class features methods that are reliant on operations that are spread across a number of different operations in various different classes, you have what is known as a dispersed coupling on your hands. Because of this, if a change is made in the dispersively coupled method, changes will also be required in all the classes it accesses. &lt;/p&gt;

&lt;p&gt;Global Hub: This is a commonly found component where you see a high number of global dependents attached to it, and the component in question also being globally dependent on an excessive amount of other components. Too many outgoing and incoming dependencies see the same component become both a global butterfly and a global breakable. This results in a situation where the code becomes too complicated to follow, and maintenance becomes a major issue as well. A global hub will also result in a situation where the reusability of a module is broken.   &lt;/p&gt;

&lt;p&gt;Brain Method: As is indicated by its name, a brain method is the type of anti-pattern that carries out a majority of the functions in its class. Rather than these functions being distributed across several methods, having them included within a single brain method sees the requirement for too many conditional branches. The brain method is like a scaled-down version of a god class, and as such, suffers from similar issues. The code that makes up a brain method is too long and a little too complex, making it difficult to understand, and almost impossible to reuse. The presence of a deep nesting situation with the high number of conditional branches means that it needs to be tested more thoroughly than usual as well. Furthermore, the presence of multiple functions in one method will result in a situation where the method is reliant on a high number of temporary variables, making it more susceptible to errors. &lt;/p&gt;

&lt;p&gt;These are just a few examples of the many anti-patterns that can throw a wrench in your code, causing further delays as your DevOps team identifies and fixes the issues created as a result of their presence. Anti-patterns are classic examples of how not to do things, and since they often masquerade as working code, sniffing them out can be quite the challenge. You can’t really know you’re doing something wrong until your mistake is pointed out to you, right? This is where static code analysers can help. Advanced static code analysis tools can detect these with ease. Read more: &lt;a href="https://docs.embold.io/anti-patterns/#what-are-anti-patterns"&gt;https://docs.embold.io/anti-patterns/#what-are-anti-patterns&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Generally, utilising a static code analysis tool will ensure that your programming resources are consumed for the right reasons. You can focus on development, innovation, expansion, and progress rather than on putting out fires, troubleshooting, going back and constantly re-checking code, and fixing all the issues that will crop up thanks to anti-patterns. &lt;/p&gt;

</description>
      <category>java</category>
      <category>design</category>
      <category>architecture</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
