Tech Tips Digest: 2


Hope you liked the first Tech Tips Digest yesterday.

I'll be covering every five Tech Tips in each blog post.. And since I'm way behind, today I'll be posting the second part.. Perhaps tomorrow if I had enough time I just might post the third part as well :)

Previous Tips: Tip No. 1

Tech Tip No6: Don't forget to release resources such as Files, Sockets, etc... Sharing is caring, other services might want them

I remember working on a memory leak issue, and I remember coming across many parts of the systems that needed repair. That was a very exciting task for me, scanning through the code others wrote and trying to find CPU or memory abusive parts.

I had eventually managed to find the main source of the memory leak. It was simply the fact that connections to a middle-ware queue system were never disconnected!

Closing connections to files, sockets, database, queues ... etc (The list can go on!), is one of the most important aspects engineers should pay attention to, in order to avoid data corruption and memory exhaustion.

Engineers working with C or C++ know this for sure, others working with Java might think that garbage collection facilities in the runtime environment provide a fool proof measure that they neglect such important actions.

Java Sample suggestion a possible approach to proper handling of files
PrintWriter writer = null;

try {
    writer = new PrintWriter(new FileWriter("/tmp/file.txt"));
    writer.println("Data");
} catch (IOException e) {
    // Log the exception
} finally {
    if (writer != null) {
        writer.flush();
        writer.close();
    }
}


Tech Tip No7: Haven't you heard the news, they've already invented the wheel! Re-use and create genuine modules

We as engineers have great egos and pride in our work. We think we are the best at what we do. Well it's true most of the time or not I'm not in a position to judge :-p

But this gets us to the point where we sometimes come across certain parts of some source code, perhaps also modules, or APIs and think to ourselves.. "Heck, I can do a better job!"

Perhaps it's true, perhaps it's not, but why reinvent the wheel, I mean if there's a well known highly regarded API for say handling complex Date operations, why should we attempt and create our own library for that same purpose.

I say that is simply a waste of time (Not pure waste time, as you might be learning many things along the way) but why re-invent the wheel when you can simply use that well known API and build even more interesting stuff!


Tech Tip No8: Read and understand enough design patterns, they'll help you solve many basic and advanced problems

Design patters, are simple solutions to very common problems in software design. They're always categorized based on the class of problems they solve.

One of the most common and most abused is the Singleton design pattern..

Here's a link that should get you on the right path: Design Patterns (Wikipedia).


Tech Tip No9: Always do proper unit testing. If you're using Java like I do, you may want to consider JUnit it's very easy to use

Have you ever heard of the Extreme Programming paradigm or of Agile development in general. It has many great beneficial aspects, and among them is the focus on Unit Testing. It's at the heart of the source code. you start off by writing the test for the class you are implementing.

You might find this to be a bit weird, but I encourage you to read more about it. it's really interesting.

Source code unit testing is simply writing code to test your code in an organized manner so that it can be carried out over and over again seamlessly. It helps you pin point issues after adding certain changes to your source code and increase your confidence in the whole system stability.

JUnit is one of the most popular Unit testing APIs for Java, there's a ported version of it for all of the .NET developers out there :)


Tech Tip No10: Always use the proper operators in your code, for instance and unless you mean it, don't compare Strings with '=='

I'll end up today's digest with this simple tip. Have you ever come across some code that really didn't make sense!

For instance, the '==' equality operator in Java simply compares references and not the content of the objects, so it would yield wrong results when used on String objects for instance.

Also, why use eager boolean operators when most of the time you only need the short circuit version. that's much faster.


Finally, I encourage you to read more and explore the source code of others. In my own humble opinion, that is one of the most important sources of knowledge you could ever get.

There is plenty of brilliant open source projects out there with very brilliant engineers working on them, download the source code and read on.

Comments

Popular posts from this blog