Sometimes you need advice, Ask a teacher to solve your problems.

Make a Difference with education, and be the best.

Make a Difference with education, and be the best.

Putting Children First. Preparing Children For Success In Life

Putting Children First. Preparing Children For Success In Life

How you can get top grades, to get a best job.

How you can get top grades, to get a best job.

Quiz to

crack JEE

Explore

Home Work to

crack JEE

Explore

Masterclass to

crack JEE

Explore

Masterclass to

crack JEE

Explore

Latest Posts

Friday, 20 May 2022

Hibernate

Skill is Vision

 Hibernate is the best ORM (Object Relational Mapping) framework in Java. Let's understand how...

 

We are living in the world of software which is build using programming language e.g. Java. Software works with data. Data can be primitive values e.g int, float, string, double or in object (student object, employee object).

Student object will have 3 values and it should be stored somewhere (database).Database can be anything MySQL, Oracle, SQL server maybe Postgres, Adobe

Database is used to store data. The concept of storing data is called as persistence. Variables are transient which means we don't store data inside database directly.

To connect the java application with database, concepts of JDBC (Java database connectivity) is used. As we are using JDBC and have a database, we need SQL language now. But, not all developers are comfortable with SQL queries. 

We need a solution to write data in DB without writing SQL queries.

class Student

{

roll no;

name;

marks;

}

Student studentObj and it can be directly store data in database by just writing save(studentObj) without writing SQL queries.

Here, ORM comes to rescue these developers and helps in storing data into database without using SQL.

How to convert java object into table?

As shown below, Class structure is similar to table structure. It can be achieved by creating relationship between Object Programming Language and Database system using ORM. ORM is a concept and to implement ORM we need some tool which is ORM tools e.g. hibernate, ibatis, JPA, Toplink etc. 

ORM object relational mappingObject-relational Mappers (ORMs) - Full Stack Python

Using hibernate, objects can be stored into database directly using save(studentObj) method.

create SessionFactory -> create Session object -> session.save(studentObj)

Configuration of database needs to be provided in Session Factory like driver name, database url which is connect string, username, password and it can be done in different ways e.g. xml, java configuration.



Wednesday, 18 May 2022

Wildcard Imports and its Performance effect

Skill is Vision

 



Here we will discuss about
  • Wildcard Imports
  • How wildcard imports affect performance 
  • How to disable wildcraft imports in IntelliJ

Java developers use the wildcard (*) in import statements to import all the classes in a particular package. But in code reviews, most of you may have asked to remove those wildcard imports and add the actual class name.

Is it really matter having wildcards in import statements in Java?

Purpose of this article is to discuss the above problem. I’m approaching it in 2 ways,

  • Performance
  • Clean code

What is Import Statements
Java import statement is a syntactical sugar. Import statement tells the compiler where to find the names ( Classes, static methods, etc.) used in the source code. Which means, if you use the fully qualified names in the source code, then you don’t need import statements💡
    

package com.example;
import java.util.ArrayList;
import java.util.List;
public class WithImport {
public static void main(String[] args) {
List myList = new ArrayList();
}
}
Example 1 — Code with import statements

In the above code block, I’ve used List and ArrayList classes from thejava.util package. We can write the same code without import statements as bellow.

package com.example; public class WithoutImport {
public static void main(String[] args) {
java.util.List myList = new java.util.ArrayList();
}
}

In the above example, I’ve used fully qualified names for List and ArrayListclasses. If we compile and run these codes, the output would be the same.

Then what happens with the wildcard import?

Wildcard imports tell java compiler to search for class names in the given package. Hence, using wild card imports, the compile-time performance may lower a bit. But it won’t be a notable impact in most cases. In run time, there’s no performance issue at all because these import statements are compiler directives and we can’t find them in the byte code. To clarify this we’ll write the above example with wildcard imports.

package com.example; import java.util.*;
public class WithWildCard {
public static void main(String[] args) {
List myList = new ArrayList();
}
}
Example 3 — Code with wildcard imports

If we get the byte code of this class, it would look like following


Compiled from "WithWildCard.java" public class com.example.WithWildCard { public com.example.WithWildCard();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: new #2 // class java/util/ArrayList
3: dup
4: invokespecial #3 // Method java/util/ArrayList."<init>":()V
7: astore_1
8: return
}

IF we compare it with the byte code of example 1 (Code with import statements), both have the same byte code. In the runtime, Java uses the byte code, not the source code. In the byte code, there’s no import statements. That clearly implies that using wildcard imports does not affect the runtime performance of a Java application.


Coding best practices

The main drawback of using wildcard imports in Java is possible naming conflicts. Let’s assume that we have developed an ArrayList class ourself which implements the java.util.List interface. But if we use the import statement import java.util.* as in example 3, Compiler would pickup the class java.util.ArrayList instead of our implementation. And sometimes, if we import using the wildcard and the developer of that imported package adds a new class or rename one to a class that we are already using in our code, then the code won’t compile due to class name conflicts.

Apart from the class name resolving issue, code readability would drop with the wildcard import since the developers don't get a clear idea of the actual classes using by the code. But if you are using a large number of resources from a package, using the wildcard looks cleaner sometimes.

How to disable wildcraft imports in IntelliJs

To force IntelliJ to include each and every import individually.

Go to Preferences ( + , on macOS / Ctrl + Alt + S on Windows and Linux) > Editor > Code Style > Java > Imports tab set Class count to use import with '*' and Names count to use static import with '*' to a higher value. Any value over 99 seems to work fine.

Cyclomatic Complexity

Skill is Vision

Image Blog QAC Cyclomatic Complexity


Here, we explain what is cyclomatic complexity, cyclomatic complexity McCabe, and provide cyclomatic complexity examples.


What Is Cyclomatic Complexity?

  • It is a metric for software quality
  • Developed by Thomas J. McCade Sr.
  • Refers to the no. of linearly independent path through a program's source code.

Cyclomatic complexity (CYC) is a software metric used to determine the complexity of a program. It is a count of the number of decisions in the source code. The higher the count, the more complex the code. 

What are the effects of high Cyclomatic Complexity on your code?

The higher the number, the more complex the code.

And, the more complex the code the more likely it will:

  • Have defects
  • Be difficult to test
  • Be difficult to read
  • Be difficult to maintain
Complex code is inefficient, unreliable and low quality.

Why Is Cyclomatic Complexity Important?

Cyclomatic complexity can be used in two ways, to:

  • Limit code complexity.
  • Determine the number of test cases required.

How to Calculate Cyclomatic Complexity

Use the following formula to calculate cyclomatic complexity (CYC):
CYC = E – N + 2P

In this equation:

  • E refers number of edges or transfers of control
  • N refers number of nodes or sequential group of statements containing only one transfer of control
  • P refers number of disconnected parts of the flow graph such as a calling program or a subroutine
This translates to the number of decisions + 1.

Binary decisions — such as “if” and “while” statements — add 1 to complexity.

Boolean operators can add either one or nothing to complexity. For instance, one may be added if a Boolean operator is found within a conditional statement.


Two Cyclomatic Complexity Examples

Cyclomatic Complexity Example 1

Here's an example of CYC.

void foo(void)
{
    if (a && b)
        x=1;
    else
        x=2;
}

At first, it looks like there is one decision in this example. So, CYC = 2.

However, when you consider the side effect of the Boolean operator, there are actually two decisions.

void foo(void)
{
    if (a)
        if (b) 
            x=1;
    else
        x=2;
 }

So, CYC = 3 in this example. 

Cyclomatic Complexity Example 2

There are other variations of CYC, too.

Myers' Interval is an extension to CYC. It accounts for complexity caused by compound predicates. It uses CYC as its lower bound. The upper bound is defined as the total number of conditions in the code plus 1. It's presented as two values separated by colon :

CYC: NUMBER OF LOGICAL OPERATORS


Here's an example using Myers' Interval.

int divide (int x, int y)
{
    if (y != 0) /* Condition 1 */
    {
    return x / y;
    }
    else if (x == 0 && y > 2) /* Condition 2*Conditional expression 1 */
    {
        return 1;
    }
    else
    {
        printf (“div by zero\n”);
        return 0;
    } 

The example above has a Myers Cyclomatic Complexity value of 3:4. Its CYC is 3. And there is one connective (&&) used in the conditions. 

However, complexity is just one measure of quality.


Manually computing Cyclomatic complexity can be a time consuming process that is vulnerable to human error. However static analyser tool such as Helix QAC, the process is quick and error free.

Learn how to reduce the complexity of your code — and maintain a readable codebase — by using a static code analyzer such as Helix QAC.




Our Team

  • Pooja Kumari
  • Arti Kumari
  • Sumit Rai