Clean code: Smart developer vs Professional developer

Adi Polak
Extend
Published in
4 min readAug 20, 2017

--

Clean code: Smart developer vs Professional developer using scala code examples

Today there are many types of developers, with an engineer diploma or without it, many people are writing code for a living and/or fun.

Yet writing production code is always more complex and demanding than others, we distinguish two types of developers, the one is extremely smart yet put less effort on maintainable code while the other is professional with strong belief in maintainable, readable and clean code.

The professional developer will have less of a queue of developers waiting for explanation regarding how to use his code, while the smart developer will maintain an endless line of people asking questions regarding his libraries.

Smart developers

We’ve all met the “smart” developer, the one that writes complex math equations, endless lambda expression and extremely generic code with little to no documentation. The one who won’t use others libraries and will develop everything from scratch, just because they know better.

While this is OK, when it comes to clean code, some smart developers fail poorly. They are writing code that is too generic. Which with adding dependencies and browder API hurts the ease of reusing the code.

Professional developers

Professional developers are a rare find, usually experienced and have great passion for programming. Professional developers know that the way they write code is the way they are perceived by their colleagues. They support clean code and high test coverage, mocks, ITs and basically everything that will ensure their signature (code) will live forever. They believe in maintainable code and their motto is :

Clarity is KING !

Scala clean code examples

Lets look at some scala code examples which distinguish professional developer from just a smart one:

Smart developer:

trait SocketFactory extends (SocketAddress => Socket)

Professional developer:

type SocketFactory = SocketAddress => Socket

a SocketFactory is a function that produces a Socket. Using a type alias is better in this case, reusable and more readable.

Creating collections in scala:

Smart developer:

scala.collection.mutable._
val set = Set()

Professional developer:

import scala.collection.mutable
val set = mutable.Set()

In the second example it is easy to understand what type of collection is being used, this is usually important when building APIs in scala.

When declaring new parameters is it better to add the type as well, this way it is much more clearer:

var set: mutable.Set[String] = mutable.Set()

Fancy lambda expression:

Smart developer:

val votes = Seq((“scala”, 1), (“java”, 4), (“scala”, 10), (“scala”, 1), (“python”, 10))
val orderedVotes = votes
.groupBy(_._1)
.map { case (which, counts) =>
(which, counts.foldLeft(0)(_ + _._2))
}.toSeq
.sortBy(_._2)
.reverse

Professional developer:

val votes = Seq((“scala”, 1), (“java”, 4), (“scala”, 10), (“scala”, 1), (“python”, 10))val votesByLang = votes.groupBy({ case (lang, _) => lang })
val sumByLang = votesByLang.map(
{ case (lang, counts) =>
val countsOnly = counts.map({ case (_, count) => count })
(lang, countsOnly.sum)
})
val orderedVotes = sumByLang.toSeq
.sortBy({ case (_, count) => count })
.reverse

The smart developer example is succinct and correct, but nearly every reader will have a difficult time recovering the original intent of the author. A strategy that often serves to clarify is to name intermediate results and parameters provided in the second example. The professional example is self explanatory for a person with basic knowledge in scala.

Know when to use type alias:

Smart developer:

type IntMaker = () => IntIntMaker

Professional developer:

class ConcurrentPool[K, V] {type Queue = ConcurrentLinkedQueue[V]type Map   = ConcurrentHashMap[K, Queue]...}

IntMaker is short and uses a common type. Yet, Queue and Map is helpful since it communicates purpose and enhances brevity.

This where some examples of writing a readable, professional code vs just a smart code. Keep in mind that code needs to be maintainable and usable otherwise, it will stink!

Some info about the scala type keyword:

Scala type keyword

  • The type keyword in Scala can do much more than just aliasing a complicated type to a shorter name. It introduces type members. Scala also allows a class to have type members. The type system just replaces the alias with the actual type when type-checking is performed.
  • Type members can also be viewed as dual of generics since much of the things you can implement with generics can be translated into abstract type members.

Example

trait Base {
type T

def method: T
}

class Implementation extends Base {
type T = Int

def method: T = 42
}

Type is a strong feature, for more details click here.

I hope these examples will guide you along the way. Remember that production code is all about readability and maintainability.

Takeaways

  • Declare your parameters types
  • Don’t go crazy with lambda functions
  • Use type keyword when needed
  • Clarity is KING !

Follow me on Medium for more posts about Scala, clean code and software engineers nonsense. Cheers !

--

--

Adi Polak
Extend

👩‍💻 Software Engineer 📚 Author of Scaling Machine Learning with Spark (O'Reilly) 🗣️ Keynote Speaker 💫 Databricks ambassador