<?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>Forem: CoderGears Team</title>
    <description>The latest articles on Forem by CoderGears Team (@codergears).</description>
    <link>https://forem.com/codergears</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%2F57022%2F7f6ba272-3845-4ef2-a584-e91eaf6fac82.png</url>
      <title>Forem: CoderGears Team</title>
      <link>https://forem.com/codergears</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/codergears"/>
    <language>en</language>
    <item>
      <title>10 Best practices to design and implement a Java class</title>
      <dc:creator>CoderGears Team</dc:creator>
      <pubDate>Wed, 05 May 2021 10:08:33 +0000</pubDate>
      <link>https://forem.com/codergears/10-best-practices-to-design-and-implement-a-java-class-1nb9</link>
      <guid>https://forem.com/codergears/10-best-practices-to-design-and-implement-a-java-class-1nb9</guid>
      <description>&lt;p&gt;To let your code easy to understand and maintain, it's better to enforce its code quality by following best practices.&lt;br&gt;
He is some best practices to design and implement a java class:&lt;/p&gt;

&lt;h2&gt;
  
  
  1- Modularize your code using Package-by-feature approach
&lt;/h2&gt;

&lt;p&gt;Package-by-feature uses packages to reflect the feature set. It places all items related to a single feature (and only that feature) into a single namespace. This results in namespaces with high cohesion and high modularity, and with minimal coupling between namespaces. Items that work closely together are placed next to each other.&lt;/p&gt;

&lt;h2&gt;
  
  
  2- Abstraction
&lt;/h2&gt;

&lt;p&gt;Data abstraction is one of the most essential and important features of object-oriented programming in Java. Abstraction means displaying only essential information and hiding the details. Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation.&lt;br&gt;
Even if this best practice is recommended by many books, web resources, conference speakers, experts. However, in many projects, this rule is ignored and we can have many class details not hidden.&lt;/p&gt;

&lt;h2&gt;
  
  
  3- Small classes are better
&lt;/h2&gt;

&lt;p&gt;Types with many lines of code should be split into a group of smaller types.&lt;br&gt;
To refactor a big Class you'll need patience, and you might even need to recreate everything from scratch. Here is some refactoring advice:&lt;br&gt;
• The logic in the BigClass must be split into smaller classes. These smaller classes can eventually become private classes nested in the original God Class, whose instances objects become composed of instances of smaller nested classes.&lt;br&gt;
• Smaller class partitioning should be driven by the multiple responsibilities handled by the Big Class. To identify these responsibilities it often helps to look for subsets of methods strongly coupled with subsets of fields.&lt;br&gt;
• If the Big Class contains way more logic than states, a good option can be to define one or several static classes that contains no static field but only pure static methods. A pure static method is a function that computes a result only from inputs parameters, it doesn't read nor assign any static or instance field. The main advantage of pure static methods is that they are easily testable.&lt;br&gt;
• Try to maintain the interface of the Big Class at first and delegate calls to the new extracted classes. In the end, the Big Class should be a pure facade without its own logic. Then you can keep it for convenience or throw it away and start to use the new classes only.&lt;br&gt;
• Unit Tests can help: write tests for each method before extracting it to ensure you don't break functionality.&lt;/p&gt;

&lt;h2&gt;
  
  
  4-Few methods per class as you can
&lt;/h2&gt;

&lt;p&gt;Types with more than 20 methods might be hard to understand and maintain.&lt;br&gt;
Having many methods for a type might be a symptom of too many responsibilities implemented.&lt;br&gt;
Maybe you are facing a class that controls way too many other classes in the system and has grown beyond all logic to become The Class That Does Everything.&lt;/p&gt;

&lt;h2&gt;
  
  
  5- Enforce Low coupling
&lt;/h2&gt;

&lt;p&gt;Low coupling is desirable because a change in one area of an application will require fewer changes throughout the entire application. In the long run, this could alleviate a lot of time, effort, and cost associated with modifying and adding new features to an application.&lt;br&gt;
Low coupling could be achieved by using abstract classes or using generic types and methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  6- Enforce High cohesion
&lt;/h2&gt;

&lt;p&gt;The single responsibility principle states that a class should not have more than one reason to change. Such a class is said to be cohesive. A high LCOM value generally pinpoints a poorly cohesive class. There are several LCOM metrics. The LCOM takes its values in the range [0–1]. The LCOM HS (HS stands for Henderson-Sellers) takes its values in the range [0–2]. A LCOM HS value highest than 1 should be considered alarming. Here are to compute LCOM metrics:&lt;br&gt;
LCOM = 1 - (sum(MF)/M*F)&lt;br&gt;
LCOM HS = (M - sum(MF)/F)(M-1)&lt;br&gt;
Where:&lt;br&gt;
M is the number of methods in class (both static and instance methods are counted, it includes also constructors, properties getters/setters, events add/remove methods).&lt;br&gt;
F is the number of instance fields in the class.&lt;br&gt;
MF is the number of methods of the class accessing a particular instance field.&lt;br&gt;
Sum(MF) is the sum of MF over all instance fields of the class.&lt;/p&gt;

&lt;p&gt;The underlying idea behind these formulas can be stated as follow: a class is utterly cohesive if all its methods use all its methods use all its instance fields, which means that sum(MF)=M*F and then LCOM = 0 and LCOMHS = 0.&lt;br&gt;
LCOMHS value higher than 1 should be considered alarming.&lt;/p&gt;

&lt;h2&gt;
  
  
  7- Comment only what the code cannot say
&lt;/h2&gt;

&lt;p&gt;Comments that parrot the code offer nothing extra to the reader. A prevalence of noisy comments and incorrect comments in a codebase encourages programmers to ignore all comments, either by skipping past them or by taking active measures to hide them.&lt;/p&gt;

&lt;h2&gt;
  
  
  8- Don't repeat yourself as possible
&lt;/h2&gt;

&lt;p&gt;It's known that the presence of duplicate code has negative impacts on software development and maintenance. Indeed a major drawback is when an instance of duplicate code is changed for fixing bugs or adding new features, its correspondents have to be changed simultaneously.&lt;br&gt;
The most popular reason for duplicate code is the Copy/Paste operations, and in this case, the source code is exactly similar in two or more places, this practice is discouraged in many articles, books, and websites. However, sometimes it's not easy to practice the recommendations, and the developer chose the easy solution: the Copy/Paste method.&lt;br&gt;
Using the appropriate tool makes easy the detection of the duplicate code from the copy/paste operations, however, there are some cases where cloned code is not trivial to detect.&lt;/p&gt;

&lt;h2&gt;
  
  
  9-Immutability is your friend for multithreading programming.
&lt;/h2&gt;

&lt;p&gt;Basically, an object is immutable if its state doesn't change once the object has been created. Consequently, a class is immutable if its instances are immutable.&lt;br&gt;
There is one important argument in favor of using immutable objects: It dramatically simplifies concurrent programming. Think about it, why does writing proper multithreaded programming is a hard task? Because it is hard to synchronize threads access to resources (objects or others OS resources). Why it is hard to synchronize these accesses? Because it is hard to guarantee that there won't be race conditions between the multiple write accesses and read accesses done by multiple threads on multiple objects. What if there are no more write accesses? In other words, what if the state of the objects accessed by threads, doesn't change? There is no more need for synchronization!&lt;br&gt;
Another benefit about immutable classes is that they can never violate LSP (Liskov Subtitution Principle) , here's a definition of LSP quoted from its wiki page:&lt;/p&gt;

&lt;p&gt;Liskov's notion of a behavioral subtype defines a notion of substitutability for mutable objects; that is, if S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program (e.g., correctness). &lt;/p&gt;

&lt;h2&gt;
  
  
  10-Use standard libraries as you can
&lt;/h2&gt;

&lt;p&gt;In the Java world, we can find for each technical need hundreds of libraries for a specific need. However, it's better to take your time and check if a need it's not already present in the Java standard library, high coupling with many external libraries might increase quickly the complexity and the support of a project.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to enforce the check of these best practices?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.jarchitect.com"&gt;JArchitect&lt;/a&gt; provides a code query language named &lt;a href="https://www.jarchitect.com/Doc_CQLinq_Syntax"&gt;CQLinq&lt;/a&gt; to query the code base like a database. Developers, designers, and architects could define their custom queries to find easily the bug-prone situations.&lt;br&gt;
With CQlinq we can combine the data from the code metrics, dependencies, API usage, and other model data to define very advanced queries that match some bug-prone situations.&lt;/p&gt;

&lt;p&gt;Writing CQLinq queries and constraints is straightforward because JArchitect provides a CQLinq editor which supports:&lt;/p&gt;

&lt;p&gt;Code completion / IntelliSense,&lt;br&gt;
 Live compile error description,&lt;br&gt;
 Integrated tooltip documentation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7b9hHaf0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.jarchitect.com/featuresA/screenshots/cqlinqscreenshot1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7b9hHaf0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.jarchitect.com/featuresA/screenshots/cqlinqscreenshot1.png" alt="cqlinq"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, once the query is compiled, it gets executed immediately and its result is well displayed and browsable:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3Idj2x1u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.jarchitect.com/featuresA/screenshots/cqlinqscreenshot2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3Idj2x1u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.jarchitect.com/featuresA/screenshots/cqlinqscreenshot2.png" alt="cqlinq"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Powerful and elaborated queries and rules can be written with CQLinq, like the following one for example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W7dvM5Ao--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.jarchitect.com/img/cqlinq/Query2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W7dvM5Ao--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.jarchitect.com/img/cqlinq/Query2.jpg" alt="cqlinq"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Short CQLinq queries can be written (or even generated) to get some immediate answers to questions about a codebase:&lt;/p&gt;

&lt;p&gt;Is the code layered correctly?&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dJ7eY4bH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.jarchitect.com/img/cqlinq/Query3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dJ7eY4bH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.jarchitect.com/img/cqlinq/Query3.jpg" alt="cqlinq"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Which methods have been refactored since the last release?&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZeuYUBag--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.jarchitect.com/img/cqlinq/Query4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZeuYUBag--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.jarchitect.com/img/cqlinq/Query4.jpg" alt="cqlinq"&gt;&lt;/a&gt;&lt;br&gt;
Which classes inherit from a particular class?&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pwxva3jH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.jarchitect.com/img/cqlinq/Query5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pwxva3jH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.jarchitect.com/img/cqlinq/Query5.jpg" alt="cqlinq"&gt;&lt;/a&gt;&lt;br&gt;
What are the 10 most complex methods?&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RKh5_2CX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.jarchitect.com/img/cqlinq/Query8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RKh5_2CX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.jarchitect.com/img/cqlinq/Query8.jpg" alt="cqlinq"&gt;&lt;/a&gt;&lt;br&gt;
Which methods assign a particular field?&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7b9hHaf0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.jarchitect.com/featuresA/screenshots/cqlinqscreenshot1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7b9hHaf0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.jarchitect.com/featuresA/screenshots/cqlinqscreenshot1.png" alt="cqlinq"&gt;&lt;/a&gt;&lt;br&gt;
Which complex method is not enough commented?&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fXwkG7Ro--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.jarchitect.com/img/cqlinq/Query9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fXwkG7Ro--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.jarchitect.com/img/cqlinq/Query9.jpg" alt="cqlinq"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I don't want that my User Interface layer to depend directly on the DataBase layer:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Tjd_O0nc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.jarchitect.com/img/cqlinq/Query10.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Tjd_O0nc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.jarchitect.com/img/cqlinq/Query10.jpg" alt="cqlinq"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Some C++ good practices from the OpenCV source code</title>
      <dc:creator>CoderGears Team</dc:creator>
      <pubDate>Wed, 11 Apr 2018 09:59:59 +0000</pubDate>
      <link>https://forem.com/codergears/some-c-good-practices-from-the-opencv-source-code-2mc</link>
      <guid>https://forem.com/codergears/some-c-good-practices-from-the-opencv-source-code-2mc</guid>
      <description>&lt;p&gt;&lt;a href="http://opencv.org/" rel="noopener noreferrer"&gt;OpenCV&lt;/a&gt; (Open Source Computer Vision) is a library of programming functions mainly aimed at real-time computer vision, developed by Intel Russia research center in Nizhny Novgorod. The library is cross-platform. It focuses mainly on real-time image processing.&lt;/p&gt;

&lt;p&gt;OpenCV is widely used, Adopted all around the world, for end users, it’s very mature and powerful, for developers it's well implemented and designed. The OpenCV developers used very basic principles which makes it very simple to understand and maintain.&lt;/p&gt;

&lt;p&gt;Let’s discover some OpenCV design choices:&lt;/p&gt;

&lt;h2&gt;
  
  
  Modularity
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1- Library based architecture&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A library-based architecture makes the reuse and integration of functionality provided more flexible and easier to integrate into other projects.In addition, the library based architecture encourages clean APIs and separation. Therefore making it easier for developers to understand, since they only have to understand small pieces of the big picture.&lt;/p&gt;

&lt;p&gt;OpenCV adopt this approach and defines many libraries, each one has a specific responsibility and all of them uses the opencv_core library.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codergears.com%2FBlog%2Fwp-content%2Fuploads%2Fopencv1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codergears.com%2FBlog%2Fwp-content%2Fuploads%2Fopencv1.png" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2- Modularize by namespaces&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;OpenCV uses widely namespaces to modularize its code base, here are for example the namespaces of the opencv_core project:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codergears.com%2FBlog%2Fwp-content%2Fuploads%2Fopencv2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codergears.com%2FBlog%2Fwp-content%2Fuploads%2Fopencv2.png" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;OpenCV uses the “Namespace-by-feature” approach. Namespace-by-feature uses namespaces to reflect the feature set. It places all items related to a single feature (and only that feature) into a single namespace. This results in namespaces with high cohesion and high modularity, and with minimal coupling between namespaces. Items that work closely together are placed next to each other.&lt;/p&gt;

&lt;p&gt;In case of OpenCV the namespaces are used for three main reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Modularize the libraries.&lt;/li&gt;
&lt;li&gt;Hide details like for “cv::detail” namespace, this approach could be very interesting if we want to inform the library user that he doesn’t need to use directly types inside this namespace and it’s only for internal use. For C# the “internal” keyword did the job, but in C++ there’s no way to hide public types to the library user.&lt;/li&gt;
&lt;li&gt;Anonymous namespace: namespace with no name. It avoids making global static variable. The “anonymous” namespace you have created will only be accessible within the file you created it in.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Defines data model as POD types&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Each project has its data model, it’s recommended to defines these data as &lt;a href="http://en.wikipedia.org/wiki/Plain_old_data_structure" rel="noopener noreferrer"&gt;POD&lt;/a&gt; types.&lt;/p&gt;

&lt;p&gt;Let’s search in the OpenCV code base for structs with no methods and having only fields. For that &lt;a href="http://www.cppdepend.com/Doc_CQLinq_Features" rel="noopener noreferrer"&gt;CQLinq&lt;/a&gt; will be used to query the code base.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codergears.com%2FBlog%2Fwp-content%2Fuploads%2Fopencv3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codergears.com%2FBlog%2Fwp-content%2Fuploads%2Fopencv3.png" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The result of this query concern 25% of the number of types defined in OpenCV projects. OpenCV defines almost all its data model in structs with only fields.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Avoid multiple inheritance&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using multiple inheritance could complicate the design, debuggers can have a hard time with it, therefore it’s not recommended by many C++ experts.&lt;/p&gt;

&lt;p&gt;Let’s search which classes inherit from more than one concrete base class in the OpenCV code base.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codergears.com%2FBlog%2Fwp-content%2Fuploads%2Fopencv4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codergears.com%2FBlog%2Fwp-content%2Fuploads%2Fopencv4.png" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Only a few classes from test projects use the multiple inheritance, this concept is avoided in the whole OpenCV code base.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Avoid defining complex functions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Many metrics exist to detect complex functions, NBLinesOfCode, Number of parameters and number of local variables are the basic ones.&lt;/p&gt;

&lt;p&gt;There are other interesting metrics to detect complex functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cyclomatic complexity is a popular procedural software metric equal to the number of decisions that can be taken in a procedure.&lt;/li&gt;
&lt;li&gt;Nesting Depth is a metric defined on methods that is relative to the maximum depth of the more nested scope in a method body.&lt;/li&gt;
&lt;li&gt;Max Nested loop equals the maximum level of loop nesting in a function.
The max value tolerated for these metrics depends more on the team choices, there are no standard values.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s search for methods that could be considered as complex in the OpenCV code base.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codergears.com%2FBlog%2Fwp-content%2Fuploads%2Fopencv5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codergears.com%2FBlog%2Fwp-content%2Fuploads%2Fopencv5.png" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Only 1% is a candidate to be refactored to minimize their complexity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Coupling
&lt;/h2&gt;

&lt;p&gt;Low coupling is desirable because a change in one area of an application will require fewer changes throughout the entire application. In the long run, this could alleviate a lot of time, effort, and cost associated with modifying and adding new features to an application.&lt;/p&gt;

&lt;p&gt;Low coupling could be achieved by using abstract classes. Here are three key benefits derived from using them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An abstract class provides a way to define a contract that promotes reuse. If an object implements an abstract class then that object is to conform to a standard. An object that uses another object is called a consumer. An abstract class is a contract between an object and its consumer.&lt;/li&gt;
&lt;li&gt;An abstract class also provides a level of abstraction that makes programs easier to understand. abstract class allows developers to start talking about the general way that code behaves without having to get into a lot of detailed specifics.&lt;/li&gt;
&lt;li&gt;An abstract class enforces low coupling between components, what’s make easy to protect the abstract class consumer from any implementation changes in the classes implementing the abstract classes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s search for all abstract classes defined by OpenCV :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codergears.com%2FBlog%2Fwp-content%2Fuploads%2Fopencv6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codergears.com%2FBlog%2Fwp-content%2Fuploads%2Fopencv6.png" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If our primary goal is to enforce low coupling, there’s a common mistake when using abstract classes, that could kill the utility of using them. It’s the using of the concrete classes instead of abstract ones, to explain better this problem let’s take the following example:&lt;/p&gt;

&lt;p&gt;The class A implements the abstract class IA which contains the calculate() method, the consumer class C is implemented like this&lt;/p&gt;

&lt;p&gt;public class C&lt;br&gt;
{&lt;br&gt;
   ….&lt;br&gt;
   public:&lt;br&gt;
      void calculate()&lt;br&gt;
      {&lt;br&gt;
        …..&lt;br&gt;
        m_a-&amp;gt;calculate();&lt;br&gt;
        ….&lt;br&gt;
       }&lt;br&gt;
       A* m_a;&lt;br&gt;
 };&lt;br&gt;
The class C instead of referencing the abstract class IA, it references the class A, in this case, we lose the low coupling benefit, this implementation has two major drawbacks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If we decide to use another implementation of IA, we must change the code of C class.&lt;/li&gt;
&lt;li&gt;If some methods are added to A not existing in IA, and C use them, we also lose the contract benefit of using interfaces.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;C# introduced the &lt;a href="http://msdn.microsoft.com/en-us/library/aa288461(v=vs.71).aspx" rel="noopener noreferrer"&gt;explicit interface implementation&lt;/a&gt; capability to the language to ensure that a method from the IA will be never called from a reference to concrete classes, but only from a reference to the interface. This technique is very useful to protect developers from losing the benefit of using interfaces.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cohesion
&lt;/h2&gt;

&lt;p&gt;The single responsibility principle states that a class should not have more than one reason to change. Such a class is said to be cohesive. A high LCOM value generally pinpoints a poorly cohesive class. There are several LCOM metrics. The LCOM takes its values in the range [0-1]. The LCOM HS (HS stands for Henderson-Sellers) takes its values in the range [0-2]. A LCOM HS value highest than 1 should be considered alarming. Here are  to compute LCOM metrics:&lt;/p&gt;

&lt;p&gt;LCOM = 1 – (sum(MF)/M*F)&lt;br&gt;
LCOM HS = (M – sum(MF)/F)(M-1)&lt;/p&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;M is the number of methods in class (both static and instance methods are counted, it includes also constructors, properties getters/setters, events add/remove methods).&lt;/li&gt;
&lt;li&gt;F is the number of instance fields in the class.&lt;/li&gt;
&lt;li&gt;MF is the number of methods of the class accessing a particular instance field.
Sum(MF) is the sum of MF overall instance fields of the class.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The underlying idea behind these formulas can be stated as follow: a class is utterly cohesive if all its methods use all its methods use all its instance fields, which means that sum(MF)=M*F and then LCOM = 0 and LCOMHS = 0.&lt;/p&gt;

&lt;p&gt;LCOMHS value higher than 1 should be considered alarming.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codergears.com%2FBlog%2Fwp-content%2Fuploads%2Fopencv7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codergears.com%2FBlog%2Fwp-content%2Fuploads%2Fopencv7.png" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Only a few types are not cohesive.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;If you take a look at the OpenCV source code, you will be surprised by the simplicity of its implementation, no advanced design concepts are used, no over-engineering, just some basic principles applied.&lt;/p&gt;

</description>
      <category>opencv</category>
      <category>cpp</category>
    </item>
  </channel>
</rss>
