[sip-comm-dev] Some notes on programming style, complexity, et al


#1

Dear all,

during our restless efforts to remove most of the warning messages
I stumbled over a nice example of code that IMHO includes many things
that shall be avoided.

This short snippet reports 4 warnings (raw type warnings, needs
unchecked conversion warnings). The most puzzling part is its
complexity: in just a few lines of code it contains two "Runnable"s,
the invocation of two threads (one hidden) and an own class. On top
it uses the name "historyList" as its main variable name at various
levels. Spending a few more lines would make the whole code more
readable.

Regards.
Werner

The code is found in **.gui.main.chat.ChatPanel:

    /**
     * Loads history for the chat meta contact in a separate thread. Implements
     * the <tt>ChatPanel.loadHistory</tt> method.
     */
    public void loadHistory()
    {
        new Thread()
        {
            public void run()
            {
                // Load the history period, which initializes the
                // firstMessageTimestamp and the lastMessageTimeStamp variables.
                // Used to disable/enable history flash buttons in the chat
                // window tool bar.
                loadHistoryPeriod();

                // Load the last N=CHAT_HISTORY_SIZE messages from history.
                Collection historyList = chatSession.getHistory(
                    ConfigurationManager.getChatHistorySize());

                if(historyList.size() > 0) {
                    class ProcessHistory implements Runnable
                    {
                        Collection historyList;

                        ProcessHistory(Collection historyList)
                        {
                            this.historyList = historyList;
                        }

                        public void run()
                        {
                            processHistory(historyList, null);
                        }
                    }
                    SwingUtilities.invokeLater(new ProcessHistory(historyList));
                }
            }
        }.start();
    }

···

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@sip-communicator.dev.java.net
For additional commands, e-mail: dev-help@sip-communicator.dev.java.net