Code Styling. What do you prefer?

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. :slight_smile:

The one on the right (brackets on the same line as the condition) is similarly known as the right way.

11 Likes

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.

1 Like

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();
1 Like

That disgusts me.

4 Likes

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.

1 Like

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 :wink:

Hereā€™s another style for you guys. A python programmer trying java:

18 Likes

@boformer Just moved the off topic posts over to this new topic!

@DarkArcana What is that madness. I canā€™t evenā€¦

2 Likes

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.

4 Likes

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)
The Banana Class
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) {}

What about for loops?
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. :slight_smile:

1 Like

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
    }
1 Like

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[]

2 Likes

ew. <ghjkl,kjhgfdsfghjk>

1 Like