Clean Code “Error Handling” try { breakStuff(); doSomeDamage(); burnTheWorld(); } catch { makeItRight(); } finally { eitherWay(); } Mike Christianson http://codeaweso.me/

It’s your job. Do it.

Error handling isn't a curse, or a bother. Things can and do go wrong. Thus, error handling is an essential part of programming. And, since it involves code, it ought to be clean.

Keep it clean. Don’t obscure logic. Don’t hide the point of the code. Don’t duplicate. Don’t forget to refactor.

Try. Catch. Finally. try: things can go sideways; nothing has to worry about what came before catch: put things back into a consistent state finally: things to do, either way

Tabs or Spaces? Err… Checked or Unchecked? People do still debate this. Anyway…

Tabs or Spaces? Err… Checked or Unchecked? People do still debate this. Anyway…

Tabs or Spaces? Err… Checked or Unchecked? People do still debate this. Anyway…

Best Case •

Worst Case

• •

Checked

Unchecked

coupling

no coupling

desirable violation of encapsulation violates open/closed difficulty versioning

desirable violation of encapsulation

Tabs or Spaces? Err… Checked or Unchecked? People do still debate this. Anyway…

Best Case •

Worst Case

• •

Checked

Unchecked

coupling

no coupling

desirable violation of encapsulation violates open/closed difficulty versioning

desirable violation of encapsulation

Tabs or Spaces? Err… Checked or Unchecked? People do still debate this. Anyway…

Best Case •

Worst Case

• •

Checked

Unchecked

coupling

no coupling

desirable violation of encapsulation violates open/closed difficulty versioning

desirable violation of encapsulation

Don’t do this.

public int getStatus(String messengerID) { try { return messengerMap.get(messengerID).getStatus(); } catch(Exception e) { return 7; } }

Don’t do this. if (windSensorObject.getSensorAlarms() != null && windSensorObject.getSensorAlarms().isEmpty() == false) { // Is Alarm this.windGroupStatus = "ALARM"; } else if (windSensorObject.getSensorWarnings() != null && windSensorObject.getSensorWarnings().isEmpty() == false) { // Is Warning alarms.setIterateWindAlarm(); this.windGroupStatus = alarms.getWindStatusIsALARM(amcsAlarmMessageWaitTime); } else if (windAlgSensorObject.reading("ws_sensor_status") != null && windAlgSensorObject.reading("wd_sensor_status") != null) { ... } else { // Is not OK alarms.setIterateWindAlarm(); this.windGroupStatus = alarms.getWindStatusIsALARM(amcsAlarmMessageWaitTime); }

Don’t do this. try { ... } else if (connType.equalsIgnoreCase("SERIAL")) { if (outputStream != null) { outputStream.close(); this.outputStream = null; } if (inputStream != null) { inputStream.close(); this.inputStream = null; } if (serialPort != null) { serialPort.close(); this.serialPort = null; } //logger.info("Successfully closed serial port.",this.getClass().getName()); } isConnected = 0; this.physicalConnection = 0; } catch (IOException e) { //logger.error(new MessengerException(0,e,null,this.getClass().getName())); // TODO either remove logging completely, and comment out or comment out. //System.err.println("ERROR DISCONNECTING SOCKET:" + e.getMessage()); //e.printStackTrace(); isConnected = 2; }

Don’t do this. } else { logger.error(new MessengerException(0, null, "Message exceeded maximum character length. Not sent: " + data, null, this.getClass().getName())); } } else { logger.error(new MessengerException(0, "Serial Port not instanciated: " + data, null, this.getClass().getName())); this.physicalConnection = 0; } } catch (Exception e2) { logger.error(new MessengerException(0, e2, null, this.getClass().getName())); isConnected = 2; this.physicalConnection = 0; } } else { logger.error(new MessengerException(0, null, "Unrecognized. Cannot send data: “ + data, null, this.getClass().getName())); isConnected = 2; this.physicalConnection = 0; } } catch (Exception e) { logger.error(new MessengerException(0, e, null, this.getClass().getName())); isConnected = 2; this.physicalConnection = 0; }

Clean Code “Error Handling” - GitHub

It's your job. Do it. Error handling isn't a curse, or a bother. Things can and do go wrong. Thus, error handling is an essential part of programming. And, since it ...

5MB Sizes 1 Downloads 78 Views

Recommend Documents

How to make error handling less error-prone - GitHub
“Back to the Future™” is a trademark of Universal City Studios, Inc. and Amblin ... In the Apple OpenSSL “goto fail;” bug, the cleanup code was written incorrectly, ...

Expectify Rich Polymorphic Error Handling with llvm ... - GitHub
7 days ago - Composition. Expected T std::unique_ptr storage. T .. .... multiple times. - deduplicate according to __FILE__ and __LINE__ ...

Clean Code “Unit Tests” - GitHub
It's more what you'd call a "guideline" ... LinkedHashMap groups = ... assertEquals("+SHRA +FC FZDZ +TSRAGR", groups.get(WEATHER));.

Handling pedigrees - GitHub
A 3-column data.frame or matrix with the codes for each individual and its parents. • A family effect is easily translated into a pedigree: – use the family code as ...

Mining API Error-Handling Specifications from Source ...
file system failures, and slow system response. As a simple ... error handling, a send procedure, which sends the content of a file across the network as ... tection and recovery. ...... IEEE Standard for Information Technology - Portable Operating.

Code Library - GitHub
Sep 13, 2013 - Contents. 1 Data Structure. 1. 1.1 atlantis . .... 4.25 Second-best MST . ...... prime is good. 10 static const int MAXX=47111; // bigger than. √ c. 11.

error code 3030.pdf
Fusing thermistor open. The fusing ... Fusing thermistor out of its position because of. incorrect installation • Fusing ... Displaying error code 3030.pdf. Page 1 of 5.

A New Error Correction Code
communications (e.g., satellite communications, digital .... octal values have been shown in Table I (Iteration 2 and 3). Obviously, at the end of coding ... Figure 4. Error Resolution Table with hot-bits and the error-bit. TABLE III. COMPARISON OF T

robert martin clean code pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. robert martin ...

The Pirate Copyright Code - GitHub
law should be in the information age not the way it has to be to satisfy the last milleniums ... subsequent exploitation regulations for the case the economic rights expired with the author still ..... dicial proceedings or public security; c. use fo

101 Ruby Code Factoids - GitHub
You can add the current directory to your load path with: .... Apple.chew. # => "munch munch" def Apple.cut. "chop chop" end. Apple.cut ..... 61) Method#owner.

Old school code audit? - GitHub
Awesome week! * Hack anything you want. * Just boring code audit? No shit--->. * Telco sec shit are looks awesome. Step-1: Software defined radio-->listen to ...

Data-capable network prioritization with reject code handling
Mar 27, 2009 - System”, Non-Access Stratum functions related to Mobile Station. (MS) in idle mode, .... calls and/or sending and receiving data over a wireless com munication .... 3 portable e-mail devices). In particular, a GPRS/GSM-capable networ

Data-capable network prioritization with reject code handling
Mar 27, 2009 - has the capability to communicate with other computer sys tems on the .... tions, such as entering a text message for transmission over a.

MeqParm: Parameter Handling in the MeqTree System - GitHub
Astronomical Data Analysis Software and Systems XV. P78 ... nected ParmTable, the best solution should be determined from either a default funklet or via ...

man-136\electrolux-oven-error-code-f10.pdf
technology making your downloading and install Electrolux Oven Error Code F10 finished. Even. you don't want to read, you can directly shut guide soft file as well as open Electrolux Oven Error. Code F10 it later on. You can also quickly obtain the b

Open Source Code Serving Endangered Languages - GitHub
ten called low-resource, under-resourced, or minority lan- guages) ... Our list is updatable more ... favorites on other social media sites, and, generally, a good.

Marching Up and Down the Code - GitHub
CONTENTS. 0 Starting with Python's IDLE. 1. 0.0 Introduction . ..... Knowing how to program a computer is a great skill to have, even if you are not a ..... need to take the value of age from the present year, 2015, and then add on 100. ..... Draw a

HDL-BUS control and operate code - GitHub
Operate code. Function. Targets address. Additional data format(every 9 data) ..... x value of volume(79 small-----0 big). Return #Zz,ON,SRC1,VOL38.