cyclometric complexity increases due to case keyword

28 views
Skip to first unread message

priyanka goel

unread,
Oct 12, 2017, 9:03:16 AM10/12/17
to Scalastyle users
Hi

I have added Scalastyle to eclipse and written simple Scala class. As soon as I added case class , cyclometric complexity of code increases by 1.
Also while iterating map or RDD[tuple] i used case pattern matching instead of tuple. 

Please let me know using case class is good practice or not . Does it actually add a cyclometric complexity in code.

Thanks
Priyanka Goel

Matthew Farwell

unread,
Oct 18, 2017, 4:52:16 AM10/18/17
to Scalastyle users
Sorry for the late reply,

The cyclomatic complexity checker does count a case as an extra branch. As always, cyclomatic complexity is a guide, not a rule.

When you iterate over a map, you have the choice of using either a .map(t => t._1, t._2) where t is a tuple or a { case (k, v) => } which I think most people use to avoid having to use the _1 and _2.

I can't speak for your coding conventions, but when I see the { case (k, v) => } I always have to ask what would be the other possible cases. I do understand why people use it to avoid the _1 and _2 though. I switch between the two, so I don't even have a consistent standard for my own code.

I take it that you're asking this question because Scalastyle is raising an error. Maybe you need to raise the threshold? :-)

Thanks.

Matthew Farwell
Reply all
Reply to author
Forward
0 new messages