Tuesday, March 08, 2016

What?! Pre-interview code challenge?!

It doesn't matter how many years of experience you have, how many coding challenges you've solved in your spare time or how many open source contributions you've made, companies still want you to complete their tests or coding challenges. We can debate it but there's very little we can do about it.

But then I bumped into a new beast - pre-interview tests. You write a coding challenge in order to secure an interview. In other words, don't bother to send in you resume unless you spend several hours writing a program. And as in this instance (I'm not going to disclose the company's name) they ask you to make it public on github. Great idea ... if you want to analyze other people's approach :-) And that's what I did.

After analyzing several repositories I found a few patterns. Their common denominator is:

People don't really read requirements.

And here's why.

  • The code should be written in Java but you can see people trying to outsmart by providing Python, Go or Haskell version. It may be fine as a supplemental implementation to impress the recruiter but not on its own.
  • None of the implementations I could check came with any tests at all! Unit, integration, anything. That's a very bad sign. Although I personally don't advocate TDD I still think that tests are important, especially when it comes to coding some system logic. They also speed up the development cycle as you don't need to build and run your application just to check it the code works.
  • Although people were asked to structure their application as if it was a production grade application some still put everything in one class.
  • Missing documentation. At least javadoc, better yet stand alone handcrafted document outlining how the application works.
  • Build instructions. People rarely bother to write quick instructions about how to build the application. Although it may be clear that gradle/maven has been used it's still nice to provide this in writing.
  • Some assume that a CSV document can be created with simple List.join(",").
  • Some even assume that parsing JSON means splitting a string by ,.
  • People ignore application return values. They just spit out an error message and bail out (so the return value is 0 indicating successful completion).

Advice to people applying to those companies. It's unlikely you're the first one applying. Search github, bitbucket, gitlab and the likes. That can give you an idea about your competition. It could also help you out with your code should you get stuck.

And a closing message to companies trying to make it easier for them by asking people to write code before applying. Asking people to code first, massively limits the pool of people applying. There are thousands of great developers who don't want to spend hours coding just to wait if you come back to them. I'd also say that it attracts wrong kind of people.

  1. people who like to solve challenges and have time to do that but are not really into a long term relationship
  2. people with little experience hoping to skip the line
  3. cheaters who smell a chance of landing a job

I still think it is much better and efficient to do a quick screening (via skype or the likes) and ask a candidate to join you for a day (preferably pay the person for working with you that day).

Friday, February 12, 2016

A farewell to Node.js ... for now

It's tough. Choices have to be made and decision taken.

After spending years experimenting (and even professionally working) with Node.js I think it's time to say farewell and thank for all the fish.

Why?

There's several reasons but the main for me are:

Asynchronous IO is tough. Really tough. Callbacks, Promises, Async/Await all that stuff. Sure, Async/Await will make this easier but under the bonnet it will still be async. I think the cause of this is that we think sequentially - do this, do that do something else. Not do this for user1, do this for user2, do this for user3, do that for user1, do that for user2 ... The fact that we serve several users at a time doesn't have to be reminded to us all the time.

Debugging and testing is pain with callbacks. As async functions are normally executed in their separate threads you lose continuity when your functions resumes. It's hard to track down where you'd been before the callback got called. And simulate callbacks in unit tests doesn't help either.

Dependency management. NPM everywhere. Node.js doesn't have its own packaging, module system and relies on NPM to do the hard work. Problem with NPM is, that if package A depends on package B and that depends on package C it's all nested and not reused. That means, you can end up with hundreds of packages C in your project. I still remember my Ghost blog project. It downloaded 500MB of NPM modules just to run!

Spartan core libraries. Node has very limited number of standard functions. It means that even for simple tasks like working with dates you have to turn to libraries like moment.js. Most of the stuff you can find in standard libraries in other languages needs to added as a dependency in Node. So you always facing decisions like `lib1 has these features but lacks some other` so you mix and match all the time to get basic stuff done.

No opinion. We all know that today's patterns and best practices are tomorrow's anti-patters. But this cycle is massively accelerated in the Node.js world. Grunt, Gulp, NPM. This is rapidly changing and there's no sign of consolidation. It's hard to operate in a world where tools are discontinued on a daily basis (with no replacement). Node needs something that would define and guarantee the basics so you can focus on programming and not worry if your code will be buildable in a year's time.

No explicit types. We can probably debate this for ages but as there's no way to enforce types (at least optionally) it's very hard to define contracts between libraries and their users. There's nothing that would warn you that a return value can't be assign to your variable. Or calling a function isn't possible because of incompatible types. That puts extra pressure on library authors to well document their code so the types are documented, somehow.

To sum it up. On front-end I'm left with no choice - it has to be JavaScript or transpiled JavaScript. For backend though, I stick to something else.

Wednesday, February 10, 2016

Striking the right balance

We all know that software can be really complex. Software solves complex problems and needs to be complex. Period. But when it comes to our toolkit we should always prefer simple, streamline solutions.

Software development can sometimes morph into some kind of competition. Some developers and sometimes whole development teams can turn into macho style super heroes producing pretty complex code just to show the world how good they are. So you want an example - look no further than AngularJs. That's what happens when smart guys over-engineer their solution.

Dependency injection for a functional language? Services, providers, factories? Parsing function bodies to figure our parameters? You get all this and more. The question is why? Functional languages always prefer explicitness before implicitness. Functional approach tries to eliminate auto-magicalness. And why have so many service types? Who needs to use magic string constants to describe what a directive can be applied to? Yes, we know the guys behind Angular are smart but do we need to worship them everyday?

Again the same story. All you get is something to learn. And once you learn it you the guys release version two do zero with no backward compatibility. And you start over again.

On the other end you have things like Hazelcast. You don't need a diploma to use it. You plug it in as a library or use it as stand-alone application and it just works. Don't be fooled. Hazelcast is far more complex than Angular. Both in terms of code and the problem area it covers. But the guys there don't try to awe you. And that's the trick. That's why React is so popular.

So the rule is - don't try to convince your users that you're a smart guy by writing complex difficult libraries - convince them (if you really need to convince anyone) with simplicity.

One rule of simplicity is to reuse whatever is out there or at least allow this existing tools/libraries/utilities to be used with whatever you're coding.

Don't be revolutionary - be evolutionary.

Wednesday, July 15, 2015

Why testing is good and TDD evil

In our lives nowadays we have safety nets everywhere. We are scared and live in a world of scaremongering. Many people are even scared to go outside. They just sit in front of their computers afraid of failure. What if I'm not seen in the best light? What if my application fails? What if someone sees my code's not perfect?

But the problems are not failures, errors and epic fails - the problem is our inability or unwillingness to accept they're here to stay. It's about how we respond to failures not about how we make sure there are no failures. It's about us accepting failures are inevitable then spending time writing tests for every possible scenario so we have excuses.

Let's be brave - hope for the best and be prepared for the worst. Roll up our sleeves take a leap of faith and change that bit of "untested" code. If there are problems let's fix them. Have a roll back solution at hand so you could revert back if your code change breaks things up.
TDD is an epic failure. And rightly so. It instils false sense of security through passing tests and creates culture of excuses through so called "test coverage" and "code metrics" - and we all know how bosses like metrics, don't they.

Let me repeat again - TDD is not cure. You can find bugs in every software. TDD was meant to eradicate bugs but the bugs are still around. TDD actually makes it harder to tackle bugs because all your passing tests lead developers to believe that the bugs are actually caused by infrastructure, 3rd party code and so on. You can always come around and say "but all my tests are passing so it's not my problem".

And let's be clear here. Tests are code. The same code we try to tests. So why your code may be buggy and your tests are always right? And what does it mean 100% test coverage? Is the code really tested to work correctly (whatever that means) with all possible input parameters and changing condition? To give you an example have a look it this Java code:

public class Adder {
    int addOne(int input) {
        return input++;
    }
}

public class AdderTest {
    @Test
    public void addOneWorksWithNumber1() {
        int result = Adder.addOne(1);
        assertEquals(2, result);
    }

    @Test
    public void addOneWorksWithNumber0() {
        int result = Adder.addOne(0);
        assertEquals(1, result);
    }
}

Is this code thoroughly tested? It appears so. Test coverage is 100%, tests passing but what value the test gives you? To answer that lets add this test:

@Test
public void addOneWorksWithVeryLargeNumber() {
    int result = Adder.addOne(0xFFFFFFFF);
    assertEquals(0x100000000L, result);
}

It's not passing! And you could probably test other edge cases. In short, we tests ONE line of code, we've already written 30 lines code to test it but we're still not 100% sure the code does what we expect it to do. So the tests are just a smoke screen. The code can still fail, it's not tested for null values, for multithreading, for runtime exceptions and so on. You could spend years of writing tests and the code could be still buggy. Remember one thing - only the code you don't write can't fail. The less code you write, the better.

Other aspect of TDD is rigidness of the solution. Any code change, and changes are the only thing that doesn't change, usually results in either retiring some tests or worse amending them. So developers try to resist changes more. They know that they will have to not only change the code, but also God knows how many tests directly or indirectly related to the code they're about to change.

TDD also leads to the new blame culture. You commit and now tests are broken. And your name is dragged around like if you were worse than Usama Bin Laden. Some organizations go to that lengths as to punish (or promote) developers according to build failures. So it pays off to sit in the corner and touch nothing.

So instead of fighting this futile battle let's just accept we can't win here by simply planning our defenses. This is akin to playing a game of chess  by simply laying out the best possible defense scenario. No matter what, you can't win this way. You have to go out to the battle and prove you can win.

I mean don't try to avoid the battle. Do not try to defer the battle by writing super defensive code. It only slows you down, distracts you from the real target. Focus on writing maintainable code, focus on quick release cycles (ideally be able to only minutes between committing you bug fix and this bug fix being released to test and prod environment), focus on clean code, use defensive programming for mission critical modules and so on. Because what really matters in the end of the day isn't bug free code but your ability to quickly fix bugs for only one thing is sure in this world - your code is buggy!

To avoid any confusion - I'm not against TDD per se (and definitely not against testing in general). It makes sense to have tests making sure core parts of your functionality (usually something related to finance calculations) are covered by tests. It also makes sense to run automated sanity testing after your CI build.

Addendum: This has nothing to do with test frameworks. They are actually quite good when it comes to ad hoc testing and verifying some functionality both in isolation (unit) and as part of a bigger system (integration).

It also makes sense to have plentiful of tests if you're lucky enough to work on a framework, library or something which is intended to be highly re-useable.

Friday, May 29, 2015

Of corporate software development cycle

This piece is based on my experience working for big corporations but somehow, surprisingly applies to smaller establishments too.

Now for a while imagine a company. This reputable company with established business model has a portfolio of applications to support that business. From of-the-shelf Microsoft Exchange server and file sharing to customized boxed products to bespoken applications. Now this company has a system, let's system A, that helps with the core od the company business. Be it selling stuff, taking orders, managing customers. The system A is pretty hairy one - has been around for a while. As the company's employees come and go the company finds itself in situation where the core developers (and subject matter experts) have left the company but the company still needs to maintain the system.

First, they try to understand the system. But it's hard. Lots of these systems are a) quite complex b) poorly (if at all) documented c) written long time ago hence using languages and frameworks no longer used. All in all the most sensible approach is to actually rebuild the system from ground up.

To do so, the company hires managers. First, managers come up with business case. Sometimes they skip this step, but that's unlikely. Once business case is approved, they come up with things like governance, procurement et cetera. Obviously they also introduce planning and estimation. In most cases, no developer is involved. And all the reported indicators show green. One of the first steps in the process of superseding the system A with system B is to call in a consultancy company. This company quickly rubber stamps the need for replacing system A highlighting many shortcomings of it. Based on their findings a PowerPoint presentation is made and shown to the whole company (department) that clearly shows incompetence and ill-thinking of the authors of the system A. Many times the presentation contains analogies to old cars, houses, broken tools - you name it, so it's absolutely clear that system A is beyond repair and the authors should be punished. After the system is has been successfully humiliated it's time to build the new system B.

It all starts with requirement gathering. It's complicated. SMEs (subject matter experts) are hard to get hold of, and if you can get hold of them they sometimes don't remember everything, sometimes even contradicting to each other. But you do the best to figure out what's needed to do. After this painful exercise you move on to the development phase. All indicators are still green and everything goes according to the plan.

You usually pick the best developers with some experience with the system A. Any work on system A is put on hold in anticipation of the system B. System B will be coming soon so it makes no sense to improve it or dilute the effort. 3 months down the line and indicators go to amber. No matter what technology, the experience of your developers, there's always challenges, changes, unexpected complications. Your system B is much more complicated requiring new or hardly tested technologies. To add insult to injury users of the system A are really nervous and want their new ideas to be implemented into it. It's getting more and more difficult to explain the benefits of system B and why it is worthwhile to wait for it. So finally some developers or hired/diverted to support system A. That further slows downs progress of the development of the system B. First, you lost some hands, second you made the development of system B a moving target. As new features are added to system A you need to add them to system B as well.

At this stage many managers get nervous and start to go for compromises. No more TDD, no more free weekends, no more this, no more that. That's the disillusion stage. The hardest one. Also the breaking point. Decision made during this stage shape the delivery/not delivery of system B. It's up to the product managers, organization, processes and other factors if the system B is going to be delivered or shelved and people participating blamed.

There's a few things you should keep in mind when replacing some older core company system.

1) Even during the reimplementation phase it's the system you're replacing what pays the bill - so be considerate.
2) Never stop working on the system you're replacing. The replacement system may never be replacing the original one and you don't want to lose out.
3) Appreciate that the the replacement system will lack all the features and complexities the original system has.
4) Avoid any big bang releases. Shout when you here this "System B goes live on Saturday and we decommission system B the day after that". That smells.
5) It usually helps if you replace one aspect of the legacy system at a time. Just to prove the system can be replaced in its entirety.
6) Give user quick demo as you're building the replacement system every now and then. To keep the users in loop and engage. You going the need the users' support to get you through the teething problems phase.
7) Make sure there's sound reasons for replacing the legacy system and all other options have been exhausted. "Java is too verbose", "it doesn't support HTML5", "it's not an SPA" are hardly considered as sound reasons.
8) Before starting to implement the new system make sure it's the user who actually want it. Make sure it's them who make the IT team to write it.
9) Virtually all medium to large size corporations have their own bespoken or heavily customized systems. Be very suspicious when the rain sellers start to appear to sell you the of-the-shelf solution that needs to be tweaked a bit and will do exactly what you need.
10) I'm sure there's thousands and thousands more things you should be aware of.

Wednesday, April 29, 2015

My problem with conferences

We all know the numbers - 10% of developers are active in the community whilst 90% is the programming dark matter. To have two sides of barricade is quite all right - actually we all need each other as much as we need the audience - the users of our products.

Now of the 10% of developers you have the rock stars - the talk givers. It is perceived as the highest honour to go to a conference to give a talk. As a talk giver you waltz in to a room full of the usual business developers (means configurators or integrators) and start talking about stuff they've not worked with. Say the talk giver starts explaining, in great details, the technicalities of tail recursion. Or the goodness of the new shinny feature that allows you to curry functions from right (i.e. right curry). And the audience is in awe. They can't believe their eyes - all these features. We have to use them. NOW!

Then they return to their cubicles. They launch their IDEs (see, they use IDEs unlike the talk givers happy to use 6 ... VI) and try to use tail recursion. But they couldn't. Why? Because the problem domain they're in has no use for tail recursion. They rarely use any recursion and if they do then the recursion is so complicated that you simply can't use the tail recursion.

And that's the problem with the rock star talk givers. They want to impress. They don't care about your daily chores, they're rock stars. They are the rock stars even when their presentation is a shitty Powerpoint with a stolen background, they're the rock stars when they can't really explain what the brilliant feature is good for, they're the rock stars even if their personal web page (if they have one) is a poorly knitted html4 full of marquee tags, they're the rock stars even if they have only 5 years of experience under their belts and their github is full of presentations but no code at all.

So when you go to a conference choose wisely - unlike the talk givers you don't get paid for attending, you actually have to pay the admission and find some spare time. Pick the talks you want to go to based on what you can really use in your daily life not the buzzwords. Don't be afraid to leave if the talk is of no use to you. Give the organizers a feedback and vote with your feet.

Or if you've been to many conferences try unconferences. Go to meetup groups. Smaller venues, direct contact with other developers.

disclaimer: not all conferences feature rock star talk givers. And some are happy to listen to this sweet talk just to have something to dream about.

Friday, December 19, 2014

Groovy, Gorm with Spring XD


This piece covers the setup and use of Gorm (Grails ORM layer) as a stand-alone entity that can be used by Spring XD jobs and modules.


Foremost - why? What's Gorm and why use it with Spring XD?


Gorm is an extension to Hibernate in a way. Hibernate is good at ORM i.e. mapping classes to database objects. Hibernate is also good at querying. What Hibernate is not good at is the actual usage. There's simple too much you have to do get things going and too much noise afterwards. You have to annotate classes (or worse use XML descriptors), you have to setup persistent context, you have to use Hibernate session to execute actual queries. Now these are the areas addressed by Gorm. With Gorm you can use meaningful defaults. Let's have a look at this Groovy class

class EntityName {
String name
String value
String someOtherValue
Date lastUpdated
Date dateCreated
}

A class like this when seen by Gorm would automagically get converted into something similar

@Entity 
@Table (name = "entity_name")
class EntityName {
@Id @GeneratedValue
    @Column(name = "id")
int id

@Version
int version

    @Column(name = "name")
    String name
    
    @Column (name = "value")
    String value
    
    @Column (name = "some_other_value")
    String someOtherValue

    @Column (name = "last_update")
Date lastUpdated

@Column (name = "date_created")
Date dateCreated

    
    @PrePersist
    void onCreate() {
       dateCreated = new Date();
    }

    @PreUpdate
    protected void onUpdate() {
       lastUpdated = new Date();
    }
}

As you can see you can get a lot for free just by using Gorm. Now that's just a tip of the iceberg as with Gorm you also get validations, constraints and many, many more.

Hope it's clear now that it makes sense to use Gorm even outside of Grails. So what else you have to do to get Gorm running in Spring XD?

First of all, you have to get all Gorm dependency classes loaded by the initial classloader. To this end you can either copy all the classes into the lib folder (you don't really want to do that do you?) or tweak the startup script a bit and add another folder , say called ext, to added onto the classpath. See the attached patch for details.

Now with all the required classes on the classpath we can start playing with Gorm. First, we need to add it into our module's context.

<gorm:sessionFactory base-package="your.package" data-source-ref="dataSource" message-source-ref="messageSource">
    <property name="hibernateProperties">
     <util:map>
                <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
        </util:map>
    </property> 
</gorm:sessionFactory>        

Obviously you have to define the messageSource and dataSource (dataSource is normally already bound the one define in the main yaml file) and the messageSource is easy to define

<bean id="messageSource"
          class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages"/>
</bean>

Just add all your messages files on the classpath (your module config will suffice). And that's pretty much it.

All your domain classes need to be annotated by the import grails.persistence.Entity`annotation and have to implement equals and hash - for the lazy use the import groovy.transform.EqualsAndHashCode annotation and an AST transformation will take care of it for you.

There are several things you have to keep in mind should you wish to share the model classes between stand-alone Gorm and Grails' Gorm.


  • Autostamping is not working (dateCreated, lastUpdated). You can always use the beforeUpdate closures though.
  • You have to use session explicitly (DomainClass.withSession) or make a component using Gorms domain classes org.springframework.transaction.annotation.Transactional. In both cases you just make sure the Hibernate session is threadlocal.
  • The Entity annotation adds (or more specifically AST adds) some extra fields to the domain classes. Gorm then tries to blindly maps those to the underlaying model and the fails. To avoid this add these fields into the transient section


static transients = ['errors', 'instanceGormInstanceApi', 'instanceGormValidationApi', 'staticGormStaticApi', 'instanceConvertersApi', 'attached', 'dirty'] 

The source can be found on Github.

Wednesday, November 12, 2014

Integrating Grails 2.4.4 with CMS

In our company we use Typo3 CMS to maintain our dynamic contents - copywriting, descriptions etc. The content is actually stored as GSP pages - meaning even in our CMS we use tags such as <a:message/>or our custom tags <z:whatever/>. Basically the way it works we go to CMS every time our URLMapping matches certain pattern to extract and display the content. Once we retrieve the information we use the GroovyPagesTemplateEngine, as provided by Grails, to render the page. Now, with this rendered content we just call the render method to get the actual content rendered providing the layout parameter so the content nicely fits into our app. And it works surprisingly. In a bit of pseudo-code it looks like this:


def template = HTTPClient.get (cmsURL)
def template = groovyPagesTemplateEngine.createTemplate(template, "whateverName")
def result = new StringWriter()
template.make().writeTo(result)
def pageContent = result.toString()
render(text:pageContent, encoding:"UTF-8", type:"text/html", layout: layout)

As you can see it is a fairly straight forward solution. No catches here. Obviously under the hood we do pretty crazy stuff as Grails has to parse the template and execute it in the context of the current request, however from our point of view it looks simple.

All this worked great on Grails 2.3. Then we decided to move onto Grails 2.4. We upgraded a couple of dependancies and so on and were looking forward to see our pages in the new shiny Grails 2.4 look and feel. That didn't happen though. The reason was our CMS. Pages were fetched, templates rendered however the output was stripped, we could only see blank pages and we were asking WHY!?

After several hours of looking under the bonnet of our application we'd finally figured out the reason. To actually explain the why let me just briefly explain how layouts work in Grails 2.3 and Grails 2.4. In version 2.3 Grails uses Sitemesh filter. Basically all layout related operations are stored in a request object and once they leave controllers they're processed by the Sitemesh filter. From this point of view it doesn't matter at what stage you do what with layouts - all is post-processed in the filter. Grails 2.4 has ditched the filter and applies Sitemesh in the render method. Although it looks like a minor change it actually means a lot. It most importantly means that timing is important now. If you just render your pages then the impact is zero - if you compile your GSPs on the fly you have to do several steps to make the process working.

First you have to prepare the request for the initial GSP rendering. That would normally happen during the rendering but we run our GSP before we call render so we have to do it manually. Now when rendering templates all layout elements are processed by the RenderTagLib. This library finds all layout related tags and saves them into a GSPSitemeshPage instance stored in a request. So what we do now is that before calling the initial rendering of our template coming from CMS we create an instance of that class and store it in a request. Once this is done we move that particular instance into a different place in request where the layout rendering can locate it and we do the layout rendering. After that we have our HTML nicely stored in a String variable. The last step is to render it as a text. In a pseudo code it could look like:


static final GSP_SITEMESH_PAGE ='org.codehaus.groovy.grails.web.sitemesh.GrailsLayoutView.GSP_SITEMESH_PAGE'
static final PAGE = "__sitemesh__page";


def template = HTTPClient.get (cmsURL)
request.setAttribute(GSP_SITEMESH_PAGE, request.getAttribute(GSP_SITEMESH_PAGE)?: new GSPSitemeshPage(false)) // that's where we prepare the empty sitemesh page
def sitemeshPage = request.getAttribute(GSP_SITEMESH_PAGE)
def template = groovyPagesTemplateEngine.createTemplate(template, "whateverName")
def result = new StringWriter()
template.make().writeTo(result)
def pageContent = result.toString()
def template = (GroovyPageTemplate) findLayout(request, layoutName)
result = new StringWriter()
request.setAttribute(PAGE, page) //layout rendering needs the simemesh page here
template.make()writeTo(result)
pageContent = result.toString()

render(text:pageContent, encoding:"UTF-8", type:"text/html")


And that's the end of the story. Just keep in mind that every GSP template you load from CMS is actually compiled into Java class and loaded by the classloader. That means that naming the templates is more important than you might have actually thought!

Tuesday, August 12, 2014

The rise and fall of ORM - object relational mapping

You know the story - you start programming and you find yourself in need of a data store - place to keep and lately, hopefully, retrieve your precious data. You already know some Java, HTML, CSS, probably JavaScript as well and now you here comes the database. You've got several options so you start choosing - NoSQL, SQL or something else. Modern developers pick NoSQL for sure, they just want to save data and after us, the deluge. I'm going to skip it as I truly belief that NoSQL will eventually become a marginalized minority as it was several years back.
When it comes to SQL you start to have a dilemma. You'd like to access the database in an object way however there's this hairy old SQL language which doesn't do objects. So your first instinctive thought is to start using ORM - and nowadays Hibernate is a synonym for ORM.
It goes all so well. You define your data objects, you let Hibernate to generate your database schema - happy days. You can query for objects, create, modify and delete them and it's all so rosy.
But in a while - problems. Transactions not working as expected, relations are not propagated the way you want them to, database becomes slow and keeping the application running a chore. And after a bit longer you start to be more and more unhappy with your choice.
So what's wrong with ORM?
First of all ORM adds an extra level of complexity. You have to define stuff and obviously ORM brings in additional extra megabytes of code. And this comes at price. Application start up time up, performance down, scope for errors even higher. Hibernate uses black magic to get lots of things done so you find your object and method proxied, AOPed and intercepted. And that's not all.
ORM encourages bad practices and accesses database in an uncontrolled way - instead you taking control of the database you may not even know what tables there are!
Later you figure out that optimizing queries is next to impossible. Queries are generated by the ORM framework and you've got only limited way to influence their creation. So things like adding an index can be a nightmare.
And that's not all - ORM gets you more data than you need or you think you get. Not only you can’t specify columns you want to get, you may also find that you're fancy ORM goes to database for every detail object. I'm talking about this typical scenario where you iterate a collection of details. You either get all eagerly or nothing lazily. But ORM usually has no idea how you want to get the data (and neither do you) so if a detail for a master has 1000 elements you can end up going to database 1000 times. A real performance killer.
And even if you try to optimize that thing you may struggle - ORM makes it hard to optimize DB by adding indexes due to its unpredictability when it comes to construction of SQL queries. You can, normally, dump all the queries but there's no guarantee that the same queries will be generated all the time so it may be difficult to figure what to index and how. It's also a nightmare for your DBAs as generated queries are hard to read and thus optimizing can be a good fun for a couple of weeks.

So it's the old fashioned DAO where your handcrafted SQL lives still a way to go? May be. That's mostly up to you. But either way, you better master those SQL skills as that's the only way to get your database performance where you want it to be.

Friday, June 27, 2014

Is AngularJS the new Fat client?

It was year 2000. People survived Y2k, the dot com bubble was just about to burst and we were writing our shiny applications. Many people were already doing HTML based thin clients (with a bit Javascript and mostly on Internet Explorer because Netscape was crap with anything beyond basic form validation).
But at that time there were still people doing something called "Fat client".

So what exactly was (is) a Fat client application.

In a nutshell, fat client is a concept where the client holds the logic of the application and talks to database directly. Database in this case is the point of truth. Fat clients have certain advantages.

  • Fat client utilises the users CPU and resources - thus spares the company from buying (at that time) expensive servers.
  • Fat client is pretty quick - unless you need something from the database (backend) and the database is busy.
  • Fat client's look and feel is consistent. It is a native application so users are usually familiar with the UI.


But there are certain drawbacks

  • Fat client needs to be installed and maintained locally. It's OK for several users, but thousands users can be a completely different story.
  • Fat client runs on one platform only - it is a native application in the end! It means that upgrade to newer version Windows is risky not even thinking about porting that application to Unix/MacOS.
  • Fat client can simply die or stop working and you don't know it until your users tell you (if they even bother to tell you).

And the list goes on.

Now, fast forward, year 2014. Here we are. We've got our servers but we try again to let our clients to do the heavy lifting. This time with a small twist. Instead of using native applications (we've been there, haven't we?) we use HTML/Javascript. So what exactly differs between the fat client v.1 (native application in year 2000) and fat client v. 2 (AngularJS application in year 2014)

  • We dropped native applications, we now use HTML and Javascript. So the UI may be completely unfamiliar to our users. HTML builds a very thick layer on top of the hardware so our fat client v. 2 applications may be pretty slow on older computers.
  • We ditch the installation process. Installing software is bad. Updating, maintaining it - no kudos there. Instead, we install the application over and over again as the user visits our site. But is the user really visiting our site that often? What the users just put his PC on stand by and have the page (application) running 24/7 for a days, months?
  • We're smarter now - we don't talk to database. We talk to APIs. And because we're smarter we all use JSON. JSON is fine, everyone can read it, it's nicely structured and all the network sniffers are happy - listen or port 80 (443) and all the structured data is there. 
  • As the application is written in Javascript everyone can read the source code and tweak it. All these wannabe developers rejoice.
  • We are more masochistic now. If something looks easy then everyone could do it, sure thing. So we make Angular difficult. Controllers, services, directives, dependancy injection. Who cares you only want to let user to input several parameters to get some output back, you still need to make it painful for the developers, they need to deserve their pay.
  • We learn from experience and we know users hate error messages. That's why we don't give them any. If we're in a good mood we dump something on the console and we pray for the user not to press F12 (or CMD+ALT+J). Ideally we let to user in the dark by presenting a screen where things look working but the code behind them is long time zombie.
  • Unfortunately the browser still has the reload and back button. We just hope the user will not press it or we try to come up with a smart way handling that. Users are confused but who cares about their feelings. 
  • Using browser has other nice advantages. No native printing, no access to the hardware, not working app if the user happen to lose his Internet connectivity. Some big companies allow you to continue using a crippled version of their application if you're offline but most people don't bother.
  • And we all know that data is the king. So we don't allow the user to access the data. Fat client v. 1 would allow you to save data in various formats but v. 2 saves data in the Cloud and makes sure anything that even slightly resembles the save button is removed.


So, is it all lost? I don't think so. Because there's already Fat client v. 3 here. Oh yes - the native smartphone/tablet applications! They install but keep themselves updated, they talk to APIs but in their own formats (although JSON is getting there as well). The applications still crash, allow you to save data and work offline. And now (*drumroll*) Google would like to allow these apps on their Chromebooks - eat that Angular!

And for those who want to see this side by side - here's the link!




Tuesday, January 22, 2013

picoGal - tiny little js gallery for your website

Just finished a small (tiny, pico) JavaScript gallery jquery plugin. Basically it takes a list of images (provided or loaded from DOM) and creates a gallery. Is't nowhere near to other comprehensive plugins but it's fast and lightweight. Not everyone's cup of tea, I know. For demo go here. Source is in github.

Monday, November 21, 2011

Challenging week

What a week last week. Three project - three completely different technologies.
  • I started the week bug fixing our Wheelchair portal written in Grails. Grails underpinned by my favorite language Groovy is by far framework of my choice and means that i had a pretty good time coding.
  • Next one - PoC opening Oracle CRM (EBiz suite) custom form preloaded with data coming from our new out of the shelf leasing product. Normally we would pass this information in URL but that would mean customizing Oracle CRM which is expensive and no one wants to do it. Solution - Calling system will store input data in database and CRM will load this data on form load event. This event can be handled as personalization (not customization). Technologywise - Oracle SOA to store data into database (exposed as sync webservice) and Oracle forms on CRM side.
  • Last one - creating MS Outlook tasks triggered by events in CRM. The CRM bit is outsourced but the Exchange bit is pure java witha hint of Apache Jackrabbit (client side only - no webDAV server) and composition of webDAV messages.
To be honest that somehow demonstrate how experienced developers (could this be me?) can contribute - by bringing in knowledge from many fields spanning across many technologies. I'm not saying that less experienced developers can't do that but in my opinion they're more focus on latest trends rather than having all round knowledge especially outside of the mainstream (how many of them do Oracle forms, BPEL, not to mention webDAV?).
Anyway good times last week, this week it will be more about implementing more PoC and more, unfortunately, meetings ...

Friday, November 18, 2011

Multilevel clickable menu (tablet enabled)

My first component like project in jQuery. Why? In our internal application we use YUI menu and it works fine, but it's driven by mouse ins and outs. Normally that would be OK (and most of the time it is OK) but if you want to access such menu from tablets where you don't have a mouse connected (which renders mouse hovering useless) you've got a problem. There several solutions to it one which involves writing a mobile version of our application or rewriting the menu itself. Writing a mobile version was out of the question for us as we didn't have time and resources so the quickest solution was to replace the menu with one supporting mouse clicks (on tables taps).
To our surprise between the hundreds of html menus of all styles, shapes, all singing all dancing we could not find a single usable menu driven by clicks. And then I thought - right, if there's really nothing out there lets build something.
So I spent some time trying to stitch something together with mostly no luck but slowly learning how to display and hide and style a menu. My original idea was to render the menu from within jQuery and use HTML only to define the menu structure but later I had to change my mind as rendering in jQuery seemed to be slow and hard to debug. My final option was to delegate bulk of the job to HTML/CSS and use only small jQuery script to animate menu clicks.
If you want to see the menu just visit my github page https://github.com/defectus/mainmenu.js.
Thinks to note:
  • Menu doesn't display properly in IE6. It's usable but visually just not right.
  • Menu looks somehow slow in IE. But what doesn't look slow in IE though.
  • Menu supports on three levels. Any subsequent level needs to be handled individually.
Hope that helps somebody.

Thursday, November 17, 2011

Creating an Outlook task in Java using webDAV

I've recently come across this interesting task. In Java, create MS Exchange tasks with assigned reminders and custom task body. I was told that PoC (proof of concept) had been successfully concluded and it was just a matter of integration. To be honest, the PoC came up with this idea - create a web application which would utilize jIntegra DCOM wrapper (as the web would be running in Linux so no COM there) and remotely access MS Outlook installed somewhere and from there it would create tasks in Exchange. As you can imagine, the level of complexity of this approach is enormous. This solution would do a round trip around the world just to access an Exchange server and create a task there. Still this solution would eventually fail because of Outlooks scripting protection (see it here) and would require someone permanently logged in Outlook so DCOM calls would be possible.
What other (if any) solutions are out there. One pretty straight forward and surprisingly Java friendly too. Long time ago someone invented this extension to HTTP called webDAV. Yes, it's still around - alive and kicking. As webDAV is generally speaking just a collection of additional HTTP verbs (methods) it can be easily invoked from any language as long as the program can access HTTP (i.e. all).
Below's how I did it. You only need to download Jackrabbit jar ( freely available from Apache) and run the code. It internally performs a webDAV call that creates a new Exchange task. Tested with Exchange 2003 SP2 but should work with any 2003+ version. Additional files can be found under the main one. It's just a small tweak to Jackrabbit's own PropPatch method (adding PropPatch call with the XML in a String) and HtmlEncode helper (not my invention!) class to allow html task body encoding.

More info and source can be found at these URLs:
package uk.co.mo.webDAV.utils;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;

import java.util.Date;


public class TaskCreator {

    public static void main(String[] args) throws Exception {

        // create a httpclient config
        String uri = "http://xxx.yyy.zzz/exchange/";
        // String uri = "http://xxx.yyy.zzz/exchange/";
        HostConfiguration hostConfig = new HostConfiguration();
        hostConfig.setHost(uri);

        HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
        HttpConnectionManagerParams params = new HttpConnectionManagerParams();
        int maxHostConnections = 20;
        params.setMaxConnectionsPerHost(hostConfig, maxHostConnections);
        connectionManager.setParams(params);

        HttpClient client = new HttpClient(connectionManager);
        client.setHostConfiguration(hostConfig);

        // set authentication (most exchange servers accept nt challenge-response)
        Credentials creds = new NTCredentials("admin_username", "password", "ip", "domain");
        // Credentials creds = new NTCredentials("username", "password", "domain controller", "domain");
        client.getState().setCredentials(AuthScope.ANY, creds);

        PropPatchMethodPro pPatch = new uk.co.mo.webDAV.utils.PropPatchMethodPro("http://xxx.yyy.zzz/exchange/user/Tasks/nameOfThetask" + new Date().getTime() + ".eml", getMessage());
        // PropPatchMethodPro pPatch = new uk.co.mo.webDAV.utils.PropPatchMethodPro ("http://xxx.yyy.zzz/exchange/user/Tasks/nameOftheTask") new Date().getTime() + ".eml", getMessage());

        pPatch.setRequestHeader("Content-type", "text/xml; charset=UTF-8");

        // call exchange
        client.executeMethod(pPatch);

        // is everything OK
        if (!pPatch.isSuccess(pPatch.getStatusCode())) {
            // we've got a problem
            System.out.println("PROBLEM");
            System.out.println(pPatch.getStatusCode());
            System.out.println(pPatch.getStatusText());
            System.out.println(pPatch.getStatusLine());
        } else {
            // processed
            System.out.println("OK");
        }
    }

    private static String getMessage() {
        return new StringBuilder("<?xml version=\"1.0\"?>\n")
                .append("<g:propertyupdate xmlns:g=\"DAV:\"\n")
                .append("xmlns:e=\"http://schemas.microsoft.com/exchange/\"\n")
                .append("xmlns:mapi=\"http://schemas.microsoft.com/mapi/\"\n")
                .append("xmlns:mapit=\"http://schemas.microsoft.com/mapi/proptag/\"\n")
                .append("xmlns:dt=\"urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/\"\n")
                .append("xmlns:h=\"http://schemas.microsoft.com/mapi/id/{00062003-0000-0000-C000-000000000046}/\"\n")
                .append("xmlns:i=\"http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/\"\n")
                .append("xmlns:header=\"urn:schemas:mailheader:\"\n")
                .append("xmlns:sql=\"urn:schemas-microsoft-com:mapping-schema\"\n")
                .append("xmlns:mail=\"urn:schemas:httpmail:\"\n")
                .append("xmlns:t=\"http://schemas.microsoft.com/exchange/tasks/\">\n")
                .append("<g:set>\n")
                .append("<g:prop>\n")
                .append("<g:contentclass>urn:content-classes:task</g:contentclass>\n")
                .append("<e:outlookmessageclass>IPM.Task</e:outlookmessageclass>\n") // we're creating a task
                .append("<mail:subject>Test Task</mail:subject>\n") //subject line of the task
                .append("<mail:htmldescription>") // html message - needs to html encoded
                .append(HtmlEncode.text("Task Test<br/><b>test bold</b><pre>test pre</pre><a href='www.google.com'>www.google.com</a>"))
                .append("</mail:htmldescription>\n")
                .append("<h:0x8104 dt:dt=\"dateTime.tz\">2011-11-20T01:00:00.00Z</h:0x8104>\n") //start date
                .append("<h:0x8105 dt:dt=\"dateTime.tz\">2011-11-21T01:00:00.00Z</h:0x8105>\n") //due date
                .append("<i:0x8516 dt:dt=\"dateTime.tz\">2011-11-20T01:00:00.00Z</i:0x8516>\n") //start date
                .append("<i:0x8517 dt:dt=\"dateTime.tz\">2011-11-21T01:00:00.00Z</i:0x8517>\n") //due date
                .append("<i:0x8502 dt:dt=\"dateTime.tz\">2011-11-20T15:50:00.00Z</i:0x8502>\n") // reminder date
                .append("<h:0x811C dt:dt=\"boolean\">0</h:0x811C>\n")  // completed
                .append("<h:0x8101 dt:dt=\"int\">0</h:0x8101>\n") // status
                .append("<h:0x8102 dt:dt=\"float\">0</h:0x8102>\n") //completed %
                .append("<i:0x8503 dt:dt=\"boolean\">1</i:0x8503>\n")  // reminder enabled
                .append("<header:to>apollosystem</header:to>\n") // task destination
                .append("</g:prop>\n")
                .append("</g:set>\n")
                .append("</g:propertyupdate>").toString();
    }
}
package uk.co.mo.webDAV.utils;

import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.jackrabbit.webdav.DavConstants;
import org.apache.jackrabbit.webdav.DavMethods;
import org.apache.jackrabbit.webdav.client.methods.DavMethodBase;
import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
import org.apache.jackrabbit.webdav.property.DavPropertyName;
import org.apache.jackrabbit.webdav.property.DavPropertyNameIterator;
import org.apache.jackrabbit.webdav.DavServletResponse;
import org.apache.jackrabbit.webdav.MultiStatus;
import org.apache.jackrabbit.webdav.MultiStatusResponse;
import org.apache.jackrabbit.webdav.DavException;
import org.apache.jackrabbit.webdav.Status;
import org.apache.jackrabbit.webdav.xml.DomUtil;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.HttpConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.io.IOException;

public class PropPatchMethodPro extends DavMethodBase implements DavConstants {
    private static Logger log = LoggerFactory.getLogger(PropPatchMethodPro.class);

    private final DavPropertyNameSet propertyNames = new DavPropertyNameSet();

    private DavException responseException;

    /**
     * @param uri
     * @throws IOException
     */
    public PropPatchMethodPro(String uri, Document document) throws IOException {
        super(uri);
        setRequestBody(document);
    }

    public PropPatchMethodPro(String uri, String document) throws IOException {
        super(uri);
        setRequestBodyText(document);
    }

    public void setRequestBodyText(String requestBody) {
        setRequestEntity(new StringRequestEntity(requestBody));
    }


    private Element getPropElement(Element propUpdate, boolean isSet) {
        Element updateEntry = DomUtil.addChildElement(propUpdate, (isSet) ? XML_SET : XML_REMOVE, NAMESPACE);
        return DomUtil.addChildElement(updateEntry, XML_PROP, NAMESPACE);
    }

    //---------------------------------------------------------< HttpMethod >---

    /**
     * @see org.apache.commons.httpclient.HttpMethod#getName()
     */
    @Override
    public String getName() {
        return DavMethods.METHOD_PROPPATCH;
    }

    //------------------------------------------------------< DavMethodBase >---

    /**
     * @param statusCode
     * @return true if status code is {@link DavServletResponse#SC_MULTI_STATUS 207 (Multi-Status)}.
     *         For compliance reason {@link DavServletResponse#SC_OK 200 (OK)} is
     *         interpreted as successful response as well.
     */
    @Override
    protected boolean isSuccess(int statusCode) {
        return statusCode == DavServletResponse.SC_MULTI_STATUS || statusCode == DavServletResponse.SC_OK;
    }

    /**
     * @param multiStatus
     * @param httpState
     * @param httpConnection
     */
    @Override
    protected void processMultiStatusBody(MultiStatus multiStatus, HttpState httpState, HttpConnection httpConnection) {
        // check of OK response contains all set/remove properties
        MultiStatusResponse[] resp = multiStatus.getResponses();
        if (resp.length != 1) {
            log.warn("Expected a single multi-status response in PROPPATCH.");
        }
        boolean success = true;
        // only check the first ms-response
        for (int i = 0; i < 1; i++) {
            DavPropertyNameSet okSet = resp[i].getPropertyNames(DavServletResponse.SC_OK);
            if (okSet.isEmpty()) {
                log.debug("PROPPATCH failed: No 'OK' response found for resource " + resp[i].getHref());
                success = false;
            } else {
                DavPropertyNameIterator it = propertyNames.iterator();
                while (it.hasNext()) {
                    DavPropertyName pn = it.nextPropertyName();
                    success = okSet.remove(pn);
                }
            }
            if (!okSet.isEmpty()) {
                StringBuffer b = new StringBuffer("The following properties outside of the original request where set or removed: ");
                DavPropertyNameIterator it = okSet.iterator();
                while (it.hasNext()) {
                    b.append(it.nextPropertyName().toString()).append("; ");
                }
                log.warn(b.toString());
            }
        }
        // if  build the error message
        if (!success) {
            Status[] st = resp[0].getStatus();
            // TODO: respect multiple error reasons (not only the first one)
            for (int i = 0; i < st.length && responseException == null; i++) {
                switch (st[i].getStatusCode()) {
                    case DavServletResponse.SC_FAILED_DEPENDENCY:
                        // ignore
                        break;
                    default:
                        responseException = new DavException(st[i].getStatusCode());
                }
            }
        }
    }

    /**
     * @return
     * @throws IOException
     */
    @Override
    public DavException getResponseException() throws IOException {
        checkUsed();
        if (getSuccess()) {
            String msg = "Cannot retrieve exception from successful response.";
            log.warn(msg);
            throw new IllegalStateException(msg);
        }
        if (responseException != null) {
            return responseException;
        } else {
            return super.getResponseException();
        }
    }
}
package uk.co.mo.webDAV.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Encodes text and URL strings in various ways resulting HTML-safe text.
 * All methods are <code>null</code> safe.
 */
public class HtmlEncode {

    protected static String EMPTY_STRING = "";

    protected static final char[][] TEXT = new char[64][];
    protected static final char[][] BLOCK = new char[64][];
    protected static final char[][] URL = new char[256][];

    /**
     * Creates HTML lookup tables for faster encoding.
     */
    static {
        for (int i = 0; i < 64; i++) {
            TEXT[i] = new char[]{(char) i};
        }
        for (char c = 0; c < 256; c++) {
            try {
                URL[c] = URLEncoder.encode(String.valueOf(c),
                        "ISO-8859-1").toCharArray();
            } catch (UnsupportedEncodingException ueex) {
                ueex.printStackTrace();
            }
        }

        // special HTML characters
        TEXT['\''] = "'".toCharArray(); // apostrophe (''' doesn't work - it is not by the w3 specs)
        TEXT['"'] = """.toCharArray(); // double quote
        TEXT['&'] = "&".toCharArray(); // ampersand
        TEXT['<'] = "<".toCharArray(); // lower than
        TEXT['>'] = ">".toCharArray(); // greater than

        // text table
        System.arraycopy(TEXT, 0, BLOCK, 0, 64);
        BLOCK['\n'] = "<br>".toCharArray(); // ascii 10, new line
        BLOCK['\r'] = "<br>".toCharArray(); // ascii 13, carriage return
    }

    // ---------------------------------------------------------------- encode

    public static String text(Object object) {
        if (object == null) {
            return EMPTY_STRING;
        }
        return text(object.toString());
    }

    /**
     * Encodes a string to HTML-safe text. The following characters are replaced:
     * <ul>
     * <li>' with &#039; (&apos; doesn't work)</li>
     * <li>" with &quot;</li>
     * <li>& with &amp;</li>
     * <li>< with &lt;</li>
     * <li>> with &gt;</li>
     * </ul>
     *
     * @see #block(String)
     */
    public static String text(String text) {
        int len;
        if ((text == null) || ((len = text.length()) == 0)) {
            return EMPTY_STRING;
        }
        StringBuilder buffer = new StringBuilder(len + (len >> 2));
        for (int i = 0; i < len; i++) {
            char c = text.charAt(i);
            if (c < 64) {
                buffer.append(TEXT[c]);
            } else {
                buffer.append(c);
            }
        }
        return buffer.toString();
    }

    // ---------------------------------------------------------------- enocde text

    public static String block(Object object) {
        if (object == null) {
            return EMPTY_STRING;
        }
        return block(object.toString());
    }

    /**
     * Encodes text into HTML-safe block preserving paragraphes. Besides the {@link #text(String) default
     * special characters} the following are replaced, too:
     * <ul>
     * <li>\n with <br></li>
     * <li>\r with <br></li>
     * </ul>
     * <p/>
     * <p/>
     * Method accepts any of CR, LF, or CR+LF as a line terminator.
     */
    public static String block(String text) {
        int len;
        if ((text == null) || ((len = text.length()) == 0)) {
            return EMPTY_STRING;
        }
        StringBuilder buffer = new StringBuilder(len + (len >> 2));
        char c, prev = 0;
        for (int i = 0; i < len; i++, prev = c) {
            c = text.charAt(i);
            if ((c == '\n') && (prev == '\r')) {
                continue; // previously '\r' (CR) was encoded, so skip '\n' (LF)
            }
            if (c < 64) {
                buffer.append(BLOCK[c]);
            } else {
                buffer.append(c);
            }
        }
        return buffer.toString();
    }

    // ---------------------------------------------------------------- encode text strict

    public static String strict(Object object) {
        if (object == null) {
            return EMPTY_STRING;
        }
        return strict(object.toString());
    }

    /**
     * Encodes text int HTML-safe block and preserves format using smart spaces.
     * Additionaly to {@link #block(String)}, the following characters are replaced:
     * <p/>
     * <ul>
     * <li>\n with <br></li>
     * <li>\r with <br></li>
     * </ul>
     * <p/>
     * This method preserves the format as much as possible, using the combination of
     * not-breakable and common spaces.
     */
    public static String strict(String text) {
        int len;
        if ((text == null) || ((len = text.length()) == 0)) {
            return EMPTY_STRING;
        }
        StringBuilder buffer = new StringBuilder(len + (len >> 2));
        char c, prev = 0;
        boolean prevSpace = false;
        for (int i = 0; i < len; i++, prev = c) {
            c = text.charAt(i);

            if (c == ' ') {
                if (prev != ' ') {
                    prevSpace = false;
                }
                if (prevSpace == false) {
                    buffer.append(' ');
                } else {
                    buffer.append(" ");
                }
                prevSpace = !prevSpace;
                continue;
            }
            if ((c == '\n') && (prev == '\r')) {
                continue; // previously '\r' (CR) was encoded, so skip '\n' (LF)
            }
            if (c < 64) {
                buffer.append(BLOCK[c]);
            } else {
                buffer.append(c);
            }
        }
        return buffer.toString();
    }
}
Enjoy while it lasts!