[2018 oracle code one] Var with Style

Var with Style – Local Variable Type Inference with Java
Speaker: Stewart Marks
Oracle

#VarWithStyle

For more blog posts, see The Oracle Code One table of contents


Local variable type inference

  • “var” tells compiler to infer the type
  • Still a static type. Should compile same as if typed the class name
  • Type inference is not new. Lambdas did it in Java 8

Where can use

  • local variables
  • resource variables in try-with-resources
  • loop variable in for-each loop
  • [and lambadas in Java 11]
  • can’t use in places where API/written into class file/affects binary compatibility

How compiler determines type

  • Looks at initializer’s type
  • Can’t use when not info info
    • var customer = null (not allowed because ambiguous)
    • var fun = (int a, int b) -> a + b (not allowed because lambda needs type for context)

Other notes

  • Can reassign using normal assignment rules. (aka same or compatible type as to what var really is)

Benefits of var

  • Add features to language so can write better code
  • Using var in right way results in better code
  • Reduces verbosity/clutter/redundancy
  • Let’s variable name stand out which is more important
  • About readability; not amount of typing.

Local Variable Type Inference Style Guide

You can write code with or without var. See style guide to mitigate usage concerns – new idea to release style guide at same time as feature. There’s also a FAQ

Principles

  • P1 – Reading code is more important than writing code
  • P2 – Code should be clear from local reasoning – had considered inferring from context all over class. But then hard to figure out and reader has to type inference in his/her head
  • P3 – Code readability shouldn’t depend on IDEs
  • P4 – Explicit types are a tradeoff

Guidelines

  • G1 – Choose variable names that provide useful information
  • G2 – Minimize the scope of local variables
  • G3 – Consider var when the initializer provides sufficient information to the reader
  • G4 – Use var to break up chained or nested expressions with local variables
  • G5 – Don’t worry to much about “programming to the interface” with local variables
  • G6  – Take care when using var with diamond or generic methods.
  • G7 – Take care when using var with literals.

General

  • “you read the internet and it’s such a great source of conference talks”
  • var x = getSomething() – already terrible code because a bad method name
  • InputStream inputStream = socket.getInputStream() – mentioning input stream twice instead of three times seems ok. Covers G1 and G3. The variable name and context is sufficient
  • try with resources – often long code so hard to spot variables. Using var aligns them.
  • Long class names penalizes doubly. Using var lets you put in once.
  • Helps with generics. Have a very long type name
  • var list = new ArrayList,String>. Can’t use diamond operator or would have ArrayList<Object>. Want to avoid using var and diamond together. Note that the type is now ArrayList and not List. Ok because just using as local variable. The method return type would still be List.
  • Also be careful with methods returning generics. List.of() returns List<Object> when don’t pass any parameters.
  • Fun side effect – less imports because type names not all mentioned explicitly

My take: This was an awesome session. I like that he covered the background and style guide. I also REALLY liked the before/after examples to show when it is good to use var. Also, time flew in this session!

[2018 oracle code one] monday keynote

Monday keynote

For more blog posts, see The Oracle Code One table of contents


GitHub – Matthew McCullough

  • Project Skara – investigate alternative SCM and code review options – https://github.com/Project-Skara/jdk
  • Building a development community
    • Power of 1 – just do one thing
    • If everyone does, the global community makes progress
    • ”think small to accomplish more”
    • Java dominates
  • GitHub loves Java
    • Resque – redis
    • Webhooks  kestrel
    • Analytics – kafka
    • Hydro – all systems can be powered by same data structure – kafka – distributed log
    • Like Ruby but some downside. Each parallel process uses memory. Lack of strong typing, JVM dominates streaming ecosystem
  • Tools to support the effort
    • Looking to move some? All? JDK projects from Mercurial to Git
    • Showed a pull request, a comment and a static analysis check

Oracle (missed name)

Last year announced three commitments and met them

  • Provided closed source tech to Open JDK. Delivered Oracle JDK under Open JDK
  • Deliver enhancements and innovation more rapidly – 6 month release cadence
  • Enterprise long term support  $25/month/processor (or server) and $2.50/month/developer

Oracle (Mark Reinhold – Chief architect of Java platform)

  • Left behind slow release model [picture of elephant followed by cheetah for fast].
  • Worried about keeping up [picture of cat pounding keyboard]
  • Other languages release once or twice a year Java must to remain competetitive
  • Modules and internal APIs
    • Monoloithic. Removed some modules, but still large. Hard to clean up object graph. Originally needed Corba for hello world!
    • Can modularize existing app, but don’t have to. Might not be worth if for legacy app and that’s ok.
    • Three of high CVEs would have been prevented with modules. (That’s half of them.)
    • Prevents sun.* an .*internal.* packages. Treated as public because didn’t want to break. Modules prevent this problem by not exporting
    • JDK 9-11 will run internal APIs with warnings, but will no longer compile. Not sure when this will end, but will be Java 12 or later. In future release, will not run at al.
    • jdeps —jdk-internals my.jar – detects use of internal APIs. Even gives a suggested replacement where available
  • Finally started removing things from Java. Removed two modules in Java 11. Costly to maintain, source of several security vulnerabilities. Available elsewhere or irrelevant (CORBA). Also removed some APIs. Looked at use in the wild to confirm hardly anyone uses.
  • Deprecate for removal at least one release in advance and possibly more. This allows compier to warn you about it.
  • Release can contain anti-feature (feature removal)
  • Top misconceptions about new release model
    • Each release will be as disruptive – No. pace of innovation not changing. [agreed]
    • Non LTS releases are experimental – No.  its about support [agreed]
    • To remove an old feature, must be deprecated three years in advance. No. Just requires working build. [Mark said this last year. I don’t know anyone who built everything with Java 9/10]
    • Can ignore non-LTS releases if don’t migrate often. No. Should build each so ready for LTS [Mark said this last year. I don’t know anyone who built everything with Java 9/10]
  • I using Java 9+, update all your dependencies
  • Projects
    • Amber – right sizing language ceremony
      • Already have some type inference with generics and lambdas and var
      • Raw string literals in backslash. No need to backslash everything. Useful in regex!
      • This gives us multi line strings!
      • Java 12 preview feature – 99% done. Trying to get feedback. So need to turn on in order to use. But will be in Java 13 presumably
      • Switch case. Implies detecting if missed a case [this looks cool bt don’t understand how it works. Also a preview feature.
      • record – implies POJO – getter/setter/equal/hashcode. Can @Override to customize. Can’t add instance fields.
      • Pattern matching – avoid casts. Works well with switches
    • Loom  – continuations and fibers
      • Fiber – assgned to a thread when actually run it. Less resource intensive and less complicated than Threads.
      • Continuations – behind the scenes for implementing fibers
    • Panama – Foreign functional/data interface
      • JNI is intentionally slow
      • [I didn’t pay much attention to this as it isn’t a feature I use now]
    • Vahalla  – value types and specialized generics

Nevermore than 6 months/3 years of support. No. Depends on what community does [RedHat already spoke up]

My take: Fun fact. There’s a wifi hotspot in a box under the chair in front of me. Also Mark was awesome! Mark did have some “speaker note” issues which he handled well. I’m really excited about Amber. And I like that Mark did a live demo for Loom. Even though it didn’t work 100%, that’s ok because it’s very early software! Funny how the keynote was Mark and “opening acts for Mark”!

[2018 oracle code one] Better Software, Faster: Principles of Continuous Delivery and DevOps

Better Software, Faster: Principles of Continuous Delivery and DevOps
Speaker: Bert Jan Schrijver

For more blog posts, see The Oracle Code One table of contents


Audience survey

  • Started with a survey on how long doing development. A lot of people 10-15 years. One guy over 30
  • Most people said they were doing CI. Many hands went down when asked if commit daily, if build fals and if fixed within ten minutes

Definitions

  • Continuous Integration – integrate frequently.
  • Continuous Delivery – build and test so can release at any time
  • Continuous Deployment – every change goes through build/test pipeline and automatically goes to prod
  • DevOps – dev and ops engineers working together; jointly responsible

Principles

  • Automate (almost) everything – faster and more repeatable
  • Keep everything in version control
  • If it hurts, do it more often. Bring the pain forward. Get better at it that way
  • Build quality in.
  • “Done” means actually released. It needs to be in a build/deployed
  • Entire team is responsible for delivery process
  • Continuous improvement

Ingredients of CD

  • Culture & Organization
    • agile
    • build the right thing/build the thing right
    • support what you build
    • cross functional teams
    • leave room to experiment/fail
    • Fun approach: biggest screw up of week got to park closest to door
  • Design & Architecture
    • Version control
    • Modularity
    • Branching strategy – don’t have long lived branches
    • Database changes
    • Design for failure
    • Feature toggles
  • Build & Deploy
    • Pipelines – automated sequence of stages to deliver software from version control to your users
    • Types of pipelines: build and deployment – try to roll forward, not back
  • Test & Verification
    • Need a testing strategy
    • Test automation – can’t keep testing manually
    • Non functional requirements – when requirement not met, it makes the system non- functional
    • Security testing
    • Performance testing
    • Verify expected business value is met
  • Information & Reporting
    • Static code analysis
    • Traceable pipelines
    • Automatic change logs
    • Usage metrics – actual data to determine if data is used
    • Dynamic dashboards – let users adapt to what need
    • Data driven decisions – act on metrics
    • Fix problems before users notice

Continuous Delivery vs DevOps

  • The term DevOps came first
  • DevOps is about freedom and responsibility. It is about having empathy. Other teams are neighbors, not blockers.
  • Lack of CD excuses – regulation, not building website, too much legacy code, people not smart enough
  • Actual reasons for lack of CD – culture, architecture

Pattern or antipattern?

  • Continuous delivery without devops – pattern
  • Uniform build pipelines – both. Easy to change, but limits flexibility
  • Long pipelines – anti-pattern. People won’t wait. Feedback slow.
  • Obsess over test automation – pattern
  • Logging and metrics for ops only – anti-pattern
  • Obsess over feedback loops – pattern
  • Manual steps in a delivery pipeline – both. Good to have automated
  • Long living branches – anti-pattern
  • Dev/prod parity – anti-pattern
  • Design for failure – pattern
  • Tests don’t provide business value – anti-pattern. They prevent issues. A working system is definitely is business value.
  • Parallelized pipelines – Pattern
  • Continuous Delivery = Continuous Deployment – anti-pattern. For most companies, deploying to prod every sprint/two weeks is fine.
  • Automate database changes – pipeline
  • Testing NFR’s in the build pipeline – pattern

My take: This was an excellent review. Bert started by saying this was an intro. On some level, he’s right. Although I think this would be a drinking from a firehose thing if completely new. I’m at the point where having the review was a good way of settling in my mind why we do certain things. I missed a couple of the patterns/anti-pattern q & a but they were really good