Developer Discussion – SmartThings Developer Best Practices

  1. Home
  2. /
  3. Developer Discussions
  4. /
  5. Developer Discussion – SmartThings Developer Best Practices
Best practice on blackboard

Developer Discussion – SmartThings Developer Best Practices

Posted in : Developer Discussions on by : Tim

Here’s a look back at the developer Discussion from July 13, 2016.

We talk about SmartThings development best practices and how to make your SmartThings development as successful as possible. Here is a preview of some of the practices we talk about.

Code should be readable

Code is executed by machines, but read by humans. Readability can be subjective, but there are some general guidelines that should be followed:

 

Don’t repeat yourself

Follow the DRY principle (don’t repeat yourself).

Don’t copy/paste code blocks – pull common code out into a shared utility method.

 

Methods should serve a single purpose

Methods should serve a single purpose, and be concise. If a method definition doesn’t fit on a standard computer screen, it’s way too big.

Look for opportunities to split out code into utility methods. For example, parsing a large HTTP response inline can bloat a method; instead, split out the parsing into a method that can then be called. This facilitates easier understanding of the code, and promotes better separation of concerns.

Comment appropriately

Comments can add clarity and context to code when used appropriately. When over-used, they clutter the code and provide no value.

There are some guidelines that should be followed:

  • In general, when the code is doing something out of the ordinary, a comment is appropriate
  • Device Handler custom commands and attributes should have a comment describing the purpose, parameters, and exception conditions (if applicable)
  • Non-trivial methods should be documented with comments describing what it does, its return type, exception conditions, and parameters. JavaDoc style comments can be used, though there is no tooling in place to generate documentation from the source.
  • Comments should add value – commenting every line of readable code simply clutters the code and is unnecessary

Checkout the recording of the call to see all the SmartThings development best practices.