{"id":23799,"date":"2023-05-17T09:37:10","date_gmt":"2023-05-17T09:37:10","guid":{"rendered":"https:\/\/www.booksofall.com\/ja\/?post_type=product&#038;p=23799"},"modified":"2023-05-17T09:38:35","modified_gmt":"2023-05-17T09:38:35","slug":"effective-scala","status":"publish","type":"product","link":"https:\/\/www.booksofall.com\/ja\/effective-scala\/","title":{"rendered":"Effective Scala"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p><a href=\"http:\/\/www.scala-lang.org\/\">Scala <\/a>is one of the main application programming languages used at Twitter. Much of our infrastructure is written in Scala and <a href=\"http:\/\/github.com\/twitter\/\">we have several large libraries<\/a>\u00a0supporting our use. While highly effective, Scala is also a large language, and our experiences have taught us to practice great care in its application. What are its pitfalls? Which features do we embrace, which do we eschew? When do we employ \u201cpurely functional style\u201d, and when do we avoid it? In other words: what have we found to be an effective use of the language? This guide attempts to distill our experience into short essays, providing a set of\u00a0<em>best practices<\/em>. Our use of Scala is mainly for creating high volume services that form distributed systems \u2014 and our advice is thus biased \u2014 but most of the advice herein should translate naturally to other domains. This is not the law, but deviation should be well justified.<\/p>\n<p>Scala provides many tools that enable succinct expression. Less typing is less reading, and less reading is often faster reading, and thus brevity enhances clarity. However brevity is a blunt tool that can also deliver the opposite effect: After correctness, think always of the reader.<\/p>\n<p>Above all,\u00a0<em>program in Scala<\/em>. You are not writing <a href=\"https:\/\/www.java.com\/en\/\">Java<\/a>, nor <a href=\"https:\/\/www.haskell.org\/\">Haskell<\/a>, nor <a href=\"https:\/\/www.python.org\/\">Python<\/a>; a Scala program is unlike one written in any of these. In order to use the language effectively, you must phrase your problems in its terms. There\u2019s no use coercing a Java program into Scala, for it will be inferior in most ways to its original.<\/p>\n<p>This is not an introduction to Scala; we assume the reader is familiar with the language. Some resources for learning Scala are:<\/p>\n<ul>\n<li>Scala School<\/li>\n<li><a href=\"http:\/\/www.scala-lang.org\/node\/1305\">Learning Scala<\/a><\/li>\n<li><a href=\"http:\/\/matt.might.net\/articles\/learning-scala-in-small-bites\/\">Learning Scala in Small Bites<\/a><\/li>\n<\/ul>\n<p>This is a living document that will change to reflect our current \u201cbest practices,\u201d but its core ideas are unlikely to change: Always favor readability; write generic code but not at the expensive of clarity; take advantage of simple language features that afford great power but avoid the esoteric ones (especially in the type system). Above all, be always aware of the trade offs you make. A sophisticated language requires a complex implementation, and complexity begets complexity: of reasoning, of semantics, of interaction between features, and of the understanding of your collaborators. Thus complexity is the tax of sophistication \u2014 you must always ensure that its utility exceeds its cost.<\/p>\n<p>And have fun.<\/p>\n<h2>Formatting<\/h2>\n<p>The specifics of code\u00a0<em>formatting<\/em>\u00a0\u2014 so long as they are practical \u2014 are of little consequence. By definition style cannot be inherently good or bad and almost everybody differs in personal preference. However the\u00a0<em>consistent<\/em>\u00a0application of the same formatting rules will almost always enhance readability. A reader already familiar with a particular style does not have to grasp yet another set of local conventions, or decipher yet another corner of the language grammar.<\/p>\n<p>This is of particular importance to Scala, as its grammar has a high degree of overlap. One telling example is method invocation: Methods can be invoked with \u201c<code>.<\/code>\u201d, with whitespace, without parenthesis for nullary or unary methods, with parenthesis for these, and so on. Furthermore, the different styles of method invocations expose different ambiguities in its grammar! Surely the consistent application of a carefully chosen set of formatting rules will resolve a great deal of ambiguity for both man and machine.<\/p>\n<p><a id=\"Formatting\"><\/a>We adhere to the\u00a0<a href=\"http:\/\/docs.scala-lang.org\/style\/\">Scala style guide<\/a>\u00a0plus the following rules.<\/p>\n<h3>Whitespace<\/h3>\n<p>Indent by two spaces. Try to avoid lines greater than 100 columns in length. Use one blank line between method, class, and object definitions.<\/p>\n<h3>Naming<\/h3>\n<dl class=\"rules\">\n<dt>Use short names for small scopes<\/dt>\n<dd><code>i<\/code>s,\u00a0<code>j<\/code>s and\u00a0<code>k<\/code>s are all but expected in loops.<\/dd>\n<dt>Use longer names for larger scopes<\/dt>\n<dd>External APIs should have longer and explanatory names that confer meaning.\u00a0<code>Future.collect<\/code>\u00a0not\u00a0<code>Future.all<\/code>.<\/dd>\n<dt>Use common abbreviations but eschew esoteric ones<\/dt>\n<dd>Everyone knows\u00a0<code>ok<\/code>,\u00a0<code>err<\/code>\u00a0or\u00a0<code>defn<\/code>\u00a0whereas\u00a0<code>sfri<\/code>\u00a0is not so common.<\/dd>\n<dt>Don&#8217;t rebind names for different uses<\/dt>\n<dd>Use\u00a0<code>val<\/code>s<\/dd>\n<dt>Avoid using\u00a0<code>`<\/code>s to overload reserved names.<\/dt>\n<dd><code>typ<\/code>\u00a0instead of\u00a0<code>`type<\/code>`<\/dd>\n<dt>Use active names for operations with side effects<\/dt>\n<dd><code>user.activate()<\/code>\u00a0not\u00a0<code>user.setActive()<\/code><\/dd>\n<dt>Use descriptive names for methods that return values<\/dt>\n<dd><code>src.isDefined<\/code>\u00a0not\u00a0<code>src.defined<\/code><\/dd>\n<dt>Don&#8217;t prefix getters with\u00a0<code>get<\/code><\/dt>\n<dd>As per the previous rule, it&#8217;s redundant:\u00a0<code>site.count<\/code>\u00a0not\u00a0<code>site.getCount<\/code><\/dd>\n<dt>Don&#8217;t repeat names that are already encapsulated in package or object name<\/dt>\n<dd>Prefer:<\/p>\n<pre><code>object User {\r\n  def get(id: Int): Option[User]\r\n}<\/code><\/pre>\n<p>to<\/p>\n<pre><code>object User {\r\n  def getUser(id: Int): Option[User]\r\n}<\/code><\/pre>\n<p>They are redundant in use:\u00a0<code>User.getUser<\/code>\u00a0provides no more information than\u00a0<code>User.get<\/code>.<\/dd>\n<\/dl>\n<h3>Imports<\/h3>\n<dl class=\"rules\">\n<dt>Sort import lines alphabetically<\/dt>\n<dd>This makes it easy to examine visually, and is simple to automate.<\/dd>\n<dt>Use braces when importing several names from a package<\/dt>\n<dd><code>import com.twitter.concurrent.{Broker, Offer}<\/code><\/dd>\n<dt>Use wildcards when more than six names are imported<\/dt>\n<dd>e.g.:\u00a0<code>import com.twitter.concurrent._<\/code><br \/>\nDon&#8217;t apply this blindly: some packages export too many names<\/dd>\n<dt>When using collections, qualify names by importing\u00a0<code>scala.collection.immutable<\/code>\u00a0and\/or\u00a0<code>scala.collection.mutable<\/code><\/dt>\n<dd>Mutable and immutable collections have dual names. Qualifiying the names makes is obvious to the reader which variant is being used (e.g. &#8220;<code>immutable.Map<\/code>&#8220;)<\/dd>\n<dt>Do not use relative imports from other packages<\/dt>\n<dd>Avoid<\/p>\n<pre><code>import com.twitter\r\nimport concurrent<\/code><\/pre>\n<p>in favor of the unambiguous<\/p>\n<pre><code>import com.twitter.concurrent<\/code><\/pre>\n<\/dd>\n<dt>Put imports at the top of the file<\/dt>\n<dd>The reader can refer to all imports in one place.<\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p><iframe style=\"width: 100%; height: 750px; border: none;\" src=\"https:\/\/online.visual-paradigm.com\/share\/book\/effective-scala-1cw1oyqmpq?p=1\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n","protected":false},"featured_media":23803,"template":"","meta":{"_yoast_wpseo_title":"","_yoast_wpseo_metadesc":"Scala is a statically typed programming language that combines object-oriented and functional programming paradigms. Learn more about it in this book now!"},"product_brand":[],"product_cat":[388],"product_tag":[],"class_list":{"0":"post-23799","1":"product","2":"type-product","3":"status-publish","4":"has-post-thumbnail","6":"product_cat-scala","8":"first","9":"instock","10":"shipping-taxable","11":"product-type-simple"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Effective Scala - BooksOfAll Japanese<\/title>\n<meta name=\"description\" content=\"Scala is a statically typed programming language that combines object-oriented and functional programming paradigms. Learn more about it in this book now!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.booksofall.com\/ja\/effective-scala\/\" \/>\n<meta property=\"og:locale\" content=\"ja_JP\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Effective Scala - BooksOfAll Japanese\" \/>\n<meta property=\"og:description\" content=\"Scala is a statically typed programming language that combines object-oriented and functional programming paradigms. Learn more about it in this book now!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.booksofall.com\/ja\/effective-scala\/\" \/>\n<meta property=\"og:site_name\" content=\"BooksOfAll Japanese\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-17T09:38:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.booksofall.com\/ja\/wp-content\/uploads\/sites\/4\/2023\/05\/Effective-Scala.jpg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.booksofall.com\/ja\/wp-content\/uploads\/sites\/4\/2023\/05\/Effective-Scala.jpg\" \/>\n<meta name=\"twitter:label1\" content=\"\u63a8\u5b9a\u8aad\u307f\u53d6\u308a\u6642\u9593\" \/>\n\t<meta name=\"twitter:data1\" content=\"4\u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.booksofall.com\/ja\/effective-scala\/\",\"url\":\"https:\/\/www.booksofall.com\/ja\/effective-scala\/\",\"name\":\"Effective Scala - BooksOfAll Japanese\",\"isPartOf\":{\"@id\":\"https:\/\/www.booksofall.com\/ja\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.booksofall.com\/ja\/effective-scala\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.booksofall.com\/ja\/effective-scala\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.booksofall.com\/ja\/wp-content\/uploads\/sites\/4\/2023\/05\/Effective-Scala.jpg\",\"datePublished\":\"2023-05-17T09:37:10+00:00\",\"dateModified\":\"2023-05-17T09:38:35+00:00\",\"description\":\"Scala is a statically typed programming language that combines object-oriented and functional programming paradigms. Learn more about it in this book now!\",\"breadcrumb\":{\"@id\":\"https:\/\/www.booksofall.com\/ja\/effective-scala\/#breadcrumb\"},\"inLanguage\":\"ja\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.booksofall.com\/ja\/effective-scala\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"ja\",\"@id\":\"https:\/\/www.booksofall.com\/ja\/effective-scala\/#primaryimage\",\"url\":\"https:\/\/www.booksofall.com\/ja\/wp-content\/uploads\/sites\/4\/2023\/05\/Effective-Scala.jpg\",\"contentUrl\":\"https:\/\/www.booksofall.com\/ja\/wp-content\/uploads\/sites\/4\/2023\/05\/Effective-Scala.jpg\",\"width\":\"827\",\"height\":\"1169\",\"caption\":\"Effective Scala\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.booksofall.com\/ja\/effective-scala\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.booksofall.com\/ja\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Categories\",\"item\":\"https:\/\/www.booksofall.com\/ja\/categories\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Effective Scala\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.booksofall.com\/ja\/#website\",\"url\":\"https:\/\/www.booksofall.com\/ja\/\",\"name\":\"BooksOfAll Japanese\",\"description\":\"Biggest IT eBooks library and learning resources - Free eBooks for programming, computing, artificial intelligence and more.\",\"publisher\":{\"@id\":\"https:\/\/www.booksofall.com\/ja\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.booksofall.com\/ja\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ja\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.booksofall.com\/ja\/#organization\",\"name\":\"BooksOfAll Japanese\",\"url\":\"https:\/\/www.booksofall.com\/ja\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ja\",\"@id\":\"https:\/\/www.booksofall.com\/ja\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.booksofall.com\/ja\/wp-content\/uploads\/sites\/4\/2022\/06\/booksofall-logo-2.png\",\"contentUrl\":\"https:\/\/www.booksofall.com\/ja\/wp-content\/uploads\/sites\/4\/2022\/06\/booksofall-logo-2.png\",\"width\":166,\"height\":30,\"caption\":\"BooksOfAll Japanese\"},\"image\":{\"@id\":\"https:\/\/www.booksofall.com\/ja\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Effective Scala - BooksOfAll Japanese","description":"Scala is a statically typed programming language that combines object-oriented and functional programming paradigms. Learn more about it in this book now!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.booksofall.com\/ja\/effective-scala\/","og_locale":"ja_JP","og_type":"article","og_title":"Effective Scala - BooksOfAll Japanese","og_description":"Scala is a statically typed programming language that combines object-oriented and functional programming paradigms. Learn more about it in this book now!","og_url":"https:\/\/www.booksofall.com\/ja\/effective-scala\/","og_site_name":"BooksOfAll Japanese","article_modified_time":"2023-05-17T09:38:35+00:00","og_image":[{"url":"https:\/\/www.booksofall.com\/ja\/wp-content\/uploads\/sites\/4\/2023\/05\/Effective-Scala.jpg","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_image":"https:\/\/www.booksofall.com\/ja\/wp-content\/uploads\/sites\/4\/2023\/05\/Effective-Scala.jpg","twitter_misc":{"\u63a8\u5b9a\u8aad\u307f\u53d6\u308a\u6642\u9593":"4\u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.booksofall.com\/ja\/effective-scala\/","url":"https:\/\/www.booksofall.com\/ja\/effective-scala\/","name":"Effective Scala - BooksOfAll Japanese","isPartOf":{"@id":"https:\/\/www.booksofall.com\/ja\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.booksofall.com\/ja\/effective-scala\/#primaryimage"},"image":{"@id":"https:\/\/www.booksofall.com\/ja\/effective-scala\/#primaryimage"},"thumbnailUrl":"https:\/\/www.booksofall.com\/ja\/wp-content\/uploads\/sites\/4\/2023\/05\/Effective-Scala.jpg","datePublished":"2023-05-17T09:37:10+00:00","dateModified":"2023-05-17T09:38:35+00:00","description":"Scala is a statically typed programming language that combines object-oriented and functional programming paradigms. Learn more about it in this book now!","breadcrumb":{"@id":"https:\/\/www.booksofall.com\/ja\/effective-scala\/#breadcrumb"},"inLanguage":"ja","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.booksofall.com\/ja\/effective-scala\/"]}]},{"@type":"ImageObject","inLanguage":"ja","@id":"https:\/\/www.booksofall.com\/ja\/effective-scala\/#primaryimage","url":"https:\/\/www.booksofall.com\/ja\/wp-content\/uploads\/sites\/4\/2023\/05\/Effective-Scala.jpg","contentUrl":"https:\/\/www.booksofall.com\/ja\/wp-content\/uploads\/sites\/4\/2023\/05\/Effective-Scala.jpg","width":"827","height":"1169","caption":"Effective Scala"},{"@type":"BreadcrumbList","@id":"https:\/\/www.booksofall.com\/ja\/effective-scala\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.booksofall.com\/ja\/"},{"@type":"ListItem","position":2,"name":"Categories","item":"https:\/\/www.booksofall.com\/ja\/categories\/"},{"@type":"ListItem","position":3,"name":"Effective Scala"}]},{"@type":"WebSite","@id":"https:\/\/www.booksofall.com\/ja\/#website","url":"https:\/\/www.booksofall.com\/ja\/","name":"BooksOfAll Japanese","description":"Biggest IT eBooks library and learning resources - Free eBooks for programming, computing, artificial intelligence and more.","publisher":{"@id":"https:\/\/www.booksofall.com\/ja\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.booksofall.com\/ja\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ja"},{"@type":"Organization","@id":"https:\/\/www.booksofall.com\/ja\/#organization","name":"BooksOfAll Japanese","url":"https:\/\/www.booksofall.com\/ja\/","logo":{"@type":"ImageObject","inLanguage":"ja","@id":"https:\/\/www.booksofall.com\/ja\/#\/schema\/logo\/image\/","url":"https:\/\/www.booksofall.com\/ja\/wp-content\/uploads\/sites\/4\/2022\/06\/booksofall-logo-2.png","contentUrl":"https:\/\/www.booksofall.com\/ja\/wp-content\/uploads\/sites\/4\/2022\/06\/booksofall-logo-2.png","width":166,"height":30,"caption":"BooksOfAll Japanese"},"image":{"@id":"https:\/\/www.booksofall.com\/ja\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/www.booksofall.com\/ja\/wp-json\/wp\/v2\/product\/23799","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.booksofall.com\/ja\/wp-json\/wp\/v2\/product"}],"about":[{"href":"https:\/\/www.booksofall.com\/ja\/wp-json\/wp\/v2\/types\/product"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.booksofall.com\/ja\/wp-json\/wp\/v2\/media\/23803"}],"wp:attachment":[{"href":"https:\/\/www.booksofall.com\/ja\/wp-json\/wp\/v2\/media?parent=23799"}],"wp:term":[{"taxonomy":"product_brand","embeddable":true,"href":"https:\/\/www.booksofall.com\/ja\/wp-json\/wp\/v2\/product_brand?post=23799"},{"taxonomy":"product_cat","embeddable":true,"href":"https:\/\/www.booksofall.com\/ja\/wp-json\/wp\/v2\/product_cat?post=23799"},{"taxonomy":"product_tag","embeddable":true,"href":"https:\/\/www.booksofall.com\/ja\/wp-json\/wp\/v2\/product_tag?post=23799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}