Groovy Powered Clean Code

58
GROOVY POWERED CLEAN CODE Noam Tenne

Transcript of Groovy Powered Clean Code

Page 1: Groovy Powered Clean Code

GROOVY POWERED CLEAN CODE

Noam Tenne

Page 2: Groovy Powered Clean Code

$WHOAMI

Developing on the JVM for the past 13 years

codefresh.io

Altogether standup guy

@NoamTenne

blog.10ne.org

Page 3: Groovy Powered Clean Code

CODEFRESH.IODocker oriented CI/CD that goes to 11

Page 4: Groovy Powered Clean Code

CODEFRESH.IOBuild your services as Docker images(You don’t even need a Dockerfile)

Page 5: Groovy Powered Clean Code

CODEFRESH.IOInstantly launch your services

(Solo or part of a composition)

Page 6: Groovy Powered Clean Code

“Writing clean code is what you must do in order to call yourself a

professional.”

–Robert C. Martin

http

://w

ww.

amaz

on.co

m/d

p/01

3235

0882

/ref=

cm_s

w_r

_tw

_dp_

GuZ

2wb1

ZS9G

XP

Page 7: Groovy Powered Clean Code

WHICH ONE IS YOUR CODE REVIEW?

Page 8: Groovy Powered Clean Code

IS THIS YOUR REFACTORING SESSION?

Page 9: Groovy Powered Clean Code

MEANINGFUL NAMES

def myItemArrayList = []

Page 10: Groovy Powered Clean Code

FUNCTIONS

breedDogAndPutItThroughAstronautTrainingAndSendItToSpace()

Page 11: Groovy Powered Clean Code

COMMENTS

//Construct object, obviouslydef object = new Obvious()

Page 12: Groovy Powered Clean Code

OBJECTS VS. DATA-STRUCTS

planetSmasher.getPlanetContext().planet = "Earth"planetSmasher.smash()

Page 13: Groovy Powered Clean Code

ERROR HANDLING

catch (Exception e) { //noop}

Page 14: Groovy Powered Clean Code

TESTS

//TODO: Write tests

Page 15: Groovy Powered Clean Code
Page 16: Groovy Powered Clean Code

HUMANS

Page 17: Groovy Powered Clean Code
Page 18: Groovy Powered Clean Code

<ENTER GR8BEER>

Page 19: Groovy Powered Clean Code

WHAT MAKES GROOVY CLEAN?

Page 20: Groovy Powered Clean Code

NATIVE JSON SUPPORT

Page 21: Groovy Powered Clean Code

ScriptEngineManager sem = new ScriptEngineManager();ScriptEngine jse = sem.getEngineByName("javascript"); String script = "Java.asJSONCompatible(" + json + ")"; Object rawJsonObject = javascriptEngine.eval(script);Map contents = (Map) rawJsonObject;

Page 22: Groovy Powered Clean Code

Nashorn?

Page 23: Groovy Powered Clean Code

import groovy.json.JsonSlurper

… def contents = new JsonSlurper().parse(json)

Page 24: Groovy Powered Clean Code

JSON/MARKUP BUILDERS

Page 25: Groovy Powered Clean Code

String html = "<html>";html += "<head>";html += "<title>Noam's guide to Copenhagen</title>";html += "</head>";...html += "</body>";html += "</html>";

Page 26: Groovy Powered Clean Code
Page 27: Groovy Powered Clean Code

new MarkupBuilder(it).html {head {

title("Noam's Guide To Copenhagen")}body {

...}

}

Page 28: Groovy Powered Clean Code

DocumentBuilderFactorydocFactory = DocumentBuilderFactory.newInstance();DocumentBuilder docBuilder =docFactory.newDocumentBuilder();

Document doc = docBuilder.newDocument();Element rootElement = doc.createElement("gah");doc.appendChild(rootElement);

Page 29: Groovy Powered Clean Code
Page 30: Groovy Powered Clean Code
Page 31: Groovy Powered Clean Code

def xml = new MarkupBuilder()xml.guide(type:"guide"){

title("Noam's Guide To Copenhagen")}

Page 32: Groovy Powered Clean Code
Page 33: Groovy Powered Clean Code

def json = new JsonBuilder()def root = json {

title "Noam's Guide To Copenhagen"}json.toString()

Page 34: Groovy Powered Clean Code

CHECKED EXCEPTIONS

Page 35: Groovy Powered Clean Code

private void readTheStream() {

URI.create("gr8conf.eu").toURL().openStream();

}

throws IOException

Page 36: Groovy Powered Clean Code

private void readTheStream() {

URI.create("gr8conf.eu").toURL().openStream();

}

Page 37: Groovy Powered Clean Code

WHAT MAKES GROOVY REALLY CLEAN?

Page 38: Groovy Powered Clean Code

import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader;… Reader isr = new InputStreamReader(it);

Page 39: Groovy Powered Clean Code
Page 40: Groovy Powered Clean Code

def isr = new InputStreamReader(it);

Page 41: Groovy Powered Clean Code
Page 42: Groovy Powered Clean Code

DEFAULT IMPORTS

import java.lang.*import java.util.*import java.io.*import java.net.*import groovy.lang.*import groovy.util.*import java.math.BigIntegerimport java.math.BigDecimal

Page 43: Groovy Powered Clean Code

import org.slf4j.*import groovy.util.logging.Slf4j@Slf4jclass GroovyDoge { public static void main(String[] args) { log.info "Wow! Much Groovy! So Log!" } }

Page 44: Groovy Powered Clean Code

AST TRANSFORMATION

1. Initialization2. Parsing3. Conversion4. Semantic Analysis 5. Canonicalization6. Instruction Selection7. Class Generation8. Output9. Finalization

Page 45: Groovy Powered Clean Code

List list = []assert list instanceof java.util.ArrayListlist.plus([])

Page 46: Groovy Powered Clean Code

REGISTERED META METHODS

POGO Metaclass

MetaclassRegistry

Page 47: Groovy Powered Clean Code

REGISTERED META METHODS

Page 48: Groovy Powered Clean Code

def fileContents = Paths.get('/path/to/file').text

Page 49: Groovy Powered Clean Code

EXTENSION MODULESclass MyExtension { public static String asString(Integer target) { ... }}

Page 50: Groovy Powered Clean Code

EXTENSION MODULESclass MyExtension { public static String asString(Integer target) { ... }}

Register the class with a descriptor in META-INF/services

Page 51: Groovy Powered Clean Code

EXTENSION MODULESclass MyExtension { public static String asString(Integer target) { ... }}

Register the class with a descriptor in META-INF/services

Page 52: Groovy Powered Clean Code

EXTENSION MODULESclass MyExtension { public static String asString(Integer target) { ... }}

Register the class with a descriptor in META-INF/services

1337.asString()

Page 53: Groovy Powered Clean Code

SCRIPTS

def message = "Much script! Wow! So code!"println message

Page 54: Groovy Powered Clean Code

SCRIPTSimport org.codehaus.groovy.runtime.InvokerHelperclass DogeScript extends Script { def run() { def message = "Much script! Wow! So code!" println message } static void main(String[] args) { InvokerHelper.runScript(DogeScript, args) }}

Page 55: Groovy Powered Clean Code

SCRIPTS

Page 56: Groovy Powered Clean Code

LANGUAGE

Page 57: Groovy Powered Clean Code

QUESTIONS?

Page 58: Groovy Powered Clean Code

TAK!