Author Topic: Programming style  (Read 391 times)

Online Mobius

  • Administrator
  • Distinguished King
  • *****
  • Posts: 9424
Programming style
« on: April 30, 2021, 08:20:32 PM »
When editing files, keep new code and changes consistent with the style in the files. For new files, it should conform to the style for that component.
Last, if there's a completely new component, anything that is reasonably broadly accepted is fine.

Java:
1. We use Allman style braces, where each brace begins on a new line. Single line statement block must be enclosed in braces.
2. We use tabs for spacing.
3. We use _lowerCamelCase starting with underscore (_) for class private fields, use final where possible.
4. We use lowerCamelCase with no underscores (_) for naming methods, method parameters and variables. Use final only for non parameter variables where possible.
5. Static read only field names are writen in capital letters and can use underscores.
6. Avoid more than one empty line at any time. For example, do not have two blank lines between members of a type.
7. Avoid extra free spaces. For example avoid if (someVar == 0)..., where the dots mark the extra free spaces.
8. Avoid the creation of objects by using primitives. (When you can, use int vs Integer, take in mind autoboxing etc...).
9. Avoid the use of constants, methods, interfaces for code that is not used more than once.
10. Do not declare variables in the same line (int a, b = 0).
11. Use switch when there are three or more cases to compare, otherwise use if.
12. Try to avoid using Stream since it may lead to significant overhead or memory leaks.
13. Avoid the use of NonNull annotations. A simple == null or != null will suffice.
14. The use of var modifier is prohibited in general, to achive (a) not hiding variable types (i.e. var stream = OpenStandardInput()) and (b) understandable code with a fast look.
15. Add author and date comment for the classes you create. Add your name to an existing class only after doing significant changes. For more that 70% content changes remove previous author(s).
16. For the most part, keep code Java 1.8 compatible.

Example code written in java.
Code: [Select]
/*
 * This file is part of the L2J Mobius project.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package ai.others;

import ai.AbstractNpcAI;

/**
 * @author Mobius
 */
public class TestClass extends AbstractNpcAI
{
private static final int STATIC_READ_ONLY_FIELD_1 = 11111;
private static final int STATIC_READ_ONLY_FIELD_2 = 22222;

private int _classPrivateField = 0;

public TestClass(int classPrivateField)
{
_classPrivateField = classPrivateField;
}

public void method(int value)
{
int methodField = value + 10;
if (value == 10)
{
_classPrivateField = 0;
}

switch (_classPrivateField)
{
case 0:
{
methodField += 5;
break;
}
case 10:
{
methodField += 10;
break;
}
case 50:
{
methodField += 15;
break;
}
}

_classPrivateField = methodField / 2;
}

public int getFieldPlusValue()
{
return STATIC_READ_ONLY_FIELD_1 + _classPrivateField;
}

public int getFieldMinusValue2()
{
return STATIC_READ_ONLY_FIELD_2 - _classPrivateField;
}

public boolean isMethod(int value)
{
return value == (STATIC_READ_ONLY_FIELD_1 + _classPrivateField);
}
}
As you see even gibberish code like the one above seems to be quite readable.
Using Eclipse automatically formats to the above programming style upon saving.


XML:
1. We use tabs for spacing (Pretty print with line breaks).
2. Try to use comments at the of the line (same line) when needed to describe an npc, item or skill id.
3. Do not add extra comments or author names.
4. Do not leave more than two empty lines.


HTML:
1. We use lowercase tags <html> in favor of <HTML>.
2. For text blocks we change line when <br> or <br1> tag is reached.
3. Do not leave empty lines.