What is the "proper" form in which to write Java? [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Personally, I write my Java like this:
class xyz {
...
}
OR
if (condition) {
...
}
etcetera...
In so many examples, I see code written like this:
class xyz
{
...
}
OR
if (condition)
{
...
}
etcetera...
To me, my way makes the most sense because not only does it take less lines to write, but (again, in my opinion) looks more proper and professional. I'd love to know people's opinions - and reasoning behind them - as to which form is better and why.

The first one with opening braces on the same line is almost the standard Java programming guideline for years.
The original Sun Java guideline, that was never updated since 1999.
http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-141270.html
Google Java guideline
https://google.github.io/styleguide/javaguide.html#s4.1-braces
Android style
https://source.android.com/source/code-style.html
On the opposite side, many open source platforms like Apache/maven advocate brace in new line

Related

Why java variables can only contain underscore and dollar sign [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I was wondering what is the reason behind this and why we cannot use any symbol than those two, I think I never use a variable with those symbols but it is worth to know the cause
The $ in variables is a historical convention that is commonly used in Linux systems (among other languages) to call variables. In a Linux system, if I set a variable, I call it with this key (i.e. x = 4, echo $x).
Underscores and Camel Case are 2 common ways to identify multi-word variables. If I want to declare the variable iLoveJava or I_Love_Java, both are easily readable.
Of course, all of this is a rationalization of stylistic choices made by Sun Microsystems when they made the language, but I think it's a fair interpretation ;). If you want more details on why the others aren't used in detail, the link posted by Sercan is excellent information as well.

Does method overloading make code more readable? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I have read on many blogs and website that method overloading make code more readable:
Method overloading increases the readability of the program.(Source)
where as official documentation say:
Overloaded methods should be used sparingly, as they can make code
much less readable.
In that case you have to do some observation to come to the conclusions
Which is better?
Case 1
System.out.print(“Hello”);
System.out.print(12);
OR
Case 2
System.out.printString(“Hello”);
System.out.printInt(12);
As seen Case 1 is more readable and can be remember easily in comparison to Case 2 . This above Case 1 print method is overloaded as seen in source . So thats why java creator dint implement Case 2.
Hence ill go with overloading is more readable and also if its not readable then java developers would have implemented Case 2.

Why java has no not function/ alternatives for same [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Ok so if you want to check if a list is not empty
we would need to do something like
if(! mylist.isEmpty())
this affects code readability, so how can we write the same thing in a readable way, calling out negation of condition check.
One of possibility is to have a static helper function like:
static boolean not(boolean condition) { return !condition;}
How bad is this idea? Are there other options in apache common or guava etc? Or any other way you have achieved this?
It is not a bad idea in itself and it will not affect anything.
However I think many people will disagree with you regarding the "unreadability" of the normal way of negating things using !.
if ( ! list.isEmpty() )
versus
if ( not( list.isEmpty() ) )
does not make much of a difference IMHO.

Java naming convention for identifiers that begin with a number [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have to deal with a domain object that's real name is 351K-Report. According to the Java naming convention its forbidden to use a number at the beginning of an identifier.
I don't want to fully spell out the number. And, I also think that it's a bad idea to place an underline in front of the number.
But what is the recommended alternative?
UPDATE
There are also other reports, like SpecReport, TopReport, LF10Report and so on. So I'm very doubtful that inverting parts of the noun changes the meaning of the whole project.
Maybe reverse it. For example:
report351K
That would be very bad..
Imagine this:
int 1d = 3;
double d = 1d * 2;
What would be d?
Alternatives:
Since variables that begins with _ usually indicates for class member, I would use report351K.
if you really want to do this then _351KReport but I don't think you should do this. try to make something meaningful of it and at the same time is convineient to Java

What's the proper way to format "if" statements in Java with multiline "ands" or "ors"? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Improve this question
Apparently "if", "and", and "or" are such generic search parameters that I can't find the answer on google for my life. Which of these is the correct format according to the Java standards?
Option 1:
if (condition1
&& condition2
&& condition3) ...
or Option 2:
if (condition1 &&
condition2 &&
condition3) ...
The Oracle/Sun guidelines ("Code Conventions for the Java TM Programming Language") tell us to break before an operator. And they give this example.
if ((condition1 && condition2)
|| (condition3 && condition4)
||!(condition5 && condition6)) {
doSomethingAboutIt();
}
Many companies that I've worked for adopt the Oracle/Sun guidelines as the standard for their own code.
Refer http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-136091.html#248
I believe the correct answer is <CTRL>+<SHIFT>+F in Eclipse :)
This is completely subjective. Coding standards vary from shop to shop. To steal from yshavit's comment: Do whatever you want unless you are working in a team environment, in which case you should follow the team standards.
I'm a fan of the second pattern.
This is a preference question. But usually you want to follow the style guidelines of either your company or whoever else is working on the codebase with you. As long as it's consistent, any style is fine.
Personally, I'd keep everything on the same line. (ever dealt with tabs vs. spaces?)
However, if you have enough conditions to fill multiple lines, you should probably break your problem into smaller pieces.

Categories