Coping with Methods with Many Parameters

I will put my captain Obvious cape now and write in short how to get rid of methods with many parameters, especially if some of the parameters are optional, or depend on each other somehow (e.g. go in pairs, one excludes another, etc.). Let’s take a simple RestClient class. You need to be able to pass the target resource, optional HTTP headers, HTTP method, request body, timeouts, etc. All of these, except the URL, are optional. How do you write this class? The first thing that comes to mind is overloading: public <T> T request(String url); public <T> T request(String url, HttpMethod method, Object body); public <T> T request(String url, HttpMethod method, Object body, Map<String, String> headers); public <T> T request(String url, HttpMethod method, Object body, Map<String, String> headers, long timeout); Good? No. Because you may want to call a URL with GET, specifying a timeout, but not specifying body or headers. And whenever a new parameter is to be added (e.g. for authentication), then you should copy all the methods and append the parameter. To put it simply: you cannot and should not have an overloaded method for each possible combination of the parameters. How should we approach the problem? There are several ways, and the one I prefer is using something like the Builder pattern. The Builder pattern is supposed to be used for constructing objects and to replace constructors taking multiple arguments. But its philosophy can be transferred to the above problem. For example: public <T> T request(RestCall call); What is RestCall? It’s a mutable object that you configure, preferably with a fluent interface, in order...

Ideas Aren’t Worthless

It’s common knowledge that “ideas are worthless”. An idea will bring you nowhere – you need implementation, focus, a good team, the right environment, luck, etc. And I won’t argue with that – obviously, an idea doesn’t bring you anywhere by itself. Just google for “ideas are worthless” and you can find dozens of convincing articles. Ideas change, ideas evolve, initial idea doesn’t matter. But that’s the perspective of the businessman. A person whose interest is to make profit (nothing wrong with that, of course). Emphasizing the worthlessness of ideas brings a whole slew of startups that are there for the sake of being a startup. We’ll build something. We’ll figure it out along the way. Then they master the skills of fundraising, they focus on Lean, MVP, time-to-market, exit strategies, etc. etc. And that’s all great. Except that it’s not technology. The perspective of a “hacker” is different. A hacker is thrilled by technology, he wants to be challenged. The ultimate point is not to have a startup, to get acquired, to make money. The ultimate point is to make something cool, something different, something technologically innovative. I can have a dozen ideas per day. Things that may actually be turned into a startup. I know all of the above buzzwords, like Lean and MVP, so probably I can turn them into some sort of company. Do I do that? No. And not because I’m afraid not to have a stable income or wasting a couple thousand bucks. I don’t do that just because I’m not convinced they are cool, technological and challenging enough. Probably, that’s the difference...

Digital Signatures With Javascript

Several years ago I used to work on a project that required the use of smart cards and digital signatures. I’ve already shared my experience with signing with javascript and then verifying the signature on the server side (with Java). What I did today is to simply get the javascript, tidy it up and put it on GitHub (js-signer), so that people don’t need to copy-paste from a blogpost, and also to be able to update the project whenever there are new developments (for example Chrome and Safari implement DOMCrypt) What are the use-cases of that? Electronic identification most often makes use of smart cards. And so software that requires to identify a citizen securely (most often banking or government software) and let him perform some actions online, needs to read the smartcards certificate details and to digitally sign content – e.g. you digitally sign your bank transfer, or you sign your request for a government service. There are several ways to do that – e.g. Java Applet (like this one) or a custom-made ActiveX control. And while the applet and ActiveX is fine for internal use in organizations, where you can be sure that there’s Java installed or that everyone is using IE, these solutions are not so great for the mass public, because making the user install something is a bad user experience, and luckily not everyone is using IE. So a javascript solution must exist. Of course, the preconditions are, that the user has a digital signature, issued by a CA, and has followed the CA’s manual for installing the certificate in a browser. If these...

I Don’t Like Scala

Yes, it is my opinion, and yes, it might seem like a hate-post. But I’ll try to address the issues I have with Scala and its surroundings. I have used Scala in a single, relatively small project (currently working in production), so I guess I’m somewhere between “n00b” and “intermediate”. there are multiple right ways to do the same thing – there are a lot of tasks that you can achieve with one or two lines in Scala. But often there is more than one way to do it, and there’s no obvious winner. The options vary on the brevity-readability scale, on the functional-imperative scale, and in many other ways. I know what currying, partial functions, partially applied functions, folding, collect, map, apply, etc. do, but given a simple task I can use all of them I don’t know which one. Probably because I’m a beginner – true, and over time I guess I’ll build a preferred set of approaches. Which will likely differ from those of any other co-worker, and the projects will me “messy”. It would be something like having half the project with curly brackets on same line, and the other half – on the next line. it’s cryptic – you can probably be ok with all the symbols flying around after some coding, but I find it plain ugly. It is a high-level language and the use of so many symbols is troublesome. Take a look at this stackoverflow question to see what I mean. binary incompatibilities – I have an error that in a Scala 2.10 project I can’t use a library compiled with...

Why The Web Isn’t Semantic Yet

The semantic web. You’ve heard about it. What is it? Chances are, you don’t have a completely clear idea. But why isn’t the web “semantic” yet? First, it’s complex to understand. There are tons of standards around it – RDF, RDFa, RDFS, URI, OWL, SPARQL, etc. It’s hard to get started, because you have to put a lot of unfamiliar stuff in your head. Compare that to the simple, old web page using HTML, where you write some tags and you have a webpage. Pretty easy. Complexity wouldn’t be that much of an issue, if there were visible benefits. What the semantic web relies on, is that every website should conform to a semantic notion (e.g. use RDF). But what if you expose your data in a structured way? Nothing. Yes, you page becomes easier to make sense of my machines, but you don’t get anything. Why would a web shop or a social network or a personal website be “semanticized”? It requires extra effort for some invisible and hard to understand benefits. Note that I’m not saying there aren’t benefits – there certainly are, but they are too far away from the mind of the average website / online business owner. It’s a bit of a chicken-and-egg problem – there’s no immediate benefit, and so people are not making their web sites semantic. And because there are so few semantic websites, there aren’t any popular products that would provide the desired benefit. And here comes one great example – the Facebook Open Graph protocol. It is a proprietary solution (never mind the “open” in the title) that defines...