transitioning to scala — medium

21
8/7/14 Transitioning to Scala — Medium https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 Transitioning to Scala  

Upload: farhan-faruque

Post on 11-Oct-2015

15 views

Category:

Documents


0 download

DESCRIPTION

Scala

TRANSCRIPT

  • 8/7/14 Transitioning to Scala Medium

    https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 1/21

    Transitioning toScalaAdvice from a developer who helpedrebuild Walmart.ca with Scala and Play

    From late 2011 to early 2014 I was a lead developer at NurunToronto, a digital agency focused on e-commerce solutions.Facing a pipeline of new projects with nothing but Spring inone hand and Java in the other, I realized that it was time toquickly explore alternatives.

    Working at an agency puts things in perspective. Fucking upa contractual deadline because Java sucks isnt going toimpress clients. Using the same tools and techniques weused to build applications back in 2004 wont help us buildapplications in 2014. In 2004, restarting an app server to testa single line of code was perfectly acceptableWebSpheresaverage 120 second restart time was considered a good

  • 8/7/14 Transitioning to Scala Medium

    https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 2/21

    opportunity to grab a coffee while pondering the mysteriesof a 1,200 line XML configuration file. Today that kind ofwaste will put an agency or a developer out of business.

    Our applications are no longer simple forms sitting in frontof database tables, theyre invaluable tools that help us getshit done in our daily lives.

    Why the Typesafe stack?After implementing a past project with Ruby on Rails I knewwhat I didnt want; I didnt want to use another dynamicallytyped, interpreted language or another stateful webframework. I wanted a statically typed language thatcompiled to Java bytecode and came with a healthyecosystem of tools and a stateless web framework forscalability. I also knew that our corporate clients wouldrequire the peace of mind only possible with professionalsupport contracts through a reputable vendor.

    This left me with a few choices to explore, the most intuitivebeing the Typesafe stack. It had everything we needed.

    What started as a proof-of-concept micro e-commerce site tointernally demonstrate the simplicity of Scala code anddeveloper productivity of Play eventually gained traction andbecame the foundation of Walmart Canadas new e-

  • 8/7/14 Transitioning to Scala Medium

    https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 3/21

    commerce platform.

    Why is Scala perceived to becomplex?Scala is flexible. Flexibility comes at the price of simplicity,but in the right hands Scala is a very elegant language that ismuch more than just a better Java.

    The biggest challenges of transitioning to Scalaor anythingneware rarely technical. Talented programmers can learna new syntax, new concepts, and a new IDE. Rather, changeoften brings out the weaknesses in other areas like processand culture.

    tl;drits all about people.

    The rest of this article is not a Scala programming tutorial.There are already plenty of those, and Im not the mostqualified Scala developer in the world to teach you the latesttricks from the deepest pools of advanced Scala.

    What follows is a collection of advice geared towardsdevelopers, team leads, and managers who are thinking oftransitioning their teams to Scala. The advice is based on myown personal experience as a team lead on an enterprise

  • 8/7/14 Transitioning to Scala Medium

    https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 4/21

    Scala project.

    Advice for developers andmanagers thinking of makingthe switch to Scala

    1. Understand the language featuresEvery new Scala developer, team lead, and manager shouldread Martin Oderskys Scala levels guide.

    After a year and a half of full-time Scala development andone major enterprise Scala project under my belt, Im not

    Scala. Better than a hot cup of joe!

  • 8/7/14 Transitioning to Scala Medium

    https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 5/21

    afraid to rank myself as a level A2.5/L1.5 Scala developeraccording to Martins guide. Ive used techniques defined inA3/L3, but I simply havent had the need to use the majorityof them on the relatively straightforward task of buildingweb applications. Ive yet to use the cake pattern or learnabout higher-kinded types. This doesnt make me a badprogrammer or bring up any intense symptoms of impostorsyndrome, it just reflects that I have a limited amount oftime in my life and I need to focus on learning what will giveme the most bang for my buck. I also play drums, guitar, taketwo dance lessons per week, drink a lot of coffee, and go outon dates. Time is precious.

    On the Walmart.ca project, with the exception of combinatorparsers and folds, we stuck to techniques from the shallowend of the level guide. Even with shallow Scala weproduced a significantly better platform than thepredecessor it replaced. We didnt run into anyimplementation headaches. The code we wrote is infinitelymore maintainable than the code it replaced. It runssignificantly faster in production. It scaled perfectly fine onboth Black Friday and Boxing Day, something many Java-based e-commerce platforms cant claim.

    Isnt that what counts?

  • 8/7/14 Transitioning to Scala Medium

    https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 6/21

    So dont make the mistake of dismissing simple Scala aslesser Scala. Look at all the tricks you can master at levels A1and A2:

    Simple closures

    Collections with map, filter, etc

    Pattern matching

    Trait composition

    Recursion

    XML literals

    This is way more than Java with a few extra goodies. This is anew way to write expressive, maintainable software.

    Some of the techniques at A3 are still fairly easy to masterand pretty important for those using Akka or other parallelcomputing librariesbut not insurmountably hard orrequiring a degree in math to understand:

    Folds

    Streams and other lazy data structures

    Actors

    Combinator parsers

  • 8/7/14 Transitioning to Scala Medium

    https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 7/21

    A pleasant side effect of mastering these techniques is thatyou will become a better programmer in all of the languagesyou work with. Im a much better JavaScript developer nowthat Ive had a chance to really master closures and othertechniques that are a part of both Scala and JS.

    2. Take your timeA lot of Scala developers come from Java looking forimmediate gratification but dont fully appreciate that Scalais a completely different language. Everything new needspractice to master. Scala is no different.

    The good news is that A2/L1 Scala developers are fullyqualified to work on typical Scala applications while evenproviding mentorship for junior developers. Not every Scaladeveloper needs to understand advanced purely-functionaldata structures, type theory, or higher-kinded types.

    Remember that Scala is a programming language forprofessional developers building next-generationapplications. It will take time to learn and even more time tomaster.

    3. Dont be afraid to studyIf youre a Java developer, I strongly recommend exploringScala learning materials in the following order:

  • 8/7/14 Transitioning to Scala Medium

    https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 8/21

    Read Scala for the Impatient. Its a good starting point forimperative developers, especially if you do the exercises. Istill comb through my original answers and refer to themwhen I get stuck on something.

    Read Programming in Scala by Martin Odersky, LexSpoon, and Bill Venners. Its a more detailed read thanScala for the Impatient, so its nice to go through afterhaving a broader view of the language features.

    If possible, take Courseras Functional ProgrammingPrinciples in Scala course. Coursera uses Scalathemselves!

    Explore the Typesafe Activator templates. Onlinedocumentation and learning materials for Scala aresparse compared to other languages, but theres no betterway to learn than hacking on someone elses codeespecially if the code is written by talented developerslike James Ward.

    If possible, take Courseras Principles of ReactiveProgramming course. This is a great resource fordevelopers who would like to explore the use of Scala andAkka for processing large volumes of data.

    Attend Scala meet-ups and learn what other workingprogrammers are using Scala for!

    Read Functional Programming Patterns in Scala and

  • 8/7/14 Transitioning to Scala Medium

    https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 9/21

    Clojure. After seeing first hand how much more legiblemy functional style code looks compared to my oldimperative style code, Im thirsty to learn more evenmore functional languages. Haskell is a bit too much forme, but Ive begun teaching myself Clojure. My Scalacode is becoming leaner and meaner by the second.

    4. Take what you read online with a grainof saltIncredibly smart developers such as Tony Morris, the creatorof Scalaz, come from a different world than I do; the world ofHaskell, functional programming, and mathematics.

    He argues against function signatures such as:

    defreverse[A]:List[A]=>List[A]

    In favour of function signatures such as:

    defB):List[A]=>List[B]

    The next time you see a function named

  • 8/7/14 Transitioning to Scala Medium

    https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 10/21

    robust than those familiarto comprehend the code. What isits type? What are its algebraic properties? Are there

    parametric properties to exploit?

    Thats the beauty of Scala. Tony is right. I am right. Theresno wrong, theres simply a matter of personal and teampreference. I prefer reverse over

  • 8/7/14 Transitioning to Scala Medium

    https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 11/21

    doesnt get in the way. I love that defining

  • 8/7/14 Transitioning to Scala Medium

    https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 12/21

    are self-taught programmers, others hold doctorates. Someare trying to solve business problems on a tight budget,others are trying to push the boundaries of computing. Someare writing applications, others are writing libraries.

    Understanding and respecting everyone in the community isimportant to becoming a good developer. It may seem as if asimple question on StackOverflow leads to baffling repliesthat require years of category theory study to understand,but keep in mind that Scala is a new language and thecommunity is still finding its voice. As more non-academicprogrammers move into the world of Scala and answer morequestions, discussions may become a little more focused onthe application of concepts rather than the theory behindconcepts.

    If you ever become discouraged, look at the source code oflibraries such as Finagle by Twitter or Smoke by mDialog.Finagle and Smoke are excellent examples of practical Scalaimplementations used in production environments today.Not all Scala is over-the-top complex.

    7. Set realistic expectationsNew Scala developers coming from Java should accept thefact that they will not learn advanced, functional Scalaovernight. Nor is advanced functional Scala required to be

  • 8/7/14 Transitioning to Scala Medium

    https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 13/21

    successful with typical business application programming.

    New Scala developers coming from a functional backgroundwill have an easier time adjusting as Scala naturallypromotes functional style. However, experienced functionalprogrammers will have to tailor their style if working on ateam with mostly imperative programmers.

    Its all about team balance and making sure the code youwrite is maintainable by the people who will be responsiblefor it. The most elegant code in the world is useless if its notmaintainable.

    8. Pair programming and code reviews aremandatoryPair programming is almost essential to make sure Scalacode doesnt become too divergent from the average skill setand style of the team as a whole. The last thing you want iscode so advanced that mere mortals cant touch it, or asingle functional programmer going off on a tangent byrewriting perfectly working imperative code before the teamas a whole is ready for that leap. Experimentation isimportant, but thats what Git is for. Fork baby, fork.

  • 8/7/14 Transitioning to Scala Medium

    https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 14/21

    Because of the flexibility of Scala, its easy to slip over theedge of complexity and into unfamiliar territory asdevelopers start to unlock the expressive power of Scala andexplore new language features.

    Culture is always important on a development team, but itsextra important for a new Scala team to move together intandem when first learning.

    9. Keep it simpleScala is new, so people are still learning what languagefeatures to use and what language features to avoid. UntilDouglas Crockford masters Scala and writes Scala: The GoodParts, development teams are left to figure out the valueproposition of each language feature for themselves. Theresno right and no wrong, just trial and error. Dont be afraid toquestion what doesnt make sense.

    When the Reflection API was first introduced in Java, everyJava developer wanted to flex their intellectual prowess and

    Dont let this happen to your team.

  • 8/7/14 Transitioning to Scala Medium

    https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 15/21

    use reflection for everything. This lead to a portion of acodebase I was working on at the time to be a completemaintenance nightmare for no reason other than a singlerogue developer going postal and overusing a feature theydidnt understand. Its better to start off slowly and writeclean, simple Scala before dipping your toes into everyobscure language feature. Id rather work with cleanimperative style Scala than a mishmash of poorlyimplemented advanced techniques any day.

    Just like good music, good code is elegant and sparse. Goodfood is made with few high quality ingredients. Nobody

    Avoid the deep end of Scala until youre ready. Take your time.

  • 8/7/14 Transitioning to Scala Medium

    https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 16/21

    wants to eat a plate full of mush with every spice imaginable,so why write code like that? A confident level A1 Scaladeveloper will write easier to maintain code than someonearbitrarily writing level A3/L3 code before they understandwhat theyre doing and why theyre doing it.

    10. Call out ugly codeIts possible to write extremely ugly Scala, just like its alsopossible to write extremely ugly Java, extremely ugly Perl,and extremely ugly English.

    There is one major difference between ugly Java and uglyScala, however.

    Ugly Scala may be ugly in many different ways. It may be uglyimperative Scala, or it may be ugly functional Scala, or it maybe an ugly mishmash of both. It may be ugly in ways that youdont understand yet because its written in a style thats stillunfamiliar. Scala is new, so its not yet possible to instantlyspot anti-patterns like we can do in more mature languageslike Java. This can lead to a team of developers mistakingugly code as beautiful code and beautiful code as ugly code.Developers may begin to emulate ugly patterns as they learn,which leads to even uglier code in the future. The viciousfeedback loop begins.

  • 8/7/14 Transitioning to Scala Medium

    https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 17/21

    Its better to avoid this altogether rather than try to fix itlater.

    If you see a brilliant developer write ugly Scala, call it out.Ask questions. Dont assume that because youre new to alanguage you dont have anything to offer. The worst casescenario is that youre wrong and the code yourequestioning is elegant, but now youll understand why itselegant.

    11. Scala is only one part of the puzzleThe way web applications are developed is much differenttoday than it was a decade ago. Im currently working on aScala, Play, AngularJS, and MongoDB app. The bulk of mywork is client-side. In the past year Ive written more Angularthan Scala, which isnt a bad thing, its just reality.

    The beauty of Scala is that I can write clean, stable, wellperforming server-side code, while avoiding the uglyboilerplate of Java and the brittleness of a dynamicinterpreted language like Ruby. I can spend my time writingclient-side code with the confidence that the server-sidelogic I implemented with Scala is bulletproof and ready torock when I need it.

    As much as I would like to become the grand-master of all

  • 8/7/14 Transitioning to Scala Medium

    https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 18/21

    things Scala, Im a working programmer who needs to staysharp on a wide variety of technologies; HTML5, SASS,AngularJS, RequireJS, SQL, MongoDB, etc, etc, etc, etc.

    Even if you dont have time to master every aspect of a singlelanguage, Scala is a great technology to dive into. Reactiveprogramming will be the de-facto style of development overthe next decade and I believe Scala will be at the forefront ofthis paradigm shift. The performance gains and scalabilitybenefits of reactive applications are impossible to ignore.

    Coast-to-coast JSON is replacing bulky XML. REST ismurdering overly-complicated SOA patterns. Mobile isreplacing the desktop. Transforming massive amounts ofdata into actual knowledge is made possible with theperformance of Akka without investing in ridiculousamounts of hardware.

    Scala is only one part of the puzzle, but its at the heart ofmany other puzzle pieces that are coming together to createa new breed of applications.

    12. Learning Scala will make you a betterprogrammerIts rare that a corporate developer has to really stretchthemselves. Whens the last time you learned a totally

  • 8/7/14 Transitioning to Scala Medium

    https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 19/21

    different approach to software development?

    The last major shiftand the only major shift Ive witnessedin my careerwas the migration from procedural languagesto OO languages. I was lucky enough in 1998 to work on oneof the very first Java applications in a Canadian bank while Iinterned at CIBC. Most of the developers I worked with wereex-COBOL and C programmers who took a leap of faith totry something different. For all the heat Java gets today, atthe time Java was very practical when we had to deploy thickclients on both Windows and OS/2.

    Working alongside programmers with 20 or 30 years ofexperiencesome of whom actually programmed withpunch cardswas a valuable learning experience and mademe realize that I wouldnt stick with one style ofprogramming forever. As I learned Java I also had tomaintain JCL. I had to dig into old COBOL and 360Assembly code for months before coming back to Java. Itkept things in perspective. JCL and COBOL were not sexy,but they did the job they were designed to do. My time as anintern was spent exploring everything from Smalltalk toExcelExcel being my first experience with functionalprogramming!

    Scala catches a lot of unwarranted flack. Rather than stretch

  • 8/7/14 Transitioning to Scala Medium

    https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 20/21

    themselves, some developers with short attention spans lashout and run away to more familiar languages. Thats fine. Theproblem is that they leave a trail of online discouragement intheir wake, which is unfair to Scala as a language and makesinterested developers question the value of the language.

    If you read something discouraging about Scala, try to learnabout who wrote it. They may have different requirementsthan you do. Dont be easily swayed, anything challenging isbound to discourage some people. Explore the successstories of Scala, which are becoming more and morecommon.

    ConclusionScala is not only a technical investment, but also aninvestment in culture. The rewards are well worth theinvestment, and shouldnt you be doing this anyways?

    If youre working on a new project that needs to be scalable,reliable, and easy to maintain, or you simply want to stretchyourself as a programmer, Scala is definitely worth it. Withsome common sense its not as difficult a transition as itseems.

    Remember that Scala is ready for the enterpriseandalready in the enterprisenow!

  • 8/7/14 Transitioning to Scala Medium

    https://medium.com/@kvnwbbr/transitioning-to-scala-d1818f25b2b7 21/21

    WRI TTEN BY

    Kevin WebberIm a freelance Scala developer from Toronto, Canada. I do it all for thecookies. http://kevinwebber.ca