You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shenyu.apache.org by XiaoYu <xi...@apache.org> on 2021/09/06 10:50:27 UTC

Discussion on shenyu code suggestions

Hi community
A project's code habits are very important,
especially for projects like ours that are entirely community-based.
So I'd like to invite everyone to come together and suggest better code to
make the shenyu project better.

If you have good suggestions you can reply directly to the email.
Or reply this issue : https://github.com/apache/incubator-shenyu/issues/2033

Thanks!

Re: Discussion on shenyu code suggestions

Posted by XiaoYu <xi...@apache.org>.
## General

* Use as many `lambda function` expressions as possible.

* Method parameters must be modified with `final`.

* Constants modified `static final` must be named in upper case.

* Methods should not have too many parameters.

* Clear unused classes and methods.

* Use linux line separators.

* Tab size is 4 and keep indents on empty lines.

* All code passage of Checkstyle:
https://github.com/apache/incubator-shenyu/blob/master/script/shenyu_checkstyle.xml



## Object

* Ues Optional transform Null.

> Optional.ofNullable(xxx).orElse(obj)

* Ues Objects Judgment Null Or NotNull.

> Objects.isNull(obj) OR Objects.nonNull(obj)

* Ues `Objects.equals` Judgment are they equal.

> Objects.equals(obj1, obj2)

* Creater objects outside of a for loop.

```java
Object object = null;
for () {
    object = new Object();
}
```

## Collection

* must indicate initial capacity to avoid recalculate capacity.

### List

* Use `LinkedList` when you need to add or delete elements, Else use
`ArrayList`.

* Ues `org.apache.commons.collections4.CollectionUtils` Judgment Is empty
Or Not empty.

> CollectionUtils.isEmpty(list) or CollectionUtils.isNotEmpty(data)

### Map

* Use `ConcurrenHashMap` when considering concurrency of threads, Else use
`HashMap`

* Iterate over map using the most efficient way Or use `lambda function`.

```java
Set<Map.Entry<String, String>> entrySet = map.entrySet();
Iterator<Map.Entry<String, String>> iter = entrySet.iterator();
while (iter.hasNext()) {
        Map.Entry<String, String> entry = iter.next();

}
```

## String

* Ues `org.apache.commons.lang3.StringUtils` Judgment Is empty Or Not empty.

> StringUtils.isEmpty(list) or StringUtils.isNotEmpty(data)

* `String.join` should be used when string concatenation occurs.

> String join(CharSequence delimiter, CharSequence... elements)


## Exception

*  Do not use try...catch in a loop, it should be on the outermost layer.

```
try {
  for () {
  }
} catch () {

}
```

* Do not use `printStackTrace()`.

* Please use custom exceptions or `ShenyuException`.


## Resource

* Please use `try with resource` to close resource.

Kunshuai Zhu <jo...@gmail.com> 于2021年9月10日周五 下午11:51写道:

> All the unit tests are suggested to be a final class, as it is not
> likely to extend them.
> ## Unit test class
> define unit test class as a final class
> ```java
> public final class AesUtilsTest {
>     ...
> }
> ```
>
> XiaoYu <xi...@apache.org> 于2021年9月6日周一 下午6:50写道:
> >
> > Hi community
> > A project's code habits are very important,
> > especially for projects like ours that are entirely community-based.
> > So I'd like to invite everyone to come together and suggest better code
> to
> > make the shenyu project better.
> >
> > If you have good suggestions you can reply directly to the email.
> > Or reply this issue :
> https://github.com/apache/incubator-shenyu/issues/2033
> >
> > Thanks!
>

Re: Discussion on shenyu code suggestions

Posted by Kunshuai Zhu <jo...@gmail.com>.
All the unit tests are suggested to be a final class, as it is not
likely to extend them.
## Unit test class
define unit test class as a final class
```java
public final class AesUtilsTest {
    ...
}
```

XiaoYu <xi...@apache.org> 于2021年9月6日周一 下午6:50写道:
>
> Hi community
> A project's code habits are very important,
> especially for projects like ours that are entirely community-based.
> So I'd like to invite everyone to come together and suggest better code to
> make the shenyu project better.
>
> If you have good suggestions you can reply directly to the email.
> Or reply this issue : https://github.com/apache/incubator-shenyu/issues/2033
>
> Thanks!