Featured FREE Whitepapers

What's New Here?

grails-logo

Test Driven Traps, part 1

Have you ever been in a situation, where a simple change of code, broke a few hundred tests? Have you ever had the idea that tests slow you down, inhibit your creativity, make you afraid to change the code. If you had, it means you’ve entered the Dungeon-of-very-bad-tests, the world of things that should not be. I’ve been there. I’ve built one myself. And it collapsed killing me in the process. I’ve learned my lesson. So here is the story of a dead man. Learn from my faults or be doomed to repeat them. The story Test Driven Development, like all good games in the world, is simple to learn, hard to master. I’ve started in 2005, when a brilliant guy named Piotr Szarwas, gave me the book “Test Driven Development: By Example” (Kent Beck), and one task: creating a framework. These were the old times, when the technology we were using had no frameworks at all, and we wanted a cool one, like Spring, with Inversion-of-Control, Object-Relational Mapping, Model-View-Controller and all the good things we knew about. And so we created a framework. Then we built a Content Management System on top of it. Then we created a bunch of dedicated applications for different clients, Internet shops and what-not, on top of those two. We were doing good. We had 3000+ tests for the framework, 3000+ tests for the CMS, and another few thousand for every dedicated application. We were looking at our work, and we were happy, safe, secure. These were good times. And then, as our code base grew, we came to the point, where a simple anemic model we had, was not good enough anymore. I had not read the other important book of that time: “Domain Driven Design”, you see. I didn’t know yet, that you can only get so far with an anemic model. But we were safe. We had tons of tests. We could change anything. Or so I thought. I spent a week trying to introduce some changes in the architecture. Simple things really: moving methods around, switching collaborators, such things. Only to be overwhelmed by the number of tests I had to fix. That was TDD, I started my change with writing a test, and when I was finally done with the code under the test, I’d find another few hundred tests completely broken by my change. And when I got them fixed, introducing some more changes in the process, I’d find another few thousand broken. That was a butterfly effect, a chain reaction caused by a very small change. It took me a week to figure out, that I’m not even half done in here. The refactoring had no visible end. And at no point my code base was stable, deployment-ready. I had my branch in the repository, one I’ve renamed ‘Lasciate ogne speranza, voi ch’intrate’. We had tons and tons of tests. Of very bad tests. Tests that would pour concrete over our code, so that we could do nothing. The only real options were: either to leave it be, or delete all tests, and write everything from scratch again. I didn’t want to work with the code if we were to go for the first option, and the management would not find financial rationale for the second. So I quit. That was the Dungeon I built, only to find myself defeated by its monsters. I went back to the book, and found everything I did wrong in there. Outlined. Marked out. How could I skip that? How could I not notice? Turns out, sometimes, you need to be of age and experience, to truly understand the things you learn. Even the best of tools, when used poorly, can turn against you. And the easier the tool, the easier it seems to use it, the easier it is to fall into the trap of I-know-how-it-works thinking. And then BAM! You’re gone.The truth Test Driven Development and tests, are two completely different things. Tests are only a byproduct of TDD, nothing more. What is the point of TDD? What does TDD brings? Why do we do TDD? Because of three, and only those three reasons. 1. To find the best design, by putting ourselves into the user’s shoes.By starting with “how do I want to use it” thinking, we discover the most useful and friendly design. Always good, quite often that’s the best design out there. Otherwise, what we get is this:And you don’t want that. 2. To manage our fear. It takes balls, to make a ground change in a large code-base without tests, and say “it’s done” without introducing bugs in the process, doesn’t it? Well, the truth is, if you say “it’s done”, most of the time you are either ignorant, reckless, or just plain stupid. It’s like with concurrency: everybody knows it, nobody can do it well. Smart people are scared of such changes. Unless they have good tests, with high code coverage. TDD allows to manage our fears, by giving us proof, that things work as they should. TDD gives us safety 3. To have fast feedback. How long can you code, without running the app? How long can you code without knowing whether your code works as you think it should? Feedback in tests is important. Less so for frontend programming, where you can just run the shit up, and see for yourselves. More for coding in the backend. Even more, if your technology stack requires compilation, deployment, and starting up. Time is money, and I’d rather earn it, than wait for the deployment and click through my changes each time I make them. And that’s it. There are no more reasons for TDD whatsoever. We want Good Design, Safety, and Feedback. Good tests are those, which give us that. Bad tests? All the other tests are bad. The bad practice So how does a typical, bad test, look like? The one I see over and over, in close to every project, created by somebody who has yet to learn how NOT to build an ugly dungeon, how not to pour concrete over your code base. The one I’d write myself in 2005. This will be a Spock sample, written in groovy, testing a Grails controller. But don’t worry if you don’t know those technologies. I bet you’ll understand what’s going on in there without problems. Yes, it’s that simple. I’ll explain all the not-so-obvious parts. def 'should show outlet'() { given: def outlet = OutletFactory.createAndSaveOutlet(merchant: merchant) injectParamsToController(id: outlet.id) when: controller.show() then: response.redirectUrl == null } So we have a controller. It’s an outlet controller. And we have a test. What’s wrong with this test? The name of the test is “should show outlet”. What should a test with such a name check? Whether we show the outlet, right? And what does it check? Whether we are redirected. Brilliant? Useless. It’s simple, but I see it all around. People forget, that we need to: VERIFY THE RIGHT THING I bet that test was written after the code. Not in test-first fashion. But verifying the right thing is not enough. Let’s have another example. Same controller, different expectation. The name is: ‘should create outlet insert command with valid params with new account’ Quite complex, isn’t it? If you need an explanation, the name is wrong. But you don’t know the domain, so let me put some light on it: when we give the controller good parameters, we want it to create a new OutletInsertCommand, and the account of that one, should be new. The name doesn’t say what ‘new’ is, but we should be able to see it in the code. Have a look at the test: def 'should create outlet insert command with valid params with new account'() { given: def defaultParams = OutletFactory.validOutletParams defaultParams.remove('mobileMoneyAccountNumber') defaultParams.remove('accountType') defaultParams.put('merchant.id', merchant.id) controller.params.putAll(defaultParams) when: controller.save() then: 1 * securityServiceMock.getCurrentlyLoggedUser() >> user 1 * commandNotificationServiceMock.notifyAccepters(_) 0 * _._ Outlet.count() == 0 OutletInsertCommand.count() == 1 def savedCommand = OutletInsertCommand.get(1) savedCommand.mobileMoneyAccountNumber == '1000000000000' savedCommand.accountType == CyclosAccountType.NOT_AGENT controller.flash.message != null response.redirectedUrl == '/outlet/list' } If you are new to Spock: n*mock.whatever(), means that the method “whatever” of the mock object, should be called exactly n times. No more no less. The underscore “_” means “everything” or “anything”. And the >> sign, instructs the test framework to return the right side argument when the method is called. So what’s wrong with this test? Pretty much everything. Let’s go from the start of “then” part, mercifully skipping the oververbose set-up in the “given”. 1 * securityServiceMock.getCurrentlyLoggedUser() >> user The first line verifies whether some security service was asked for a logged user, and returns the user. And it was asked EXACTLY one time. No more, no less. Wait, what? How come we have a security service in here? The name of the test doesn’t say anything about security or users, why do we check it? Well, it’s the first mistake. This part is not, what we want to verify. This is probably required by the controller, but it only means it should be in the “given”. And it should not verify that it’s called “exactly once”. It’s a stub for God’s sake. The user is either logged in or not. There is no sense in making him “logged in, but you can ask only once”. Then, there is the second line. 1 * commandNotificationServiceMock.notifyAccepters(_) It verifies that some notification service is called exactly once. And it may be ok, the business logic may require that, but then… why is it not stated clearly in the name of the test? Ah, I know, the name would be too long. Well, that’s also a suggestion. You need to make another test, something like “should notify about newly created outlet insert command”. And then, it’s the third line. 0 * _._ My favorite one. If the code is Han Solo, this line is Jabba the Hut. It wants Hans Solo frozen in solid concrete. Or dead. Or both. This line, if you haven’t deducted yet, is “You shall not make any other interactions with any mock, or stubs, or anything, Amen!”. That’s the most stupid thing I’ve seen in a while. Why would a sane programmer ever put it here? That’s beyond my imagination. No it isn’t. Been there, done that. The reason why a programmer would use such a thing is to make sure, that he covered all the interactions. That he didn’t forget about anything. Tests are good, what’s wrong in having more good? He forgot about sanity. That line is stupid, and it will have it’s vengeance. It will bite you in the ass, some day. And while it may be small, because there are hundreds of lines like this, some day you gonna get bitten pretty well. You may as well not survive. And then, another line. Outlet.count() == 0 This verifies whether we don’t have any outlets in the database. Do you know why? You don’t. I do. I do, because I know the business logic of this domain. You don’t because this tests sucks at informing you, what it should. Then there is the part, that actually makes sense. OutletInsertCommand.count() == 1 def savedCommand = OutletInsertCommand.get(1) savedCommand.mobileMoneyAccountNumber == '1000000000000' savedCommand.accountType == CyclosAccountType.NOT_AGENT We expect the object we’ve created in the database, and then we verify whether it’s account is “new”. And we know, that the “new” means a specific account number and type. Though it screams for being extracted into another method. And then… controller.flash.message != null response.redirectedUrl == '/outlet/list' Then we have some flash message not set. And a redirection. And I ask God, why the hell are we testing this? Not because the name of the test says so, that’s for sure. The truth is, that looking at the test, I can recreate the method under test, line by line. Isn’t it brilliant? This test represents every single line of a not so simple method. But try to change the method, try to change a single line, and you have big chance to blow this thing up. And when those kinds of tests are in the hundreds, you have concrete all over you code. You’ll be able to refactor nothing. So here’s another lesson. It’s not enough to verify the right thing. You need to VERIFY ONLY THE RIGHT THING. Never ever verify the algorithm of the method step by step. Verify the outcomes of the algorithm. You should be free to change the method, as long as the outcome, the real thing you expect, is not changed. Imagine a sorting problem. Would you verify it’s internal algorithm? What for? It’s got to work and it’s got to work well. Remember, you want good design and security. Apart from this, it should be free to change. Your tests should not stay in the way. Now for another horrible example. @Unroll('test merchant constraints field #field for #error') def 'test merchant all constraints'() { when: def obj = new Merchant((field): val)then: validateConstraints(obj, field, error)where: field | val | error 'name' | null | 'nullable' 'name' | '' | 'blank' 'name' | 'ABC' | 'valid' 'contactInfo' | null | 'nullable' 'contactInfo' | new ContactInfo() | 'validator' 'contactInfo' | ContactInfoFactory.createContactInfo() | 'valid' 'businessSegment' | null | 'nullable' 'businessSegment' | new MerchantBusinessSegment() | 'valid' 'finacleAccountNumber' | null | 'nullable' 'finacleAccountNumber' | '' | 'blank' 'finacleAccountNumber' | 'ABC' | 'valid' 'principalContactPerson' | null | 'nullable' 'principalContactPerson' | '' | 'blank' 'principalContactPerson' | 'ABC' | 'valid' 'principalContactInfo' | null | 'nullable' 'principalContactInfo' | new ContactInfo() | 'validator' 'principalContactInfo' | ContactInfoFactory.createContactInfo() | 'valid' 'feeCalculator' | null | 'nullable' 'feeCalculator' | new FixedFeeCalculator(value: 0) | 'valid' 'chain' | null | 'nullable' 'chain' | new Chain() | 'valid' 'customerWhiteListEnable' | null | 'nullable' 'customerWhiteListEnable' | true | 'valid' 'enabled' | null | 'nullable' 'enabled' | true | 'valid' } Do you understand what’s going on? If you haven’t seen it before, you may very well not. The “where” part, is a beautiful Spock solution for parametrized tests. The headers of those columns are the names of variables, used BEFORE, in the first line. It’s sort of a declaration after the usage. The test is going to be fired many times, once for for each line in the “where” part. And it’s all possible thanks to Groovy’s Abstract Syntaxt Tree Transofrmation. We are talking about interpreting and changing the code during the compilation. Cool stuff. So what this test is doing? Nothing. Let me show you the code under test. static constraints = { name(blank: false) contactInfo(nullable: false, validator: { it?.validate() }) businessSegment(nullable: false) finacleAccountNumber(blank: false) principalContactPerson(blank: false) principalContactInfo(nullable: false, validator: { it?.validate() }) feeCalculator(nullable: false) customerWhiteListEnable(nullable: false) } This static closure, is telling Grails, what kind of validation we expect on the object and database level. In Java, these would most probably be annotations. And you do not test annotations. You also do not test static fields. Or closures without any sensible code, without any behavior. And you don’t test whether the framework below (Grails/GORM in here) works the way it works. Oh, you may test that for the first time you are using it. Just because you want to know how and if it works. You want to be safe, after all. But then, you should probably delete this test, and for sure, not repeat it for every single domain class out there. This test doesn’t event verify that, by the way. Because it’s a unit test, working on a mock of a database. It’s not testing the real GORM (Groovy Object-Relational Mapping, an adapter on top of Hibernate). It’s testing the mock of the real GORM. Yeah, it’s that stupid. So if TDD gives us safety, design and feedback, what does this test provide? Absolutely nothing. So why did the programmer put it here? Because his brain says: tests are good. More tests are better. Well, I’ve got news for you. Every single test which does not provide us safety and good design is bad. Period. Those which provide only feedback, should be thrown away the moment you stop refactoring your code under the test. So here’s my lesson number three: PROVIDE SAFETY AND GOOD DESIGN, OR BE GONE. That was the example of things gone wrong. What should we do about it? The answer: delete it. But I yet have to see a programmer who removes his tests. Even so shitty as this one. We feel very personal about our code, I guess. So in case you are hesitating, let me remind you what Kent Beck wrote in his book about TDD: The first criterion for your tests is confidence. Never delete a test if it reduces your confidence in the behavior of the system. The second criterion is communication. If you have two tests that exercise the same path through the code, but they speak to different scenarios for a readers, leave them alone. [Kent Beck, Test Driven Development: by Example] Now you know, it’s safe to delete it. So much for today. I have some good examples to show, some more stories to tell, so stay tuned for part 2. Reference: Test Driven Traps, part 1 from our JCG partner Jakub Nabrdalik at the Solid Craft blog....
junit-logo

Test Driven Traps, part 2

The Story of a Unit in Unit Tests In the previous part of this article, you could see some bad, though popular, test samples. But I’m not a professional critic (also known as a troll, or a hater), to grumble about without having anything constructive to say. Years of TDD have taught me more than just how bad the things can go. There are many simple but effective tricks, that can make you test-life much easier. Imagine this: you have a booking system for a small conference room in a small company. By some strange reason, it has to deal with off-line booking. People post their booking requests to some frontend, and once a week you get a text file with working hours of the company, and all the bookings (for what day, for how long, by whom, submitted at what point it time) in random order. Your system should produce a calendar for the room, according to some business rules (first come, first served, only in office business hours, that sort of things). As part of the analysis, we have a clearly defined input data, and expected outcomes, with examples. Beautiful case for TDD, really. Something that sadly never happens in the real life. Our sample test data looks like this: class TestData { static final String INPUT_FIRST_LINE = '0900 1730\n'; static final String FIRST_BOOKING = '2011-03-17 10:17:06 EMP001\n' + '2011-03-21 09:00 2\n'; static final String SECOND_BOOKING = '2011-03-16 12:34:56 EMP002\n' + '2011-03-21 09:00 2\n'; static final String THIRD_BOOKING = '2011-03-16 09:28:23 EMP003\n' + '2011-03-22 14:00 2\n'; static final String FOURTH_BOOKING = '2011-03-17 10:17:06 EMP004\n' + '2011-03-22 16:00 1\n'; static final String FIFTH_BOOKING = '2011-03-15 17:29:12 EMP005\n' + '2011-03-21 16:00 3';static final String INPUT_BOOKING_LINES = FIRST_BOOKING + SECOND_BOOKING + THIRD_BOOKING + FOURTH_BOOKING + FIFTH_BOOKING;static final String CORRECT_INPUT = INPUT_FIRST_LINE + INPUT_BOOKING_LINES;static final String CORRECT_OUTPUT = '2011-03-21\n' + '09:00 11:00 EMP002\n' + '2011-03-22\n' + '14:00 16:00 EMP003\n' + '16:00 17:00 EMP004\n' + ''; } So now we start with a positive test: BookingCalendarGenerator bookingCalendarGenerator = new BookingCalendarGenerator();@Test public void shouldPrepareBookingCalendar() { //when String calendar = bookingCalendarGenerator.generate(TestData.CORRECT_INPUT);//then assertEquals(TestData.CORRECT_OUTPUT, calendar); } It looks like we have designed a BookingCalendarGenerator with a “generate” method. Fair enough. Lets add some more tests. Tests for the business rules. We get something like this: @Test public void noPartOfMeetingMayFallOutsideOfficeHours() { //given String tooEarlyBooking = '2011-03-16 12:34:56 EMP002\n' + '2011-03-21 06:00 2\n';String tooLateBooking = '2011-03-16 12:34:56 EMP002\n' + '2011-03-21 20:00 2\n';//when String calendar = bookingCalendarGenerator.generate(TestData.INPUT_FIRST_LINE + tooEarlyBooking + tooLateBooking);//then assertTrue(calendar.isEmpty()); }@Test public void meetingsMayNotOverlap() { //given String firstMeeting = '2011-03-10 12:34:56 EMP002\n' + '2011-03-21 16:00 1\n';String secondMeeting = '2011-03-16 12:34:56 EMP002\n' + '2011-03-21 15:00 2\n';//when String calendar = bookingCalendarGenerator.generate(TestData.INPUT_FIRST_LINE + firstMeeting + secondMeeting);//then assertEquals('2011-03-21\n' + '16:00 17:00 EMP002\n', calendar); }@Test public void bookingsMustBeProcessedInSubmitOrder() { //given String firstMeeting = '2011-03-17 12:34:56 EMP002\n' + '2011-03-21 16:00 1\n';String secondMeeting = '2011-03-16 12:34:56 EMP002\n' + '2011-03-21 15:00 2\n';//when String calendar = bookingCalendarGenerator.generate(TestData.INPUT_FIRST_LINE + firstMeeting + secondMeeting);//then assertEquals('2011-03-21\n15:00 17:00 EMP002\n', calendar); }@Test public void orderingOfBookingSubmissionShouldNotAffectOutcome() { //given List<String> shuffledBookings = newArrayList(TestData.FIRST_BOOKING, TestData.SECOND_BOOKING, TestData.THIRD_BOOKING, TestData.FOURTH_BOOKING, TestData.FIFTH_BOOKING); shuffle(shuffledBookings); String inputBookingLines = Joiner.on('\n').join(shuffledBookings);//when String calendar = bookingCalendarGenerator.generate(TestData.INPUT_FIRST_LINE + inputBookingLines);//then assertEquals(TestData.CORRECT_OUTPUT, calendar); } That’s pretty much all. But what if we get some rubbish as the input. Or if we get an empty string? Let’s design for that: @Test(expected = IllegalArgumentException.class) public void rubbishInputDataShouldEndWithException() { //when String calendar = bookingCalendarGenerator.generate('rubbish');//then exception is thrown }@Test(expected = IllegalArgumentException.class) public void emptyInputDataShouldEndWithException() { //when String calendar = bookingCalendarGenerator.generate('');//then exception is thrown } IllegalArgumentException is fair enough. We don’t need to handle it in any more fancy way. We are done for now. Let’s finally write the class under the test: BookingCalendarGenerator. And so we do. And it comes out, that the whole thing is a little big for a single method. So we use the power of Extract Method pattern. We group code fragments into different methods. We group methods and data those operate on, into classes. We use the power of Object Oriented programming, we use Single Responsibility Principle, we use composition (or decomposition, to be precise) and we end up with a package like this:We have one public class, and several package-scope classes. Those package scope classes clearly belong to the public one. Here’s a class diagram for clarity:Those aren’t stupid data-objects. Those are full fledged classes. With behaviour, responsibility, encapsulation. And here’s a thing that may come to our Test Driven minds: we have no tests for those classes. We have only for the public class. That’s bad, right? Having no tests must be bad. Very bad. Right? Wrong. We do have tests. We fire up our code coverage tool and we see: 100% methods and classes. 95% lines. Not bad (I’ll get to that 5% of uncertainty in the next post).But we have only a single unit test class. Is that good? Well, let me put some emphasis, to point the answer out: It’s a UNIT test. It’s called a UNIT test for a reason! The unit does not have to be a single class. The unit does not have to be a single package. The unit is up to you to decide. It’s a general name, because your sanity, your common sense, should tell you where to stop. So we have six classes as a unit, what’s the big deal? How about if somebody wants to use one of those classes, apart from the rest. He would have no tests for it, right? Wrong. Those classes are package-scope, apart from the one that’s actually called in the test. This package-scope thing tells you: “Back off. Don’t touch me, I belong to this package. Don’t try to use me separately, I was design to be here!”. So yeah, if a programmer takes one of those out, or makes it public, he would probably know, that all the guarantees are voided. Write your own tests, man. How about if somebody wants to add some behaviour to one of those classes, I’ve been asked. How would he know he’s not breaking something? Well, he would start with a test, right? It’s TDD, right? If you have a change of requirements, you code this change as a test, and then, and only then, you start messing with the code. So you are safe and secure. I see people writing test-per-class blindly, without giving any thought to it, and it makes me cry. I do a lot of pair-programming lately, and you know what I’ve found? Java programmers in general do not use package-scope. Java programmers in general do not know, that protected means: for me, all my descendants, and EVERYONE in the same package. That’s right, protected is more than package-scope, not less a single bit. So if Java programmers do not know what a package-scope really is, and that’s, contrary to Groovy, is the default, how could they understand what a Unit is? How high can I get? Now here’s an interesting thought: if we can have a single test for a package, we could have a single test for a package tree. You know, something like this:We all know that packages in Java are not really tree-like, that the only thing those have with the directory structure is by a very old convention, and we know that the directory structure is there only to solve the collision-of-names problem, but nevertheless, we tend to use packages, like if the name.after.the.dot had some meaning. Like if we could hide one package inside another. Or build layers of lasagne with them. So is it O.K. to have a single test class for a tree of packages? Yes it is. But if so, where is the end to that? Can we go all the way up in the package tree, to the entry point of our application? Those… those would be integration tests, or functional tests, perhaps. Could we do that? Would that be good? The answer is: it would. In a perfect world, it would be just fine. In our shitty, hanging-on-the-edge-of-a-knife, world, it would be insane. Why? Because functional, end-to-end test are slow. So slow. So horribly slow, that it makes you wanna throw them away and go some place where you would not have to be always waiting for something. A place of total creativity, constant feedback, and lightning fast safety. And you’re back to unit testing. There are even some more reasons. One being, that it’s hard to test all flows of the application, testing it end-to-end. You should probably do that for all the major flows, but what about errors, bad connections, all those tricky logic parts that may throw up at one point or another. No, sometimes it would be just too hard, to set up the environment for integration test like that, so you end up testing it with unit tests anyway. The second reason is, that though functional tests do not pour concrete over your code, do not inhibit your creativity by repeating you algorithm in the test case, they also give no safety for refactoring. When you had a package with a single public class, it was quite obvious what someone can safely do, and what he cannot. When you have something enclosed in a library, or a plugin, it’s still obvious. But if you have thousands of public classes, and you are implementing a new feature, you are probably going to use some of them, and you would like to know that they are fine. So, no, in our world, it doesn’t make sense to go with functional tests only. Sorry. But it also doesn’t make sense to create a test per class. It’s called the UNIT test, for a reason. Use that. Happy coding and don’t forget to share! Reference: Test Driven Traps, part 2 from our JCG partner Jakub Nabrdalik at the Solid Craft blog....
career-logo

Disrupt Tech Recruiting II – So You Want Ari Gold?

After publishing How to Disrupt Technical Recruiting – Hire an Agent and reading the subsequent feedback from readers at Hacker News and elsewhere, it is clear that at least some subset of engineers believe two things:The technical recruiting industry is at times remarkably flawed, and the financial incentives inherent to the system will not always lead a recruiter to represent the job seeker’s best interests. There is some demand for a talent/agent model for tech professionals, and it is a service several would be willing to pay for.And it is also worth noting that the only agent that engineers have know is Ari Gold (or at least that is the agent they want). Not even ONE Jerry Maguire reference? It seems engineers want to hug it out and care less about being shown the money. During my dissection of the industry, I somehow overlooked perhaps the biggest flaw that is at the absolute core of the issues with the recruiting industry. I’m a bit embarrassed that I missed it, and it wasn’t mentioned in the threads, so here goes: Secrecy and privacy of information is THE cornerstone to traditional recruiting. Why is that? Well think about it. If hiring companies knew every local candidate that possessed the skills they were seeking, many would not use an agency recruiter to handle the process and would simply have HR/managers contact them directly. Likewise, there would be many candidates that distrust recruiters applying directly to jobs if every available job were listed in one single place to search. There would certainly be some companies and candidates that recognize the value the recruiter brings beyond the initial introduction, but if all information were available to both sides we would find much less demand in the industry. Why do you think recruiters don’t generally list their clients’ names publicly? Why are there so many complaints about recruiters being unwilling to share a deep amount of detail about projects or their open jobs? There are two main reasons.To prevent other recruiters from learning about who they represent (NOTE: this is a big one, particularly for recruiters that work exclusively with start-ups that fly under the radar). To prevent candidates from applying directly to those companies, thus cutting out the chance for the recruiter’s fee.So in order to preserve their chances of being paid a fee, recruiters need to keep candidates and companies from knowing about each other until a precise moment where they make the introduction to both sides, and then hope that the company doesn’t respond with some evidence of prior contact and that the candidate doesn’t say that he/she applied to that job yesterday. If either of those two scenarios happen, the recruiter has some incentive for that deal to NOT work out, even though it is his/her client! Let’s assume that company has one vacancy, and my candidate applied to it a day before I discussed it with him/her. My incentive instantly goes from wanting my candidate to get the job (so I can get a fee) to a financial incentive that the candidate isn’t successful (so the job will still be available for another candidate I can find). That is a very dark and unfortunate set of incentives inherent to the system. It’s very easy to see how the incentives are built in this model. Recruiters have the incentive to help out their client companies to fill jobs, but only if the hire comes from my agency. The recruiter has the incentive to help a candidate find a job, but only if he/she takes a job at one of our clients. This is the unfortunate symptom of contingency work, and thankfully in my current business model (which isn’t contingency) I don’t have these same incentives to the same degree. Who are contingency recruiters really providing a service to anyway? Why do you think recruiters see LinkedIn as both a blessing and a curse? We use it to find new candidates and keep in touch with past candidates and clients, yet we realize that everyone else now has access to the same information and companies can much more easily find candidates. As a recruiter, you are probably likely to list your contacts as ‘private’ for this reason. The agent model would not have this privacy incentive built into the system. I could see an argument made where an agent might not want other agents to know who he/she is representing, but I think that is a much smaller problem than what we have today. When one of an agent’s talents is seeking work, there would be a campaign of open information to try and get the talent noticed. Having a business model based on privacy and secrecy is much less attractive than an agent model with openness. Enough about what’s broken, let’s talk about solutions. Based on the discussions, here are the services that the talent knows they want:Negotiation in job changes – Engineers seem to agree that an agent would probably be better at negotiating compensation packages. Another added benefit to having an agent in act as a proxy in negotiations is the preserved comfort factor after you potentially start the job. Picture a very trying or heated negotiation between an engineer and a start-up CEO that comes to an eventual agreement, and then the two must work in adjacent cubicles the following Monday. A buffer would have helped prevent any potential awkwardness. Job identification and coordination – Many in the community made it pretty clear that they are not interested or skilled in cold calling and introducing their services to potential clients. Having an agent do the legwork to identify potential employers and then to contact them and schedule meetings on the talent’s behalf will save lots of time to do other things. For consultants paid hourly, remember that every hour spent on job search is an hour you can’t bill to a client. Competitive salary/rate information – There seems to be a general distrust for websites that provide market information, and engineers may be somewhat insulated when it comes to what others with similar skills are earning. An agent would have some solid evidence regarding street value of any talent and would do the research necessary. Marketing/PR/Selling – It has become clear to me over the years that there is a sizable percentage of talented engineers that are uncomfortable talking up their own skills, and that was mentioned a couple times as another asset of an agent. Marketing and PR would probably mean different things for different career levels, but it could include work to increase the talent’s blog traffic, booking a presenter’s invite to a users group/meetup, or strategy on how to build the talent’s brand.There were some services and advantages that were somewhat surprisingly not mentioned:Handling incoming job solicitations – All of the complaints about recruiter spam and cold calls are gone for the agent’s talent. Forward calls, emails and LinkedIn spam to your agent, and he/she will investigate. Put your agent’s name on your website and LinkedIn profile. If the opportunity is legitimate and meets the criteria the talent sets for sharing, your will get the details. If not, you never have to waste your time hearing about it. Interview coaching – Not just your standard fare interview coaching you can find on web sites, but also any inside tips on the specific interview. This could include past experiences others have had with the company, some research on the background of the people you are meeting, and anything else that will be helpful to the talent. Remember that the agent is representing you at this point and not the company. As one reader noted, a good agent would not want to do companies a disservice by providing specific interview questions (as that would harm the agent model, the hiring company, and the agent himself/herself), but it is fair to offer the talent some guidance on what format to expect and who they will be meeting. Internal company information – As your representative, the agent should be providing talent with details on both their current employer and any that you may be considering for future employment. The information learned from discussions with others in the business will be helpful to the agent’s entire stable of talent. Consider it the type of info you may find on Glassdoor, but from verified sources. Career advice in non-search situations – Are you thinking about moving from code into management, or considering taking a leadership position on a project doomed for failure? Considering a jump into contracting, or thinking about abandoning your current independent consulting project to join a start-up? Want to talk about it? The value of this advice could be quite large yet difficult to quantify. Negotiation of promotions or raises – Changing jobs is not the only scenario where negotiation may take place. An agent could help you get the best deal from your current employer, either through direct negotiation or by coaching you on how to maximize your chances of getting a good number. Resume creation/curating – This one isn’t a huge service but it’s a few hours out of your work year. Transparency! – The agent model is completely transparent to everyone involved. The agent represents the talent, and the talent pays for that representation. There is no question about the agent’s loyalty as there is in contingency recruiting models (is the recruiter representing me or the company?).Some potential issues were identified in the commentsWould there be any conflict of interest when representing multiple candidates? Doubtful. I guess there could be a rare situation where two talents vie for the same position, and in that scenario the agent would simply represent both fairly and let the best person for the job win. The agent would probably have no financial incentive favoring either candidate. Transparency would be important (revealing the situation to both talents). Would there be backlash by hiring entities against candidates who use an agent? It’s hard to tell, but it’s conceivable that there would be some organizations that might react hesitantly at first. However, the key here is that an agent would be providing a service to a company for free (or if not completely free, definitely much less expensive than recruiters). It would be hard to think that firms would not be very pleased with a cold call from an agent stating that there is a qualified candidate interested in your company, and you will get all the normal recruiter services for the process with no recruiter fee at the back-end. How much would the agent need to know about technology? This one comes up quite a bit in criticisms of technical recruiters and then in the discussion of an agent. The agent needs to know enough about tech that he/she won’t misrepresent the talent, and will not waste time marketing talent to positions that are not a technical fit. An agent should also have some sense of tech trends, what skills are in demand, and the overall market for different types of talent. The biggest complaint seems to be wasting time with discussions of jobs that are not a fit or being sent on interviews that are not appropriate. An agent would not have the same incentive to send you everywhere imaginable, as that incentive is not built into the agent model. Would the service be just for contractors? No, not the way I see it. The pricing model for contractors is very easy to consider, as paying a percentage of hourly rate to the agent is already a common practice for staffing firms (what is called ‘margin’). The value of an agent’s service to engineers with salaried positions seems very obvious in helping to manage the overall career as well as the career moves.How do we compensate the agent? This is a bit tricky and I believe that the pricing model might be different for contractors and talent in direct salaried positions. For contractors, the existing model (a ‘per hour’ cut) is well-known and accepted. I think the main difference is an agent would have some fixed transparent rate ($x/hr or y%/hr. Many recruiting firms do not reveal the full bill rate to the contractor, which tends to cause distrust when the contractor finds out the actual rate. Having a fixed percentage or dollar per hour figure would make the relationship much more comfortable for both sides. For talent in permanent salaried positions, there needs to be more discussion. My vision would be some flat annual representation fee for service with an additional charge/bonus for times of active job search (where the level of services and time invested would increase). The job search fee could have several models built in – perhaps an hourly rate, flat fee per week/month, flat fee per job search, or some percentage of salary. The problem with the percentage of salary model is that it gives some incentive for the agent to suggest you take the highest paying job even if that is not best for your career. Another model would be a negotiation bonus above some level set and agreed upon by the agent and talent. “For anything I get you over 120K, I get a one-time bonus of 50% of the difference.” That leads to some incentive to take the highest paying job, but it also gives more motivation to negotiate. The reason I include an annual representation fee into the discussion is it helps to eliminate an agent’s incentive for candidates to change jobs, and it allows the agent to justify investing time with the talent to discuss career-related issues that come up. In the traditional recruiting model, the recruiter hopes everyone is looking for a job at all times. An agent would have much less of a financial incentive for you to change employers, so the agent wouldn’t be as likely to suggest you leave a job that is right for you. CONCLUSION Traditional contingency recruiting incentives are:Privacy and secrecy of client company and candidate information. Only help client company when it results in a fee. Only help candidates when they take jobs at my clients. A desire for people to leave jobs as often as possible.Agent model incentives:Keep the talent happy so they remain your talent. ???Let’s keep the dialogue going on this. Based on lots of direct feedback, it seems the community could be on to something. Agree, disagree, suggest? Reference: Disrupt Tech Recruiting II – So You Want Ari Gold? from our JCG partner Dave Fecak at the Job Tips For Geeks blog....
javafx-logo

Pure Java JavaFX 2.0 Menus

In recent posts on JavaFX, I have focused on using JavaFX 2.0′s new Java APIs without use of the JavaFX 1.x’s JavaFXScript and without use of JavaFX 2.0′s new FXML. All of these examples have been compiled with the standard Java compiler and executed with the standard Java launcher. In this post, I continue the theme of using pure Java APIs supported by JavaFX 2.0 while demonstrating development of JavaFX 2.0 menus. I list the entire code listing for this example later in this post, but I first show snippets of the code to make it easier to focus on each piece. A good starting point for using JavaFX 2.0 menus is to instantiate an instance of MenuBar. This is straightforward as shown next. Instantiating a javafx.scene.control.MenuBarfinal MenuBar menuBar = new MenuBar(); A MenuBar can contain Menu instances as its children and each Menu instance can have instances of MenuItem as its children. The next code listing demonstrates instantiation of a Menu, adding of MenuItem instances (or an instance of SeparatorMenuItem) to that Menu instance, and then adding the Menu instance to the instance of MenuBar.Adding Newly Instantiated Menu and MenuItem Instances to MenuBar// Prepare left-most 'File' drop-down menu final Menu fileMenu = new Menu('File'); fileMenu.getItems().add(new MenuItem('New')); fileMenu.getItems().add(new MenuItem('Open')); fileMenu.getItems().add(new MenuItem('Save')); fileMenu.getItems().add(new MenuItem('Save As')); fileMenu.getItems().add(new SeparatorMenuItem()); fileMenu.getItems().add(new MenuItem('Exit')); menuBar.getMenus().add(fileMenu); The example above is too simplified for realistic uses. There are no event handlers or actions associated with clicking on any of the menu items and there are no ways to select the menu items via keystroke rather than via mouse clicking. The next code listing demonstrates instantiation of MenuItem instances that include more than just a text string. In this code listing, there is an example of using MenuItemBuilder to build a much more complex MenuItem that includes association to a key combination and includes an association to an action handler.More Sophisticated MenuItem Instantiation with Keystroke and Event Associations// Prepare 'Help' drop-down menu final Menu helpMenu = new Menu('Help'); final MenuItem searchMenuItem = new MenuItem('Search'); searchMenuItem.setDisable(true); helpMenu.getItems().add(searchMenuItem); final MenuItem onlineManualMenuItem = new MenuItem('Online Manual'); onlineManualMenuItem.setVisible(false); helpMenu.getItems().add(onlineManualMenuItem); helpMenu.getItems().add(new SeparatorMenuItem()); final MenuItem aboutMenuItem = MenuItemBuilder.create() .text('About') .onAction( new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { out.println('You clicked on About!'); } }) .accelerator( new KeyCodeCombination( KeyCode.A, KeyCombination.CONTROL_DOWN)) .build(); helpMenu.getItems().add(aboutMenuItem); menuBar.getMenus().add(helpMenu); Besides demonstrating MenuItemBuilder, associating a key combination (CTRL-A in this case) with a menu item, and associating an action with a menu item, this code example also demonstrates making a menu item disabled (grayed out) with setDisable(boolean) or making it not appear at all with setVisible(boolean). Although I could have specified disabling the menu item or making the menu item invisible with a MenuItemBuilder, I intentionally used ‘set’ methods on the MenuItems in this example to contrast that approach with using the MenuItemBuilder. For completeness, here is the entire code listing of my example.JavaFxMenus.java (The Complete Listing)package dustin.examples;import static java.lang.System.out;import javafx.application.Application; import javafx.beans.property.ReadOnlyDoubleProperty; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyCodeCombination; import javafx.scene.input.KeyCombination; import javafx.scene.paint.Color; import javafx.stage.Stage;/** * Example of creating menus in JavaFX. * * @author Dustin */ public class JavaFxMenus extends Application { /** * Build menu bar with included menus for this demonstration. * * @param menuWidthProperty Width to be bound to menu bar width. * @return Menu Bar with menus included. */ private MenuBar buildMenuBarWithMenus(final ReadOnlyDoubleProperty menuWidthProperty) { final MenuBar menuBar = new MenuBar();// Prepare left-most 'File' drop-down menu final Menu fileMenu = new Menu('File'); fileMenu.getItems().add(new MenuItem('New')); fileMenu.getItems().add(new MenuItem('Open')); fileMenu.getItems().add(new MenuItem('Save')); fileMenu.getItems().add(new MenuItem('Save As')); fileMenu.getItems().add(new SeparatorMenuItem()); fileMenu.getItems().add(new MenuItem('Exit')); menuBar.getMenus().add(fileMenu);// Prepare 'Examples' drop-down menu final Menu examplesMenu = new Menu('JavaFX 2.0 Examples'); examplesMenu.getItems().add(new MenuItem('Text Example')); examplesMenu.getItems().add(new MenuItem('Objects Example')); examplesMenu.getItems().add(new MenuItem('Animation Example')); menuBar.getMenus().add(examplesMenu);// Prepare 'Help' drop-down menu final Menu helpMenu = new Menu('Help'); final MenuItem searchMenuItem = new MenuItem('Search'); searchMenuItem.setDisable(true); helpMenu.getItems().add(searchMenuItem); final MenuItem onlineManualMenuItem = new MenuItem('Online Manual'); onlineManualMenuItem.setVisible(false); helpMenu.getItems().add(onlineManualMenuItem); helpMenu.getItems().add(new SeparatorMenuItem()); final MenuItem aboutMenuItem = MenuItemBuilder.create() .text('About') .onAction( new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { out.println('You clicked on About!'); } }) .accelerator( new KeyCodeCombination( KeyCode.A, KeyCombination.CONTROL_DOWN)) .build(); helpMenu.getItems().add(aboutMenuItem); menuBar.getMenus().add(helpMenu);// bind width of menu bar to width of associated stage menuBar.prefWidthProperty().bind(menuWidthProperty);return menuBar; }/** * Start of JavaFX application demonstrating menu support. * * @param stage Primary stage. */ @Override public void start(final Stage stage) { stage.setTitle('Creating Menus with JavaFX 2.0'); final Group rootGroup = new Group(); final Scene scene = new Scene(rootGroup, 800, 400, Color.WHEAT); final MenuBar menuBar = buildMenuBarWithMenus(stage.widthProperty()); rootGroup.getChildren().add(menuBar); stage.setScene(scene); stage.show(); }/** * Main executable function for running examples. * * @param arguments Command-line arguments: none expected. */ public static void main(final String[] arguments) { Application.launch(arguments); } } The next series of screen snapshots attempt to demonstrate what this application looks like when executed using the java launcher. The images show the initial appearance of the application, the drop-down menu presented when ‘File’ menu is clicked on, the drop-down menu presented when the ‘Help’ menu is clicked on, and finally an image that shows the message written to standard output when the ‘About’ menu item is clicked on under the ‘Help’ menu. The code in the example featured in this post has numerous syntax features that should look familiar to Swing developers. In fact, many of the JavaFX classes used above have the same names as AWT classes and so care must be used to import the correct class when using the IDE’s automatic import suggestions. The example above also provides an example of JavaFX binding. In particular, the width of the menu bar is bound to the width of the stage’s width. This is useful because it looks better to have the menu bar span the entire top of the visual rather than being just wide enough to hold the menu labels. Building menus is fairly straightforward in JavaFX 2.0 and can be implemented using basic Java tools and the JavaFX 2.0 JAR. Happy coding and don’t forget to share! Reference: (Pure Java) JavaFX 2.0 Menus from our JCG partner Dustin Marx at the Inspired by Actual Events blog....
packt-logo

Android books giveaway for celebrating Packt’s 1000 title

Fellow geeks, We are pleased to announce that we have once again teamed up with Packt Publishing and we are organizing another giveaway for you! The occasion is the celebration of 1000 IT titles by Packt! So, 4 lucky winners will have the chance to win a copy of Packt’s best-selling books on Android (ebook format). Read along to find more details about the contest. Packt Publishing is about to publish its 1000th title. Packt books are renowned among developers for being uniquely practical and focused, covering highly specific tools and technologies.     For this occasion, Packt is giving everyone a surprise gift! However, the surprise won’t be revealed until September 28, 2012, which is when the 1000th title is expected to be unveiled. To receive this gift, simply register for an account on the Packt website and return between September 28 and 30.     Another thing to notice is that Packt supports many of the Open Source projects covered by its books through a project royalty donation, which has contributed over £300,000 to Open Source projects up to now. As part of the celebration Packt is allocating $30,000 to share between projects and authors, soon to be disclosed on their website. And of course Packt was kind enough to provide some cool Android books only for the readers of Java Code Geeks. Here are the books!     AndEngine for Android Game Development Cookbook: RAWOverview of the uber-cool AndEngine game engine for Android Game Development. Includes step by step detailed instructions and information on a number of AndEngine functions, including illustrations and diagrams for added support and resultsThis book is currently available as a RAW (Read As we Write) book. Android 3.0 Application Development CookbookQuickly develop applications that take advantage of the very latest mobile technologies, including web apps, sensors, and touch screens. Excellent for developing a full Android application. Appcelerator Titanium Smartphone App Development CookbookLeverage your JavaScript skills to write mobile applications using Titanium Studio tools with the native advantage! Android User Interface Development: Beginner’s GuideLeverage the Android platform’s flexibility and power to design impactful user-interfaces. How to Enter? Just send an email here using as subject “Packt Android Giveaway”. An empty email will do, it’s that simple! Optionally in your email, you may mention which book would you prefer the most! We are waiting for your emails! (Note: By entering the contest you will be automatically included in the forthcoming Java Code Geeks Newsletter.)     Again, don’t forget to sign-up for a free account in Packt before the 30th of September, 2012. Visit their website: www.PacktPub.com. A pleasant surprise gift is in order for each one of you! We would love it if you shared this! Spread the world people! ;-)...
software-development-2-logo

Trying to understand CAP

The CAP theorem, stated by Brewer and proved by Gilbert and Lynch specifies a property of distributed systems. It states that such a system cannot guarantee at the same time Consistency, Availability and Partition tolerance. It is also often said as a catchy phrase: Consistency, Availability, Partition Tolerance – pick any two used mostly when talking about NoSQL databases and suggesting that a distributed system can be characterized as either CA, AP or CP (see e.g. here). For some time I’ve been trying to understand the different combinations and what do they mean in practice; having some time at various airports I caught up on reading, and here’s what I came up with. What’s C, A and P? First let’s define how I understand the three guarantees, basing on some of the articles I’ve read. Consistency is the easiest one. It roughly means that the clients get the same view of data. By saying that a system is consistent we often mean strong consistency, but it also can come in different flavors, e.g. casual. Availability is a property saying that every request to a non-failing node will return a (meaningful) response. The response may not contain all data (so the harvest will not be 100%, see the appropriate section in [3]), but it should be useful for the client. Partition tolerance means that the system will continue working even if any number of messages sent between nodes is lost. This can be e.g. a network failure between two datacenters, where nodes in each datacenter form a partition. Also note that a failure of any number of nodes forms a partition (it is not possible to distinguish between a network failure and a node failing and stopping to respond to messages). The hardest part for me is understanding the difference between Availability and Partition tolerance. Also, the various articles don’t specify what they mean by saying that a system is “working” after being e.g. partitioned – does it mean that every request gets a response with useful data, or are responses “Sorry, I can’t give you data right now” acceptable also? P+C? Let’s assume that a system is partition tolerant and that it has more than one node. If a partition is formed, splitting the system in two, the system should continue working. Hence both partitions allow clients to write. But then, how to guarantee consistency? If one client writes to partition 1, and another to partition 2? Hence: P => ~C. A+~P? Suppose now that a system is available and that we have more than one node. As the system is available, it should respond to requests even if some nodes die. As noted above, some nodes dying are equivalent to a partition. So if the system is still working, we have partition tolerance. Hence: A => P. A+C? Summarizing the two implications above (A => P and P => ~C), we get: A => ~C, so that an available system cannot be consistent (if it has more than one node). In practice however, there are of course AC systems, e.g. single-node RDBMS. Or even master-slave/master-master replicated RDBMS, provided there’s a central router knowing which nodes live and directing client appropriately. Such a router is then a single point of failure (SPoF). Relax? I suspect that in reality, when e.g. NoSQL/NewSQL systems are characterized with the CAP properties, they assume some relaxed form of C/A/P. Unfortunately, all of the definitions flying around seem to be pretty vague and are more of hand-waving than proper, precise statements. I think it would be much easier to explore the ever-growing ecosystem of new datastores if they could be more easily characterized; maybe the CAP vocabulary is just not enough? Please correct me if I’m wrong somewhere, and I probably am! :) And don’t forget to share! Reference: Trying to understand CAP from our JCG partner Adam Warski at the Blog of Adam Warski blog....
software-development-2-logo

Motivation, Creativity and Innovation

I am a strong believer in that every individual on this earth can be creative and that hard focused work beats natural talent in every way. Personally I get really inspired by observing skillful people come up with amazing and creative ideas. Others may feel rivalry and/or envy. I am not sure why certain people react one way or another but dare I speculate that it insecurity have something to do about it? I feel confident to say that truly creative people does not feel intimidated or sentimental about their ideas being trumped by better ideas. In fact they encourage it. Because they know competition will inspire and push themselves further. There is nothing to fear if you are humble enough to accept and build on other peoples ideas to find ways to continuously elevate your intellect. Isaac Newton indicated that innovation is the continuous process of creating value from existing ideas, knowledge and inventions. So it is reasonable to think that most companies want to have motivated employers that are original, think differently and are highly creative. Right. And CEOs should not forget that this is just as important for them as well. So how do you engage people to be high performing, innovative and creative? I think Dan Pink is spot on.Autonomy: The desire to be self directed. Mastery: The urge to get better at stuff. Purpose: People are happiest when working for something larger than ourselves.Economical rewards are great. But they stop working as motivator for intellectual work when reaching a certain level – it is a lot better to empower people with freedom to express their creativity. I have observed that management sometimes carry out this form of hypocrisy, pushing employees in this direction without giving them the freedom to actually start doing it. As an example, do you recognize the following leadership style? According to Command-and-Conquer, managers-slash-leaders figure out where the business is going to go, and then issue the appropriate orders to their lieutenants to move the business in that direction. Their lieutenants in turn divide up the tasks into smaller chunks and command their reports to implement them. This continues down the org-chart until eventually someone at the bottom actually does some work. In this model, a programmer is a cog in the machine: a typist who carries out one part of management’s orders. Some businesses actually run this way. You can always tell when you are dealing with such a business, because the person you are talking to is doing something infuriating and senseless, and they know it, and they might even care, but there’s nothing they can do about it. Joel Spolsky: The Development Abstraction Layer This is an excellent example of how to efficiently kill motivation and creativity; encouraging a workforce with breaths smelling like urine and an awful way of growing skillful developers. Visionary people who have learned self-motivation and able to visualize patterns and connect the dots in a conceptual age and converging world will not accept this kind management and will find ways to escape it one way or another. Leadership is a crucial part in business and military success in the same way as creating a culture of innovation. If innovation does not happen in the organization, your leaders are probably killing it one way or another. For example, only trusting your users to tell you how to run your business. If I had asked people what they wanted, they would have said faster horses. Henry Ford Users know what problem they have, which is important and you should listen to your users. But listening to users *exclusively* will never make you entrepreneurial. Steve Jobs told customers what they wanted instead of asking for feedback. And before i forget – Henry Ford built cars… What will happen if peoples ideas are rejected over and over again, maybe because their leaders does not trust them? They will quickly stop sharing them because clearly their ideas are worthless to the company. But lets assume that you have a lot of creative people with mind blowing, innovative and relevant ideas. It takes almost as much creativity to understand a good idea as to have it in the first place. Alan Kay Having wonderful ideas is not enough. If you have to spend just as much time to convince people of the idea as on the idea themselves there will be no innovation left at the end of the day. Leaders must realize that innovation is fragile and must actively try to find and encourage it. Stakeholders must also accept risk and communicate that failure is expected and not punish failures. At the same time – execution is critical. Ideas are not useful if they are irrelevant to business or if you cannot make reality out of them. It is important to build excellent teams that are able take an idea and work with it. If you give a good idea to a mediocre group, they’ll screw it up. If you give a mediocre idea to a good group, they’ll fix it. Or they’ll throw it away and come up with something else. Ed Catmull, Pixar: Keep Your Crises Small This is why people should be allowed to work on the things that motivates them. Motivation is of course not a guarantee for success. But with a good ideas, passion and dreams your chances for success increase drastically since individuals will be way more productive and caring than on average. Ignore this and you may find your co-workers spending most of their time at the coffee machine or surfing the web instead of doing incredible work. Some say “ignorance is bliss” and I guess they are right. But I do not want to take the easy way out… My brain is simply allergic to ignorance. Fish rot from the head as do organisations. Remember that. Reference: Motivation, Creativity and Innovation from our JCG partner Kristoffer Sjogren at the deephacks blog....
apache-ant-logo

Useful Ant build tags

Questions: How to do following tasks in ant file?Make zip file. Run command. Copy files to remote machine. Run commands on Remote Linux machine. Open an input box and respond to input value. Make an ant call.Answers: 1. Make zip file: Following is the xml for making zip file in ant: <zip destfile='${destination.folder}/zipName.zip'> <fileset dir= '${Source.folder}' /> </zip> In here “destfile” is the name and location of the created zip file. Inside fileset tag the dir attricute is used to specify source folder form where all files will be zipped. 2. Run commands: Here I will show you how to start tomcat in ant file to demonstrate how to run commands in ant. Following is the xml for this: <exec dir='${tomcat.home}/bin' executable='cmd' os='Windows XP'> <arg line='/c startup.bat'/> </exec> Here “${tomcat.home}” is the path of the tomcat folder. The command is given in “<arg>” tag in “line” attribute. Note: To run following commands you will need JSCH jar. 3. Copy files to remote machine: If remote machine is in your LAN and you can access it directly as \\remote_machine\path then you can use simple copy command as follows: <copy file='${source.folder.file} ' todir='\\remote\path'> </copy> To copy files on remote machine that supports SCP use following tag: <scp file='${source.folder.file} ' todir='${remote.user}@${remote.host}:${remote.path.where.to.do.copy}' password='${remote.password}' trust='true'> </scp> In above both commands “file” is the source file which you want to copy with its path. And “todir” is the remote machine folder path where you want to copy the file. 4. Run commands on remote machine: You can use following tag to execute commands on remote Linux machine. <sshexec host='${remote.host}' username='${remote.username}' password='${remote.password}' command='${command.to.run}' trust='true' /> For example to start tomcat use following tag: <sshexec host='${remote.host}' username='${remote.user}' password='${remote.password}' command='sh ${tomcat.home}/startup.sh' trust='true' />5. Open an input box and respond to input value: To open an input dialog use following tag: <input message='Enter id: ' addproperty='my.id'> </input> Here “my.id” in “addproperty” is the variable name which holds input value. Now to check if user has denied to enter value in input: <condition property='do.abort'> <equals arg1='n' arg2='${my.id}'/> </condition> <fail if='do.abort'>Build aborted by user.</fail> And if user enters value and press OK then after you can refer to entered value as “${my.id}”. 6. Make an ant call: <antcall target='targetName'> </antcall> Here “target” is the name of the target that will be executed. Note: In above examples all values starting with “${”and ending with “}’’ are variables and you may have to put appropriate values in them to successfully run them. Reference: Useful Ant build tags from our JCG partner Harsh Raval at the harryjoy blog....
java-logo

Checked and unchecked exceptions in Java

Java has two types of exceptions – checked and unchecked. In short – checked are meant for cases when the developer can reasonably recover from the exception, while unchecked exceptions are programming errors that can’t be dealt with. This article explains when to use which. But it is not that simple – checked exceptions are making the code “uglier”. They force the developer to write try/catch blocks or to rethrow the exception. But rethrowing hides another problem – some exceptions should not cross module boundaries. The most common thing to do when you are forced to catch a checked exception, which you have no idea what to do with, is to simply wrap it in a RuntimeException and rethrow it. Actually, it might not be the most common – especially novice programmers tend to swallow exceptions, with empty catch blocks. Log & rethrow leads to doubled stacktraces sometimes, if there is some general layer for exception handling. Anyway, there are multiple bad practices here that lead to code that’s hard to debug and maintain. Some say checked exceptions should be eliminated completely, given the verbosity, tediousness and error-proneness they introduce. C# does not have checked exceptions at all. Of course eliminating them should take into account backward compatibility. I, however, believe the decision to have these two types of exceptions has its plus(es). It forces the developer to think that an exception is likely to happen in this situation, so he must take measures. The API declares that it will throw exceptions and the developer sees this compile-time. It enforces compile-time safety. You should not wait until the code goes to production to discover that something might fail. Javadoc? Well, a good option to say that, but I bet no one will read the javadoc until the exception actually happens. So, how to have the “best of both worlds” ? I had a bizarre idea (explained in details here) for APIs to define two interfaces (linked through inheritance, so that only one interface is actually supported), and providing a way, through a factory, to get either an implementation whose methods throw checked exceptions, or one that wraps the checked exceptions into unchecked ones. It may be viable, it may be stupid, I can’t tell. For now it just looks strange. But the above is a workaround at best. Then another idea came – introduce @RethrowExceptions annotation on methods. It will tell the compiler that in this method you don’t want to handle checked exceptions, but you also don’t want to declare them to be thrown. (The name of the annotation can be improved). In the simplest implementation that comes to my mind this can simply tell the compiler to surround the whole method body with try {..} catch (Exception ex) { throw new RuntimeException(ex);}. The benefits:you are still warned by the compiler that the method you are using can throw an exception and you must think of handling it you don’t make your code ugly by needless try/catches. And you don’t force your callers to think what to do with the exception the possibility of swallowing the exception is decreased.So in short – such an annotation would mark a method as one that is unable to handle the exception and does not want to propagate this decision to its callers. This idea sounds less bizarre. I guess it can be implemented even now, using a compiler plug-in. Or perhaps it is already implemented in something like project lombok? Don’t forget to share! Reference: Checked and Unchecked Exceptions in Java from our JCG partner Bozhidar Bozhanov at the Bozho’s tech blog blog....
scala-logo

Fault-tolerance primitives in Scala: links and traps

Over the last decade Actor Model is actively marketed as a simple, effective and save concept for building concurrency. Being popularized by Erlang in early nineties, and used as a primary concurrency construct in Scala, Actors offer share-nothing event-driven model, where creating and maintaining a single actor is cheap (so that you can keep running millions of them), and remote actors working at distributed nodes look and feel as the local ones. However, being used unwisely, actors may bring even more pain and boilerplate, than thread-based concurrency or STM. Lack of the in-depth documentation on actor-based modeling leads to misconceptions and myths that can be broken working through best-practices and design guidelines of building fault-tolerant systems in Erlang. Fault-tolerance primitives Defensive programing forces you to put a huge effort to protect the system against all kinds of unexpected behavior you may think of, checking inputs and current state whenever possible, and defining ways to escalate the problem on a higher level. Being a defensive programmer you should always be proactive and look for a new potential problems to handle, because even the smallest one causing a failure in insignificant component may lead to a huge system crash. Fault-tolerant systems as apposed don’t tend to predict all possible failure reasons (to prevent a single component from crash), but rather isolate the component from the rest of the system and restart it, keeping the system work consistently. If restart of the component doesn’t help, the problem may be propagated to the higher level, in order to [possibly] restart the parties that exchange messages with the component. As Joe Armstrong says on his book “Programming Erlang”, when one actor dies in the room full of actors, others should probably notice that and start fixing the problem (cleaning out the bodies). In Erlang and Scala this behavior is achieved by linking actors. In the most basic form, when two actors are linked, and one of them dies, it sends an exit signal to another actor to terminate it too. Actors linking is bidirectional operation, so, when you link actor A to B, B is on the background linked to A, and death of any of them cause sending an exit signal to another. In Erlang it’s possible to create unidirectional linking with monitors (when monitored process dies, ‘DOWN’ message is sent to the handler). There’s no analogue to the monitors in Scala standard library, however implementing it on demand would be easy. Linked actor can create an exit trap, so that exit signal will be processed as a normal message, not causing actor termination. Exit signal normally contains the reference to the failed actor (that can be used to restart it), and failure reason. In “Programming Erlang” several link/trap exit scenarios are represented with a simple example: -module(edemo1). -export([start2]). start(Bool, M) -> A = spawn(fun() -> a() end), B = spawn(fun() -> b(A, Bool) end), C = spawn(fun() -> c(B, M) end), sleep(1000), status(b, B), status(c, C). In the code above three processes are created, synchronized with sleep, in order to give them time to process the passed messages (ugly in general, but works for a simple example), and afterwards, their state is checked. Actors are defined as follows: a() -> process_flag(trap_exit, true), wait(a). b(A, Bool) -> process_flag(trap_exit, Bool), link(A), wait(b). c(B, M) -> link(B), case M of {die, Reason} -> exit(Reason); {divide, N} -> 1N, wait(c); normal -> true end. Process A always traps exits, process B is linked to A and traps exits depending on the function input, process C linked to B receives messages and either makes computation or fails. wait(Prog) -> receive Any -> io:format('Process ~p received ~p~n' ,[Prog, Any]), wait(Prog) end. Method wait recursively receives messages printing out the message content. Being translated into Scala, using standard Actors library, the example looks as follows: object LinkedActors { case class Die(reason:AnyRef) case class Divide(number: Int) def status(name: String, actor: Actor) = println('Actor %s is %s' format(name, actor.getState)) def printMessage(actorName: String):PartialFunction[Any, Unit] = {case msg => println('Actor %s received %s' format(actorName, msg))} def start(isTrapExit: Boolean, message: Any) = { val A = actor{ self.trapExit = true loop(react(printMessage('a'))) } val B = actor{ self.trapExit = isTrapExit self link A loop(react(printMessage('b'))) } val C = actor{ self link B loop{ react{ case Die(reason) => exit(reason) case Divide(number) => 1number } } } C! message Thread.sleep(1000) status('b', B) status('c', C) } } Essentially, code is the same with the difference that messages accepted by actor C are classified with case classes and receive behavior of actors B and C is represented with partial functions. Let’s pass some inputs to the start method in order to see, how will the chained actors behave, when some of them die: scala> start(false, Die('abc')) Actor a received Exit(scala.actors.Actor$$anon$1@dc8f6d,abc) Actor b is Terminated Actor c is Terminated Actor C receives message Die and exists with the reason “abc”. Actor B linked to C doesn’t trap exits, therefore it terminates too. As long as A connected to B traps exits, when actor B terminates it sends A a message with the reason why it has failed (case class with the following signature): ** An `Exit` message (an instance of this class) is sent to an actor * with `trapExit` set to `true` whenever one of its linked actors * terminates. * * @param from the actor that terminated * @param reason the reason that caused the actor to terminate * case class Exit(from: AbstractActor, reason: AnyRef) In the same time, when exit is expected (not caused with computational exception), linked actors are untouched: scala> start(false, Die('normal)) Actor b is Suspended Actor c is Terminated In the snippet below, unhandled division by zero exception causes C and B to die: scala> start(false, Divide(0)) Actor a received Exit(scala.actors.Actor$$anon$1@113eb9c,UncaughtException (scala.actors.Actor$$anon$1@1a1446d,Some(Divide(0)),Some(scala.act ors.ActorProxy@14f83d1),java.lang.ArithmeticException: by zero)) Actor b is Terminated Actor c is Terminated If we force B to trap exit, the actor stays alive in all the scenarios described above: scala> start(true, Die('abc')) Actor b received Exit(scala.actors.Actor$$anon$1@13e49a8,abc) Actor b is Suspended Actor c is Terminated Compared to the first snippet, now B receives exit message from C. Unhandled errors are also not propagated to A: scala> start(true, Divide(0)) Actor b received Exit(scala.actors.Actor$$anon$1@119f779,UncaughtException (scala.actors.Actor$$anon$1@119f779,Some(Divide(0)),Some(scala.act ors.ActorProxy@14f83d1),java.lang.ArithmeticException: by zero)) Actor b is Suspended Actor c is TerminatedBasic Supervisor Exit message contains the reference to the failed actor that can be used to restart it. It’s possible to implement a very simple actor supervisor, by analogy with supervisor behavior in Erlang. case class ChildSpecification(worker: Worker, restartBehavior: Child RestartBehavior.Value = permanent) case class OneForOne(maxRestarts: Long = 3, maxTime: Long = 3000) extends RestartStrategy case class OneForAll(maxRestarts: Long = 3, maxTime: Long = 3000) extends RestartStrategy class Supervisor(supervisorId: String, strategy: RestartStrategy, childSpecs: List[ChildSpecification]) extends Worker { ... override def act = { self.trapExit = true linkWorkers loop { react{ case Exit(worker: Worker, reason) => println('Worker [%s] has failed due to [%s]' format(worker.id, reason)) if(worker.restartsInPeriod(strategy.maxTime) >= strategy .maxRestarts) exit('Maximum restart intensity for %s is reached!' format(worker.id)) strategy match { case str:OneForOne => restartWorker(worker) case str:OneForAll => childSpecs.foreach{spec => restartWorker(spec.worker)} } case Terminate(reason) => println('Supervisor terminated with reason [%s]' format(reason)) exit(reason) } } } ... } Supervisor itself is a normal Scala Actor that traps messages from the other actors linked to it (workers, in terms of supervision terminology), and restarts either only one failed actor or all supervised actors. When restart frequency reaches the limit specified by the restart strategy, supervisor terminates, so that supervisor at the higher hierarchy position may try to handle the problem. In the simplest scenario, supervisor restarts the actor terminated due to uncaught exception: 'Actor terminated due to uncaught exception is restarted by the supervisor' in { val worker = new SimpleWorker('simple_worker') Supervisor('basic_supervisor', OneForOne(), List(ChildSpecification(worker))).start worker !? (1000, Divide(0)) (worker !? (1000, Divide(1))).asInstanceOf[Option[Int]] must be equalTo Some(1) } Output of the specification is: Starting worker simple_worker Worker [simple_worker] has failed due to [UncaughtException(com.vasilrem.linked. SupervisorSpec$SimpleWorker@fd54ec,Some(Divide(0)),Some(scal a.actors.Channel@16167ab),java.lang.ArithmeticException: by zero)] Restarting worker [simple_worker]... [info] + Actor terminated due to uncaught exception is restarted by the supervisor In a more complex scenario, when supervisors are linked in a tree, high-level supervisor restarts low-level supervisor, when it dies, that causes restart of the workers linked to it: 'High-level supervisor restarts low-level supervisor and the wrokers linked to it' in{ val worker = new SimpleWorker('simple_worker') val lowLevel = Supervisor('lowlevel_supervisor', OneForOne(), List(ChildSpecification(worker))) val highLevel = Supervisor('lowlevel_supervisor', OneForOne(), List(ChildSpecification(lowLevel))).start worker.getState must not be equalTo(State.Terminated) lowLevel ! Terminate('Kill lowlevel') Thread.sleep(1000) worker.getState must not be equalTo(State.Terminated) } Test output is following: Starting worker lowlevel_supervisor Starting worker simple_worker Supervisor terminated with reason [Kill lowlevel] Worker [lowlevel_supervisor] has failed due to [Kill lowlevel] Restarting worker [lowlevel_supervisor]... Starting worker simple_worker [info] + High-level supervisor restart low-level supervisor You can find more specifications and code of the supervisor here. Happy coding and don’t forget to share! Reference: Fault-tolerance primitives in Scala: links and traps from our JCG partner Vasil Remeniuk at the Vasil Remeniuk blog blog....
Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close