<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Gidi Meir Morris on Medium]]></title>
        <description><![CDATA[Stories by Gidi Meir Morris on Medium]]></description>
        <link>https://medium.com/@chekofif?source=rss-183a877031e6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1_fB1Z2K-iA6LbQWdn0c8Q.png</url>
            <title>Stories by Gidi Meir Morris on Medium</title>
            <link>https://medium.com/@chekofif?source=rss-183a877031e6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 03:02:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chekofif/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[When Personal Ethics Clash With Industry Norms]]></title>
            <link>https://medium.com/unruly-engineering/when-personal-ethics-clash-with-industry-norms-3cc41d0bec7a?source=rss-183a877031e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/3cc41d0bec7a</guid>
            <category><![CDATA[hiring]]></category>
            <category><![CDATA[recruitment]]></category>
            <category><![CDATA[ethics]]></category>
            <category><![CDATA[google-project-maven]]></category>
            <category><![CDATA[ethics-in-tech]]></category>
            <dc:creator><![CDATA[Gidi Meir Morris]]></dc:creator>
            <pubDate>Tue, 24 Jul 2018 09:40:43 GMT</pubDate>
            <atom:updated>2018-07-24T09:40:43.867Z</atom:updated>
            <content:encoded><![CDATA[<p>Last week I had the profound pleasure of attending the first ever <a href="https://www.coedethics.org/">COED:ETHICS</a> conference in London.</p><p>A first of its kind, the organisers of this conference posed a question which has long been on my mind: <strong>Can developers become agents for change which might inspire, inform and enforce ethical standards within the tech industry?</strong></p><h3>Motivation</h3><p>I found the conference truly unique and inspiring, but mostly, it really struck a personal chord with me and I thought it was worth taking the time to clarify why I felt it is important to take part in such an event.</p><p>Throughout my career I have been heavily involved in the recruitment process into the tech industry. I am also extremely passionate about improving the diversity within the industry and I am actively working to improve the inclusivity of the community of developers I am a part of.</p><p>Recently I found myself confronted with an ethical question which required an immediate answer: <strong>Are my personal ethics at odds with those of the industry I work in?</strong> And, more importantly, how much leeway do I have in following my own ethical guidelines when participating in this industry?</p><h3>Ethics As Part Of The Recruitment Process</h3><p>At my current company all developers take part in the interview process of candidates, which means that as part of my job (though I am in no way a member of our <em>People Team</em>) I assist in screening the CVs of candidates.</p><p>Several months ago a CV came across my desk from a developer whose previous employer had been named by newspaper headlines on a semi-regular basis. For the sake of keeping this post on topic and to avoid triggering an unrelated debate, I won’t name the company explicitly.</p><p>Instead lets use a pseudonym.</p><p>I shall hence force refer to this employer as <em>Oxford Statistica</em>; a totally made up company name. Totally.</p><p>This candidate was likely choosing to distance themselves from the reputation this company was amassing, which is a perfectly legitimate thing to do, but I found myself asking a serious question:</p><blockquote>How relevant is the nature of this company’s business practices to the question of this person’s future employment?</blockquote><p>As I contemplated this question, both in the context of current events and in the context of my personal experience in the industry, I came to two important conclusions.</p><h4><strong>The Industry Doesn’t Deem This Relevant</strong></h4><p>Thinking back over my time in the tech industry, I have come to the conclusion that at this moment in time, the general question of the relationship between the ethical aspects of a company’s business practices and their bearing on the individuals working at the company, are limited to what a legal system might define as <em>culpability</em>.</p><p>What I mean by this is that the only place where it is considered legitimate to hold a candidate to account for the ethical practices of their previous employer is when these practices were found to be illegal and that the law had found this individual responsible in some form or another for this wrongdoing.</p><p>In fact, I would go so far as to say that, as far as I can tell, the industry in general would find the dismissal of candidate on ethical grounds without proven <em>culpability</em> as a form of <em>discrimination</em>, just as dismissing a candidate for being “A Tory” would be discrimination.</p><h4><strong>I Am Biased</strong></h4><p>The second, and perhaps most important one, was that I am in fact biased against this candidate. As an often overly vocal voice in the <em>D&amp;I</em> efforts of my employers, I talk often about the concept of the <em>biases</em> that interviewers are often tripped-up by when interviewing candidates.</p><p>In the case of this candidate, I posed, I am no longer capable of avoiding <em>Confirmation Bias</em>, specifically I am more likely to support the thesis that this candidate has taken part in unethical practices, than to be convinced otherwise.</p><p>This is the very definition of bias, and as I had already come to the conclusion that my concerns are not deemed relevant by the industry, I chose to withdraw myself from this candidate’s interview process.</p><p>This means that another developer stepped in and conducted the interview in my place. This meant the candidate got a fair and balanced interview, instead of having to face me, an interviewer who was already biased against them.</p><p>I felt comfortable with my decision in this case, but found myself severely torn up inside, as I realised this question was one I am likely to encounter more and more as we uncover more misdoings by companies and individuals throughout our industry.</p><h3>Asking The Question</h3><p>In the wake of this interview I came across the mention of the <strong>COED:ETHICS</strong> conference and noticed this blurb on the conference site:</p><blockquote>After the Facebook, Uber and Volkswagen scandals, where are we as an industry? How did we get here and will things get better or worse? <strong>80% of developers don’t feel it’s their responsibility if unethical products go live. Are they right?</strong></blockquote><p>No single more relevant question could have been asked of me at that very moment, and so I quickly grabbed myself a ticket, booked the day off and prepared mentally to seek out other individuals who might be as frustrated as myself in the quest to answer it.</p><h4>Have I Found An Answer?</h4><p>Though I discussed the topic with several people at the conference, I neither got a clear answer, nor was I expecting to.</p><p>The participant at such a conference, are by the vary nature of such a grassroots event, individuals who are not entirely comfortable with the status quo.</p><p>This means that they would likely lean towards change, not retaining things as they are, and as the status quo seems to be that of stifling debate on ethics in favour of company profits, I knew that any answer I got would likely be biased towards one edge of the spectrum.</p><p>That said, I have come up with my own position on this question and I thought this would be a nice opportunity to voice it.</p><h4>Why the Ethics and Practices of Former Employers Are Relevant</h4><p>My personal position is that we <em>should</em> be asking candidates about their position on the ethics of the business practices of their former employers.</p><p>There are several reasons for my position.</p><h4>They Are Often Applicable To The Work</h4><p>In the case of the candidate from <em>Oxford Statistica</em>, the company has been accused of dancing on the edge of criminality in its use of personal data, avoiding legal action by the skin of its teeth thanks to the archaic nature of our legal system.</p><p>In the case of my current employer, we could potentially use similar methods to boost our revenues, but we explicitly choose to prioritise privacy over shady business practices. We rely heavily on the ethics of our employees to act as safeguards, especially those of our developers, data scientists, and analysts as they could easily violate these guidelines without the rest of the business ever knowing.</p><p>In fact, we employ a <strong>Privacy By Design</strong> practice by which every single Story, Developer Task and Operation is assessed for privacy issues by the developers as part of the design process, to ensure they keep privacy concerns at the forefront of their minds.</p><p>If a candidate is coming from an environment where the demanded ethics around privacy are divergent from our own, then how can we possibly ignore that? Surely, the only way to ensure that our practices are retained, is to ensure candidates are willing to adhere to them.</p><h4>Questioning a candidate on such questions is the opposite of exhibiting bias</h4><p>As I explained above, in the case of the <em>Oxford Statistica</em> candidate, I did not feel empowered by the industry to ask this question. Nor did I feel that my company could take a divergent stance on this issue on its own, as it would end up being labeled a discriminator. With that in mind I decided I could not question the candidate on such issues, but without debating these topics I was afraid I would end up unintentionally biasing against the candidate, which is why I chose to withdraw.</p><p>It is my belief however that had I been able to ask the candidate about their position on such question it would actually allow me to avoid these unintentional biases.</p><p>To understand my position it is first crucial to understand that the intention behind such questions <strong>is not to hold the candidate to account for the actions of their former employer</strong>.</p><p><em>Firstly</em>, such a demand would be unfair as many employees might have been completely oblivious to the methods employed by their employers.</p><p><em>Secondly</em>, even if a candidate was aware, or even took part, it is important to give them a chance to explain themselves, as it might be that they did in fact speak up, did try to change things and most of all, as human beings, they too can make mistakes, learn from them and improve. All we’re doing is making the question of ethics part of the conversation, and with the right empathy we can make these questions as valid as technical exercises.</p><p><em>Thirdly</em>, on a personal level, it would be completely <strong>hypocritical</strong> on my part to hold such actions against the candidate, as I spent the first 7 years of my career working for the Defence industry. I would not feel it fair for a potential employer to reject me on that basis without first giving me the opportunity to discuss the issue, as ethics are often subjective and are open to interpretation.</p><p><em>Fourth</em>, equally important, by discussing these ethical questions with the candidate we, as potential employers, are also giving the candidate the opportunity to asses <strong>our ethics</strong>. As a candidate I would want to know about the ethical position of a company before I chose to work there, and I would definitely appreciate the opportunity to ask a potential employer tough questions on how <em>my work</em> might be used.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KXxo-7cbLr3F-ip44AADXg.png" /><figcaption><a href="https://www.theguardian.com/commentisfree/2018/may/16/google-business-war-project-maven">Google Project Maven</a></figcaption></figure><p>As we saw when Google’s bid for a Pentagon contract which would have offered its AI technology to isolate targets for attack by scanning drone feeds, employees care a great deal about how their work is used. At the time, an open letter was signed by over 3,000 Google employees forcing the company executives to pull the plug on the project. Had these employees had the opportunity to discuss these topics with the company as part of their interview process, they could have avoided this situation all together and could have avoided offering their expertise to a company that would use the product of their work in a manner which might be contradictory to their personal ethics.</p><p>One might argue that had they not been employed by Google they would not have had the power to stop the project, but my argument is that by forcing the ethics into the conversation we can separate the “ethical” companies from the “unethical” ones and by doing so we can hold the unethical companies to account by using the power of the community as a whole.</p><h3>Where From Here</h3><p>Nothing much has changed, but I feel that after the COED:ETHICS conference I am slightly more empowered to express my point of view.</p><p>By holding the conference our community is beginning to challenge the status quo, and even though it is still far from affecting change, it is sowing the seeds.</p><p>I, for one, will continue to keep my ethical questions to myself when interviewing candidates. Despite the importance I feel they hold, I don’t feel the industry is ready to accept this change, but I will continue to discuss the validity of such questions as I make my way through the industry.</p><p>Perhaps change isn’t here yet, but if it’s up to me, change will come long before I retire.</p><p>Read more on Gidi’s personal blog at <a href="http://www.gidi.io/">gidi.io</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3cc41d0bec7a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/unruly-engineering/when-personal-ethics-clash-with-industry-norms-3cc41d0bec7a">When Personal Ethics Clash With Industry Norms</a> was originally published in <a href="https://medium.com/unruly-engineering">Unruly Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Betting on Reason]]></title>
            <link>https://medium.com/@chekofif/betting-on-reason-6d67a8676ef6?source=rss-183a877031e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/6d67a8676ef6</guid>
            <category><![CDATA[reasonml]]></category>
            <category><![CDATA[ocaml]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[javascript-development]]></category>
            <dc:creator><![CDATA[Gidi Meir Morris]]></dc:creator>
            <pubDate>Wed, 18 Jul 2018 21:10:01 GMT</pubDate>
            <atom:updated>2018-07-18T21:10:01.512Z</atom:updated>
            <content:encoded><![CDATA[<p>Approximately a decade ago I chose to focus on Javascript as a specialism, taking a chance on a language that was, at the time, considered a joke.</p><p>Today, looking back, I see that as the best professional decision I made, sending me on an adventure spanning multiple exciting companies and products.</p><p>But now I’m looking ahead and taking another bet — this time on ReasonML, and I’d love to tell you why.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ipfot3sVCYswg1yewYvu-Q.png" /></figure><p>That said, I have decided to stop hosting my content on Medium (nothing personal against then, but I’m turning my back on walled gardens), so I’d love for you to read about it on my site:</p><p><a href="http://gidi.io/2018/07/14/betting-on-reason.html"><strong>http://gidi.io/2018/07/14/betting-on-reason.html</strong></a></p><p>Thanks for following me and giving my thoughts the time of day!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6d67a8676ef6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[I’m No Longer Blogging On Medium]]></title>
            <link>https://medium.com/@chekofif/the-magical-quest-for-componentisation-76a0d609a103?source=rss-183a877031e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/76a0d609a103</guid>
            <category><![CDATA[components]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Gidi Meir Morris]]></dc:creator>
            <pubDate>Mon, 28 Nov 2016 16:32:02 GMT</pubDate>
            <atom:updated>2018-07-15T20:17:08.910Z</atom:updated>
            <content:encoded><![CDATA[<p>I love Medium and I think they’re doing some great things, but I’ve reached a point where the limitations of Medium are getting in the way of my blogging goals.</p><p>You’re hereby invited to read my content on my personal blog at: <a href="http://gidi.io/"><strong><em>Gidi.io</em></strong></a></p><h3>The magical quest for Componentisation</h3><p>I would like, if I may, to ask you an existential question.</p><p><strong>What is the core of our job as developers today?</strong></p><p>It probably <strong>isn’t</strong> <em>figuring out the next super cool &amp; complex algorithm.</em></p><p>But rather, what our job probably <strong>is</strong>, is to <em>take an existing abstraction and compose it with a second abstraction in order to form a third abstraction</em>.</p><p>What we’re still trying to figure out is how best to do that, right?<br>For me, that quest began with modularisation.</p><h3>Modularisation</h3><p>Predominantly focussed on the Front End over the past decade, I spent several years evangelising <em>Backbone</em> as a solution to tackling the challenge of Front End architecture. As a relatively early adopter of Backbone, in early 2011, I found the move from a messy soup of global Javascript objects with complex prototypical hierarchies, to a structured MVC (or rather, MVP) pattern an extremely pleasing step forward.</p><p>Backbone allowed us to focus less on how the pieces of our code fit together, and more on what they actually did. This was made possible by abstracting away a lot of the boilerplate and defining a clear API for each piece of our system. This might seem obvious in hindsight, but at the time this felt groundbreaking, mostly because we were so used to our Javascript existing as <em>a pile of code held together by duct tape</em> that we accepted our fate and found it hard to see the potential for a better option.</p><p>But the key <em>unique selling point</em> of Backbone, beyond removing boilerplate and providing an enforced API, was laying the groundwork for proper <strong>modularisation</strong> of our Javascript codebases.</p><p>Modules are a corner stone of good code structuring, as they encourage us to break our code down into individual logical pieces. This makes it easier for us to write maintainable code that’s easier to reason about, to test and to reuse.</p><h3>Rachel’s Trifle</h3><p>Backbone, though, as it turned out, was not the be-all and end-all solution to our problems.</p><p>We could definitely leverage the benefits of MVC to achieve several things, such as breaking down our architecture into multitiered Object Oriented designs, to strive for separation of concerns by layering our modules and to design inheritance chains meant to improve code reuse and reduce complexity. But it seemed we would always end up hitting the same inevitable wall.</p><p>As a codebase would grow, requirements would change and assumptions would turn out to be incorrect, we often found that our abstractions began to fail us.</p><p>What we would initially envision as a well thought out and layered Trifle, turned out to be more a mixed up recipe, where layers don’t quite make sense anymore, they end up seeping into the layers above and bellow them and something just doesn’t taste quite right anymore. This in turn makes it harder to reuse code, to test it and to maintain it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*6mWeeYO2BZrgc4nX.gif" /></figure><p><em>Rachel’s Trifle? Good.</em> <strong><em>Code Trifle</em></strong><em>? Bad.</em></p><p>In other words, our code would develop a smell… the unmistakable smell of a Custard, Jam, Mashed Potatoes and Meat dessert.</p><h3>Wait, but why?</h3><p>Einstein never actually said that <em>insanity is doing the same thing over and over again and expecting different results</em>, but I’d like to think that he <em>would</em> agree that when we find ourselves repeating a mistake, we should probably rethink our initial approach.</p><p>I tried to identify the root causes for why so many of the teams I’d worked on ended up with these code smells and I narrowed them down to the following four:</p><p><strong>Building complex and / or distributed state machines</strong> <br>We often try to manage state in many different places and try to reconcile this state throughout the lifecycle of the application. This is hard to do well, and leads to many implicit assumptions which easily break.</p><p><strong>Big bags of instruction rather than atomic declarative pipes</strong> <br>Instructions may be easy to follow, but not always easy to understand. They usually describe <em>what</em> is done, rather than <em>why</em> and <em>how</em>. This means that the developers are left to figure out the <em>why</em> themselves. We’ve all found ourselves doing this, often referring to it as <em>reverse engineering our own code</em> and we often end up misinterpreting the <em>why</em>.</p><p><strong>Complex dependency chains reducing our ability to make atomic pieces of code</strong> <br>We often find it hard to make truly atomic pieces of code as they have an inherent <em>need</em> for a piece of data or operation that is the concern of another piece of the code. Dependency will always exist in a complex systems, but in lieu of a unified way of decoupling the dependency from its definition, you end up with strong cohesion between multiple components. This makes it extremely hard to test one component without having to build complex mocking mechanisms. These mechanism will often end up coupled to the dependency’s implementation, leading to frequent breakage and difficult maintenance as dependencies change over time.</p><p><strong>Trying to predict the future</strong><br>By far the biggest culprit for the dank code smell is a habit developers have of trying to predict how their code will be used instead of focusing on what is needed. This can lead to many problematic implementations, but most of all it leads to over engineering of solutions, which often leads to code that is very hard to <em>delete</em> later. I’m a firm believer that good code should be easy to delete and over engineering often makes that much harder to do.</p><h3>Searching for answers</h3><p>Having boiled down my problem to its root causes, the next step would be to figure out what I could do to reduce their impact on how I write my code. I believed these causes could be reduced to one core issue, which is that my code would often end up with <strong>High Efferent Coupling &amp; Fragmented State</strong>.</p><p>Which begged the question:<br><strong><em>How on earth do we reduce our efferent coupling &amp; fragmented State?</em></strong></p><p>Searching high and low for an answer I hit the internet, the books, the tech talks, my dad (I didn’t literally hit my dad, I just asked his opinion, as he’s been writing code since the 70s, presumably sporting glorious hippie hair and a spliff) and found that there are in fact many possible answers to this question.</p><p>I won’t enumerate all of them, but one answer would show up repeatedly, and that was a quote which I’d heard many times before:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kmFm-UpmGrqkQ83nrmaqHQ.png" /><figcaption>Note the year in which this book, considered a religious tome by many developers, was released. 1994. That’s 22 years ago, that’s it. If anything serves to remind us that we’re still “just figuring out this shit, because it’s bloody new”, it’s that.</figcaption></figure><p>In fact, this quote has been repeated so many times by so many people, I’ve began to find it <em>eye-roll inducing</em>.</p><p>The reason I found this quote slightly annoying was that the more I discussed this topic with experienced developers, the more it would remind me of another quote.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xLbN8aMcuXZ-CYIme_8oWQ.png" /></figure><p>While it seemed many developers appreciated the wisdom in this advice, reflected in the fact that it has been featured in innumerable talks and blog posts, very few could actually show me <em>how to achieve this</em>.</p><p><em>Composability</em> is not an inherently clear idea, and while many developers seems perfectly capable of coding up an example in an IDE, they never seemed to actually follow through when it came to their codebase. And most annoying was the fact that they were aware of this discrepency, but simply couldn’t quite translate the theory into practice once they had real problems they needed to solve.</p><h3>What is Composition?</h3><p>So to figure out how to achieve <em>composability</em>, we first need to understand what <em>composition</em> actually means.</p><p>To the best of my understanding there are two kinds of composition.</p><p>The first is <strong>functional composition</strong>. I’ll assume for the sake of succinctness, that you already know what functional composition is, as this blog post is already way too long.</p><p>But just to make sure we’re on the same page I’ll clarify that when I say functional composition, what I mean is that, in the following snippet, the <strong>h</strong> function is the <em>functional composition</em> of the <strong>g</strong> and <strong>f</strong> functions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a38a13d882a3877aef341784a856be8a/href">https://medium.com/media/a38a13d882a3877aef341784a856be8a/href</a></iframe><p>The second kind of composition is <strong>object composition</strong>, and this is where I got stuck.</p><p>I just couldn’t wrap my brain around how I was supposed to take the theory of Functional Composition and apply it to complex objects. How to structure the pieces of my code and make them work together without relegating back to old and smelly practices.</p><h4>What is Object Composition?</h4><p><a href="https://twitter.com/_ericelliott">Eric Elliot</a> gave a really good talk at <a href="https://www.youtube.com/watch?v=lKCCZTUx0sI">Fluent 2013</a> in which he explains object composition and its benefits in a much better way than I ever could, so I’d highly recommend watching his talk.</p><p>But after experimenting with Eric’s proposed solution, a library called <a href="https://github.com/stampit-org/stampit">StampIt</a>, I still couldn’t see how I could avoid the high Efferent Coupling I was experiencing, nor did it do much to help me tackle the fragmented state.</p><p>This feeling of frustration with the smell of my code would stay with me for a while, until recently, when I began using <em>React</em> in production. It wasn’t immediately apparent to me how React seemed to alleviate many of the problems I was experiencing, nor do I want to tote React as necceserily a <em>solution</em> to these problems.</p><p>What I have found <em>React</em> to be though, is an opportunity to alleviate many of the causes for these code smells, and that’s the actual topic I want to talk about.</p><h3>Composability via Componentisation</h3><p>I often hear developers talk about the difference between the <em>react</em> package and the <em>react-dom</em> package. They seem to always focus on how the separation provides the ability to use <em>React</em>’s top level API to define <em>loosely coupled</em> components for different Renderers and Reconciler in order to share components and logic between different target technologies. They will also discuss how the uniderectional flow of data aids in avoiding <em>fragmented state</em>. Sadly, though, it’s much more unusual though to hear them discuss what it is about the top level API that makes it so well suited for building such a wide variety of components with relatively low efferent coupling.</p><p>React is often toted around as highly declarative, composable and encapsulated. In fact, these descriptions are stated on the React homepage, but no where in the documentation is it discussed what it is about React’s API that allows for these traits. These traits are simply stated as fact.</p><p>I guess the rest of the Javascript community is much more practical than I am, because I found myself much more concerned with <em>why</em> it worked than anyone else I talked to.</p><h3>From Function to Component</h3><p>It wasn’t until I started analysing the React API that I got my “AHA!” moment. What I realised was that a direct parallel can be made between the API of Functional Composition and the React API.</p><p>Lets break down the API of Functional Composition and label the different building blocks:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/60aaa7f14ca855128e4fb58ecb78c3d8/href">https://medium.com/media/60aaa7f14ca855128e4fb58ecb78c3d8/href</a></iframe><p>While this might not be immediately apparent, the above composition is actually reminiscent of <em>React</em>’s top level API. In order to see it though, we’ll have fade away some of the layers of abstraction provided by JSX.</p><p>If you look at the React docs you’ll find an <a href="https://facebook.github.io/react/docs/jsx-in-depth.html">article explaining JSX’s compilation target</a> (and you can play around with the JSX to JS compilation <a href="https://jsx-live.now.sh/">here</a>.).<br>Specifically what might interest you is how:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6ca1a5d13a046043c4e2b86917b770b/href">https://medium.com/media/d6ca1a5d13a046043c4e2b86917b770b/href</a></iframe><p>While it might not be apparent at first glance, this API could easily be a wrapper for the functional composition we built a couple of snippets ago.</p><p>Lets replace the React Component references and props with the labels we defined in our snippet.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f36e8c9d57f9b3a77b908414de71ae2/href">https://medium.com/media/1f36e8c9d57f9b3a77b908414de71ae2/href</a></iframe><p>This might seem obvious in hindsight, but by adding a little abstraction and expanding flexibility by allowing the parent functions to have their own arguments in addition to the return values of the child functions, React has actually provided us with a highly flexible API for Object Composition. By defining a strict API for the communication between all pieces of your code, you achieve an ideal situation where:</p><ol><li>Every piece of your code knows how to talk to every other piece</li><li>Every piece of your code can be passed as an argument to any other piece</li></ol><p>I tried asking a couple of members of the React core team what they would call this design pattern, but sadly never got an answer. Considering how swamped they must be with issues, emails and life, I decided not to pursue them further and simply named this design pattern thus: C<strong>omponentisation</strong>.</p><h3>The rules of Componentisation</h3><p>Considering the componentisation API as a generic design pattern I defined a set of 7 rules which any library must follow in order to provide the composability I strive for:</p><ol><li>Every composed object must be either a Component or at most a built in type</li><li>There is one parent object</li><li>There can be multiple child objects</li><li>A parent object can receive its own arguments (rather than being limited to receiving the return value of child objects)</li><li>child objects can receive their own arguments</li><li>A parent object can interact with a single child independently and with all children as a group</li><li>A component doesn’t have to “know” its children in order to interact with them</li></ol><p>I believe that any library that follows these rules could, in theory, provide the high level of composability required to build complex systems without compromising on low cohesiveness and efferent coupling.</p><h3>Bottom Line</h3><p>The point of this article isn’t to claim that React or Componentisation is a magical solution to all your problems. Rather, that Componentisation is about providing a framework (as in a basic structure, not as in a <em>Javascript Framework</em>) which can facilitate the authorship of loosely coupled code, with less code smell.</p><p>But it is also important to keep in mind that componentisation alone cannot promise you these traits. In order to achieve these, you must first understand <em>what it is</em> about componentisation that provides them and then you need to maintain the dicipline of constantly making sure the code you author sticks to the framework and only deviates when absolutly neccesery.</p><p>I don’t know about you, but I find the fact that the Componentisation API is becoming so ubiquitous thanks to React, a really exciting point. It means we can start using this approach in our code, not as an <em>“awesome in-house library that new developers need to learn”</em> but rather a well accepted approach to modeling code, that developers coming from the React ecosystem already know how to reason about. It means we can start <strong>thinking in components</strong> throughout our codebase, not just the UI.</p><p>And so this has been my quest for the secret of componentisation. When I set out I had no idea what I might find, if at all, and there were points where I almost gave up on the search. And like all good quests, this one leads to another one, a sequal if you will, to find a way to start using componentisation in more aspects of my work, not just building UIs, to which we’ll come in a future article I’m working on.</p><p>In addition I’m putting together an article in which I will present you with various design patterns we can follow with componentisation which can help you take advantage of the componentisation paradigm.</p><p>So there’s plenty to look forward to!</p><h3>Learning more</h3><p>Following the React core team on Twitter and Github provided true insight into how this API is transforming the way developers are writing code since the library was introduced.</p><p>For example, the following thread by Sebastian Markbåge caused my brain to turn inside out at one point.</p><blockquote><em>I refer to the number of components in your application layer, component layer, layout layer, rendering layer as: Horizontal surface area.</em></blockquote><blockquote><em>— Sebastian Markbåge (@sebmarkbage) </em><a href="https://twitter.com/sebmarkbage/status/704737113007259648"><em>March 1, 2016</em></a></blockquote><blockquote><em>The number of layers for a feature is: Vertical surface area. React can expand to include more verticals without growing horizontally.</em></blockquote><blockquote><em>— Sebastian Markbåge (@sebmarkbage) </em><a href="https://twitter.com/sebmarkbage/status/704737495448055808"><em>March 1, 2016</em></a></blockquote><blockquote><em>This is what will keep React from becoming a huge monolith yet get synergy effects from vertical integration. </em><a href="https://t.co/oukDDLw7a6"><em>https://t.co/oukDDLw7a6</em></a></blockquote><blockquote><em>— Sebastian Markbåge (@sebmarkbage) </em><a href="https://twitter.com/sebmarkbage/status/704738517641920512"><em>March 1, 2016</em></a></blockquote><p>I <a href="https://twitter.com/sebmarkbage">would</a> <a href="https://twitter.com/dan_abramov">highly</a> <a href="https://twitter.com/_chenglou">recommend</a> <a href="https://twitter.com/zpao">following</a> <a href="https://twitter.com/soprano">them</a> and reading the <a href="https://github.com/facebook/react/issues">issues on the GitHub repo</a>. I’ve learned alot from doing just that and I’m sure you will too.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=76a0d609a103" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using ES6's Proxy for safe Object property access]]></title>
            <link>https://medium.com/@chekofif/using-es6-s-proxy-for-safe-object-property-access-f42fa4380b2c?source=rss-183a877031e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/f42fa4380b2c</guid>
            <category><![CDATA[proxy]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Gidi Meir Morris]]></dc:creator>
            <pubDate>Sun, 07 Feb 2016 22:08:11 GMT</pubDate>
            <atom:updated>2018-07-15T20:24:21.791Z</atom:updated>
            <content:encoded><![CDATA[<p>I can’t get over the feeling that ES6 (still refusing to say ES2015 ^_^) is a kind of playground offering me all these new toys and attractions to play around with.<br>Currently my focus is on exploring the Proxy object which gives us a new way to intercept fundamental operations on Objects, among them property access, which is something I want to play around with today.</p><h4>TL/DR</h4><p>Sick and tired of accessing a nested object under a property in a deep object just to have the <strong><em>Uncaught TypeError: Cannot read property ‘foo’ of undefined</em></strong> thrown in your face?<br>Well, by using a Proxy you can get around those errors without excessive key checking in a reusable manner.<br>The code example for doing this is down bellow, an <strong>npm</strong> module is available via <strong><em>npm install safeobj</em></strong>, with the source on <a href="https://github.com/gmmorris/safeobj">Github</a>.</p><p><strong>Please continue reading at:</strong> <a href="http://gidi.io/2016/02/07/using-es6-s-proxy-for-safe-object-property-access.html"><em>http://gidi.io/2016/02/07/using-es6-s-proxy-for-safe-object-property-access.html</em></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PmrsNSNMsoDBD72ESL_Wrg.png" /></figure><h3>I’m No Longer Blogging On Medium</h3><p>I love Medium and I think they’re doing some great things, but I’ve reached a point where the limitations of Medium are getting in the way of my blogging goals.</p><p>You’re hereby invited to read my content on my personal blog at: <a href="http://gidi.io/"><strong><em>Gidi.io</em></strong></a></p><h3>You Can Read The Original Article Here:</h3><h4><a href="http://gidi.io/2016/02/07/using-es6-s-proxy-for-safe-object-property-access.html">http://gidi.io/2016/02/07/using-es6-s-proxy-for-safe-object-property-access.html</a></h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f42fa4380b2c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Turning Spaghetti into Lasagne]]></title>
            <link>https://medium.com/@chekofif/turning-spaghetti-into-lasagne-e93ee16d4090?source=rss-183a877031e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/e93ee16d4090</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Gidi Meir Morris]]></dc:creator>
            <pubDate>Sat, 29 Aug 2015 15:17:16 GMT</pubDate>
            <atom:updated>2015-08-29T15:17:16.782Z</atom:updated>
            <content:encoded><![CDATA[<p>just over a year ago I started a project which I went into thinking ‘<em>I’m not sure how to do this</em>’ and now, a year and a half later, it is finally <em>almost </em>done and I find myself reflecting on what I’ve learned from the process.</p><p>I thought I’d share a few of these lessons, as perhaps, someone else might find them relevant to their job as well.</p><h3>What was the job?</h3><p>I’ve been doing Web Development for about 12 years now, and during that time I’ve gone back and forth between the Front End and Back End of things, but since my stint as Pluralis’s Senior Front End developer I’ve found myself gravitating heavily to the client-side.</p><p>Just over a year and a half ago I took the role of Lead Front End Architect for Loveholidays.com. Yes, thats an explosive title that means absolutely nothing other than the fact that I am now responsible for anything relating to the e-commerce company’s actual website.</p><p>In essence I was hired because there was a growing feeling in the company that the Front End was becoming the <em>weakest link</em> in the company’s codebase, and as the company was in the early stages of a mass expansion (in my time in the company, so far, we have grown from 15 employees to over 180) the CTO wanted to make sure it wasn’t the Front End that caved under the pressure.</p><p>In the year and a half prior I had been working as a freelance consultant for multiple companies, mostly helping them figure out Front End architectures for their greenfield projects.<br>This challenge was going to be quite different for several key reasons:</p><ol><li>The site was already up and running, serving high traffic, generating revenue and there was a fear that a major overhaul of the codebase could do more harm than good.</li><li>The company was, and is, growing at a rapid pace and there was no time to <em>take a year off dev for a rewrite</em>. Any rewrite would have to be done in conjunction with product development.</li><li>Like any startup environment the product had gone through many different prototypes, contradictory directions and an overall approach of “duct tape it and deploy it”.</li><li>There was never any Front End leadership in the company, and over 5 years the company had had multiple Front End developers, many of which were contractors with contradictory approaches.</li></ol><p>The codebase was, to put it mildly, Spaghetti. I know I might be stepping on the toes of some former employees here, but we, as professionals, must take responsibility for our work. I am still ashamed by some of my past projects… thank god they are no longer in the air.</p><p>When I say Spaghetti, I don’t mean mild problems like “tight coupling”, which would imply a pile ofSpaghetti which fell off the plate.<br>No, when I say Spaghetti, I mean more like <em>Four different types of Pasta, one of them severally overcooked into a mush, tossed around by your two toddlers and smeared on the walls; sauce and all</em>.<br>OK OK, now I <strong>know</strong> I’m stepping on toes… and stomping on them.. but it really was that bad.</p><p>The key problems in the Front End code were:</p><ol><li>Almost no compartmentalization of the code. Most of the code was in variables on the global scope. That which wasn’t, was defined in two separate package libraries — one being Require.js (and an old outdated version, at that) and the rest was in some kind of in-house attempt at MVC which didn’t do much of a job in separating concerns as the definition functions shared a scope.</li><li>Components all communicated through document level event triggers on the document. So you never knew which component was talking to which and when. Try debugging that.</li><li>A rapidly evolving product led to a lot of redundant code which didn’t actually do anything, but was none the less, in the codebase. This also led to a lack of separation of concerns with different code, effectively, doing the same thing.</li></ol><p>I’m sorry if I sound harsh, especially considering I’m talking about what is now <em>my codebase</em>, but I have to respect the company’s CTO for identifying and flagging this problem and prioritising it enough to push for the company investing time and money into rewriting the codebase before it broke.</p><p>And now, a year and a half later, there is (almost) no legacy code left on the site, the whole site has been rewritten, with no downtime and with rapid product development.</p><h3>So, what have we learned?</h3><p>So how did we do it? Well, here are the lesson we learned, which also explain how we did it.</p><h4>First Lesson: Don’t sell a rewrite, sell a feature</h4><p>The CTO and I realised that selling a rewrite to a non techie CEO would be no easy feat. To do so we would have to provide a real, business oriented, added value to rewriting the code.<br>Very early on we picked up on the CEO’s interest in improving the company’s revenue by A/B testing different portions of the site. It was also clear to me, based on my experience with Pluralis, that achieving this with the current code base was nearly impossible.</p><p>Hence, selling a rewrite of components throughout the site, with the express intention of AB testing their functionality would be easier to sell to the CEO.<br>We explained what we were doing, and that it would allow us to both develop new functionality and provide an ability for A/B testing but that due to our need to improve the quality of the codebase, it might take a little longer than planned (and with a few late nights we could cram in more than expected).</p><p>Understanding that <em>quality</em> is important, in addition to the <em>revenue</em> growth and <em>functionality</em> improvements, we found selling the rewrite a rather easy (even if lengthy) process.</p><h4>Second Lesson: Don’t rewrite pages, rewrite functionalities</h4><p>The first stage in rewriting the site was to start turning our Spaghetti into Lasagne.<br>To avoid a clash between our new code, which much of it would have to use similar (if not identical) module names and components to the legacy code, we would use <a href="http://gmmorris.github.io/Modularizer/">The Modularizer</a> which would allow us to sandbox our new architecture and slowly grow it until it effectively takes over the whole site.<br>You can read more about this component <a href="https://medium.com/@chekofif/why-on-earth-would-you-create-yet-another-package-manager-for-javascript-a5c5a232b3f">here</a>.</p><p>Once in place we could start building a proper MVC layered architecture to build our site on top of.</p><p>Our Lasagne is made out of a stack of components (what is commonly referred to as N-Tier), layered on top of each other (hence, the Lasagne) to allow a logical progression and separation of concerns.</p><p>In effect, what this allowed us to do is convert pieces of the site’s functionality, one by one, stripping them out of the old legacy code and inserting new components in their place, providing a unified way of communicating between the two layers, but more on that in lesson three.</p><h4>Third Lesson: Analyse the legacy codebase’s weakness and exploit it</h4><p>The legacy codebase, with its many shortcomings actually provided us with one benefit. The usage of document level events to communicate between components, which is a very unmaintainable way of managing communication between components, is also very loosely coupled.</p><p>What I mean by that is that if a component, lets call it the <strong>StateController</strong>, needed to access the component in charge of calculating a navigation state, lets call it the <strong>NavState</strong>, it would do so by triggering an event on the page like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/64f33de4c4e80d2d04e883a668bd2611/href">https://medium.com/media/64f33de4c4e80d2d04e883a668bd2611/href</a></iframe><p>In turn, the NavState would then answer this document level event.<br>Very unmaintainable, but for us, this was actually a huge benefit!<br>We could build a new NavState object, wrap it in our new architecture, and then tap into the same document level event communication to communicate with legacy components, such as the StateController, until such time as we get the chance to rewrite said StateController, at which point we could change their communication to a more logical and maintainable method.</p><p>That said, obviously, we didn’t want our components using a global messaging method like that, because that would mean us rewriting our code into the same mess we were trying to escape.<br>Which leads me to the next lesson.</p><h4>Fourth Lesson: If its external, only reference it in one place</h4><p>Like most N-Tier architectures, at the bottom, we have a layer of controllers and routers, which provide us with the key logical tiers of the code.</p><p>Thats a lie though, because it isn’t really the bottom.</p><p>Under that layer there is another layer — thats the library layer. The layer of external components we import and use to avoid writing everything from scratch our selves.<br>One of the lessons I learned early in my career, obviously the hard way, is that if you use a library it might, very likely, become a hindrance when your functional requirements diverge from the library creators’ assumptions.</p><p>Sometimes this will lead you to replacing the library with another one, or, more likely, you’ll have to hack around it’s functionality to support your own evolving requirements.</p><p>To provide us with a easier way to handle this kind of issue, we have inserted another layer between our controllers — our <em>engines</em>.</p><p>An engine is a component that separates out application from a library. All (or almost) library functionality goes through an engine, which means we have one single entry point to any external service or functionality, and hence, if (and when) a need for the removal of a library or a shim around it’s behaviour arrises — we only have one place to make the required changes.</p><p>Remember our document level event problem? How did we approach it? Well, we treated it like an external library.<br>A special component, called the <strong>LegacyProtocol</strong> was created. It would be the entry point for every communication with the legacy environment and it’s messy document level event methods. Any component in the new architecture interested in communicating with legacy components would use the protocol to setup and use that connection.<br>Hence, once we were ready to rewrite a component, we knew exactly where its communication was going and coming from, which made it much easier to remove later.</p><p>When we thought we’d finished our rewrite we checked the protocol to make sure no communication was still going through it. Now we could be certain no more legacy functionality was still being plugged into and we removed the protocol. Happy days!</p><h3>Whats next?</h3><p>There are actually more lessons we’ve learned, but I feel this article is long enough, so we’ll leave those for another day.</p><p>The next step for us though, is advancing our codebase to an even better place. The continued growth of the company seems unavoidable and one of our challenges now, like most tech companies, is recruitment.</p><p>Recruiting good people is very hard, and one of the key requirement good developers have of their employers is this- <em>I don’t want the codebase to piss me off</em>.<br>If it is a nightmare for me to work with your code, I need to either get the opportunity to fix that or I don’t want to be part of the company.</p><p>I’m happy to announce that the code no longer pisses me off. I love it. Its elegant, maintainable and most of all — built for 2015.<br>But now, we want it to grow. No more <em>Code of 2015</em> but rather, <em>Code of 2017</em>.<br>For that to happen we’re now dipping our feet in the deep waters and experimenting with moving our modules from the Modularizer’s capable hands, into those of ES2015&#39;s module system along with Babel to use them in our browsers. We’re starting to convert our views from Backbone views into React views and we’re looking into more technologies that are meant to both make our code more advanced and to assist in exciting our existing, and hopefully our future, developers on their day to day tasks.</p><p>Exciting days ahead!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e93ee16d4090" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why on earth would you create yet another package manager for Javascript?]]></title>
            <link>https://medium.com/@chekofif/why-on-earth-would-you-create-yet-another-package-manager-for-javascript-a5c5a232b3f?source=rss-183a877031e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/a5c5a232b3f</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[requirejs]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Gidi Meir Morris]]></dc:creator>
            <pubDate>Sat, 29 Aug 2015 12:45:41 GMT</pubDate>
            <atom:updated>2015-08-29T13:00:19.831Z</atom:updated>
            <content:encoded><![CDATA[<p>I recently got asked this question by a coworker…. as I tried to explain I found myself going back four years to my next to last job, where I developed the component I recently released known as <a href="http://gmmorris.github.io/Modularizer/">The Modularizer</a> at one of the most exciting rollercoasters I ever rode — the now deceased A/B testing startup Pluralis.</p><p>As I explained to my coworker what the needs were it occurred to me that as I also found a fresh usage for this component in my current role as Lead Front End Architect of Loveholidays, it might be worth explaining this publicly, so that perhaps it can help another company somewhere, now that I’ve open sourced it.</p><h3>What is it?</h3><p>Well, simply put, Modularizer is a simplified Javascript package manager, built using the now standard AMD API (whose most widely used implementation is Require.js), which can exist completely independent of the global scope, unlike other AMD implementations which usually rely on a global-scoped set of functions.<br>You can read more technical details about it’s usage on the documentation page <a href="http://gmmorris.github.io/Modularizer/">here</a>, but what I’d like to explain in this post is the answer to the simple question: <em>Why bother?</em></p><h3>Why Bother?</h3><p>While Modularizer was created in 2011 (and of course maintained since then; It isn’t a relic, but hasn’t been open source for long, so I’m the sole maintainer), and today perhaps other available options are better, I still found it an ideal component for the recent rewrite of the Loveholidays website, which I spearheaded and recently completed.</p><p>Like I said, the main difference between Modularizer and other implementations like Require.js, is that it stays clear of the global scope. When I want to put together a package of JS modules, I simply create an instance of a Modularizer package and define the module inside of it, using the same API as RequireJS uses, but in a sandboxed environment, so that no clash can happen between the modules in my package and other packages on the page.</p><p>Usually this isn’t much of a requirement, because you usually have sole control of the JS modules on your pages, so there is little-to-no danger of a clash, but there is another situation where you might need a module package manager where the danger of a clash is very high — an embedded component which is injected into other people’s websites, and thats where Modularizer came into play and where it had it’s first <em>fire-test</em>.</p><h3>The Pluralis Use Case</h3><p>To understand the need you must first understand what Pluralis did.<br>Pluralis was a crowd sourced A/B testing platform.<br>It tried to create a mix between the personalised A/B testing environment provided by the likes of <em>Optimizely</em> and the crowd sourced platforms like <em>99designs.</em></p><p>** take a deep breath and read the next two paragraphs quickly **</p><p>The idea was that a client would visit our site, they would input their site’s url and would immediately be presented with their own website, but with our code injected into theirs, so that they could start marking sections they wanted tweaked and improved.<br>Our crowed sourced community would then be able to edit part of the client’s site, using an on page WYSIWYG editor, to optimize the client’s page for him. Whoever optimized the page to the best result (with Pluralis actally performing the Conversion Rate measurment between the different versions) would win a the contest and the client would pay the winning optimizer a bounty, after which, once all parties are paid, the client would receive a document explaining how to implement the optimized version on their site.</p><p>** OK, breath *</p><p>That was the idea… its not that relevant to this post, but rather the architecture required for this is the point. Pluralis had a need for a very complex Front End architecture (which, as the sole FE developer for the first 9 months of the company, rested solely on my shoulders), in which we had the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*Pcryv5Y-2KjROGGrQ0MSCw.png" /><figcaption>Over simplified, but it gives a vague idea of the complexity any module manager would need to handle for our use case</figcaption></figure><p>This is an over simplified little slice out of an entire architecture, but the challenge for any module manager we chose was this: It had to be able to handle sharing different modules between several different packages, where each package had it’s own dependancies, own environment to live in and had to allow us a comfortable working platform for both Dev environments and Production environments.</p><p>These modules would also have to live well on both our own website and our clients’ websites. And as the saying goes <strong><em>its</em></strong><em> </em><strong><em>a jungle out there</em></strong>.<br>The moment your code needs to live on other people’s website, it has to be as encapsulated and safe as possible.<br>If our product were to cause clashes on our clients’ websites and product — we were as good as finished. No one will use a product that hurts their website, and we had to be able to assure them, and our selves, that our code cannot clash with theirs no matter what.</p><h3>The Technical Requirements</h3><p>When Amnon Lahav, my CTO at Pluralis (and in fact, the only other developer in the room for the first 9 months or so), and I sat down to discuss our needs for a module package manager we stood in front of a white board and wrote down every requirement we had when analysing the existing libraries:</p><ol><li>Has to be cross browser compatible, supporting everything from IE 7 and up.</li><li>Has to provide file fetching as well as module management.</li><li>Has to be as usable for us in Dev environments as on production, allowing us to work with our modules in both individual-file mode for easier development and debug, and also in concatenated and minified mode for production.</li><li>Wouldn’t clash with identical libraries on the client’s site — we couldn’t have our RequireJS environment clash with the client’s Rquire.js, and that kind of thing.</li><li>Would make it comfortable to maintain dependancies and make module names easy to handle (At the time Require.js only supported filenames as module identifiers, and we didn’t want the file’s location to limit our ability to reference a file, as a module would be shared between different packages at the same time. Today R.js can handle this, at the time, it couldn’t).</li></ol><p>No package manager we could find (and we tried every single one that existed in 2011) could handle all of these requirements. Require.js came close, but fell short when used embedded in clients’ sites, and so the need to build our own came up.</p><p>I hacked up Modularizer in a week and I’ve been maintaining it ever since.<br>I only got permission to Open Source it last year when Pluralis finally bit the dust, and the first thing I did was repurpose it for Loveholidays’s rewrite for which I knew I needed a library to live side by side with a local Require.js installation, but more on that later.</p><h3>Is it really still useful?</h3><p>I’ll be honest- I’m not sure it really is.<br>Its important to understand that when Modularizer was first written, in 2011, other options, such as Require.js, were still very young (a year old at most), lacking in features and build tools, and still had many bad assumptions in their code, such as code going directly to <em>window.define() </em>which made it almost impossible to keep them off of the global scope.</p><p>Today this is far from being the case.<br>First of all — we have Node.js and CommonJS on our side, and with ES2015 modules coming into play now, there really are many far better options.<br>Second — the better maintained tools like Require.js with it’s R.js build tool are far more feature rich and adaptable to our needs.</p><p>So why bother with Modularizer?<br>Only one reason — its a piece of engineering I’m still proud of. :)</p><p>This post is, by no means, a <em>call-to-action</em> on an open source project. It is simply me sharing my thoughts about a component I developed, which I think provides a slightly different approach than I’ve seen in other places. At least, in the Front End world (Most of the concepts behind Modularizer are “stolen” from Back End languages, and merged with Front End ones)…</p><p>… and Modularizer has served me well in both Pluralis and Loveholidays. I’ll be writing a more extensive article about the rewrite of Loveholidays.com, but that is for another day.</p><p>Cheers.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a5c5a232b3f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simmer.js: A Javascript reverse CSS selector engine]]></title>
            <link>https://medium.com/@chekofif/how-i-built-a-reverse-css-selector-engine-42cb8062a8be?source=rss-183a877031e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/42cb8062a8be</guid>
            <category><![CDATA[css]]></category>
            <category><![CDATA[jquery]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Gidi Meir Morris]]></dc:creator>
            <pubDate>Sun, 26 Jul 2015 16:37:05 GMT</pubDate>
            <atom:updated>2018-06-28T23:37:24.519Z</atom:updated>
            <content:encoded><![CDATA[<h2>Simmer.js: How to build a Javascript reverse CSS selector engine</h2><p>As Front End developers we use CSS selector engines all the time. Most commonly, Sizzle, which is baked into jQuery.<br>The selector engine provides us with a comfortable way to search for elements on the page in the same way as we’re already used to doing in our stylesheets, which makes it simple for us to wrap our brain around.</p><p>But what happens when instead of having a selector in hand and finding it’s corresponding element on the page, what I have is the element and what I want is a corresponding CSS selector?</p><h4>tl;dr</h4><p>I’ve built and open sourced an engine which generates a CSS selector for an element on the page so that you can persist it and find it later, and its on Github at <a href="https://github.com/gmmorris/simmerjs">SimmerJS</a>.</p><h4>But why?</h4><p>Why indeed? Well, why not?<br>There are many situation which I can think of in which we would want to translate an element to a persistable selector.<br>The specific reason I had to develop this was a project I worked on a while back for an AB testing company. This company, which has now sadly gone the way of all flesh, provided it’s users with the ability to mark parts of their page and then generate scripts which would find those parts on every page load and replace them with some predefined content.<br>This was done on the Front End for many reasons, which I won’t go into, but what is important to note is that this tool could be used anywhere on the web, ranging from simple static landing pages to complex web apps- hence the saying <em>It’s A Jungle Out There</em> was more accurate than ever for this particular project.</p><h2>The Concept</h2><p>Approaching the challenge of building this reverse selector engine, I made note of several issues I’d have to deal with:</p><ol><li>The pages in which this engine would be used could be dynamic, so identifying the element consistently was going to be very tricky. Any selector would have to be specific enough to consistently choose the right element over and over again, but it wold also need to be flexible enough to deal with changing pages with revolving DOM structures (a very common occurrence on advertising landing pages which often contain dynamic ad sections).</li><li>The selectors created by this engine would have to be relatively backwards compatible because many of our clients were targeting a very wide audience with relatively older browsers, so we’d have to keep our usage of CSS3 down to a minimum, and always opt where possible for simpler selectors.<br>This also meant that we need to take into account how quirky some browsers are in regards to CSS selector parsing, such as broken nth-child selectors in older versions of IE.</li><li>We would often have to run our components on auto-generated HTML pages or pages maintained by people whose understanding of HTML was mediocre at best, hence we would have to deal with malformed HTML (to a point), getting around broken tags or duplicate IDs on the page.</li></ol><h3>The Challenge</h3><p>The way to approach finding a unique selector for an element is to find a balance between the most specific way of describing an element to the one with the least number of moving parts.</p><p>To understand what I mean, lets look at these two CSS selectors and associated DOM:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dacfbae931ed0d0e2d8dee6860bb2c54/href">https://medium.com/media/dacfbae931ed0d0e2d8dee6860bb2c54/href</a></iframe><p>It is very clear that if the element we’re trying to parse is the <strong>em</strong> element then both of those selectors will work, but knowing that the first is shorter (less elements in the query) we have a little more wiggle room for the danger of that DOM being changed by some dynamic content.<br>That said, it is also true that the shorter selector is also less specific, and is more prone to becoming invalid if the dynamic content generates some element on the page that happens to answer to the same selector.</p><p>This balance, between specificity and flexibility, is a tough nut to crack, but I think I’ve managed to achieve a working one. The current implementation, which I’ll describe in a moment, has been used on hundreds of sites and has performed at a very satisfactory level. For the vast majority of pages and tests we managed to achieve very predictable results.</p><h3>The Building Blocks</h3><p>At first, the process of calculating this well balanced selector wasn’t as clear as I’d thought it would be when I first started.<br>Like every Front End developer I knew that different CSS selectors have different strengths. For example I knew that an ID is stronger than a class and that a selector consisting of a class and a tag was weaker than two class, but I wasn’t sure how exactly that calculation is done and I had to make sure this calculation is done to the letter, otherwise I might end up with a result which is different than that which the selector engine would actually need when deciphering the selector later.</p><p>For this I had to dig into the actual spec for CSS. Something I never thoght I’d actually need to do.<br>Luckily for us, the spec is actually <a href="http://www.w3.org/TR/css3-selectors/#specificity">very clear and readable</a> in regards to calculating what we call a selector’s <strong>Specificity weight</strong>.<br>The Specificity Weight is the score we give a selector by which the browser can then decide which CSS selector “wins” when choosing how to style that element.<br>In our case we don’t need to choose between two competing selector. What we need is to find a selector which is specific enough to narrow down a whole document to a specific element of interest.</p><p>Essentially the guideline for calculating specificity is this:</p><ol><li>IDs are worth 100 point each.</li><li>Classes, attributes and pseudo classes are worth 10 points each.</li><li>Tag names and pseudo elements are worth 1 point each.</li></ol><p>To be accurate, as my Friend <a href="https://github.com/RonnyO">Ronny O</a> (of <a href="http://www.reallygood.co.il/">Really Good</a>) likes to remind me, this isn’t a standard Base 10 system (you can <a href="https://css-tricks.com/specifics-on-css-specificity/">learn more about that here</a>) but for the sake of this article it simplifies the explanation.</p><p>Knowing that this is the hierarchy of specificity really simplifies things, because it means that if we can generate a selector using an ID or even a pair of IDs, then we should have a winner. It also tells us that if we need to resort to a selector made out of a bunch of classes we’ve got a very weak and breakable selector.</p><h3>The Algorithm</h3><p>The sequence for finding the most specific selector while still maintaining a less breakable structure boils down to the following</p><ol><li>HIERARCHY = A stack of the element’s hierarchy, climbing up the DOM tree</li><li>For each element in the HIERARCHY analyse the following in sequence:</li></ol><p>2.1 Check for an ID<br>2.2 Check for unique Attributes<br>2.3 Check Tags<br>2.4 Check sibling relations</p><p>3. Merge the resulting stack into a single CSS selector</p><p>Seems simple enough, right? Well, not quite.<br>Here are some notes about this sequence:</p><h4>Hierarchy</h4><p>Lets start by looking at the hierarchy we build.<br>If you’ve looked at the library you may have noticed we have a <strong>depth</strong> configuration value, which by default has the value <strong>3</strong>.<br>This means we will, by default, only look at the element’s parent and grand parent when calculating the selector.<br>I found this to be the best depth simple pages but on our production version for the AB testing component we actually expanded that to a depth of <strong>6</strong> because we found that gave us the best balance between specificity and flexibility. The reason we had to climb so high was due to most of our clients using very “semantically bad” web pages, with tables in tables in tables, which meant they often needed a selector for a TD element with no classes or IDs on it’s ancestor row and table.</p><h4>IDs</h4><p>Checking for a ID on an element in the hierarchy seems straight forward enough, but as I mentioned — its a jungle out there. More often than not we fond that people do not adhere to the whole <em>only one unique element to an ID</em> <em>paradigm</em>.</p><p>Checking for a ID on an element in the hierarchy seems straight forward enough, but as I mentioned — it’s a jungle out there. More often than not we fond that people do not adhere to the whole <em>only one unique element to an ID</em> <em>paradigm</em>.</p><p>Though that may seem ridiculous it actually is a serious problem, because if you have two elements with the same ID you may think you have a specific selector in hand, but in fact, it’s useless. So when using an ID you also need to check it’s uniqueness.</p><p>You may have noticed we check for unique attributes before testing for classes. This may seem like a surprising choice, but it is actually for a very simple reason — unique attributes, such as a unique HREF or SRC attribute are more common than a unique class. Since classes are by their very nature reusable, it is actually unlikely to find a unique class on an element.</p><h4>How far do we go?</h4><p>Another thing to note about the algorithm is that, in theory, it could end up with a very complex selector which breaks our need for flexibility and simplicity in our ideal selectors.</p><p>To get around this we have specified another configurable property called <strong>specifictyThreshold</strong>. This threshold is esentially the minimum specificity weight we need our selector to reach before we say enough.</p><p>If, for example, I find a selector which consists of a unique ID on the page, then I have a specificity of 100 points. This means that if I set my threshold to 100 points, the engine will deem this selector as <em>specific enough</em> to quit the parsing process.</p><blockquote>#myUniqueElement</blockquote><p>That is, assuming, that the IDed selector is specific enough to provide us with the original element. If, for example, the ID isn’t on our element, but rather on our element’s parent, we may need another layer in there — such as the element’s tag, and we’ll finish with a elector worth 101 points in specificity, such as:</p><blockquote>#myUniqueElement &gt; em</blockquote><h4>Sibling relations</h4><p>The last point I’d like to point out is sibling relation selectors.<br>What I’m referring to here is such selectors as <em>:nth-child(x)</em> or <em>:nth-of-type(y).</em></p><p>These selectors may seem the ideal selectors for this kind of unique selector generation, but in fact, I found these to be severally lacking.<br>The main reason is that these are actually very unreliable on malformed pages. In fact, so much so, that I had to disable <em>:nth-of-type</em> all together!</p><p>This is why we keep these selectors to the end, as a sort of last resort, and we avoid them when possible.</p><p>There are several situations where these selectors break down. These are often browser specific problems, rather than a general rule and you may be surprised to hear that IE is far from being the only culprit, with Safari, and even Firefox, actually giving me much grief as well.</p><p>Here are several examples:</p><ol><li>When you have a self closing element, such as a <em>&lt;br/&gt; </em>the behaviour is very erratic, with some browsers skipping these element when they aren’t closed properly, which can then, accidentally, be skipped by their implementations of <em>:nth-of-type</em>.</li><li>When a block element is placed in a non block element, such as a div inside of a paragraph, some browsers will eject the div from the paragraph, while others might duplicate the paragraph element, placing it twice on the page. Which seems to effect the pseudo-selectors worse than the class and ID selectors, as the browsers usually skip the duplication of these attributes.</li></ol><blockquote>&lt;p&gt; &lt;div&gt;Lorem Ipsum&lt;/div&gt;<br>&lt;/p&gt;</blockquote><p>Might become:</p><blockquote>&lt;p&gt;&lt;/p&gt; &lt;div&gt;Lorem Ipsum&lt;/div&gt;<br>&lt;p&gt;&lt;/p&gt;</blockquote><p>There are several more problems with these selectors which make them less reliable than their counterparts.</p><h3>The vetting process</h3><p>At this point there was still one more element to this puzzle. How would this engine actually check which selectors work best?<br>Sure, we can calculate a very specific selector, but that doesn’t mean it really is unique.</p><p>To address this issue I built Simmer with an internal mechanism for finding the best possible query engine for it to use.<br>We knew that we’d be packaging jQuery with both our AB testing manipulation library and Simmer’s editing environment, so we knew that we could count on the same algorithm being used wherever we used Simmer, but I also wanted the engine to be a comfortable engine to drop-in in other projects.</p><p>To that end Simmer has been equipped with a “super complex mechanism” — it asks the page what it can use.<br>It looks around for a jQuery engine and if it find one — it uses that. If it can’t find jQuery it searches for a stand-alone Sizzle engine. If that can’t be found either it defaults to the browser’s <em>document.querySelectorAll</em> implementation.</p><p>It also provides a configurable callback for querying in case the developer want to use an other DOM querying language.</p><h3>Final thoughts</h3><p>I loved writing Simmer. It was a fun and challenging project, but I don’t think it is anywhere near done. I still have so many ideas, such as expanding configuration to give developers a higher level of control over the final selectors and allowing developers to plug in their own parsing functions to expand support for custom doctypes and tags (React module and JSX support, perhaps?).</p><p>As CSS3 gains wider adoption (we’re almost there now, aren’t we) additional selectors could be, and should be, added into the mix. Also, as Selector Level 4 (wrongly referred to as CSS4, while in is in fact extensions to CSS3) gains adoption we’ll need to add these too.</p><p>I’ve been involved in two companies that needed this tool, so I have a sneaky suspicion there is more need for Simmer than initially meets the eye. Though legal realities forced me to set Simmer aside for a while, it is now officially Open Sourced and available for usage, so I can finally put the effort into it that it deserves.<br>I hope more people find this library useful and find an interest in helping me take this code base to the next level.</p><p>The <a href="https://github.com/gmmorris/simmerjs">Github repo</a> is waiting for you!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=42cb8062a8be" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>