Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Stan Prokop
Stan Prokop

Posted on

     

All Glory to the Principle of Least Astonishment! ❤

ThePoLA is one of my favorite principles and I wish it's known as much as theKISS principle, maybe even more. It's also a playful way to think about anything you want to create and is supposed to be used by others.

Meet the PoLA

The idea behind it is straightforward - anything you create must not astonish or negatively surprise. As little WTFs as possible, which can help you to avoid bugs introduced by humans and provide a good user experience. This basically means to follow consistency, common user expectations and user's experience. The principle is universally applicable similarly as KISS and as a software developer I use it often to define interfaces, code behavior or in software architecture discussions

An example

Let me demonstrate that on the famous Javascript comparison operator==. The comparison behaves this way for values"",[] and0:

>> [] == 0true>> "" == 0true>> "" == []true

For these values it behaves like atransitive relation. However when you replace"" with"0","0" == [] returns actuallyfalse:

>> [] == 0true>> "0" == 0true>> "0" == []false

If you know all the necessary Javascript details this behavior might make sense to you, but for the rest of the humanity it, well... it totally doesn't. And that's the point. How does it break the principle of least astonishment specifically?

You might say that the last statement should betrue so it follows the behavior of the rest of the comparisons, but I think to really follow the principle of least astonishment the rest of the comparisons shouldn't returntrue in the first place. They should either throw an exception (comparing something which isn't really comparable), returnundefined (result of such comparisons shouldn't be really defined) or just returnfalse (they are not really equal). This actually shows nicely PoLA since the right solution sometimes really depends on the context.

To be fair Javascript also has strict operator=== which has less astonishing behavior, but hey, that wouldn't really demonstrate PoLA, would it? And== is the first thing which anyone coming from a different programming language would use.

So...

The PoLA is a simple but really powerful principle. When designing with PoLA in mind it helps to explicitly consider more perspectives which leads to less bugs and problems.

Do you have any examples of PoLA violations? They are usually great lessons learned.

Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
atomicstan profile image
Stan Prokop
  • Location
    Prague
  • Joined

I've never actually thought about Word and Chrome this way, but thanks to you I realized that these are really great examples. In context of any operating systems and their look and feel they don't really fit into any system known to humanity.

CollapseExpand
 
mareskav profile image
Václav Mareška
  • Location
    Prague
  • Work
    Ing.
  • Joined

About JavaScript, to be honest... I have never fully remembered JavaScript's true / false or truphy / falsy rules well. I have my favourite "cheat sheets" where I can have a look, but still it's real hell! Typescript brought a bit more strict checking.

What I really love is Typescript's "optional chaining" instead of JavaScript talkative conditionals. And I guess I'm not the only one because JavaScript new version is going to use it too 😊 (for example:javascript.info/optional-chaining)

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    Prague
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp