Just a little scripting

Scripting on Windows

There’s this flame war between Linux and Windows. Like Linux is user-friendly, it’s just more selective with its friends. Another one is, that it’s easy if you don’t mind clicking. Windows 10 contains a bash shell, still, this won’t end just now.

So, there is this flame war, old and still funny. But this post is about something else. I’m trying to explain why I always install a cygwin / mingw on a corporate windows box.

Obviously, I install them because we’re not using Windows 10 yet. Also, I’m installing mingw because git-bash is based on that. Still, this isn’t the real reason.

Why is it useful?

The real reason is that I can solve complex problems quickly. Say for instance, the jd-gui had some problems. It couldn’t decompile some classes and it wouldn’t save all the decompiled sources either. I could’ve downloaded some other versions and played with them. However, I already had mingw and procyon on my machine so I started to write a for loop. In a couple of minutes I had this:

cp -r classes sources # this creates a paralel directory structure
find sources -name '*.class' | xargs rm # this removes the class files

# a for loop through the classes. I could've excluded the inner classes too..
for f in `find classes -name '*.class'`; do
  # creating the name of the destination java file
  j=`echo $f | sed -e 's/^classes/sources/g' -e 's/.class$/.java/g'`; 

  # invoking the decompiler
  java -jar /f/Programs/decompilers/procyon-decompiler-0.5.30.jar $f > $j;
done

The point here isn’t the code. Bash can do loops, branches, and other stuff in a weird way. The point is that I could substitute some gui functionality with some code in a couple of minutes. It took as much as starting up eclipse and creating a new project.

It’s not only useful. Writing code seeing it run instantly – it’s almost magical. Others might prefer the REPL for Lisp, python, powershell or ruby. Still, bash is the most ubiquitous from all above.

Enhancements

So, I wrote this one-time script. There are a few enhancements I could’ve made.

I.e. this is how you can filter out the inner classes from the decompiler loop:

for f in `find classes -name '*.class' | grep -v '\$'`; do
  #...
done

But deleting them isn’t much harder either:

find . -name '*.java' | grep '\$' | xargs rm

I can also mass-edit the decompiled classes:

# a loop for the java sources
for j in `find src/ -name '*.java'`; do
  # retrieving the java class name from the path
  f=`echo $j | sed -e 's/^.*\///g' -e 's/.java$//g'`;

  # creating the command - it will be useful for debugging
  cmd=`echo "sed -i 's/\b$f\.//g' $j"`;
  echo $cmd; # some feedback during the script run
  eval $cmd; # invoking the actual command
done

Other approaches

As you can see, I’m using sed and command substitution extensively. There might be other approaches that I’m unfamiliar with. Still, these tools help me a lot to solve complex problems quickly.

What now? We can follow @climagic. We can learn how to parametrize procyon to do what we want. I could learn awk.

What are your favorite command-line tools? How could you improve these scripts?

 

Posted in programming | Tagged , , , , | Leave a comment

How to deal with criticism

In the previous post we saw how we can help a low performer to work better. In this post we’ll see the flip side of that story. Sometimes you’re at the receiving end of this help. Which is, very bluntly, criticism. You need to be able to deal with that.

There are several types of feedback. I’ll show you here how to deal with them. Continue reading

Posted in programming, soft skills | Tagged | Leave a comment

How to help a teammate up

Software is a crazy industry. Requirements are changing quickly. Technologies are changing even faster. Customers generally cannot specify what they want. No wonder some of us fail to do their job well.

Salaries are high, professionals are valued high, which attracts people with various personality disorders. Programming is some sort of trance, which attracts people with other kind of problems. No wonder we usually cannot help the ones who are left behind. Continue reading

Posted in soft skills | Tagged , , | Leave a comment

How to program by coincidence – five techniques to ruin your programming carrer

No idea what I’m doing

The Pragmatic Programmer book popularized a term: programming by coincidence. According to the book, it means that you write code that you don’t understand. So it works but you don’t know why. Consequently, if when it fails, you don’t know why either.

I used to believe that the only use of this term was to insult your coworkers. Saying “You’re programming by coincidence.” literally means this meme.

I don’t believe that anymore. I discovered a couple of techniques that help smart programmers like yourself to program by coincidence. I’m sharing them with you for $200. Here they are:

1. Copy and paste

This is a classic one. Whenever you find a problem, then google it. If you find a page, copy the code and paste it into your IDE. See if it works.

If you see two lines of code, you will have the urge to look up the idioms in the API docs. Don’t do that. Just add the code and see if it works.

More advanced users can copy code from the current code base. This is very useful because you can copy tens of lines of code. You can keep deleting stuff until it works.

Pro tip: you can paste SQL code to a jdeveloper connected to the production machine.

2. Ignoring stack traces

What do you do when your code fails? You have a stack trace that doesn’t fit into the screen. Ignore it.

Never ever read the stack trace. You can read the first line if absolutely necessary. So you’ll know see whether it’s a NullPointerException or just AnyOtherException.

If it’s a NullPointerException, just add some null checks. Otherwise you can tweak a loop index. Or paste something. Or comment out a method.

Do whatever you have to do to make the exception go away. That something can break a feature – but hey, that’s somebody else’s problem.

Pro tip: never ever learn, how to read an exception.

3. Ignorance is bliss

Don’t learn the frameworks you use.

If you don’t learn your frameworks then you can think out of the box. Say for instance, you can paste JPA queries into your jdeveloper, then complain that this SQL doesn’t work.

You shouldn’t learn your version control systems either. Just find a good UI that helps you to get by. When you really messed up, somebody will help you clear up the mess.

4. Confidence

When you don’t know how a framework works, then you can guess. You can call these guesses assumptions so you’ll sound smart. The trick is that you never ever need to test your assumptions. Just say aloud what you think and program accordingly.

If your assumption fails then blame the framework. It’s difficult to understand, isn’t it?

5. No automated tests

You shouldn’t write unit tests either. Unit tests give you quick and accurate feedback. Unit tests could tell you which method does what.

Pro tip: if you have to write unit tests, then make them long. It can be more complicated than the code its testing.

Disclaimer

Yes, it’s sarcasm. If it hurts then probably you need to revise your practices.

Unless you’re a beginner. It’s OK for a novice programmer to make these mistakes. Just try to get over them in a couple of years.

More antipatterns

Did I miss an antipattern? Please let me know in comments.

Posted in programming | Tagged , | Leave a comment

Preparing too much for a job interview

Everybody agrees that we should prepare for a job interview. We shouldn’t be shocked by questions like “What’s your five greatest weakness?” or “What’s your salary target?”. Refreshing our memories about frameworks that we haven’t used for years – that can be useful too.

On the other hand, if you need to prepare for a month or so – that looks too much for me. Maybe you’re trying to cover too much. Maybe you want to sell yourself as more expert than you actually are. E.g. learning trick questions and answers in a language you hardly used – it’s cheating. Continue reading

Posted in soft skills | Tagged , | 2 Comments

Lessons learned from the IoC framework comparison.

In the previous posts I compared a few IoC frameworks.

It was an interesting journey to write the prototypes, then the posts one by one. I also learned valuable lessons from comments and questions. I’m trying to summarize them below.

XML still has an advantage

XML had a big hype a couple of years ago. Now people mostly hate it. Still, it has something on its side: Using XML for DI config, it’s extremely difficult to mix configuration and code logic.

So you’ll have a declarative configuration, and that’s good. On the minus side, this configuration will be difficult to maintain. I.e. it’s hard to read and it has no compile-time checking.

You can choose this with Spring and Java EE.

Constructor injection is ugly but safe

If you have constructor injection, then you can make sure that your beans are always fully configured. So you have one less thing to test.

In an ideal world you have good unit test coverage anyway. So you can switch to setter or field injection. These big constructors are ugly. They make testing difficult too.

Again, in an ideal world, your beans are well-factored and they don’t have that many dependencies. In an ideal world you have lots of small classes and everybody in your team can deal with it. Pun intended.

You can choose this with Spring and Guice.

Java configuration provides compile-time check.

If you build your beans with java then you’ll have a strong compile check. I.e. the interfaces and the implementations will always match. Your constructors parameters will be in the right order too.

In an ideal world you have good unit tests for this. But we already covered that🙂

On the other hand, this approach requires some discipline. Otherwise you just add a little logic here, a little there, and you have a nice spaghetti configuration. You don’t want that, I suppose.

You can choose this with Spring and Guice.

Annotations have a caveat

It’s really nice to add annotations to your code. It makes development easy because you don’t need to check the separate configuration files during development.

There is one problem though: with framework-specific annotations, like @Autowired, you build dependencies on the framework. You effectively break the Hollywood Principle.

If you’re sure you won’t replace your framework, you’ll be fine. You can enjoy how easy it is to embed metadata into your code. Otherwise you’ll need another approach.

You can choose this with Spring. It’s mandatory with Guice and Tapestry-DI.

Your insights

Did I miss something crucial? Did I bad-mouth your favorite feature? Please let me know in comments.

Posted in programming | Tagged , , , | 2 Comments

Dependency Injection frameworks

If you want to deeply understand a concept, then you need to see several implementations. This is why I created the following slides.

Based on the feedback, I’m kinda alone with this mindset. Anyway, I hope you find them useful.

Posted in java, programming | Tagged , , , , , , | 1 Comment

Practical introduction to dependency injection

In the previous post we checked whether the template method pattern is an alternative to the dependency injection. There we saw the simplest dependency injection solution, without any framework.

Just to recap, this is the fizzbuzz class:

public class FizzBuzzManualInjection {

    private final FizzBuzzNumberProvider numberProvider;
    private final FizzBuzzTextProvider textProvider;
    private final FizzBuzzPrinter printer;

    public FizzBuzzManualInjection(final FizzBuzzNumberProvider numberProvider,
        final FizzBuzzTextProvider textProvider,
        final FizzBuzzPrinter printer) {
        this.numberProvider = numberProvider;
        this.textProvider = textProvider;
        this.printer = printer;
    }

    public void doFizzBuzz() {
        numberProvider.getFizzBuzzNumbers().forEach(num -> {
            final String msg = textProvider.getFizzBuzzText(num);
            printer.printFizzBuzz(msg);
        });
    }

}

And this is how we can configure it:

public class Main {

    public static void main(final String[] args) {
        final FizzBuzzNumberProvider numberProvider = new FizzBuzzNumberProvider();
        final FizzBuzzTextProvider textProvider = new FizzBuzzTextProvider();
        final FizzBuzzPrinter printer = new FizzBuzzPrinter();
        final FizzBuzzManualInjection fizzBuzz = new FizzBuzzManualInjection(numberProvider, textProvider, printer);
        fizzBuzz.doFizzBuzz();
    }

}

Motivation

Consider this graph:

One object creates another

One object creates another

Node a represents a object that creates two other objects, b and c. They create other objects to the bottom of the graph.

Suppose you want to inject a new collaborator to node j. Well, then you have to go through the path a-c-g-j and modify all the constructors.

Is this something you’re likely to do when the deadlines are close?

Then consider a flat tree hierarchy, like this one:

flat-creation

There is a main node that instantiates a framework. The, the framework creates every other object. If they need references to each other, then framework will inject them.

So you want to add something to node j? Well, all you need to do is to change the configuration of your framework, tweak node j a little, and you’re done.

Again:
Is this something you’re likely to do when the deadlines are close? I guess not. So let’s take a closer look at a few frameworks. Continue reading

Posted in Uncategorized | 3 Comments

Template Method or Dependency Injection?

Mystical

Dependency Injection used was always something very mystical to me. Until I learned what it’s really about.

I’d like to share this revelation with you. So let’s take an easy problem and solve it in couple of different ways.

Continue reading

Posted in java, programming | Tagged , , , | 1 Comment

Learning Tapestry, the Inversion of Control Framework

The framework

I recently started to use Apache Tapestry for work.

Tapestry is a web component framework with ambitious goals. E.g. it’s a web component framework that’s easy to extend. Also, it gives you the ease of development of scripting languages within the java ecosystem. Moreover, it provides an Inversion of Control (IoC) container with nice aspect-oriented features too.

This is my learning log: I completed the official tutorial. Then completed an in-house tutorial that shows how tapestry and the other moving parts fit together in our application. Finally, I read the Tapestry 5 book.

Earlier I wrote about its component and scripting parts. Here I’ll dissect its Dependency Injection parts

Continue reading

Posted in java, programming, web | Tagged , , , | 4 Comments