| 121 | |
| 122 | = Switch statements = |
| 123 | |
| 124 | Very often the use of switch statements can be considered code smell. |
| 125 | We should think of replacing the switch statement with polymorphism. |
| 126 | Thus, 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 | |
| 130 | Extremely large number of improper exception handlings appear in the code. |
| 131 | NEVER 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 | |
| 147 | Printing the stack trace is NOT exception handling,though it may be useful for debug. |
| 148 | The fact that the method continues running the same way both if there is an exception and if there isn't in fact means that |
| 149 | we are not interested in the result of the method in the 'try' clause. |
| 150 | If we write the exception handling like this there is no matter if it works ok, or not - it is all the same for us. |
| 151 | Indeed 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 | |
| 159 | Always rethrow the exception if You dont have information how to handle it. |
| 160 | Since PropertyVetoException is checked exception, it may be good to wrap it in a RuntimeException. |
| 161 | Consult other people and think of exception handling policy. |
| 162 | |
| 163 | Proper 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 | }}} |