Skip to main content

Posts

Showing posts from September, 2021

Simple code: Simplicity

Simplest solutions are usually the best solutions. We as software developers work with hard problems and solve a lot of small problems every day. Solving a hard problem itself is a hard job. Though in my opinion it's not enough to solve a hard problem in any possible way but a hard problem should be solved with a simple solution. When a developer comes up with a simple solution to a hard problem then they can declare the problem solved. First a disclaimer. Coming up with a simple solution to a hard problems is itself a very hard problem and takes a lot of time, effort and practice. I've seen my share of "clever" solutions for hard problems and the problem with those is that usually the solution itself is so hard to understand that depending on the size of the problem it may take a developer from hours to days or even weeks to understand how that "clever" solution works. It's a rare occasion when a developer has come up with a simple solution to a hard pr

Simple code extra: Readability examples

Seven ways to write the same code snippet  Here are eight ways to write the exactly same code. Some are easier to read than others and all are a variation of a code I've seen in a real code base. My personal favorite is #7, what's yours?  #1 One liner DAO.filter { it.name == "foo" }.map { it.company }.toSet() #2 two lines, three operations DAO.filter { it.name == "foo" }   .map { it.company }.toSet() #3 Evaluation on it's own line DAO.filter {   it.name == "foo" }.map { it.company }.toSet() #4 Each operation and evaluation on their own lines DAO.filter {   it.name == "foo" }.map { it.company } .toSet() #5 All function calls and evaluation on their own lines DAO   .filter {     it.name == "foo"   }.map { it.company }   .toSet() #6 Everything on it's own line DAO   .filter {     it.name == "foo"   }   .map { it.company }   .toSet() #7 All function calls on their own lines DAO   .filter {  it.name == "foo&quo

Simple code: Readability

Readability, understandability, two key incredients of great code. Easier said than done, right? What one person finds easy to read and understand another one finds incomprehensible. This is especially true when programmers have different levels of understanding on various subjects e.g. object oriented vs. functional or Node.js vs. Java. Even though there are obvious differences between paradigms and programming ecosystems there are some common conventions and ways to lower the barrier. Different approaches It's natural that in programming things happen sequentally e.g. you can have a list of objects and you need to do various things to the list like filter some values out and count a sum of the remaining objects based on some property. With the given list const stories = [   {name: "authentication", points: 43},   {name: "profile page", points: 11},   {name: "shopping cart", points: 24},   {name: "shopping history", points: 15},   {name: &qu