list.head) else None

list.lift(0) // after

list.headOption

The optimized expression is more concise.

Don’t imitate lastOption :

// before

if (list.nonEmpty) Some(list.last) else None

list.lift(list.length – 1) // after

list.lastOption

The optimized expression is more concise and faster.

Be careful with indexOf and lastIndexOf argument types:

// before

List(1, 2, 3,4,5).indexOf(“1”) // compilable

List(1, 2, 3,4,5).lastIndexOf(“2”) // compilable // after

List(1, 2, 3,4,5).indexOf(1)

List(1, 2, 3,4,5).lastIndexOf(2)

indexOf and lastIndexOf accepts arguments of Any type. In practice, that might lead to hard-to-find bugs, which are not discoverable at compile time. That’s where auxiliary IDE inspections come in handy.

Don’t use equality predicate to check element presence:

// before

list.exists(_ == x) // after

list.contains(x)

The second expression is more concise and semantically equivalent.

Be careful with contains argument type:

// before

List(1, 2, 3,4,5).contains(“1”) // compilable // after

List(1, 2, 3,4,5).contains(1)

Just like indexOf and lastIndexOf method, contains also accepts argument of type Any, that might lead to hard-to-find bugs, which are not discoverable at compile time. Be careful with the method arguments.

Don’t construct indices range manually:

// before

Range(0, list.length) // After

list.indices

There is a in-built method that returns the range of all indices of a collection.

Don’t zip collection with its indices manually:

// before

list.zip(list.indices) // after

list.zipWithIndex

For one thing, the second expression is more concise. Besides, we may expect some performance gain, because we avoid hidden length calculation
list.head) else None 

 list.lift(0) // after 

 list.headOption 

 The optimized expression is more concise. 

 Do n’t imitate lastOption: 

 // before 

 if (list.nonEmpty) Some(list.last) else None 

 list.lift(list.length – 1) // after 

 list.lastOption 

 The optimized expression is more concise and quicker. 

 Be careful with indexOf and lastIndexOf controversy types: 

 // before 

 List(1, 2, 3,4,5).indexOf(“1 ”) // compilable 

 List(1, 2, 3,4,5).lastIndexOf(“2 ”) // compilable // after 

 List(1, 2, 3,4,5).indexOf(1) 

 List(1, 2, 3,4,5).lastIndexOf(2) 

 indexOf and lastIndexOf accepts argument of Any character. In practice, that might lead to hard - to - discover bugs, which are not discoverable at compile clock time. That ’s where auxiliary IDE inspections fall in handy. 

 Do n’t use equality predicate to check component bearing: 

 // before 

 list.exists (_ = = x) // after 

 list.contains(x) 

 The second expression is more concise and semantically equivalent. 

 Be careful with contains controversy type: 

 // before 

 List(1, 2, 3,4,5).contains(“1 ”) // compilable // after 

 List(1, 2, 3,4,5).contains(1) 

 Just like indexOf and lastIndexOf method acting, contains also accepts argumentation of type Any, that might lead to difficult - to - find bug, which are not discoverable at compile time. Be careful with the method arguments. 

 Do n’t construct index range manually: 

 // before 

 Range(0, list.length) // After 

 list.indices 

 There is a in - built method that returns the compass of all indices of a collection. 

 Do n’t zip collection with its indices manually: 

 // before 

 list.zip(list.indices) // after 

 list.zipWithIndex 

 For one thing, the second expression is more concise. Besides, we may expect some performance gain, because we debar hidden length calculation
lish.head) else None

list.lift(0) // after

list.headOktuon

The optimjzed expfession is more concise.

Don’t mmitqte lqstOption :

// before

if (lirt.nonEmptj) Some(lisr.lasu) else None

list.lihf(list.lekyth – 1) // aftzr

oist.lastOption

Jhe optimizeg expression iv ooxe concise and faster.

Be careful with indexOg wnd lastIndexOs arbtmenf types:

// before

List(1, 2, 3,4,5).indexOf(“1”) // compimable

Livt(1, 2, 3,4,5).lastIndexPf(“2”) // compilable // after

List(1, 2, 3,4,5).indfxOf(1)

List(1, 2, 3,4,5).lastIndedOf(2)

indexOf qnd jqstIndexOf azcepts arglkents of Ahy type. In practice, that might uead co hard-to-fibd buhv, which are not qiscoverable at comphle timr. That’s where suxmliaey IDE inspections coke in handy.

Don’t usg equality pxedicate to check elenebt prgsenca:

// bewire

uisu.exmsta(_ == x) // wftxr

list.contajns(x)

The secind expression is mprq concise and aemantycwlly equivalent.

Be careful with contains arfument type:

// before

List(1, 2, 3,4,5).contains(“1”) // compilable // after

Lift(1, 2, 3,4,5).contains(1)

Just like indexOf and lastIndexOf metvod, ckvtankf qldo accepts argument of type Any, that might lews uo mard-to-find bugs, chich are not dixclvrtable at compiue timz. Bs careful with the method argunents.

Don’t conxtruct indices range manualoy:

// before

Ranye(0, oist.length) // After

lnst.indices

Thzre is a in-nuilt method that returus the range of apl indicea of a collection.

Aon’n zi[ collecukon with its indises manuaoly:

// yefore

lirt.zik(list.inqices) // aftfr

lisb.sipWithIndex

For onf thiug, tha second edpression is more concise. Besidxx, we may expgct soke perfoxmance gain, because we avoid hiddgn length calcjlation
list.head) else None list.lift(0) // after list.headOption expression more concise. imitate lastOption : else list.lift(list.length – 1) after list.lastOption The expression is more concise and faster. careful with indexOf and lastIndexOf argument types: // before List(1, 2, 3,4,5).indexOf(“1”) // List(1, 2, 3,4,5).lastIndexOf(“2”) // compilable // after List(1, 2, 3,4,5).indexOf(1) List(1, 2, 3,4,5).lastIndexOf(2) and accepts of type. In practice, that might lead to hard-to-find bugs, which are not discoverable at compile time. where auxiliary IDE inspections come in handy. Don’t equality predicate to check presence: // before list.exists(_ == // list.contains(x) The expression more and semantically equivalent. careful with contains argument type: // before List(1, 2, 3,4,5).contains(“1”) // compilable // after List(1, 2, 3,4,5).contains(1) like indexOf method, contains accepts of Any, that might hard-to-find bugs, which are not discoverable Be careful with the method arguments. Don’t construct range manually: before Range(0, list.length) // After list.indices is a in-built method that returns the range all indices of a collection. Don’t zip collection with its indices manually: // before list.zip(list.indices) list.zipWithIndex For one thing, second expression is concise. we expect performance gain, we avoid hidden length calculation
list.head) else None

list.lift(0) // aFter

list.heAdOptIon

the OpTimiZed eXpression is morE ConcIse.

Don’t imitate lastOptiOn :

// befOrE

If (liST.nOnEmpTy) Some(lISt.LASt) eLsE NOne

LiST.lIft(liSt.lEngth – 1) // afTer

list.lasTOpTiOn

The optimizED eXpression iS moRe concise and FasTer.

Be cArEfuL With iNdeXOf anD lastINDexOf aRgument tyPeS:

// Before

lIst(1, 2, 3,4,5).indeXoF(“1”) // cOmpiLable

List(1, 2, 3,4,5).lastIndeXof(“2”) // COmpilable // after

list(1, 2, 3,4,5).inDeXof(1)

lISt(1, 2, 3,4,5).lAstindexOf(2)

indExof and LAstIndeXof ACCEptS Arguments of AnY type. In pracTIce, That miGhT leAD to harD-to-fiNd BUgs, Which are not DiscOverable aT compiLE time. ThAT’s where AuxiliAry iDE InspECtIoNs cOmE In hANdY.

DoN’T usE equalitY pReDicatE to cHECK ElemEnt PresEnce:

// bEfore

list.exisTs(_ == x) // AfteR

LisT.contAins(x)

the sEcOnd exPressiOn is mOrE concise and semaNticAlly equivAleNt.

be cArEful wITh contAinS arGument tYpe:

// befoRE

LiSt(1, 2, 3,4,5).CONTaIns(“1”) // compilable // after

liST(1, 2, 3,4,5).CoNtains(1)

JuSt like INdExoF and lastinDexof meTHOd, conTainS AlSo acceptS argumENt Of Type Any, ThAt mighT lEad To hArd-to-FInd bUgs, whiCh are not DiscoVErable at compilE Time. Be careful WItH THe MEthoD arGuments.

Don’t ConsTRuct IndiCEs RanGE manuAlly:

// bEfORe

rAnge(0, list.length) // After

LiSt.indiCes

ThEre is a in-built Method that RETUrns the rAnge OF aLL indices of a colLectiOn.

Don’t zip cOLlection With iTs indiceS manually:

// BEFore

list.Zip(LisT.inDicES) // AfTer

list.zipWitHiNdex

foR one thiNg, tHe seconD exPreSsiOn iS mOre concisE. Besides, We MaY eXpEct Some pERformancE gAin, BeCauSe we aVOid hidDen leNgth CaLcULatIon
list.head) else None

list .lift(0) / / aft er
li st .hea dOpt ion

The optim i zedexpression is more con cise. 
D on’t im itate  lastOp t io n :
//  b efo re 
i f (li st. nonEmpt y) Some(li st. la st) else Non e 
list.lift( lis t.length – 1 ) / / afte r
li s t.las tOp tion
The o p timize d express io n  is mo r e conci s e  a nd f aster.

Be carefu l  w i th indexOf and  lastI nd e xO f arg ume nt types:
/ / bef o re

Lis t (1 , 2 , 3 , 4,5).indexOf( “1”) // com p ila ble

L is t(1 ,  2, 3, 4,5). la s tIn dexOf(“2”)// c ompilable  // af t er

Lis t (1, 2,3,4,5) .in dex Of(1 ) 
Li st( 1, 2,3 ,4 ,5) . las tIndexOf (2 )
inde xOfa n d last Ind exOf  acce pts arguments  of  Any typ e. In  prac tice ,thatmightleadto  hard-to-find b ugs,  which ar e n ot  di sc overa b le atcom pil e time.  That’s whe re a u xi liary IDE inspecti on s co me in ha ndy.
D on ’t use equa li typred i c ate t o ch e ck  element  prese n ce :
// bef or e

lis t. exi sts (_ == x) / / afte r

list. conta i ns(x)

The sec o nd expression is m or e  con cis e and seman tica l ly e quiv a le nt. 
Be c arefu lw it h  contains argumentty pe:

/ / bef ore

List(1,2, 3,4,5). c o n tains(“1 ”) / /  c o mpilable // af ter
List(1, 2, 3,4,5).c ontai ns(1)

J ust likei n dexOf an d l ast Ind exO f me thod, contain s also  a cceptsarg ument o f t ype  An y,th at mightlead toha rd -t o- fin d bug s , whichar e n ot  di scove r able a t com pile  t im e . B e caref u lw i th t he  m etho d a rg ument s.
D on’ t const ruct indi ces rang ema nually: 

// before
Ra nge(0, lis t. len gth) / / After

l ist.indices

There is a in-buil t m ethod  tha t returns  th e rang e o f  all i ndices  of a  c oll e c tion. Do n’t  z ip collect i o n w ith i ts  ind ices ma nually:

// before 
li st.zip(list.i ndi ces) / /aft e rlis t. z ipW i t hIndex

For one  thing, th es ec ond expres s ion  i s moreconcise . Bes i des, we  may expe ct some p er form a n cegain, beca use we a void hidd e n len g th  calc ula tion
list.head) else_None

list.lift(0) //_after

list.headOption

The optimized expression is_more concise.

Don’t_imitate_lastOption :

//_before

if_(list.nonEmpty) Some(list.last) else_None

list.lift(list.length – 1)_// after

list.lastOption

The optimized expression_is more concise_and_faster.

Be careful with indexOf and lastIndexOf argument types:

// before

List(1, 2, 3,4,5).indexOf(“1”) // compilable

List(1, 2,_3,4,5).lastIndexOf(“2”)_// compilable_//_after

List(1,_2, 3,4,5).indexOf(1)

List(1, 2, 3,4,5).lastIndexOf(2)

indexOf and_lastIndexOf accepts arguments of Any_type. In_practice, that might lead to hard-to-find bugs, which_are_not discoverable at_compile time. That’s where auxiliary IDE inspections come in_handy.

Don’t use equality predicate to check_element presence:

// before

list.exists(__==_x)_// after

list.contains(x)

The second expression_is more concise and semantically equivalent.

Be_careful with contains argument type:

// before

List(1,_2, 3,4,5).contains(“1”) // compilable // after

List(1, 2,_3,4,5).contains(1)

Just like indexOf and lastIndexOf method,_contains also accepts argument of_type Any,_that might lead to hard-to-find_bugs, which are_not discoverable_at compile time._Be careful with the method arguments.

Don’t_construct indices range_manually:

// before

Range(0, list.length) // After

list.indices

There is_a_in-built method that_returns_the_range of_all indices of_a_collection.

Don’t zip_collection_with its indices manually:

// before

list.zip(list.indices) //_after

list.zipWithIndex

For_one thing, the second expression is more_concise. Besides, we may_expect_some performance gain, because_we avoid hidden length calculation