DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Core Badge
Avatar

Peter Verhas

DZone Core CORE

Java Architect at EPAM Systems @verhas

Brütten, CH

Joined Dec 2012

About

I am a senior software architect specializing currently in enterprise Java development. I have past experience with JavaScript, Python, C and going back in time even FORTRAN and assembly were still not extinct at the start of my career. Currently, I work on different customer projects on behalf of EPAM system leading an 8 person agile group. As a hobby independently from my employer I maintain and develop open source projects, like ScriptBasic Classic, ScriptBasic for Java, License3j, Jamal, Java::Geci (google them if interested).

Stats

Reputation: 3279
Pageviews: 1.7M
Articles: 37
Comments: 76
  • Articles
  • Comments

Articles

article thumbnail
Programming Language With No Syntax?
Ouroboros is a new programming language that has no syntax, and at the same time, it has no syntax. Is that possible? To some extent, yes.
September 17, 2024
· 5,177 Views · 4 Likes
article thumbnail
Threaded Streams
In this tutorial, take a deep dive into an open-source library aimed at experimenting with a new method of parallelizing stream operations.
April 18, 2024
· 2,507 Views · 5 Likes
article thumbnail
Cloud Solutions Are Expensive, or Are They?
Many times companies jump into cloud solutions without understanding the price implications. This article talks about the real price of cloud solutions.
November 24, 2023
· 2,561 Views · 1 Like
article thumbnail
Programming With AI
How will generative programming transform programming languages? What problems do we have to overcome that we already see at this early stage?
September 1, 2023
· 5,443 Views · 3 Likes
article thumbnail
Do Not Stop Threads!
Follow this story from 2006 about losing a $20,000 contract due to calling Thread.stop() and how no one realized what was actually happening.
August 14, 2023
· 4,590 Views · 7 Likes
article thumbnail
Managing IntelliJ Live Templates
Editing IntelliJ live templates can be cumbersome when you have to manage many of them. Learn a method to ease the burden of using Jamal as a DSL tool.
July 6, 2023
· 3,329 Views · 5 Likes
article thumbnail
Adding Mermaid Diagrams to Markdown Documents
In this article, learn about how to integrate Mermaid diagrams into your Asciidoc, Markdown, or any other markup document.
June 6, 2023
· 5,151 Views · 2 Likes
article thumbnail
A Few Words About Rust for Managers
This article is for developers and managers who have heard about Rust but do not know much about it. I write what I see and give some “unsolicited” advice.
February 23, 2023
· 3,817 Views · 2 Likes
article thumbnail
Docs as Code Is Not Enough
Treating documentation as source code is a good idea and a good start. It can, and should, however, be extended to include more features.
February 9, 2023
· 2,318 Views · 1 Like
article thumbnail
Hidden Classes in Java 15
The why and how of hidden classes, with a helping hand from SourceBuddy.
February 5, 2023
· 7,118 Views · 7 Likes
article thumbnail
Introducing Java Record
Learn more about the latest JEP — Java Records!
October 18, 2019
· 30,035 Views · 5 Likes
article thumbnail
Your Code Is Redundant, Live With It!
It's time to deal with code redundancy.
September 20, 2019
· 15,241 Views · 2 Likes
article thumbnail
Java Hexadecimal Floating Point Literal
Learn more about the hexagonal floating point literal, a new functionality of Java::Geci.
July 8, 2019
· 23,119 Views · 3 Likes
article thumbnail
Converting Objects to Map and Back
Learn how to convert data objects to and from Map in Java applications.
June 21, 2019
· 55,193 Views · 1 Like
article thumbnail
Extending Abstract Classes With Abstract Classes in Java
Check out this post to learn more about extending abstract classes with Java.
June 18, 2019
· 27,276 Views · 3 Likes
article thumbnail
Generating Setters and Getters Using Java::Geci
Learn more about generating setters and getters using the Java::Geci framework.
Updated May 31, 2019
· 21,123 Views · 4 Likes
article thumbnail
Creating a Java::Geci Generator
Learn more about creating a Java::Geci generator in your Java projects.
May 14, 2019
· 17,030 Views · 2 Likes
article thumbnail
Var and Language Design in Java
Learn everything you need to know about var in Java.
March 20, 2019
· 12,061 Views · 13 Likes
article thumbnail
Comparing Files in Java
Take a look at how you can use memory mapping to easily compare files in Java in a performant, somewhat resilient manner.
February 1, 2018
· 78,055 Views · 31 Likes
article thumbnail
Java 9 Module Services
This deep dive into Java 9 module services tackles how the ServiceLoader class has changed, how to browse the classpath, and how it all ties together in Jigsaw.
January 14, 2018
· 21,862 Views · 15 Likes
article thumbnail
Split a File as a Stream
Curious about when you would actually use the splitAsStream method? Here's a use case of splitting a file into chunks that can be processed as streams.
November 30, 2017
· 30,618 Views · 14 Likes
article thumbnail
New RegEx Features in Java 9
RegEx fans will want to brush up on the changes coming to Java 9. Check out how they'll be packaged with the module system and the new tweaks coming.
August 17, 2017
· 14,749 Views · 9 Likes
article thumbnail
Try and Catch in Golang
The author compares talks about exception handling in Java, the absence of exception handling in Go, and a way to mimic it.
May 30, 2016
· 105,749 Views · 4 Likes
article thumbnail
Comparing Golang with Java
The first impressions of a Java enthusiast when looking at Go's features.
May 11, 2016
· 73,915 Views · 24 Likes
article thumbnail
Java Dynamic Proxy: What is a Proxy and How can We Use It
Learn all about Java dynamic proxies: what they are, when to use, how and when to use in code.
February 8, 2016
· 144,819 Views · 17 Likes
article thumbnail
Creating a Proxy Object Using cglib
A Java-based proxy object can be utilized when you need a method invocation handler. Here's a look at creating a proxy object with cglib, a super useful Java bytecode generation library.
January 30, 2016
· 19,057 Views · 2 Likes
article thumbnail
Pointers in Java
Learn the basics of differentiating references and pointers.
January 19, 2016
· 25,144 Views · 9 Likes
article thumbnail
Is it Worth it to Use 'Optional' in Collections?
There's a practical reason to use Optional in collections. It's just other developers who ruin it for you.
August 28, 2015
· 18,939 Views · 1 Like
article thumbnail
If You Do It Do It Right
This is a philosophical or ethical command. Very general. It is something like “fail fast”. The reason it came up to my mind is that I wanted to compile and release License3j using Java 8 and JavaDoc refused to compile during release build. This package is a simple license manager, which has some established user base who require that I keep up with the new versions of BouncyCastle. It itself being a cryptography package should not be outdated and programs are encouraged to use the latest version to avoid security issues. When I executed mvn release:prepare I got many errors: [ERROR] * [ERROR] ^ [ERROR] /Users/verhasp/github/License3j/src/main/java/License3j.java:132: error: unexpected end tag: [ERROR] * [ERROR] ^ [ERROR] /Users/verhasp/github/License3j/src/main/java/License3j.java:134: warning: no @param for args [ERROR] public static void main(String[] args) throws Exception { [ERROR] ^ [ERROR] /Users/verhasp/github/License3j/src/main/java/License3j.java:134: warning: no @throws for java.lang.Exception [ERROR] public static void main(String[] args) throws Exception { [ERROR] ^ [ERROR] /Users/verhasp/github/License3j/src/main/java/com/verhas/licensor/ExtendedLicense.java:73: warning: no @param for expiryDate [ERROR] public void setExpiry(final Date expiryDate) { [ERROR] ^ [ERROR] /Users/verhasp/github/License3j/src/main/java/com/verhas/licensor/License.java:196: warning: no description for @throws [ERROR] * @throws IOException [ERROR] ^ [ERROR] /Users/verhasp/github/License3j/src/main/java/com/verhas/licensor/License.java:246: warning: no description for @throws New JavaDoc Wants You DIR The errors are there because the java doc of License3j is a bit sloppy. Sorry guys, I created the code many years ago and honestly it is not only the java doc that could be improved. As a matter of fact one of the unit tests rely on network and the reachability of GitHub. (Not anymore though, I fixed that.) The new Java version 8 is very strict regarding to JavaDoc. As you can see on the “Enhancements in Javadoc, Java SE 8” page of ORACLE: The javadoc tool now has support for checking the content of javadoc comments for issues that could lead to various problems, such as invalid HTML or accessibility issues, in the files that are generated by javadoc. The feature is enabled by default, and can also be controlled by the new -Xdoclint option. For more details, see the output from running “javadoc -X”. This feature is also available in javac, although it is not enabled by default there. To get the release working I had the choice to fix the JavaDoc or to use the configuration org.apache.maven.plugins maven-javadoc-plugin 2.9 attach-javadocs jar -Xdoclint:none in pom.xml. (Source is stackoverflow.) But You Just Won’t DIR You can easily imagine that you will opt for the second option when you are under time pressure. You fix the issue modifying your pom.xml or other build configuration and forget about it. But you keep on thinking about why it is the way like that? Why is the new tool strict by default? Is it a good choice? Will it drive people to create better JavaDoc? (Just for now I assume that the aim of the new behavior was to drive programmers to create better JavaDoc documentation and not simply to annoy us.) I am a bit suspicious that this alone will be sufficient to improve documentation. Programmers will: Switch off the lint option. Delete JavaDoc from the source. Write some description that Java 8 will accept but is generally meaningless. or some of them will just write correct java doc. Some of them who were writing it well anyway and will be helped by the new strictness. How many of us? 1% or 2%? The others will just see it as a whip and try to avoid. We would need carrot instead. Hey, bunnies! Where is the carrot?
June 1, 2015
· 5,563 Views
article thumbnail
Break Single Responsibility Principle
Single Responsibility Principle (SRP) is not absolute. It exists to help the code maintainability and readability. But from time to time you may see solutions, patterns that break the SRP and are kind of OK. This is also true for other principles, but this time I would like to talk about SRP. Singleton breaks SRP The oldest and simplest pattern that breaks SRP is the singleton pattern. This pattern restricts the creation of an object so that there is a single instance of a certain class. Many thinks that singleton actually is an antipattern and I also tend to believe that this is better to use some container to manage the lifecycle of the objects than hard coding singletons or other home made factories. The anti pattern-ness of singleton generally comes from the fact that it breaks the SRP. A singleton has two responsibilities: Manage the creation of the instance of the class Do something that is the original responsibility of the class You can easily create a singleton that does not violate SRP keeping the first responsibility and drop the second one public class Singleton { private static final Singleton instance = new Singleton(); public static Singleton getInstance() { return instance; } private Singleton() {} } but there is not much use of such a beast. Singletons are simple and discussed more than enough in blogs. Let me look at something more complex that breaks SRP. Mockito breaks SRP Mockito is a mocking framework, which we usually use in unit tests. I assume that you are familiar with mocking and mockito. A typical test looks like the following: import static org.mockito.Mockito.*; List mockedList = mock(List.class); when(mockedList.get(0)).thenReturn("first"); System.out.println(mockedList.get(0)); mockedList.add("one"); mockedList.clear(); verify(mockedList).add("one"); verify(mockedList).clear(); (sample is taken from the Mockito page, actually mixing two examples). The mock object is created using the static call 1 List mockedList = mock(List.class); and after it is used for three different things: Setup the mock object for its mocking task. Behave as a mock mocking the real life object during testing. Help verification of the mock usage. The call when(mockedList.get(0)).thenReturn("first"); sets up the mock object. The calls System.out.println(mockedList.get(0)); mockedList.add("one"); mockedList.clear(); use the core responsibility of the mock object and finally the lines verify(mockedList).add("one"); verify(mockedList).clear(); act as verification. These are three different tasks not one. I get the point that they are closely related to each other. You can even say that they are just three aspects of a single responsibility. One could argue that verification only uses the mock object as a parameter and it is not the functionality of the mock object. The fact is that the mock object keeps track of its mock usage and acts actively in the verification process behind the scenes. Okay, okay: these all may be true, more or less. The real question is: does it matter? So what? Does the readability of the code of Mockito suffer from treating the SRP this way? Does the usability of the API of Mockito suffer from this? The answer is definite NO for both of the questions. The code is as readable as it gets (imho it is more readable than many other open source projects) but it is not affected by the fact that the mock objects have multiple responsibilities. As for the API you can even say more. It is readable and usable even more with this approach. Former mocking frameworks used strings to specify the method calls like mailer.expects(once()).method("send"); warehouse.expects(once()).method("hasInventory") (fragment from the page), which is less readable and error prone. A typo in the name of the method is discovered test run time instead of compile time. What is the morale? Don’t be dogmatic. Care programming principles, since they are there to help you to write good code. I do not urge anyone to ignore them every day. On the other hand if you feel that some of the principles restrict you and your code would be better without it, do not hesitate to consider writing a code that breaks the principles. Discuss it with your peers (programming is a team work anyway) and come to a conclusion. The conclusion will be that you were wrong considering to break SRP in 90% of the cases. In 10%, however, you may come up with brilliant ideas.
May 8, 2015
· 6,798 Views

Comments

Hidden Classes in Java 15

Feb 12, 2023 · Peter Verhas

After DZone published it, I cannot change the title.

I do not see why your suggested title would be better than the original title.


Split a File as a Stream

Nov 04, 2020 · Mike Gates

Your comment sounds like a correction to the article, but I do not see what you want to correct. On the contrary: there is a sentence that says exactly just like what you have said. Could you be more specific about what you want to correct?

Why Builder Is Often an Antipattern and How to Replace it With Fluent Builder

Jul 31, 2020 · Sergiy Yevtushenko

"may fire employe using it"

It is a little harsh. I hope this is more an exaggeration and you are not serious. I rather approach any employee who is using a construct with questions about the why and then perhaps mentoring. Perhaps some of them may tell you that they know something that you do not and that may change your opinion.

You are absolutely correct that this is more boilerplate code. Boilerplate code must not be written by the developers, but sometimes we cannot avoid it. That is when the code generators come in place. Java::Geci (https://github.com/verhas/javageci) is framework to write code generators in a very simple way that can in many cases replace the manual maintenance of boilerplate code with code generation. As a matter of fact the framework has a sample code generator, fluent, which does generate the boilerplate code for fluent builders from a simple syntax description string. (See my comment about it above.) You may like this solution.

Why Builder Is Often an Antipattern and How to Replace it With Fluent Builder

Jul 31, 2020 · Sergiy Yevtushenko

There is a close to ideal solution. The fluent API that can be created can be modeled using FSA. An FSA can be described by a regular expression. In this case, not a conventional regex, where the alphabet contains the characters, but rather a regular expression over the set of methods as an alphabet.

The code generator "fluent" being part of the core code generators of the code generation framework Java::Geci (https://github.com/verhas/javageci) automatically converts the regular expression, which is a syntax description of the fluent API to the interfaces and inserts these interfaces into your class. If you modify the syntax it runs again and updates the generated part of your code.


This eliminates most of the burden of the maintenance of the fluent API. What you have to pay is that you have to include a new test dependency (Java::Geci generators run as unit tests and fail if they modified the code), fire up a new unit test configuring the generator and put a @Fluent(...) annotation on the class with the syntax in the place of the ...

Generating Setters and Getters Using Java::Geci

Feb 01, 2020 · Lindsay Burk

Extending Abstract Classes With Abstract Classes in Java

Jun 18, 2019 · Lindsay Burk

Many times composition is better than inheritance. In principle they are equivalent. As a matter of fact, Go implements inheritance through composition... kind of.

I would be more than delighted if you could look at the example issue and help me how to use composition instead of inheritance there. The simplified A, F, C example may be oversimplified for the sake of the example.

Generating Setters and Getters Using Java::Geci

Jun 07, 2019 · Lindsay Burk

"why another generator"

Not really. Java::Geci is not a source code generator. It is a source code generator framework, or source code generator support library, whichever you like more naming it. Creating setter and getter only would not be a big deal. It is only an example demonstrating how easy it is to code such a generator using Java::Geci.

"What is the support in Intellij idea"

The generated code goes into segments that are delimited by editor folds, which can be collapsed in the editor. It is supported by all IDEs not only IntelliJ.

"Isn't better to use let's say .. Kotlin?"
This is a far-reaching question and not really related to code generation. As Kotlin is a more modern and more progressive language there are many cases when you do not feel the urge to generate code, while the same problem shouts out for code generation in Java. This does not, however, mean that all problem can be solved easily and the best ways in Kotlin without code generation. All languages have their limitations. If the language is very rich and feature full then the limitation is that developers will not learn all the features and it will be hard to maintain the code of the others' who use a feature that I have not learned or just forgot already. That is why languages have limitations in their expressiveness and features. When a problem reaches that limit you will code repetitive code or you will use code generators. The latter, in my opinion, is the simplest using Java::Geci.

This article is one of a series that details how to create code generators and not how to use them. The example is a simple one but in the project, there are much more complex samples. I do not know any other code generator, for example, that automatically creates all the interfaces for a fluent API based on the FSA, regular expression like syntax definition. Not even Kotlin does that.

Comparing Golang with Java

Jun 02, 2019 · Dave Fecak

I would recommend not to get to such conclusion so fast.

Comparing Golang with Java

Mar 14, 2019 · Dave Fecak

I absolutely agree with you that it depends on the programmer. There are programmers whose code is a pain to read no matter what language the code is written.

26 Items for Dissecting Java Local Variable Type Inference (Var Type)

Jan 15, 2019 · Anghel Leonard

Item 18, anonymous classes: like with the tuples mentioned in the comments, the methods that are new and not overriding the abstract method can also be invoked. The type when inferred is not the type of the abstract class, which is extended, but the generated name of the anonymous class.

I see no reason to exploit this possibility, but it is to know.

Very comprehensive and simple article. I mean simple as a good thing. Articles should be simple so that I can understand them.

How to Change Private Static Final Fields [Snippet]

Nov 14, 2018 · Grzegorz Ziemoński

If you need to change the value of that field then do not declare it to be final. Full stop. If you need to change a final field then it is a code smell. Even if you want to do that to perform tests only.

Comparing Files in Java

Oct 14, 2018 · Mike Gates

At the source publication I updated the article (that I cannot do here):

UPDATE:
https://twitter.com/snazy pointed out that the part of the code

for (int pos = 0; pos < size; pos++) {
if (m1.get(pos) != m2.get(pos)) {
System.out.println("Files differ at position " + pos);
return;
}
}
can be replaced using the built-in ByteBuffer::mismatch method. The code is simpler, it does exactly what the example code is aiming and it is probably faster.

Using GitHub as a Maven Repository

May 21, 2018 · Anupam Gogoi

You can create a release offline and push to github to publish you OSS projects. Simpler than nexus service.

Java 9 Module Services

Jan 17, 2018 · Mike Gates

"In addition, if the service is not in the application module"

What does it mean that the service is not in the application module? There are two separate things: the interface that defines the behavior of the service and the implementation. You have to 'requires' the interface if it is defined in a different module and it has to be exported by that module.

If the service is the "interface" then the statement is correct. If it means the implementation then it is not. The fact that the documentation was misleading me, who has a lot of experience with Java indicates that either the verbiage of the statement above is not the best choice or I start to be senile.

Summing up: what you say is absolutely correct.

Cache vs. Session Store

Jan 05, 2018 · Ana Jones

Your article is detailed and describes the difference between the *usual* session store and *usual* caching. However when you hear the sentence "Our app runs fast because we cache all the session data" you can not be sure what cache they are talking about. (Btw: was it web session or javax.mail.Session? Possible, though not likely.)

As a summary: the article is good and outstanding compared to other DZONE articles. On the other hand, you could emphasize more that you are talking about one specific and most frequently used session store and cache implementation.

Split a File as a Stream

Jan 04, 2018 · Mike Gates

Yes, those are all valid concerns that somebody who want to use the functionality for professional purpose has to face.

Split a File as a Stream

Dec 12, 2017 · Mike Gates

Yes, you are absolutely right. I missed the point that the bytes in the file may represent unicode characters. Important point if someone would like to implement something similar for professional purposes. Thanks for finding the issue and that you wrote it here.

Split a File as a Stream

Dec 11, 2017 · Mike Gates

You probably cannot do that. A `CharSequence` has to implement the method `length()` and you do not know the length of the file, which is dynamically written, as it continually grows. On the other hand the length of a character sequence is supposed to be constant.

The specification of `CharSequence` does not explicitly states that as a requirement, but as I was experimenting with `splitAsStream` the length is queried many times and the outcome is not what you would expect if the length grows.

Split a File as a Stream

Dec 11, 2017 · Mike Gates

The article does not provide any example execution. If the developer can not create samples themselves then it is better that there is no execution sample.

Split a File as a Stream

Dec 01, 2017 · Mike Gates

javax0.wordpress.com

New RegEx Features in Java 9

Aug 21, 2017 · Mike Gates

Yep. Thanks for the note. I will fix that on the original article. DZONE does not provide fix/update facility and they do not seem to care.

Everything About Method Overloading vs. Method Overriding

Jun 19, 2017 · Mike Gates

I can see articles these days use method references like in Java 8:


`Mammal::speak`

Everything About Method Overloading vs. Method Overriding

Jun 15, 2017 · Mike Gates

That is why I said "unfortunate".

Everything About Method Overloading vs. Method Overriding

Jun 15, 2017 · Mike Gates

I feel I offended you, let me try to amend that. You seem to be a talented young professional and I appreciate that. I offer you the following deal:

I suggest that you write an article about equals and hashCode and before publishing it send me. I will review and send you my comments same scrutiny as I do when I review a document on professional purposes. Free of charge, no need to reference that I reviewed, whatsoever. My contact parameters are on the internet.

Everything About Method Overloading vs. Method Overriding

Jun 15, 2017 · Mike Gates

Thanks. It does not change the my opinion that this notation in this article targeting novice programmers is unfortunate.

Your personal comments are left unattended.

Everything About Method Overloading vs. Method Overriding

Jun 15, 2017 · Mike Gates

"So, for the compiler, this call is Mammal.speak()."


So, for the compiler, this call is invoking the speak() method defined in the Mammal class.


It is not a notation. Just write it down. I'd appreciate if you could give a specific example to the Java API where the static method call like notation is used to denote non-static method call. I have no time to search for it myself. Thank you.

Everything About Method Overloading vs. Method Overriding

Jun 15, 2017 · Mike Gates

You should never implement equals and hashCode unless it is a must. Their contract says that if you override one, then you should override the other as well. Even though it is permissable to override one and not the other in some exceptional cases when a developer knows the details of how the different JDK classes relying on these methods work, but I will not get into details here, because evidently it is too complex for most of the audience including the author.

Do not advocate wrong ideas.

P.S.: I assume you meant "real time" -> "real life" as for the projects.

Everything About Method Overloading vs. Method Overriding

Jun 14, 2017 · Mike Gates

In your text you use the notation "Mammal.speak()" to denote the method 'speak()' of the class Mammal. This is an unfortunate notatiion, because juniors, who are actually the main target of your article may confuse it with static method call, which it is not.

Everything About Method Overloading vs. Method Overriding

Jun 14, 2017 · Mike Gates

"For example, we always override equals, hashCode, and toString from the Object class."


No, we do not. For example your Mammal, Cat and Human classes do not.

Java 9 (Part 4): Trying Try-with-Resources: First Look

Apr 26, 2017 · Tomer Ben David

InputStream in = null; <- input, variable name is not consistent


The example in Step-3 is totally confusing. The variable names are not consistent. The list should be separated by comma or semicolon?

Please fix these, and then delete my comment as not relevant.

Building an Intuitive DSL in Java

Apr 26, 2017 · Clayton Long

Perhaps ask him? ;-)

He is a nice person.

Building an Intuitive DSL in Java

Apr 26, 2017 · Clayton Long

You should have a look at the article

https://blog.jooq.org/2012/01/05/the-java-fluent-api-designer-crash-course/

from Eder Lukas. It is also to mention

https://www.martinfowler.com/bliki/FluentInterface.html

about fluent interfaces. It may also be worth looking at the Annotation Processing Tool

https://github.com/verhas/fluflu


Why Should I Write Getters and Setters?

Apr 23, 2017 · Grzegorz Ziemoński

If you do not use setters and getters and you introduce them then the change in the declared API will cause an error during compile time. If you use setters and getters and do not change the calling sequence (a.k.a. the names of the method and the arguments remain the same) then the error manifests during run-time.


So... what is the advantage?

Why Should I Write Getters and Setters?

Apr 23, 2017 · Grzegorz Ziemoński

There is another disadvantage: you can change the behavior of the setter and the getter without imposing change on the code that uses them. The example usually is when we handle the null as a value in a special way. The default setter and getter does not handle the null in any way special, it is just a value as any other.

The documentation may say that null value should not be passed, but the real contract is, how the code works. Average developers do not read the documentation and tend to abuse available callable methods in any way they work rather than only in the limited way the API is defined (remember Sun's unsafe package).

Why Should I Write Getters and Setters?

Apr 23, 2017 · Grzegorz Ziemoński

Let's approach this question pragmatic, like Robinson Cruspe did: what are the advantages and what are the disadvangateges.


The disadvantages are evident. More code written automatically by the IDE means more code to read and understand during maintenance. It takes expensive seconds to recognize that there is nothing else there in the code, but the generated default setter and getter.


How to Change Private Static Final Fields [Snippet]

Apr 21, 2017 · Grzegorz Ziemoński

Be aware that java and jit are free to assume that ANSWER is 42 and generate code that does not use the field to access the value. In that case, the ANSWER field is 41 and the test fails.

The Hidden Contract Between equals and Comparable

Sep 19, 2016 · Shamik Mitra

Why do you say it is hidden, when this requirement is documented in the documentation of java.lang.Comparable ?

The Shotgun Surgery Code Smell

Sep 19, 2016 · Shamik Mitra

No! It is against SRP. What is isAccountUnderflow responsible for? Checking and throwing exception, or may be not. These are two different concerns to be implemented in separate methods.

It is nice of DZONE that it provides facilities for unior styling katas.

The Shotgun Surgery Code Smell

Sep 19, 2016 · Shamik Mitra

C'mon! The method

assertAccountIsNotUnderflow

will call

isAccountUnderflow

and throw exception if needed! Basic unior stuff!

Different Types of References in Java

Sep 01, 2016 · Shamik Mitra

Using soft, weak or phantom references to create a cache is a poor solution. The cache should retain elements that will be needed in the near future and this weakly correlates with the fact that there is or there is no strong reference to the cached object. As an example the object cached may be cloned by the cache using code and thus no reference to any cached object is retained, still the same value may be soon needed again. When you need to use some caching solution in your application do not implement it yourself. There are ready made opens source and commercial solutions.

Memory Leaks and Java Code

Aug 29, 2016 · Shamik Mitra

Yes, we can predict when they will be collected. When JVM GC algorithm thinks it has to collect them to avoid out of memory situation.

Since those objects are not reacahble the GC can collect them any time it deems and thus they CAN NOT cause out of memory.


Your comment confirms that you are not only writing sloppy but you have no idea about what you write. Better don't!

Memory Leaks and Java Code

Aug 29, 2016 · Shamik Mitra

Example 1: Autoboxing

There are syntax errors in it. One may be of copy paste when creating the article (publiclong without space), but I can not be lenient enough to believe that forgetting the loop variable initialization is also copy paste error.

Let's fix this error and modify the code the following way:


public class Adder { Long sum=0L; public long addIncremental(long l) { sum =sum+l; return sum; } public static void main(String[] args) { Adder adder = new Adder(); long freeMemory = Runtime.getRuntime().freeMemory(); for(long i=0 ;i<100000000;i++) { adder.addIncremental(i); long fm = Runtime.getRuntime().freeMemory(); if( fm < freeMemory ){ freeMemory = fm; System.out.println(""+i+". "+fm); } } }


You will see that after a while the free memory does not decrease. That is because GC starts and reclatims the autoboxed objects that were created. This is a wrong example even if we consider the your definition of memory leak.


Also note that I moved the declaration of the variable sum out of the method 'addIncremental' otherwise the code just does nothing. JIT realizes after a few execution that the return value is ignored and that the method has no side effects at all and converts the method invocation to nooperation.


Please consider the review of the other examples and thrive for quality.

Memory Leaks and Java Code

Aug 29, 2016 · Shamik Mitra

Memory leak is the repetitive allocation of memory without consequential release of it when no longer used, leading to the consumption of ever increasing memory limited by external measures not controlled by the program possibly rendering the execution to a degraded state.


None of the examples in this article meet this definition. The article lists the typical mistakes that *may* lead to memory leak but then fails to present good examples. Some of the examples do not even run.

Best Practices for Code Documentation in Java

Aug 29, 2016 · Damian Wolf

I recommend the use of JUnit as a source of documentation. Unit test is a living code and if you change the code in a way that requires change in the use of the code your unit tests will get updated or fail. Unit tests are more important than JavaDoc.

I dare say that the most understandable interfaces use names and identifiers that do not need JavaDoc at all.

Understanding sun.misc.Unsafe

Aug 22, 2016 · Alec Noller

Java 9

Understanding sun.misc.Unsafe

Aug 21, 2016 · Alec Noller

Not any more.

What's Wrong With Object-Oriented Programming?

Aug 18, 2016 · Dave Fecak

There is also a Flat Earth Society. So what?

Pointers in Java

Aug 08, 2016 · John Vester

Okay. Next time I read the docs and try to be dumb. I will follow you by example. Not reference.

Visualizing Concurrency Patterns in Go

Jul 02, 2016 · Duncan Brown

Why can not DZONE fight these nasty virus spreading spams?

Visualizing Concurrency Patterns in Go

Jul 01, 2016 · Duncan Brown

This is really a great article. I love it!

I could not find the declaration of channel receivers being maintained in a FIFO. I understand that the go runtime as it is at the moment in the one and only implementation is done that way (after all you referenced the source code of the runtime implentation of channel, and it is easy to follow) but I could not find any mention of that or any guarantee in the language specification.


I just wondering if anyone creates a code that depends on this behaviour could get into trouble later.

Comparing Golang with Java

May 11, 2016 · Dave Fecak

I compared two languages of which I know well only one. I don't dare to compare two programming languages I know none of. You can find other authors on DZone who will happily do that.

Protect Your Immutable Object Invariants in More Complex Java Objects

Apr 19, 2016 · Per-Åke Minborg

Not to mention that reflection by default does not allow you to change a final field. To do that you need a reflective object to the Field object to set it 'not-final' and make it usable to "alter" the final field. Brrrr....

Protect Your Immutable Object Invariants in More Complex Java Objects

Apr 19, 2016 · Per-Åke Minborg

"Now is a good time to mention that your objects can, in theory, be modified anyhow, for example using Java Reflection. However, this is considered "cheating" ..."

It is not only cheating. It is something that is totally unreliable. In case of compile time calculated finals the Java compiler may just use the compile time calculated constants. In case of multi-thread code the compiler may opt not to ever copy a processor cache changed value to the main memory or to any other processor cache so the change may not ever be seen by other processors.

Pointers in Java

Jan 26, 2016 · John Vester

Thank you for the kind words. I am afraid you mix C# and Java. It is not a surprise to me assuming you devoted as much attention to read the article as you did using grammar.

Pointers in Java

Jan 24, 2016 · John Vester

It may be worth reading the comments of the same article at the original source at javax0.wordpress.com

Pointers in Java

Jan 24, 2016 · John Vester

ORACLE documentation and also the cited stackoverflow pages clearly describe that the variables hold references. This actually is an implementation detail. When you call a method you pass the reference.

It may be only verbiage, but the difference and the fact that there is a real difference between the two different parameter passing approach will be very important when (if ever) Java will introduce value types.

In case of value types the variable may hold the value itself or a reference to a value type record. This is an implementation detail. However when they are passed to a method the value will be copied (maybe a special new reference with copy on modify flag, which is an implemenation detail again) and not the reference. This is the case now with the primitives. They are simple built-in value types.

Pointers in Java

Jan 23, 2016 · John Vester

If Integer was not immutable you could easily swap too numbers. It would, of course, cause a lot of consequences in Java when 'int' values get autoboxed. This is one reason why the classes autoboxing primitive values are immutable.


Pointers in Java

Jan 23, 2016 · John Vester

The question you should answer first:

the variable

Integer i;

is a number or is a reference to an object that holds a number list? (Any object type could be used, Integer is only an example.)

My answer is that the variable holds a number. This way you actually _can_ implement swap(a,b) so long as long the objects contained in 'a' and in 'b' have the same type and are mutable.

More Compact Mockito with Java 8 and Lambda Expressions

Jul 17, 2015 · Marcin Zajączkowski

I had the idea for long time that argument captors are good candidate for lambda, and I hope we soon will have a version of Mockito where we have these features built in.

On the other hand using argument captor is usually sign of bad test. When you use argument captor you are eventually reimplementing a partial functionality of the mocked class. The more test data you replace with test code in your tests the more possible bugs you may inject into the testing code.

Why Android Studio Is Better For Android Developers Instead Of Eclipse

May 21, 2015 · Mehul Rajput

I do not know what software you use that you name Eclipse, but certainly it has nothing to do with the one I use and named Eclipse. I develop in an enterprise environment, 20 or more projects open, configured working set, application server started from Eclipse, maven integration, ant tasks and all the bells and legacy code you can imagine. I can work continually for days or weeks without restarting it.

You certainly miss it.

I do not say anything wrong about IntelliJ. It is a great software and if it fits your skills and prior experience then you should use that. However your bold and definitive statements, which are not inline with the majority of the experts of our industry makes me suspicious regarding your professionalism and the same of the company you work for.

Never Test Logging

Feb 28, 2014 · James Sugrue

Good point, agreed by the author of the article.

Never Test Logging

Feb 28, 2014 · James Sugrue

Good point, agreed by the author of the article.

Never Test Logging

Feb 28, 2014 · James Sugrue

Good point, agreed by the author of the article.

How to Estimate Memory Consumption

Feb 26, 2014 · Allen Coin

Fortunately Java is not only limited by phisical memory, thus it is kind of easy to measure performance with different -Xm* settings for different loads on the same test HW. Cheap memory price makes it cheap to test (for a short time) on a large instance just to see if that is really needed.

I like this article, especially pointing out that larger memory may result bigger GC time gaps, which is not widely known by developers. The usual mantra is "bigger is better". At least this is not allways true for memory.

Java Classloader - Handling Multiple Versions of The Same Class

Feb 24, 2014 · Uri Lukach

I am still interested in your use and reason of thread context class loader in the super constructor call. The documentation of the thread context class loader on the net is sparse and contradicting at times. Your use is actually not inline with what I know about the thread context class loader. Who sets in your use the thread context class loader?

It may well be the case that my knowledge is faulty since I never dared to write a class loader in production code.

Java Classloader - Handling Multiple Versions of The Same Class

Feb 24, 2014 · Uri Lukach

I am still interested in your use and reason of thread context class loader in the super constructor call. The documentation of the thread context class loader on the net is sparse and contradicting at times. Your use is actually not inline with what I know about the thread context class loader. Who sets in your use the thread context class loader?

It may well be the case that my knowledge is faulty since I never dared to write a class loader in production code.

Java Classloader - Handling Multiple Versions of The Same Class

Feb 24, 2014 · Uri Lukach

I am still interested in your use and reason of thread context class loader in the super constructor call. The documentation of the thread context class loader on the net is sparse and contradicting at times. Your use is actually not inline with what I know about the thread context class loader. Who sets in your use the thread context class loader?

It may well be the case that my knowledge is faulty since I never dared to write a class loader in production code.

Java Classloader - Handling Multiple Versions of The Same Class

Feb 24, 2014 · Uri Lukach

I am still interested in your use and reason of thread context class loader in the super constructor call. The documentation of the thread context class loader on the net is sparse and contradicting at times. Your use is actually not inline with what I know about the thread context class loader. Who sets in your use the thread context class loader?

It may well be the case that my knowledge is faulty since I never dared to write a class loader in production code.

Java Classloader - Handling Multiple Versions of The Same Class

Feb 24, 2014 · Uri Lukach

I am still interested in your use and reason of thread context class loader in the super constructor call. The documentation of the thread context class loader on the net is sparse and contradicting at times. Your use is actually not inline with what I know about the thread context class loader. Who sets in your use the thread context class loader?

It may well be the case that my knowledge is faulty since I never dared to write a class loader in production code.

Java Classloader - Handling Multiple Versions of The Same Class

Feb 24, 2014 · Uri Lukach

I am still interested in your use and reason of thread context class loader in the super constructor call. The documentation of the thread context class loader on the net is sparse and contradicting at times. Your use is actually not inline with what I know about the thread context class loader. Who sets in your use the thread context class loader?

It may well be the case that my knowledge is faulty since I never dared to write a class loader in production code.

Java Classloader - Handling Multiple Versions of The Same Class

Feb 24, 2014 · Uri Lukach

I am still interested in your use and reason of thread context class loader in the super constructor call. The documentation of the thread context class loader on the net is sparse and contradicting at times. Your use is actually not inline with what I know about the thread context class loader. Who sets in your use the thread context class loader?

It may well be the case that my knowledge is faulty since I never dared to write a class loader in production code.

Java Classloader - Handling Multiple Versions of The Same Class

Feb 24, 2014 · Uri Lukach

I am still interested in your use and reason of thread context class loader in the super constructor call. The documentation of the thread context class loader on the net is sparse and contradicting at times. Your use is actually not inline with what I know about the thread context class loader. Who sets in your use the thread context class loader?

It may well be the case that my knowledge is faulty since I never dared to write a class loader in production code.

Java Classloader - Handling Multiple Versions of The Same Class

Feb 24, 2014 · Uri Lukach

I am still interested in your use and reason of thread context class loader in the super constructor call. The documentation of the thread context class loader on the net is sparse and contradicting at times. Your use is actually not inline with what I know about the thread context class loader. Who sets in your use the thread context class loader?

It may well be the case that my knowledge is faulty since I never dared to write a class loader in production code.

Java Classloader - Handling Multiple Versions of The Same Class

Feb 24, 2014 · Uri Lukach

Never write a class loader. Possible exceptions may be to learn, experience and practice what class loader is and how it works in Java or if you are developing a framework. This, however, has to be the understanding of your customer, who is financing your effort.

In all other cases invent into research to find an appropriate framework, like OSGi, servlet environment or Spring, JEE.

Having said that I see no explanation or reason why you are using the thread context class loader in your code.

Programmers Without TDD Will be Unemployable by 2022

Jan 31, 2014 · Allen Coin

By 2022 any programmer who have never heard about TDD will easily get job so long as long they can program in COBOL.

You think this is a joke. Actually it is, but as many jokes, you can have a look at it from the other side and take it serious. COBOL can be just any legacy knowledge.

Why Static is Bad and How to Avoid It

Jul 16, 2013 · James Sugrue

Reading the comments many thoughts came up in my mind. Being a Java developer relatively inexperience in C++ I just learned recently that in C++ every function is "static" by default (they call no "static" method as virtual). If that was such a big problem it was not default by design. Or you can just say that this is also a proof of C++ being bad if you are a heated C++ hater. (Note that I use the word "static" in Java meaning in the above sentences, C++ uses static for different meaning.)

As for mocking: static methods can also be mocked, though this is a bit more difficult and needs mangling with the "function pointers" on a deeper level. PowerMock gives you tools for it. When you mock a method then the mock object is injected into the place of the original class and therefore you use the mocked method instead of the real one. In other words you replace the function reference along with the containing object. In case of static methods the containing object is the class itself. To mock that you need to replace the class. You can do that in the class loader mangling the JVM byte code in the class file. Tools do that for you.

Finally you come to the tricky part when you even want to mock classes that are loaded by the system class loader which you eventually (well, not eventually, but as a matter of fact) can not replace or pass by. But some trick may also help there and finally you can mock even the system class.

https://www.google.com/search?q=how+to+mock+the+system+class


User has been successfully modified

Failed to modify user

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: