8 git commands I use everyday

If you are already using git, then you can skip this paragraph. If you are fist hearing this word, then I can give you the basics for now. Git is the most used versioning control system. It basically is a way you can track all the versions of the program without having to get creative with folder names (version1, version1final, version1finalfinal etc.). Git also provides a way for multiple people to collaborate on the same project without stepping on each other’s toes (most of the time). You can get started with this tutorial. If you don’t know git you should start learning it now. I can’t stress enough how important and widely used it is.

If you already know about git, then you should have realized by now that keeping the git history clean is really helpful. It makes it easier to see what is merged and what needs work. You see when the changes were made and you can search through them for something that you want. It worth the effort to keep the history nice and organized for everyone. Without further ado, there are 8 git commands I use every day

git log

This is a nice and easy one. It shows all the history of the project, commit hashes, commit times and commit messages. You can search through the history by typing “/” if you want to look for something in particular.

git checkout -b branch_name

This creates a new branch of my current branch and checks the new branch out. Nice and easy way to get the task going.

git diff --cached

Once you add the changes you want for your task, you can review the staged changes to make sure everything is as expected. This command, together with git status, should save you from any surprised when doing a commit.

git commit -m "first message" -m "Second message"

The first message is a short description of what the changes are in this commit. I provide more details on the changes on the second message, which usually spans across multiple lines.

git rebase -i hash

Generally, when I work on a task I end up with a lot of commits on my branch. You know the ones: attempt at something, first iteration on something else, some minor change, oh now there is a typo. I like to have my tickets in a single commit when merging. This way, in the unlikely event, that I need to revert a ticket I can do that with just one commit.

git rebase master

I rebase a lot. Anytime I want to merge something I do a rebase on master first. This way you neatly stack all the commits for a ticket together. Makes following the git history much easier and reverting a change is easy peasy lemon squeezy.

git merge branch --no-ff

I very much prefer merging without fast forward. You get a nice commit message to celebrate and an easy to find point in the past to checkout if the need arises.

git push --delete origin/branch

Now it’s time to remove your work in progress and start anew. Great job! I prefer the more verbose way of deleting a branch. It’s harder to mess it up.

Let me know what other git commands you use everyday ?

Async programming models part II

As promised, this is the second part of my pocket-book introduction to async programming. Let’s continue!

If this article does not make sense, please check part I before shouting at me!

Map-reduce models

The map reduce approach is widely used in doing big data analysis. The model itself it’s a variation on the split-apply-combine approach, which is pretty much self-explanatory. You split the data, you send to multiple threads to tinker with it, you combine the resulting data and you get the result.

What are they good for

Map-reduce models are good at doing the same operations on large sets of data. You need to analyze numerous files, do it with a map-reduce model. Any kind of data which you can safely split into chunks, you can process with map-reduce.

Problems with map-reduce

With this model, you don’t have that much control over the threads. You just tell the thread what to do, and, generally speaking, it’s the framework’s job to split the data. Additionally, all of your threads are applying the same operation to the data set.

Map-reduce is not that good at handling interactions from the outside. If you need to handle user input, maybe this is not the way to go.

Map reduce usually implements the fork-join model. I.e. before the data is processed, you need to wait for all the threads need to finish, so if you have long tasks, then you’re going to keep lots of resources busy. Moreover, if you have network calls inside the map thread and one (or more) of those requests hang, you’re going to be in some trouble.

Where can I find this

Java parallel streams

Scala parallel collections

C++ OpemMP (albeit OpenMP offers more options than just map-reduce)

Promises

Oh boy! Promises are my new favorite toys for a while now. With this model you need to define a chain of actions that need to be performed on the data. Each action generates data that will be used by the next action and so on. After you do that, each action will be completed eventually (presumably on a thread that is not doing anything important), then the data is going to be passed to the next action and the cycle repeats.

What are they good for

Promises are a good way to parallelize a wide variety of independent flows. You have precise control over the actions in the flow and the order in which they are executed (inside the same flow at least). They can be returned from functions and that helps you keep your code cleaner and organized. Even more, you can bundle them together into bigger and more complex promises. You can pass any data to each of the actions in the flow. However, at the same time, you can do type checking on the data.

To me, the promises model is the most flexible model out there.

Problems with promises

They are hard to keep. Well, maybe not, it depends! However, the promises model is a bit hard to get the hang of.

Even though actions in a flow are executed din sequence, you have no control on the order of separate flows. This can be a problem.

Before you can use the data from the promise in the main thread you need to wait for the promises to complete. However, if you do lots of waits, (and in the wrong order) you may in fact hurt the overall performance.

Where can I find this

Javascript promise

Scala futures

Java futures

 

Async programming models part I

Asynchronous programming is everywhere around us and it’s here to stay. You might have noticed that CPU’s keep getting more cores as time goes by. Therefore, it would be a shame to let all those cores go unused in your app. In this article I shall be your guide through the various async programming patterns that I have used.

Plain old threads

These are the most powerful tools at your disposal. A kind of all-purpose swiss army knife that you can do everything with. The basic pattern is that you have a function that is going to run on a separate thread. You pass in any data that the thread needs and you are good to go.  Every pattern we will cover here can be reduced to this model (because this is what computers understand).

What are they good for

Plain threads are good for long, custom tasks that you need to run in the background. Do you want to build a server that handles multiple requests at the same time, then this is the way to go.

The problem with plain old threads

Albeit they are very powerful tools, threads can be a bit hard to use. You need to make sure you are passing the right data. Then you need to make sure you are getting the right data from the thread. And you need to all of this while make sure you are not causing any synchronization issues. That is not as easy as it seems. All the patterns share the synchronization issues.

Additionally, you need to decide on the number of threads to use and this is not a trivial task.

Where can I find this

All programming languages worth a damn provide some sort of API you can use for this

Queue of events

In this async programming model you begin with a bunch of threads that idly wait for something to happen. You also have a queue of events that keep track of what happens. When something happens, one of the threads picks up the work(usually the first idle thread). Then it does the work. Then it goes back to twiddling its thumbs until something new happens.

What are they good for

You should use this pattern when you need to respond to something happening in the world. These events can be HTTP requests if you’re building a server, user actions or other threads work.

Threads can be added or removed from the pool as needs demand. Most of the times this can be done automatically, so this is one less thing you need to worry about.

In simpler terms: If your app can be described as When this happens, then this should happen!, you can use this pattern.

Problems with a queue of events

You have significantly less control on the threads than you would normally do. Additionally, you need to add all the info required by the thread in the event data. From time to time, this can be tricky.

The queue itself can be a problem. Events can keep piling up during busy times or if there is a deadlock with the threads. If the queue grows too big events can either be dropped (causing loss of data) or they can block the system (causing much worse damage)

Where can I find this

This is all for today. Map-reduce and promises coming soon. Are there any async patterns you want me to cover?

Be a more productive coder – Some metrics you should use

What does make a good coder ? Is it the number of lines of code they write in a day ? How about the number of works they put in the office? Maybe it’s how fast they code, how quickly they solve any given issue? Well, as you might have guessed by now, I don’t fully agree with any of these statements. Here are some metrics that better reflect your proficiency as a  code writer.

Is your code easy to understand

You should always write readable code. I have an article here if you need a more in-depth opinion on what good code means. Being a productive programmer it’s not just about you. Your part of a team, a team of programmers who need to spend time understanding, maintaining and changing your code. If you have to spend a few more of your minutes to spare a few more minutes of some of your teammate’s time, then you have made the whole team more productive. Neat, isn’t it?

Forget the clever hacks and neat tricks you are tempted to use. I know, it’s hard to give up on a clever little idea you have. It happens to all of us, all the time. When it makes the code hard to read, it’s probably not worth it.

Is your code correct

This should be pretty obvious: Don’t write bugs. What it’s not obvious, however, it’s how costly bugs actually are. Tiny, little bugs cause big overheads. The QA need to file a bug report, it needs to be prioritized, scheduled into the current development cycle (call it sprint, or whatever you are using). Someone needs to pause their assigned work and start fixing it. This means they need to get up to speed with the area of the code the bug lives in, write the fix, review, and the QA needs to test it again. When you think about it this way, adding a few more automated tests and spending a couple of minutes manually testing your code doesn’t seem that bad. Am I right, or am I right?

Is your code flexible

To know if you are doing this, just remember what were your feelings the last time you were asked to change something you wrote. If your first though was: Noooo! Please don’t make me do that!!! then you should pay more attention to this. Keep in mind that requirements change all the time, and that the code is something that is constantly evolving. Therefore, you should write the code on the assumption that you will need to change it at some point, because it’s a safe bet you will. If you spend your time wisely now, you will save a lot more time later. Flexible code is an investment in your future productivity.

An important disclaimer here. Don’t overdo this. It’s a fine line between keeping your code flexible and spending waaay too much time preparing for a change that may never happen. If you are unsure, just ask someone. The PM/tech-lead/client should know how likely a change is, hence they should be able to give you the answer if it’s worth doing it. Design patterns are a good way to ensure you’re not overthinking the issue. Use them!

Summary

In the end, it’s not about how many lines of code you can write, neither about how fast you go through this current development cycle. What makes a good coder is how good they prepare their code for the next issue, and the next and so on. Therefore, in the long run, you are way more productive if you spend some time today to make your work easier tomorrow.

Do you know any other useful metrics that a programmer should use?

How to meet your deadlines – A quick guide to ensure you are never late

Oh, the dreaded deadline! You have a week’s worth of work to do by tomorrow. If that has ever happened to you, then you should know that feeling well. In the following lines I will try to give a few tips that should alleviate that feeling. Let’s begin!

Don’t underestimate the work

This is something that needs to be done in the planning stage. Estimating the amount of time a piece of work is going to take is as much an art as it is a science. It takes a great deal of experience to do it right. In most cases things are going to take longer than expected. A process that works really well for me is breaking down the work into the smallest piece of self-contained work (call them tasks, tickets, whatever you want, the concept remains the same). It is much easier accurately estimate a small piece of work, than a big one. When you have a list of small and self-contained tasks you can begin to add estimate them. At the end, add the values together and you have a deadline.

Give yourself some slack!

Estimating a task involves accounting for the unexpected. Unexpected events have an unbalanced effect on the deadlines. This is a fancy way of saying that bad things happen much more often than good things. Things will break, and they will need to be fixed, you will have to wait for other teams to be done with their work, specs and assets will be late, computers do break from time to time and that will certainly affect your deadlines. The more dependencies you have on your task, the more slack you need. This may sound extreme, but I usually add a 30% slack on the dev time and even that proves to be insufficient from time to time.

Be honest with the stakeholders

Do not commit to more work than you are able to do! Remember, in the long run, doing things right is much faster than doing things fast. If you need to rush through work, you will unavoidably make mistakes that tend to add up over time and slow down future development time. You have probably heard this before, but I’m going to say it anyway: Do quality work from the start, even if it takes slightly longer now, it will save you tons of time later down the road.

Track progress as you go along

It is much easier to make corrections to your course as you are traveling towards your destination. How does this translate into a helpful tip? Glad you’ve asked! To sum up: It’s easier to correct for small delays than large ones. That sounds reasonable enough, right? The trick here is to spot delays as early as possible. If you have a timeline for your project, you should be able to say at any moment if you are on track. When the work slips, it’s going to be easier to account for a small delay. By doing this regularly, it should never come as a surprise to have to do a month worth of work withing a week.

That’s all we have for today! Hope you have enjoyed it! See you next week!

How to write better code – 6 things I did not found on the first page of google search results

I know there are millions of answers to this question (I for one get 484,000,000 hits on google when I search for How to write better code). They all seem to revolve around the same general idea, so I will not be repeating that. You are free to do a google search yourself and read through those.

I will however, be sharing some tips that I picked up that you may find useful. Here we go:

1. Do not compromise

We all know when we are doing a hack, or a nasty work-around because the right way takes too long. We know it, and we still do it. Maybe it’s late in the day, and we just want to go home, maybe the deadline is closing fast and someone is going to be really mad if the project is not done on time or maybe we just can’t be bothered to do things the right way. My advice to you here is, you guessed it, do not compromise code quality. Compromises made now will slow down development in the future, which will in turn lead to more compromises and so on and you will end with a mess of a project that you will need to maintain. And that’s no fun 🙁

2. Less is more

A programmer’s proficiency is not measured in the number of lines of code they write, it’s more like the opposite. The more code you write, the more chances to break said code. The odds are against you. If you have to think twice as long on task to write half the code, do it, it totally worth it.

3. DRY

Don’t repeat yourself! I’ll say it again: Don’t repeat yourself! (pun intended). In my experience, the worst codebases I had to deal with are the ones which chose to implement the same logic in many (and often quite creative) different ways. Don’t do that to the person maintaining your code. If you have to use the same logic in two different places, be sure to have the code in one place.

4. Follow the existing pattern

This is an extension of the previous topic. Please try to avoid adding a new pattern to the project, unless it’s really necessary. One style of doing things should be enough for one project. I accept that this may be hard. Especially if you are just starting on a new project that seems to be doing things differently from what you are used to, but don’t give into the temptation. Keep the project simple and follow the existing pattern, even if it means more work for you now. And remember: It will get easier with time.

A notable exception to this rule is the process of replacing the existing pattern with new and improved one. If you are doing that you can have two patterns in the project at the same time, while the old one is slowly retired.

5. Automated tests

I really don’t see nearly as much as these that I would like to. If, at any point in the development process, the thought that you need more tests crosses your mind, you are probably right. As you might have guessed if you follow my blog, I’m a big fan of automated tests (maybe enough to write an article about them some day). Automated tests are great, and they have many advantages to them. They give you the confidence that what you just changed did not break any hidden functionality. They serve as an always up to date documentation for the project (as long as they are passing).

6. Common sense must prevail

We need to take a step back now and acknowledge that each and every one of the points above can be taken to extremes. As you might expect, taking internet advice to extremes is not advisable. So I leave you with this: Whatever you do, don’t forget to use your basic, human, common sense. You are free to bend and adapt the rules so that they make sense given the situation.

Keep meetings short

We have all suffered through meetings. You know that meeting that seems to just go on and on and on without an end in sight. I you hate them as much as me, then this article might just be for you. In the following lines I will share some tips and tricks that helped me organize and partake in pleasant, useful meetings.

Meetings are a necessity

As the size of the team grows and there are multiple parties involved in bringing a project to an end, the overhead associated with coordinating these multiple armies of people become significant. Meetings are just one of the tools that helps to keep people focused and on the right track. Meetings are also a distraction. They take people away from their daily work, it breaks rhythm and whoever took part in the meeting needs a bit of time to get back in the zone, and be a productive little bee again. Given the high cost that meetings have, it’s worth doing them right so you can get the most value out of them.

Establish an agenda and stay on it!

Meetings should have a purpose! They should have a clear and precise purpose. If you are just sharing information, as in there is nothing to decide, you just want to let your team know something, that’s not a meeting, that’s a presentation and it’s a whole different story on what makes presentations useful. Meetings should be about deciding things. They are the much nicer alternative to huge email chain.

Getting back to the point. Before you start a meeting, you should know what needs to be decided. This way, if the discussion goes of track you can quickly spot it and steer it back on point and shave some minutes off there.

Let participants know what the agenda is.

The meeting will go a lot smoother if participants know what they are getting themselves into. They can do a bit of research beforehand, in the safety of their own desks. The more prepared participants are, the less time they need to learn what they need to know to reach a decision, the faster the meeting ends. Simple, right ? If you are a participant to meeting, feel free to ask what the meeting is about and get all the relevant information you need to prepare.

Get the right people in the room

This one is more for the person organizing the meeting. Everyone in the room should be there with a purpose: they either have the knowledge to answer a question, or the power to decide on a course of action. Everyone else can be briefed via email after the meeting ends. Getting only the relevant people in the room saves a lot of the debate that arise from either explaining a decision to someone without sufficient knowledge on a particular subject, or dealing with opinions from people that ultimately have no saying in the project. Remember less (people) is more (useful meetings)!

Take things outside (of the meeting room)

I’m definitely not suggesting fighting when you don’t agree in meeting. Keep things civil, we are professionals after all! This bit is more about the kind of discussions that are still in the scope of the meeting, but are at a high level of detail, so that only a couple of people care about it. If most people are not getting anything from what is being discussed, then you don’t have the right people in the room and you are wasting their time. When people get into such an argument the right thing to do is to call time out and suggest they (and only they) continue this chat in private and let you all know how it went.

That’s it! Try to do that so you get to enjoy the meeting you take part of, and, on top of that, feel like you’re actually doing something useful with your time while you are at it. Cheers!

 

 

Dealing with legacy code – the bane of our existence

Unless you are really, and I mean really, lucky, you are bound to be stuck on a legacy project with a thousand-year-old codebase. Maybe not a thousand years, but close enough. If it’s any like the code I have been working on, it’s big, really big, it’s complicated, no one really understands how it fully works, and it’s riddled with anti-patterns. What to do?

Burn it to the ground and start fresh

So, delete the codebase and re-write everything in a nicer language, with design patterns and all the bells and whistles.
Phew, that was easy, we didn’t we do that from the beginning?! Problem solved! Let’s get back to checking Reddit now!

Getting back to earth

OK, so burning everything might not be a sensible solution. More often than not the code took years upon years to write and is worth a small fortune. The client is not just going to let you throw that all away and sit tight while you are starting again from scratch. You’re in a bit of a pickle, you can’t live with the code and you cannot get rid of it. What to do ?!

The only sensible solution at this point is to slowly improve the codebase. It’s a slow and time-consuming process, so don’t expect your life to improve overnight. We now have a plan, let’s see how we go about putting it into action ?

Test it, test it, then test it some more

You will need to improve the code in the future, however, it’s really hard to do that when you’re unsure if you have broken it. This is where tests come in. Unit tests, integration tests, performance tests, you name it, you probably need it. The more tests you have, the more confidence you have that the changes you made are not breaking anything. In addition to that, writing tests help you better understand what the code is doing. Hooray for you!

Isolate and control access to the old world

The plan here is to control access to the old code as tightly as possible. You can do that with nicely designed interfaces and wrapper classes. Here where the tests we previously talked about come into place. Once you know you’re not breaking functionality, then you are in a position to do some serious refactoring. Every new functionality you add should use one of the nice interfaces. If there are none, add them! If it suits your project, you can turn the old world into a web interface that can be safely isolated from the shiny new code you are adding.

Slowly retire the old world

Since the legacy code is now contained and you can control access to that dusty old thing, as time goes by you can gradually remove the calls to it. Once there are no more calls, that is when you are ready to remove that chunk of code. Do that over time and the big monolith you begin with is starting to look smaller and smaller. You may not be able to fully retire the whole legacy piece of code, but you can at least save your team the trouble of always having to work with it.

Slow and steady wins the race

I must warn you, this is NOT a quick process. Depending the size of the legacy code it may very well take years to make a dent on the original codebase. You need to exercise patience. The motto for the whole process should be Leave the code better than you have found it. You may be tempted, especially when the deadlines are tight, to cut corners and go for the easy, messy solution. Do not do that!! I’m saying it again: Do. Not. Do. That. This is how we got that big mess we started with. You cannot solve a problem by doing the same thing that caused the damn thing in the first place!

 

A super short crash-course in negotiation

I’m sure that at some point in your career you will be in a situation where you need to negotiate. It can be a negotiation with your boss about that big promotion you were hoping for, with a client about the shiny new project you want or maybe with a colleague about the temperature of the AC. Well, whatever that situation might be, it’s better to go in prepared. Here are a few tips and useful advice you can use.

Know what you are negotiating for

The first thing you need to know when you are about to start a negotiation is what do you want. Sounds simple enough, however it’s an important part so DO NOT SKIP OVER THIS! You need to have a clear idea on what you want from the negotiation. Make a plan and stick to it. This is the ruler you evaluate the negotiation on. It’s pretty much the one thing you should not compromise on.

BATNA

Again on the preparation side of things: Batna stands for Best Alternative a Negotiated Agreement (some background on the term here).  In short, BATNA is your backup plan in case the negotiations break down. It’s the worst case scenario, if you are unable to reach an agreement. You need to have a plan on what to do if things go south, what will it cost you and how you plan to deal with it. You’ll use that in your negotiation to gauge the value of the agreed solution, and quickly dismiss solutions that are worse than your BATNA so you don’t waste time on them. Also it should help you keep calm if you know you’re covered (sort of speak) if things don’t work out. The better you BATNA is, the better the position you are negotiating from is, so pay attention to this!

Establish rules

Negotiations should be fair (or at least you should try to make them fair). Fairness is a relative term and people don’t often agree on what it means exactly. Before you start negotiating, you should first agree with the other party what fairness means. As a professional (and I assume you are one) you should always negotiate fairly (this means do not try to cheat, it’s bad form). If you cannot reach an agreement on what fair means, you can just defer that judgement to someone else. Basically ask a friend you all trust if he thinks you’re being fair. It’s not that hard is it ?

Lateral thinking

OK. So much about preparations! It’s time to start negotiating! Yay! Lots of people tend to think about negotiation as a zero-sum game, if one party wins, the other certainly loses. Spoiler alert: It’s not!  You should keep in mind the reason you are negotiating (see first part), but other than that, everything goes. Most often than not your main focus it’s not the other party’s main focus so it’s entirely possible for both of you to get what you want. Try to find things to sweeten the deal. Things that are cheap for you to offer, but have a big impact for the other party. For example, to make a project more appealing for a customer you can offer training sessions for the users, or provide some form of documentation. You get the idea!

Well, this is pretty much all I had to share about negotiations. Hope it helped! Cheers!

The tells of good feedback

So, what makes a good feedback ? I know there are a lot of articles out there about this, but that doesn’t stop me from giving my two cents, so bear with me.
Weather you’re giving feedback or receiving feedback, I find there are some attributes that make some feedback’s better than others.

Actionable

The purpose of feedback is to help someone improve. If the person receiving the feedback has no idea what they can do to improve themselves, then the feedback is like a bald hedgehog (pointless). If you want meaningful feedback, then it should refer to specific actions the person can take to improve themselves, which leads me to the second point:

Concrete

You’ve all hear it: You are doing perfect, keep it up! (followed by a warm pat on the back). This is useless! First of all: I have no questions that you are an overall great person, but, whoever you are, there is no chance that you cannot improve! And second (and this is more to the point): What am I supposed to to with that information ?! Where do I go from here ?! Certainly there are some things that I am doing better than others and I would like to keep doing more of those. Feedback should focus on concrete facts. Even if you are a superstar and you are good at everything you do, there must be some aspects of your work you could be great at, so try to focus on those.

Short

This one may sound a bit off at first, but feedback should be short. You do not want a laundry list of items on your feedback. We are not good at multitasking (regardless of what you might think), so focusing on a lot of things is just not going to work. For feedback to be truly useful you should find a few points that you can improve upon. I would recommend 3 or 4 key points so you can focus on those. If you tend to get a lot of items on your feedback list, it’s probably a sign you should have feedback chats more often.

Measurable

This one is sort-of dependent on the type of feedback you are getting, so it might not apply in some cases. It feels good to know you are making progress, and it’s useful to know when you are not, so why not have that information available for you to judge. This is easy if the feedback is accompanied by some kind of metric.

And now for the fun bit! Here are some examples of feedback that can be improved upon (granted there are a bit extreme examples, but I trust you’ll get the point)

  1. “I liked your presentation! It was wonderful! You should do more presentations like that! ” versus “I liked your presentation! It was the graphics that I liked the most! They really help get the point accros! Keep doing those!” 
  2. “That meeting went bad! The clients are not happy with us! We should change that on the next meeting!” versus “The meeting went bad! We went in there unprepared and we were not able to address their questions! On the next meeting we should make sure we fully understand the requirements before the next meeting.”
  3. You have been missing your deadlines lately. You should focus more on being on time.” versus “You have been missing your deadlines lately! Can you think of anything that takes time necessary so you can remove those ? “