https://kotlinlang.org logo
#codereview
Title
# codereview
a

alchzh

11/20/2017, 2:26 PM
Can someone review my code? Just started learning Kotlin this morning, so decided to port over some existing code... https://github.com/acz13/anagrams/tree/kotlin-rough/kotlin (x-post from #getting-started )
d

dalexander

11/20/2017, 3:26 PM
I would rename the files so that they are capitalized, especially where they contains a single class (ie.
bag.kt
->
Bag.kt
) In
Bag#equals
return num == (other as Bag).num;
will cause a ClassCastException if
other
is ever not a Bag, so that should use a type check
other is Bag
prior to attempting to cast. JUnit tests are typically in their own directory, but since this looks like it’s not its own project that might not be manageable.
Note that there’s a tool in the IDE to automatically generate equals/hashcode which generally does a good job.
a

Andreas Sinz

11/20/2017, 4:09 PM
@alchzh take a look at higher order functions like
map
,
filter
and use them instead of all those
forEach
and local mutable state
a

alchzh

11/20/2017, 9:15 PM
how do
map
and
filter
compare performance wise to
for
loops and
forEach
?
a

Andreas Sinz

11/20/2017, 10:07 PM
forEach
is similar to
map
etc., they are all inlined functions. The biggest difference between
for
loops and higher order functions like
map
is, that the latter creates two additional objects. Both get compiled into a simple
while
-loop (https://blog.gouline.net/kotlin-bits-for-loops-vs-foreach-30548d7472a5) So the performance is be similar
6 Views