JavaRush /Java Blog /Random EN /Coffee break #131. The role of Java in web and mobile app...

Coffee break #131. The role of Java in web and mobile application development. Functional programming - pros and cons

Published in the Random EN group

The role of Java in web and mobile application development

Source: DZone Java is one of the most used programming languages. It is actively used in the development of corporate, web and mobile applications. Java has applications in leading tech industries, from Data Science to AR/VR. Although enterprise development in Java is the most popular use case for this language, web and mobile applications are also considered one of the areas where Java is used. Coffee break #131.  The role of Java in web and mobile application development.  Functional programming - pros and cons - 1Let's take a look at why Java development has become so popular on the web and mobile devices over the past two decades. One of the main reasons to use Java for web and mobile is that it is a high-performance programming language. It ensures that your applications can run quickly and without any processing errors. There are also other reasons:

1. Open source

Being an open source programming language, Java has a number of advantages. First, it reduces the cost of the application development process. Secondly, developers easily change the language and update it frequently since it is open source. In addition, Java has an easy-to-read syntax that makes it easy to develop applications for the web and mobile devices. Finally, developers can use the existing language codebase and improve it.

2. Cross-platform

Another benefit of programming in Java is that it is a cross-platform language. Developers can write code on Windows and run it on macOS and Linux. The principle “write once, run everywhere” applies here. This makes it easier for developers working on different systems and simplifies the process of testing on different machines. For example, developers can test whether a program will run correctly on different screen sizes and operating systems.

3. Mobile-friendly

Java is a popular technology for mobile applications. Android app developers mainly use Java language because it is easy to implement, has security features and cross-platform capabilities. Android requires a powerful language to scale an application, but Java offers amazing scaling capabilities. It also simplifies the process of testing Android applications. All this suggests that it is a reliable and dynamic programming language for creating mobile applications.

4. Tools and libraries

One of the most significant advantages of Java is its compatibility with various tools. Frameworks such as Spring, Hibernate, Struts, Spark and others greatly simplify the development process. All these tools offer various features for creating interactive and dynamic applications. Libraries like Apache Commons, Java Standard Libraries, Maven, Jackson, etc. allow developers to add functionality without writing code from scratch.

5. Active community

There are more than 7.6 million Java developers in the world. They constantly update the programming language and add new features to it. Since Java is updated every 6 months, developers have the opportunity to learn quickly and create better products. You can also use the Java community to find answers to various questions.

The Rise of Java in Web Application Development

Since its creation in 1995 by Sun Microsystems, the Java language has become one of the priorities in the programming industry. Today, companies from all over the world use this programming language to create web applications. Google, Netflix, Spotify, and Pinterest are just a few companies that use Java in their technology stack. Experts estimate that more than 60% of companies working in IT use Java in one form or another. Moreover, it remains a popular programming language due to its adoption in other areas of web development, namely cloud computing, social media, enterprise development, AR, VR, and more. It is an excellent and powerful programming language for use in enterprises that now need a cloud-based architecture. When combined with JavaScript, Java allows you to create high-performance web applications that can run on any platform.

The Rise of Java in Modern Use Cases

Apart from web development and mobile development, Java has made its mark in the modern technological world. Today, Java is an extremely useful programming language for scientific applications. It offers a secure, fast and portable environment for scientific computing. MATLAB is one of the most popular libraries for Java-based scientific applications.
  • Java is also useful in front-end and back-end development of scientific applications. You can use Struts and Java Server Pages for front-end development and Java Core for back-end development of scientific applications.

  • Java is also useful for data analysis. It allows developers to create data analytics solutions using various libraries and tools. Today, Java has found application in the Internet of Things segment. It is a key technology in embedded systems such as smart cards and sensors. Therefore, companies use this programming language in the home automation industry. In addition, it is actively used in the development of streaming device technologies and Smart TV.

  • The Hadoop big data platform is written in Java. Because it is an open source and object-oriented language, it is the cornerstone for Big Data applications. Programming languages ​​like Scala can easily run on the Java Virtual Machine and make big data analysis easier.

  • Gaming is an emerging and fast-growing industry in which Java is widely used for various gaming applications. For example, it offers support for jMonkeyEngine, the largest open source 3D game development engine. Additionally, Dalvik Virtual Machine (DVM) is primarily supported by Java and is used to create interactive games for Android.

Functional programming - pros and cons

Source: Dev.to Despite my traditional skepticism towards unfamiliar technologies, I recognize the benefits of functional programming. Below is an approximate list of the advantages that describe this programming paradigm. Coffee break #131.  The role of Java in web and mobile application development.  Functional programming - pros and cons - 2

Safe Concurrency

The functional approach encourages safe concurrency, that is, the ability to run jobs in separate threads without causing thread conflicts or modification collisions. The reason is that unlike the object approach, you are not sharing an object whose state can be changed during processing. There are entrances and exits, and the entrance does not change because of you. In Java, even if you use “thread-safe” objects, you cannot guarantee that they will have the same values ​​when your method is called. In the example below, we can see that a loop that processes elements one at a time can be easily processed in parallel using a lambda function inside a thread.
for( String item : items ) {
   process(item);
}
And now it becomes:
items.parallelStream().forEach( item -> process(item) );

Fast I/O

Functional programming supports a different approach than multithreading. This means that we no longer have separate threads waiting for I/O responses such as database calls. That is, we maximize CPU and bandwidth usage. For high-performance applications this is a clear advantage. This is implemented with the idea that a function call can return a Future, which is not the actual result of the call, but rather a promise to return the result at some point in the future. At some point in the future, a return value is obtained that runs the function. This means that the processor threads do not wait for database or REST calls to complete and can do something else.

Brevity of expression

Software developers want to be able to express complex ideas gracefully. Functional programming allows you to do this concisely. For example, common constructs such as for loops can be replaced by threads, which abstract the common for operations for which loops are used. There is no doubt that the addition of Lambda functions and threads to Java has expanded the ability to express ideas that were previously impossible.

Why not become functional?

Just to reiterate the above: there are many benefits to functional coding, so this article is not trying to generalize everything into one case and say that you should definitely do functional programming or vice versa. This decision must be made with a clear understanding of the reasons for the transition and potential problems.

It is always difficult to understand intuitively

When writing code, are you trying to communicate with the computer? If communication with a computer is so important to you, why not write in machine code? Of course, this is very difficult, so computer languages ​​were invented to make it easier. They also allow programmers to create expressions that other programmers can understand. As software becomes larger and more complex, we need strategies to manage complexity. We achieve this through abstractions and information hiding. The class you use in Java to send email is quite complex, but the interface of this class is simple. It hides the detailed implementation, revealing to us only the external side of the control. Language features such as curly braces and square brackets tell us structure such as conditional statements and loops. Only now we move loops, or rather recursion, and conditions into functions:
for( String item : items ) {
    System.out.println(item);
}
It turns out:
items.foreach( item -> System.out.println(item) );
The streaming and lambda approach are definitely shorter. You can express the same functionality with less code. The problem is that we are now hiding the actual behavior inside methods that we need to know. Loops in Java use keywords. On the other hand, lambda functions can implement various forms of recursion, and only the name here indicates what it does. For example:
boo.fooble( item -> System.out.println(item) );
You can no longer just read the code to understand the structure. This makes it difficult to track the flow of execution. Another feature is function chaining, where the result of one function is the input of the next without assigning an intermediate variable.
boolean result = boo
    .fooble( /*some code*/ )
    .bobble( /*some code*/)
    .goober( /*some code*/);
This might make sense to the author of this code because he seems to have written every function and knows what it does. However, for those new to the code, this doesn't tell you much about the parameters, processes, or return values ​​of each function. But if you write the same thing in something that assigns types, you'll get:
Car car = boo.fooble( /*some parameters*/);
Tyre tyre = car.bobble( /*some parameters*/);
int pressure = tyre.goober( /*some parameters*/);
This may not be a perfect analogy, since parameters and anonymous functions are not the same thing, but the point here is that a long sentence with unknown results and parameters is difficult to read without a deeper understanding of what the functions described below do. And so it's possible that functional programming is both highly expressive in surprisingly short segments of code, but incomprehensible to people new to the software. The point of classes and interfaces is to hide data, to create reusable pieces of code that don't require the developer to understand or perhaps even look at the implementation. They exist so that developers can work on large, complex systems without headaches. This is a convenient way to organize code associated with specific entities. However, I have little understanding of how functional programming helps structure large complex projects. There are probably subjective reasons for this.

To impress others with your skills

Some software developers are so insecure in their skills that they try to impress by writing unnecessarily complex code. As a senior developer, I don't particularly admire other software developers who try to write smart code. Of course, this doesn't just apply to functional programming; the same thing can happen in any coding style. But I have noticed a certain intellectual vanity among functional programming proponents. The point is that software developers don't work in a vacuum. This “smart” code will need to be maintained, and if it is difficult to understand, then it will be difficult to read and change, and difficult to debug. When managing teams, we have people at all levels, so the smartest thing to do is write code in a way that everyone involved can understand. This is why we have clear long variable names, so we limit the length of methods. “Smart” code will take up extra time, so it’s not very smart to write it. I believe that functional programming uses the same arguments about concise and powerful expressions that Perl made many years ago. Perl's supporters took pride in its expressiveness, the way they could write programs that were very powerful in just a few lines of code. It was true. The problem was that they were difficult to understand. Perhaps functional programming has fallen into the same trap.

Because it's a new fashion

There is always a “hot” new technology or paradigm. New technologies, languages ​​and approaches are constantly emerging. We must constantly challenge ourselves and learn new things. Being a software developer means advancing technology. But as software developers, we must evaluate for ourselves what technologies we need to create a product. Learning a new popular technology just because it's popular is not a wise use of time. This doesn't mean that you shouldn't learn functional programming, but rather that you should evaluate the pros and cons of this paradigm for the application you're writing.

Because this is the only true way

Many times before, I have seen developers promote this or that approach as the only correct one. But as a friend told me, different jobs require different tools. For example, I personally use Python and PyTorch for artificial intelligence, although there are many other tools and technologies, each with their own advantages and disadvantages. There are good reasons to consider functional programming as an option for services that need significant scalability. This is due to the concurrency safety offered by this approach. But you should also be aware of the costs and potential risks. A few years ago I had a similar experience with dependency injection and Spring being considered the best for the job. When I asked Spring supporters what benefits it brings, they didn't give me clear answers. I'm seeing something similar now with some functional programming purists who seem to be engaging in some kind of religious campaign rather than an impartial evaluation of the technology. By the way, dependency injection was a great idea and I can now clearly identify its benefits. Likewise, I think my concerns about functional programming have to do with how it is used, rather than whether it is a useful and valid approach.

Conclusion

The point of this article is not to provide a clear answer to the question of whether you should do functional programming. The point is how you evaluate any new technology or approach. The main thing: do not let your ego block an objective assessment from you. Software development is not proof of your intellectual ability or your personal qualities. It's about creating value in the real world. If functional programming helps you, then use it.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION