Java Initial Assignment

In my previous years of development in C ++, I know that this convention has always initialized your variables with something.

I passed this convention with me in Java and really any other programming language that I use.

I use IntelliJ IDEA, and I am completely satisfied with this, and usually follow his programming instructions and warnings, however I received this warning:

public String getText(By by) { String text = null; // Variable `text` initializer `null` is redundant WebElement ele = findElement(by); highlightIfDemoMode(ele); String tagName = ele.getTagName(); if (tagName.equals("input") || tagName.equals("select")) // For field elements, the text is actually held in values, not the text. text = ele.getAttribute("value"); else text = ele.getText(); return text; } 

Now, obviously, in Java,

 String text; // is the *exact* same thing as.. String text = null; 

Given that IntelliJ is very good at the following programming conventions, should I ignore this convention so as not to initialize my variables like this in Java? I feel this is a lot cleaner, but if the Java conventions tell me that, I won't.

Can someone tell me if it is better to practice for initialization or just conclude that it is null

+5
source share
4 answers

One difference in compilers

 String text; text.toString(); 

Compilation error.

 String text = null; text.toString(); 

compiles, but is a runtime error.

Compilation errors are better than runtime errors. Without declaring your variable until your value is better than any, but sometimes impossible.

 int x; try(SQLRunner runner = new SQLRunner(connectionPool) { x = runner.getX(); } 

Necessary. However, consider making your variables final in this case. In any case, this is good practice, but I personally find it too verbose and not useful for declaring every local variable that will not change as final. This is an exception because int x; tells me that he will change later without telling me how much he will change.

 final int x; 
+4
source

According to Efficient Java , it is bad practice to declare and initialize local variables before using them for the first time. (And in my opinion, but I'm not very credible.)

The idea is to limit their capabilities as much as possible, which improves readability and stops accidental reuse.

+4
source

The following snippet is a Java compilation error:

  Integer integers[] = {}; Integer max; for (Integer i : integers) { if (max == null || i > max) max = i; } 

Error in Eclipse: "The local variable max may not be initialized."

So you should initialize max to zero.

I wonder what Intellij says ...

+1
source

The local variables of the method are not initialized by default, you are thinking about class fields. Despite this, I would completely destroy the local variable text ,

 public String getText(By by) { WebElement ele = findElement(by); highlightIfDemoMode(ele); String tagName = ele.getTagName(); if (tagName.equals("input") || tagName.equals("select")) { return ele.getAttribute("value"); } return ele.getText(); } 
0
source

Source: https://habr.com/ru/post/1201828/


All Articles