[life,coding,social]@greelgorke

  • Archive
  • RSS
  • Ask me anything

The liquid harmony with solid Node - Part 1

Recently I watched a tech video. The speaker in there did complained about asynchronous coding style with callbacks and mentioned Node.js as a platform. In fact he said that Node.js === “callback hell”. He’s not the only one out there. He’s partially right. And wrong. There is callback hell in code for Node.js. But not because of the platform, callbacks or javascript, but because of lack of fundamental software design knowledge.

So I decided to start a series on that topic.

Soon you can find following parts under the hashtag #SOLIDnode. I will try to follow this constraints:

  • no magic. so you’ll see no promises, fibers, proxies, whatever here.
  • concentration on vanilla js, Node.js core API and few 3rd Party libraries, that extend Core API
  • I’ll be honest: there are limits. I won’t hide these.

A hint: Most of this practices can be used in browser too, tools like Browserify can help you to use Node.js API and libs there.

Let’s start. Today with part 1:

Callbacks in hell - We met earlier

Why do we speak about a callback hell? In javascript or Node.js we are often facing code, that uses callback chains in very painful way. But somewhere else you’ll find similar but different struggle. The term spaghetti-code isn’t bound to any language or platform. Every language or platform offers us concepts and tools to do software development, but often we fail to use the right tools right.

This is because in software development, we’re constantly facing 2 orthogonal complexities. The first complexity is the domain, the task our software have to solve. We have to understand the domain right, so we can program a system, that does exactly, what’s expected. The transition from one representation of the domain to the other is itself a complex task. This is software development, the second complexity. To reduce the complexity of the development, we need to apply our experience, the patterns and practices we learned earlier. If we have learned the right ones for the given system and the given domain, then our job is much easier. But most often we tend to think in a certain paradigm, that we have learned once, and then to fail to adjust our thinking.

This happens when we code callback spaghetti in node. Look at that code:

server.onRequest(function(params, response){
  db.getUser(params, function(user){
    if (user == null)
      return response.send(USER_NOT_FOUND)
    db.getRelated(user.related_id, function(related){
      if (related == null)
        return response.send(RELATED_NOT_FOUND)
      // do something with related
      db.save(related, function(){
        response.send(OK)
      })
    })
  })
})

re-indent and blend out the braces:

server.onRequest(function(params, response){
db.getUser(params, function(user){
if (user == null) return response.send(USER_NOT_FOUND)
db.getRelated(user.related_id, function(related){
if (related == null) return response.send(RELATED_NOT_FOUND)
// do something with related
db.save(related, function(){
response.send(OK)
})})})})

Yes, we all met this paradigm before. We’re still coding in imperative synchronous paradigm, and even worse, we think about the systems that we create often in this paradigm. Imperative programming is essential, this is how computers work for us. But mostly not how the Systems work that we imagine. Many languages did abstracted away assembler from our screens but not the way to think. But we have also discovered other ways, paradigms. They are just not as intuitive to us. We are used to be imperative in our day life. “Cut the vegetables, push the button, turn left”. Even the next native to us, the object-oriented way, is harder to accept for us. What about declarative way? “Let a be oranges and b apples then ab is fruit salad”. Or what about asynchronous processing? “I need a salad from this fruits, call me when i can get it.”

So, the first step we need to go is to learn how to switch our thinking. How and when to escape from a paradigm, to apply another one. How to be open minded.

We have learned a few principles through experience and hard lessons. They are abstract enough to be applicable in most of the situations we encounter. We just have to know, to remember and to apply them. I talk about principles of (object-oriented) software design. It appears that, still some of them are valid in functional languages, they just implemented different. Lucky for us, Javascript is object-oriented and kind-of functional. In fact JS lacks some of concepts from both paradigms, but it combines what it got to a flexible and powerful amalgam. As result we can apply many oo-patterns and principles much easier.

So we should remember a few mantras:

  • modularize, identify responsibilities
  • encapsulate state and hide internals
  • decouple from identity, commit on contracts and abstract interfaces
  • remove mutable state where possible and appropriate
  • separate along boundaries of responsibility, concern and domain

So far for the introduction. In the next part we’ll take a look closer on the callbacks and closures.

And one more thing: for all pathos, stay practical. Paracelsus said once:

“The right dose differentiates a poison and a remedy.”

Even the mantras above are poisonous, they can lead to over-engineering, module chaos, needlessly monadic code etc. Use the right dose.

    • #SOLIDnode
    • #nodejs
    • #javascript
    • #softwaredev
  • 5 days ago
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

What i have learned from my first public tech talk

On Monday 13. March 2013 i gave a talk about a Node.js topic at Hamburg Javascript user group. You can find it on github. The topic was about modularization, code structure, compatible interfaces and so on. I tried to demonstrate the idea by stepping through an evolution of a simple application.

I can honestly say, i’ve failed. Hard and painful. The talk was quite chaotic, bloated with tons of code and followed hardly any line. So, i made this mistakes and this are my lessons I’ve learned.

Choose the right scope

One problem was, that my code examples were to bloated. They were literally fully functioning applications. I wrote them so, because i wanted to prove, that the apps just work. This was, when not wrong, then at least not that important at first. To show the concepts was more important. I wanted to show them in a real life. But still, one have to concentrate the examples on the least possible amount of code, else you loose attention of your audience.

I learned: Keep examples short and condensed, they should cover the topic. Not more, not less.

Another thing: the topic was probably too big. Cut out all stuff that doesn’t add a value to your presentation.

Modularize

Also I’ve failed to follow a clear path in my talk. The main cause for it was, that i haven’t really had one. I knew which concepts I wanted to show, but I haven’t organized them well. I defined a path, but got lost myself in code, strayed away several times.

This mostly because I did structured my talk not very well. The chapters and the code did not correspond to each other, the switches form slides to code and back, from one example to another, were often confusing. So it should be, one slide, one piece of code, they should be kind of packed together in a module.

Interface

I tried to play with Mac OS spaces. And 2 screens. Didn’t work that well. Next time it’ll be one space. 2 screens are fine enough.

I didn’t give the time to my audience to understand or simply to read the things on the wall.

I learned: give time to read. Unfortunately code isn’t the best thing to read from a wall, so, go slow or develop just in time.

In the end

I wanted too much and did more than needed, but less what was required, at the same time. Smaller peaces, slower at code examples, few but clear steps. Find a red line to follow and to guide your audience, and stick with it.

I will restructure the talk, rewrite the code in more feasible way and reduce the scope of it. I’ve already found my red line on which I can adjust. And I hope I can do it again, but way better than it was on last Monday.

However, regardless all the flaws it was exciting and fun to speak to an audience. Thanks to all who was there.

  • 1 week ago
  • 2
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

blog.izs.me: Curiosity and The Practice

izs:

All too often, people do good things for the wrong reasons, and find it’s not sustainable. Compare the differences between these statements:

I want to develop software.
I want to be a software developer.

I want to write a novel.
I want to be a novelist.

I want to do yoga.
I want to be a…

it’s about doing things instead of being or becoming a thing, a title or some kind of notion. A nice post.

  • 2 weeks ago > izs
  • 11
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

blog.izs.me: Unix Philosophy and Node.js

izs:

At TxJS the other day, I gave a talk where I mentioned that the Unix Philosophy is a crucial part of the patterns, opinions, and culture of Node.js. As usual, I made my slides available online well in advance of the talk video being available.

For some reason, this brief mention of “Unix…

    • #nodejs
  • 1 month ago > izs
  • 65
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+
'\x3ciframe width=\x22500\x22 height=\x22281\x22 src=\x22http://www.youtube.com/embed/nKIu9yen5nc?wmode=transparent\x26autohide=1\x26egm=0\x26hd=1\x26iv_load_policy=3\x26modestbranding=1\x26rel=0\x26showinfo=0\x26showsearch=0\x22 frameborder=\x220\x22 allowfullscreen\x3e\x3c/iframe\x3e'

lxhunter:

“Everybody […] should learn how to program a computer… because it teach you how to think.” - Steve Jobs

Software is really about humanity

  • 2 months ago > lxhunter
  • 2
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

blog.izs.me: User Feedback

izs:

Sometimes a request is much less interesting than the motivation behind the request, which the requestor might not even fully understand.

Sometimes people demand a giraffe, when really, they just need a ladder, or better yet, they need things to not be up so high.

“There are ZERO giraffes in…

it’s all about requirements analysis and user orientation

  • 2 months ago > izs
  • 9
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

Broken promises: re-usability

Like since forever, at least since object-oriented paradigm, we live with the promise of true re-usability and modularization. In very early days, back in good old assembler days, we’ve created subroutines and functions. They were the first step, but it didn’t work out well enough. The problem was, that everyone could write functions for reuse, but only a few were interchangeable.

Objects came up with this idea of bundling data with behavior. Functions were lacking context, so they have defined the data-types they needed to work with. But the types were hardly compatible. Objects have created context, are a simpler way to define polymorphic data-types, and have the ability to transform internal data into different formats. Objects ‘know’ about their data, functions know about logic. It seemed like we got a powerful tool to create reusable code. But again, it didn’t work out well enough. We can define very generic types now, which can be handled by our functions, but on the other side we got a tighter coupling. Not because of OO itself, but because of the mistake we constantly make, to “classify” our data into models with “is a”-relations as first-class relation. As result our bigger systems are complex, contain long inheritance chains and web-like internal relations, which leads to less re-usability, and even worst, less maintainability and testability. Design Patterns and stuff like SOLID are proposals to fight this flaws. But i think they fight the symptoms not the root cause.

Good OO patterns advise you to establish loose coupling, high cohesion, very short inheritance chains and more composition. Interfaces, Dependency Inversion, etc. are the tools for it. It leads to functional programming, if you’re willing to go this way consequently. Functional programming tries to do the same as OO, but without the binding of data and logic. I find it remarkable that hardware did a big innovation push to our programming languages: multi-core processor architecture forced us to learn about FP.

FP languages usually have very strong type systems (in Haskel some say, if your code passes the compiler, the chance is very high, that your code is doing it right). On the other side the functions are able to work with everything, that fulfill the contract defined by the type, no matter if it’s another function, a list or a primitive. The disadvantage of such language is the strong learning curve and complex type system. Type inference may reduce the visible complexity of the type system, but still the concepts of FP are not that easy to understand. May be we get someday to the point, when FP feels native to us, but we are not there yet.

But what we can learn from FP now, is that the coupling of data and behavior was probably the wrong way. There are many of us, who have this insight already. Scala went this way, but still complex. There is another, pretty old player, which is fighting with kind-of functional programming. UNIX. Yes it’s not a language, but UNIX’ philosophy of small programs, which is doing one thing best, plus the use of standard streams as data-passing channel is arising again, somewhere, where it was not compellingly expected. In Node.js with its Streams and One-Function-Modules. Both made the same decisions:

  1. Small peaces of logic work with well defined data (same in FP)
  2. Exchange data via generic interfaces (Streams in Node, stdout > stdin in Unix)
  3. Your business logic, that creates context of the product, is integration, read transformation of data from one format to another if needed. This idea is as old as the dream of re-usability.

So, other ecosystems failed at several points:

  • They created modules, which are not small enough or have complex dependencies.
  • They created modules, which are bound to tight to a context. Interfaces or data representation are mostly not generic enough.
  • Transformation of data was not seen as main task of domain logic. Instead the development of new task-logic usually seen as such.

The Node.js-approach seems promising, and I’m pretty sure this will give Nodes ecosystem another boost.

    • #nodejs
    • #softwaredev
  • 5 months ago
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

A general and small overview of 6 months of Node.js. And opinions.

It’s long ago since I posted the first part of this series. What I haven’t expected back then, was that it will be not a series. So sorry for unmet expectations here.

But what I want to share with you here is an overview and few learnings made in the last 6 months of development. The product is not publicly announced yet, but we are in a closed beta already. So where to start?

Which techs did we use?

  • Node.js of course
  • RailwayJS currently rewritten and renamed.
  • which is depending on ExpressJS
  • using CoffeeScript as main development language
  • using Jade template engine and Stylus CSS transpiler
  • SequelizeJS as ORM
  • Postgres 9.2.X as DB
  • ImageMagick with this Wrapper

we used more small libs like UnderscoreJS, node-uuid etc. But it’s not about the techs in first place here.

How was it to develop?

It was fun! At least for me it was very exiting to learn new things, finally to apply my knowledge on a real-world app, to develop in one of my favorite languages and to see the results growing fast. Also it was fun to discover and and provide assistance to colleague of mine on node. I must admit, RailwayJS did its job to hide away node a bit and wrap it into Rails like mantle. But the main concepts of node still valid.

On the other side there were few hours of frustration. Most of them were caused by lacking of documentation. But still I like to read js-code, it was more a way to learn.

Rails on node?

RailswayJS was not my personal choice. To be honest it was good for this project, as well as CoffeeScript, because it did help my teammates to dive in to the development fast. Most of them know ruby on rails or similar frameworks.

On the other side, it just not felt like Node. It’s my personal opinion. The requirements of the project had points, where Node would shine, but we haven’t implemented them till now, it’s still be a classic db-to-template website. With this fact I think, that Ruby on Rails would had been a better solution and we could add real-time functionality based on Node later. I’m not saying Node isn’t good at serving dynamic Websites, we are pretty pleased with the project and product performance. But we did experienced disadvantages, which would not appear in Rails, because of its maturity.

What else have you learned besides the Rails on node approach?

Some things. Nothing new for a seasoned node developer. Small modules are good, small one-function-modules are even better. Ensure that the callbacks are called in any case. Good coding style is essential. Coaching of not so seasoned colleagues even more.

I think Node has the potential to provide true modularity. The strong focus on simple, callback-driven interfaces. The adoption of the UNIX philosophy, in which you have many small modules, which does one single thing as good as possible. The focus on streams. All this encourages us to build small and highly compatible APIs. There is this dream, that you could find the set of modules, which do, what you need, and just plug them together. Partially it happens already, but many modules out there still are to big and require to much integration effort.

Now, to get more detailed insight, what have you done?

I can’t say much about the product yet. Roughly, we present some data, which was aggregated by a big java-driven web-crawler. The data is provided via RESTful API. I’ve implemented the API client as own package. The product creates rich content out of this data, so we had to get this content. This was the second part I’ve implemented. Also I had the task to design the system architecture, it have to be able to scale and running on server-farms. I’m a back-end guy, so the shiny UI things on this product were made by my teammates.

And, as I said before, I had this role as advisor for Node and CoffeeScript topics.

When can we see your work

To be honest, I really don’t know. It’s uncertain, but it might would announced in the first quarter of 2013. I hope so.

    • #nodejs
    • #node2prod
    • #learnings
  • 5 months ago
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

a short word about CoffeeScript

There is a small war between JavaScript-purists and CoffeeScript-likers. I’m kind of in between of this fronts. I like many ideas from CoffeeScript, but also i love to read and write clean and pretty JS. For me it’s the matter of commitment in the team, and it’s OK to use one or the other language. But i do not just agree on the commitment but advocate for one or the other language before the agreement. Yes, i advocate for both depending on the situation. Here some of the points. I advocate

for pure JavaScript, when:

  • the project will or has a potential to be open-sourced and meant to be used as lib (deployed apps are bit different)
  • debugger support is crucial (may become irrelevant with source-maps support )
  • the learning curve for CoffeeScript is to high for the project constrains

for CoffeeScript, when:

  • the ruby-ish syntax and concepts are familiar to the team
  • debugging is not that important
  • the project allows to take a tighter learning curve

The last point is the most important for me. The most interesting attribute of CoffeeScript is it’s not-Java-like syntax. I learned that it is very important to destroy the likeness when Java (or other C-descendants) developers have to learn JavaScript concepts. Then i advocate for CoffeeScript with prohibition of class keyword at the start. I want to encourage my team-members to learn JS as a new language and not as a black-sheep-bastard-brother of their main programming language. When they’ve accepted JS as own language with own concepts, flaws and elegance, then i encourage them to dive into deeper fields. class of CoffeeScript is a good way to learn about JavaScripts prototypal inheritance while inspecting the generated JavaScript.

Someday they are in the position to decide between both languages, but it is their own decision then.

TL;DR;

CoffeeScript destroys the wrong feeling of likeness between JavaScript and Java/C/C++, and enforces the learning of JavaScript concepts without wrong presumption and legacy. That’s beneficial, when working in team with the high amount of Java developers.

    • #coffeescript
    • #javascript
    • #coding
  • 5 months ago
  • 1
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

Digital Artifacts: I think most of us live our lives as though there’s a point we’re...

anoemi:

I think most of us live our lives as though there’s a point we’re working towards, and in our minds, when we reach that point, everything will transform. All that time we put in at work? It will magically stop. Those bad habits, like drinking too much, or getting upset over the wrong things?…

  • 5 months ago > anoemi
  • 4
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+
Page 1 of 2
← Newer • Older →
Avatar driven by passion, attracted by #nodejs, geeking around while looking for pleasure in daily work, trying do more open-source and less nonsense

Pages

  • about:greelgorke

greelgorke@web

  • @greelgorke on Twitter
  • Google
  • My Skype Info
  • greelgorke on github

Twitter

loading tweets…

Top

  • RSS
  • Random
  • Archive
  • Ask me anything
  • Mobile
Effector Theme by Pixel Union