DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • Testcontainers With Kotlin and Spring Data R2DBC
  • Easily Update and Reload SSL for a Server and an HTTP Client
  • NullPointerException in Java: Causes and Ways to Avoid It

Trending

  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  • From Indicators to Insights: Automating IOC Enrichment Using Python and Threat Feeds
  • LLM-Powered Deep Parsing for Industrial Inventory Search
  • Zero-Downtime Deployments for Java Apps on Kubernetes
  1. DZone
  2. Coding
  3. Languages
  4. On Cosmetics vs. Intrinsics in Programming

On Cosmetics vs. Intrinsics in Programming

Code has cosmetic and intrinsic characteristics. See examples demonstrating how to achieve the same intrinsic with different cosmetics and vice versa.

By 
Nicolas Fränkel user avatar
Nicolas Fränkel
·
Aug. 15, 22 · Opinion
Likes (2)
Comment
Save
Tweet
Share
4.1K Views

Join the DZone community and get the full member experience.

Join For Free

A ruthless battle occurs every day on the World Wide Web. Its goal is to decide which programming flavor is the best: OOP or FP? I assume that imperative and procedural programming are not part of the contenders.

Arguments range from the factual to the irrelevant to the utterly stupid. A couple of years ago, I wanted to listen to a video of Martin Odersky (of Scala fame). I remember neither the exact talk nor the subject. What I remember is the introduction, though: he explained that FP was more popular than OOP... because there were many more conferences dedicated to the former than to the latter.

At the time, I didn't think popularity was a relevant factor that helped me deliver projects. At the time of this writing, I still don't. Moreover, it's akin to saying that electricity isn't popular because there aren't any conferences dedicated to it. I'm afraid that M. Odersky mistook popularity in academic research for relevancy. I stopped after his "argument," and to this day, I never watched a talk of his again.

This being said, my point is not to bash M. Odersky but to highlight the sheer vacuity of some arguments. For example, for FP aficionados, its immutability is not one, as OOP can also make good use of it. The only difference is that immutability is a requirement in FP. On the opposite side, pushing OOP too far results in languages like Java, where every method must belong to a class, even static ones. In this case, classes are just an additional namespace for methods: they bring no OOP "value."

The scope goes well beyond OOP vs. FP. Consider the following snippets:

Kotlin
 
fun router(repo: PersonRepository) = router {
    val handler = Handler(repo)
    GET("/person", handler::getAll)
}

class Handler(private val repo: PersonRepository) {
    fun getAll(r: ServerRequest) =
            ok().body(repo.findAll())
}
Kotlin
 
fun router(repo: PersonRepository) = router {
    val handler = Handler(repo)
    GET("/person/{id}", handler::getOne)
}

class  Handler(private val repo: PersonRepository) {
    fun getAll(r: ServerRequest) =
            ok().bodyValue(repo.findAll())
}


Obviously, the difference lies in the ok().body() vs. ok().bodyValue(). If you're unfamiliar with the Spring framework, you're unlikely to correctly identify the left snippet as WebMVC.fn and the right as Web Flux. It can be even more confusing if repo.findAll() is updated from blocking to non-blocking, as you won't spot any difference. You can only distinguish one from the other by looking at the package imports:

  • Blocking: org.springframework.web.servlet.function.*
  • Non-blocking: org.springframework.web.reactive.function.server.*

You can rewrite both above snippets using annotations instead of handlers.

Kotlin
 
@RestController
class PersonController(private val repo: PersonRepository) {

  @GetMapping
  fun getAll() = repo.findAll()
}
Kotlin
 
@RestController
class PersonController(private val repo: PersonRepository) {

  @GetMapping
  fun getAll() = repo.findAll()
}


Both snippets appear similar on the surface, but for the imports. Cosmetics are identical, while intrinsics - blocking vs. non-blocking - are fundamentally different.

Let's have a look at Kotlin coroutines. Here's a snippet taken from Kotlin's documentation:

Kotlin
 
measureTimeMillis {
    val one = somethingUsefulOne()                             // 1
    val two = somethingUsefulTwo()                             // 1
    runBlocking {
        println("The answer is ${one.await() + two.await()}")
    }
}


  • 1: Function points to a suspending computation.

Coroutine code cosmetically appears imperative while being asynchronous. It's the advantage of coroutines:

  • It looks imperative on the surface; hence it's easy enough to understand.
  • Behind the scene, the library runs the code asynchronously.

Code has cosmetic and intrinsic characteristics. I hope the few examples above convinced you that they are entirely orthogonal. You can achieve the same intrinsic with different cosmetics and vice versa.

We constantly argue about cosmetics, e.g., annotations vs. "functional," but it's essentially a matter of personal taste. To solve problems, we need to spend time on intrinsics a lot more: actors, asynchronous, etc.

Annotation IT Object-oriented programming Procedural programming Advantage (cryptography) Blocking (computing) Java (programming language) Kotlin (programming language) Scala (programming language) Snippet (programming)

Published at DZone with permission of Nicolas Fränkel. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • Testcontainers With Kotlin and Spring Data R2DBC
  • Easily Update and Reload SSL for a Server and an HTTP Client
  • NullPointerException in Java: Causes and Ways to Avoid It

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook