Viktar Patotski Profile picture
Feb 28 6 tweets 2 min read
Learning #Java is a never-ending story for me: I just learned about " double-brace initialization", which creates an anonymous class derived from the specified class (the outer braces), and provides an initialiser block within that class (the inner braces). e.g. 👇
For example, this code:
List<Integer> l = new ArrayList<>();
l.add(1);
l.add(2);

May look like:
new ArrayList<Integer>() {{
add(1);
add(2);
}};
Or it may look like constructor with named parameters:

public class User {
String name;
String surname;
String email;
}

User user = new User() {{
name = "John";
surname = "Doe";
}};
Note that an effect of using this double brace initialisation is that you're creating anonymous inner classes. The created class has an implicit `this` pointer to the surrounding outer class.
Whilst not usually a problem, it can cause grief in some circumstances, e.g. when serialising or garbage collecting, and it's worth being aware of this.

While this is interesting to know, it's not recommended to use it:
jesperdj.com/2016/07/19/don…
If you find this interesting:

1. Follow me @xpvit for more Java, Cloud, and Linux knowledge.
2. RT the tweet below to share this thread with your audience

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Viktar Patotski

Viktar Patotski Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @xpvit

Mar 1
The Shirky Principle

Institutions will try to preserve the problem to which they are the solution.

-- Clay Shirky
👇
The Shirky Principle suggests that complex solutions - a company, an industry, or a technology - can become so focused on the problem that they are solving, that they can inadvertently perpetuate the problem itself.
This may be deliberate (a company striving to find new nuances to a problem which justify continued development of a solution), or inadvertent (being unable or unwilling to accept or build a solution which solves the problem completely or obviates it).
Read 4 tweets
Mar 1
⚠️#Spring tests tip

I wouldn't say I like @​Transactional spring tests and don't recommend using them. I prefer cleaning/populating db with @​Sql annotation. To do this I usually create 2 files:
📍resources/clear.sql
📍resources/insert_data.sql
👇
📍clear.sql usually contains:

TRUNCATE TABLE table1, table2 RESTART IDENTITY CASCADE;

This script deletes all data from DB but leaves a structure.

📍insert_data.sql

This script populates data by executing Inserts.
Then on test class:
@​Sql(scripts = "classpath:clear.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@​Sql(scripts = "classpath:insert_data.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)

DB is clean and always in the desired state before the test ☺️
Read 4 tweets
Feb 27
The Pareto Principle (The 80/20 Rule)

Most things in life are not distributed evenly.

The Pareto Principle suggests that in some cases, the majority of results come from a minority of inputs: 👇
📍80% of a certain piece of software can be written in 20% of the total allocated time (conversely, the hardest 20% of the code takes 80% of the time)
📍20% of the effort produces 80% of the result
📍20% of the work creates 80% of the revenue
📍20% of the bugs cause 80% of the crashes
📍20% of the features cause 80% of the usage
Read 5 tweets
Feb 26
The Dilbert Principle

Companies tend to systematically promote incompetent employees to management to get them out of the workflow.

-- Scott Adams

en.wikipedia.org/wiki/Dilbert_p…
A management concept developed by Scott Adams (creator of the Dilbert comic strip), the Dilbert Principle is inspired by The Peter Principle.
Under the Dilbert Principle, employees who were never competent are promoted to management in order to limit the damage they can do. Adams first explained the principle in a 1995 Wall Street Journal article, and expanded upon it in his 1996 business book, The Dilbert Principle.
Read 4 tweets
Feb 26
#Java performance comparison.

Today I decided to check the performance of different ways of converting an int variable to a String object in Java. 🧵👇
I searched on Stackoverflow:
📍Integer.toString(i)
📍"" +i
📍String.valueOf(i)
📍new StringBuilder().append(i).toString() - a bit strange way, but it was one of the suggestions😄

I have implemented 4 benchmarks using the JMH framework (see pictures).

Results 👇
✅Integer.toString(i) - 43.424 29.721 ns/op
✅"" +i - 27.870 ns/op
✅String.valueOf(i) - 28.371 ns/op
❌new StringBuilder().append(i).toString() - ns/op

Just don't use StringBuilder approach. Other approaches are for your taste. but I personally prefer: String.valurOf(i).
Read 4 tweets
Feb 25
The Dead Sea Effect

The more talented and effective IT engineers are the ones most likely to leave - to evaporate ... [those who tend to] remain behind [are] the 'residue' — the least talented and effective IT engineers. 🤓

--Bruce F. Webster
The "Dead Sea Effect" suggests that in any organisation, the skills/talent/efficacy of engineers is often inversely proportional to their time in the company.

Typically, highly skilled engineers find it easy to gain employment elsewhere and are the first to do so.
Engineers who have obsolete or weak skills will tend to remain with the company, as finding employment elsewhere is difficult.
Read 5 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(