[ 
https://issues.apache.org/jira/browse/SSHD-824?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16476951#comment-16476951
 ] 

Guillaume Nodet commented on SSHD-824:
--------------------------------------

I don't know about articles on this subject, but I looking in google, it's 
called "double brace initializer", though the version in SSHD is a bit more 
verbose because of all the checkstyle/pmd rules
  
https://blog.jooq.org/2014/12/08/dont-be-clever-the-double-curly-braces-anti-pattern/
 
  
https://blog.nishtahir.com/2015/09/27/why-you-shouldnt-use-the-double-brace-initializer/

The main problem I have is really the fact that it creates a new type for an 
instance.
As for making the code more readable / performant / easier to debug, it really 
needs to be compared to a good alternative. See the following options:
{code}
    public static final Map<String, SimpleImmutableEntry<SocketOption<?>, 
Object>> CONFIGURABLE_OPTIONS
                = GenericUtils.<String, SimpleImmutableEntry<SocketOption<?>, 
Object>>mapBuilder()
                    .put(FactoryManager.SOCKET_KEEPALIVE, new 
SimpleImmutableEntry<>(StandardSocketOptions.SO_KEEPALIVE, null))
                    .put(FactoryManager.SOCKET_LINGER, new 
SimpleImmutableEntry<>(StandardSocketOptions.SO_LINGER, null))
                    .put(FactoryManager.SOCKET_RCVBUF, new 
SimpleImmutableEntry<>(StandardSocketOptions.SO_RCVBUF, null))
                    .put(FactoryManager.SOCKET_REUSEADDR, new 
SimpleImmutableEntry<>(StandardSocketOptions.SO_REUSEADDR, 
DEFAULT_REUSE_ADDRESS))
                    .put(FactoryManager.SOCKET_SNDBUF, new 
SimpleImmutableEntry<>(StandardSocketOptions.SO_SNDBUF, null))
                    .put(FactoryManager.TCP_NODELAY, new 
SimpleImmutableEntry<>(StandardSocketOptions.TCP_NODELAY, null))
                    .immutable();

    public static final Map<String, SimpleImmutableEntry<SocketOption<?>, 
Object>> CONFIGURABLE_OPTIONS2;
    static {
        Map<String, SimpleImmutableEntry<SocketOption<?>, Object>> map = new 
LinkedHashMap<>();
        map.put(FactoryManager.SOCKET_KEEPALIVE, new 
SimpleImmutableEntry<>(StandardSocketOptions.SO_KEEPALIVE, null));
        map.put(FactoryManager.SOCKET_LINGER, new 
SimpleImmutableEntry<>(StandardSocketOptions.SO_LINGER, null));
        map.put(FactoryManager.SOCKET_RCVBUF, new 
SimpleImmutableEntry<>(StandardSocketOptions.SO_RCVBUF, null));
        map.put(FactoryManager.SOCKET_REUSEADDR, new 
SimpleImmutableEntry<>(StandardSocketOptions.SO_REUSEADDR, 
DEFAULT_REUSE_ADDRESS));
        map.put(FactoryManager.SOCKET_SNDBUF, new 
SimpleImmutableEntry<>(StandardSocketOptions.SO_SNDBUF, null));
        map.put(FactoryManager.TCP_NODELAY, new 
SimpleImmutableEntry<>(StandardSocketOptions.TCP_NODELAY, null));
        CONFIGURABLE_OPTIONS2 = Collections.unmodifiableMap(map);
    }

    public static final Map<String, SimpleImmutableEntry<SocketOption<?>, 
Object>> CONFIGURABLE_OPTIONS3 =
            Collections.unmodifiableMap(new LinkedHashMap<String, 
SimpleImmutableEntry<SocketOption<?>, Object>>() {
                // Not serializing it
                private static final long serialVersionUID = 1L;
                {
                    put(FactoryManager.SOCKET_KEEPALIVE, new 
SimpleImmutableEntry<>(StandardSocketOptions.SO_KEEPALIVE, null));
                    put(FactoryManager.SOCKET_LINGER, new 
SimpleImmutableEntry<>(StandardSocketOptions.SO_LINGER, null));
                    put(FactoryManager.SOCKET_RCVBUF, new 
SimpleImmutableEntry<>(StandardSocketOptions.SO_RCVBUF, null));
                    put(FactoryManager.SOCKET_REUSEADDR, new 
SimpleImmutableEntry<>(StandardSocketOptions.SO_REUSEADDR, 
DEFAULT_REUSE_ADDRESS));
                    put(FactoryManager.SOCKET_SNDBUF, new 
SimpleImmutableEntry<>(StandardSocketOptions.SO_SNDBUF, null));
                    put(FactoryManager.TCP_NODELAY, new 
SimpleImmutableEntry<>(StandardSocketOptions.TCP_NODELAY, null));
                }
            });
{code}

> Do not use anonymous inner classes to initialize collections
> ------------------------------------------------------------
>
>                 Key: SSHD-824
>                 URL: https://issues.apache.org/jira/browse/SSHD-824
>             Project: MINA SSHD
>          Issue Type: Task
>            Reporter: Guillaume Nodet
>            Priority: Major
>
> This is an abuse of the Object model.
> If needed, introduce a collection builder, especially for immutable 
> collections.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to