Clean Code

Posted on 3 Comments

There’s a bunch of technical books sitting in my Amazon wishlist for quite some time. Poor books! They could not end up in my shopping cart for a myriad of reasons, laziness to read being chief amongst them.

The importance of reading technical books simply cannot be overstated. Or reading articles, for that matter. Reading is what helps you to improve your craft, keeps you up to speed with the topics you care about, makes you a better version of yourself in your desired skill. Personally, I’ve found books to be more useful than online courses. I have nothing against online learning, I’m all for it. I have end-to-end finished a tonne of courses on Udemy, Pluralsight, Coursera, etc. The things that go for me with books are that I can take them anywhere (even where there’s no Internet), highlight important parts, scribble my own thoughts and most importantly learn without distractions. The thing about books being little portable time machines is true.

Recently when I was casually digging my Twitter timeline, I came across this book called “Clean Code”. Someone had written life-changing praises about it. Out of nowhere, I decided to order. Out I went to Amazon.in, found the book, read a few reviews, noticed that there was no discount, and hit the Buy Now button.

Clean Code is a book about software craftsmanship. It’s a must-read for anyone who cares about their craft.

I am a little more than halfway through the book, and boy, it is life-changing. It’s written in an authoritative tone by a bunch of regarded software engineers with decades of development experience. The experience clearly shows! Chief among the authors of the book is Robert C. Martin, who is one of the founders and foremost popularizers of SOLID principles and Agile methodology. I have thoroughly enjoyed the book so far, and am looking forward to finishing it. It’s safe to say that this book has reignited my craving for technical books. Next up I’ll probably pick up a Martin Fowler.

Using the learned clean code principles, I was able to transform this ugly chunk of code at work in a beautiful verse below it. The code is customization on top of Sitecore‘s JSS Node Proxy open source program.

Which code snippet is easier to read?

Object.keys(tenants).forEach((key) => {
  const tenantName = tenants[key];
  const proxyConfig = config.initConfig(tenantName);

  server.post(
    `/:lang((${routesLangs}))/${tenantName}:paymentRoute((${intermediatePaymentRoutes}))`,
    bodyParser.urlencoded(),
    (req, res) => {
      handlePaymentIntermediateRoute({
        paymentData: req.body,
        path: req.params.paymentRoute,
        lang: req.params.lang || DEFAULT_LANG,
      }).then((svcData) => {
        proxyConfig.serverBundle.renderView(
          (param, html) => {
            res.send(html.html);
          },
          req.path,
          svcData,
          { userAgent: req.headers['user-agent'] }
        );
      });
    }
  );

  server.use(
    `/:lang((${routesLangs}))/${tenantName}([/].*)?`, // eg. /((en|ar))/yasisland/wishlist
    (req, res, next) => {
      if (req.originalUrl.indexOf('/-/media') === -1) {
        let originalUrl = req.originalUrl.replace(tenantName, '');
        originalUrl = originalUrl.replace('//', '/');
        req.originalUrl = originalUrl;
      }
      next();
    },
    scProxy(
      proxyConfig.serverBundle.renderView,
      proxyConfig,
      proxyConfig.serverBundle.parseRouteUrl
    )
  );
});
server.use(
  '*',
  (req, res, next) => {
    setPaymentRoutePreReqs(req, res, next);
  },
  (req, res, next) => {
    const envType = getEnvironmentType(req);
    const tenantName = getTenantName(envType, req);

    const paymentRoute = req.paymentRoute;
    if (paymentRoute) {
      handlePaymentRoute(req, res, tenantName, paymentRoute);
    } else {
      handleApplicationRoute(req, res, next, envType, tenantName);
    }
  }
);

3 thoughts on “Clean Code

  1. Second code is easier to read.

  2. Spot on! Hope you got a chance to pick up the book.

  3. […] on a team but also developers. The art of creating good software doesn’t only lie in writing clean code but also in enforcing inclusive and structured communication […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.