Changes between Version 3 and Version 4 of CODE_TASKS_REQUIREMENTS


Ignore:
Timestamp:
09/16/08 17:55:02 (17 years ago)
Author:
Tanya
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • CODE_TASKS_REQUIREMENTS

    v3 v4  
    119119        } 
    120120}}} 
     121 
     122= Switch statements = 
     123 
     124Very often the use of switch statements can be considered code smell. 
     125We should think of replacing the switch statement with polymorphism. 
     126Thus, this doesn't mean that switch statements are always code smell,but we should think whether we can avoid them before writing them. 
     127 
     128= Exception swallowing = 
     129 
     130Extremely large number of improper exception handlings appear in the code. 
     131NEVER swallow an exception!!! 
     132 
     133{{{ 
     134         public void internalFrameActivated(InternalFrameEvent e) { 
     135                //show title bar 
     136                FrameView.this.observable.getInvoker().setFocus(true); 
     137                 
     138                try { 
     139                        getParentView().getParentView().swingRepresentation().setSelected(true); 
     140                } catch (PropertyVetoException e1) { 
     141                        // TODO - fix exception handling 
     142                        e1.printStackTrace(); 
     143                } 
     144        } 
     145}}} 
     146 
     147Printing the stack trace is NOT exception handling,though it may be useful for debug. 
     148The fact that the method continues running the same way both if there is an exception and if there isn't in fact means that 
     149we are not interested in the result of the method in the 'try' clause. 
     150If we write the exception handling like this there is no matter if it works ok, or not - it is all the same for us. 
     151Indeed the example below is the same as if we write: 
     152 
     153{{{ 
     154         public void internalFrameActivated(InternalFrameEvent e) { 
     155                FrameView.this.observable.getInvoker().setFocus(true);   
     156        } 
     157}}} 
     158 
     159Always rethrow the exception if You dont have information how to handle it. 
     160Since PropertyVetoException is checked exception, it may be good to wrap it in a RuntimeException. 
     161Consult other people and think of exception handling policy. 
     162 
     163Proper exception handling, without change of the interface is: 
     164{{{ 
     165        public void internalFrameActivated(InternalFrameEvent e) { 
     166                //show title bar 
     167                FrameView.this.observable.getInvoker().setFocus(true); 
     168                try { 
     169                        getParentView().getParentView().swingRepresentation().setSelected(true); 
     170                } catch (PropertyVetoException e1) { 
     171                        throw new RuntimeException(e1); 
     172                } 
     173        } 
     174}}}