So I decided to move from the old topic (post started to get off-topic) and to start off here is the style debated:
So I decided to move from the old topic (post started to get off-topic) and to start off here is the style debated:
I like the one on the left more, even though it doesnāt look neat(Curse u OCD)
There are two types of people. Programmers will know
Thereās many names for those two styles, but the one on the left (brackets on their own line) is correctly called the wrong way.
The one on the right (brackets on the same line as the condition) is similarly known as the right way.
It depends on the languageās standard convention.
The left side is completely normal for C++/C, whereas the right side is the normal for Java.
EDIT: And, of course, theyāre both perfectly valid to crossover. Just commenting on whatās normally done.
Just found this style on wikipedia:
while (x == y)
{ something();
somethingelse();
//...
if (x < 0)
{ printf("Negative");
negative(x);
}
else
{ printf("Non-negative");
nonnegative(x);
}
}
finalthing();
That disgusts me.
I kind of like that one.
How about these two?
//Single line block
if (x == y) return true;
//Multiline block
if (x == y) {
return true;
}
Personally, I use the first one where possible. Itās less lines and still remains neat looking.
How about ==
?
Although =
should compile if x and y are booleans, right?
I also prefer the short variant, but sometimes Iām using the second to highlight the importance.
I think we should create a new topic for code style discussion
Hereās another style for you guys. A python programmer trying java:
@boformer Just moved the off topic posts over to this new topic!
@DarkArcana What is that madness. I canāt evenā¦
The whitespace is allowed. Even newlines are allowed. A statement must terminate with a ā;ā to be syntactically correct. Blocks must terminate with the ending brace to be syntactically correct. As far as the compiler is concerned, everything there looks correct. It would just be a bit of a nightmare to maintain the semicolons and braces at the end of the line. However, the rest of the syntax follows pythonās indentation and format.
Next question: Do you prefer short names (Player p
) or lowercase class names (Player player
) for internal variables?
I prefer the long names.
It depends. If it is self explanatory like:
(Player player)
(CustomType customType) | (Banana banana)
public class Banana { String color; boolean peeled; int maturity;void mature (int amount) {
//Modify the maturity
}void peel () {
//Peel that banana!!!
}void eat () {
//Eat that lovely banna
}
}
Then I prefer the single letter var names.
(Player p)
(CustomType ct) | (Banana b)
It also depends how many arguments Iām having to work with. If it looks like this:
private void doSomething (String message, int length, String[] players) {}
Then I would not have them looking like:
private void doSomething (String msg, int l, String[] p) {}
When looping an Array which one would you do:String[] names = {āKodfodā, āBoformerā, āDarkArcanaā, āDotDashā};
for (String name : names) {
System.out.println(name);
}Or would you do:
String[] names = {āKodfodā, āBoformerā, āDarkArcanaā, āDotDashā};
for (int i = 0; i < names.length(); i++) {
System.out.println(names[i]);
}Personally I would do the first one.
If was defined ealier, I would prefer p as itās easier where as if it isnāt defined I rather have it be player.
Just my two cents (There isnāt a cent emoji? Needs to be added)
Of course the āfor eachā loop is shorter than the usual āforā loop, but sometimes it is necessary to know the index of the current element (e.g. to identify the first/last).
The one on the right (brackets on the same line as the condition) is similarly known as the right way.
Actually itās now affectionately called Egyptian Brackets (number 3)
By the way, there is a third (horrible) style Iāve seen used:
if (condition)
{
// code here indented at the same level as the braces
}
void doStuff(String[] args)
or void doStuff(String args[])
Iāve seen both used to solve the same problemā¦ I prefer the first.
Thereās also String[] args[]
ew. <ghjkl,kjhgfdsfghjk>