Tech Tips Digest: 7

Back on track with a new blog post and this one covers many interesting topics.

If you've noticed, we're now moving in a bit deeper than before  talking about certain language constructs and features rather than high level concepts and principles.

Previous Tips: Tip No. 1 | Tip No. 2 | Tip No. 3 | Tip No. 4 | Tip No. 5 | Tip No. 6

Tech Tip No31: Always use blocks in control statements, even if the language you're using allows you not to do so

All modern programming languages enforce a certain level of structure to the source code, especially when it comes to blocks. Take Python for example, blocks are identified through the proper level of indentation, miss that up and your code will either not run or produce unexpected results!

Building on that note, Java (as my language of choice) uses curly braces in order to group code and enforce blocks. It also allows you to have a while loop with no braces, given it has one statement to execute in it.

I strongly suggest you don't drop those precious braces or what ever your language of choice uses, for the sake of conformity and readability.


Tech Tip No32: Avoid casting to lower precision primitives, unless you know what you're doing. You might lose some data!

In Java, the 'int' primitive type is a 32 bits signed integer and 'short' is a 16 bit signed integer. The language provides you with the means to down cast from integer to short, but imagine a case where you have an integer with a value larger than what a short is capable of handling, when you downcast, you'll most definitely have data loss.

And in that case, and unless your code is built to serve a certain purpose that doesn't really care about this data loss then you should be OK.

Regardless of all that, you should really invest enough time figuring out the data types your objects use in order to avoid those miserable nights struggling to figure out what went wrong in your system.


Tech Tip No33: Flag method parameters with 'final' if you don't intend to modify them inside the method body, for readability

Object oriented languages are built on three main building blocks. Objects with their properties and operations they can perform. Those operations have parameters of course, and those parameters might be passed by value, by reference or both depending on the language you use.

With the previous elaboration in mind, I would like to point out that most developers would tend to manipulate or update some of those parameters in the body of the operation they're being passed into. While this is a feature, I highly recommend against it, since it somehow creates vagueness, unless there's a certain pattern in use here.

Regardless of all that, method parameters that are not supposed to be updated within the operation's body should be flagged as final, enforcing other team members or future developers to not modify them since perhaps they are passed by reference and the invoking client code needs the parameter value to be the way it was passed down.


Tech Tip No34: [Java] Did you know that try/catch blocks are a bit expensive, plan your exception handling smartly

Exception handling requires a special treatment, since it's an exception to the natural flow of the source code. Hence although machines are powerful enough those days, it still adds a slight overhead on you system. Now abusing exception handling or misusing it will have a certain amount of impact on performance.

As a simple suggestion, avoid using try/catch blocks inside of itertaive blocks!


Tech Tip No35: If you use a cross platform lang such as Java make sure you don't tie your code to a certain OS, ex: EOL chars

Cross platform languages are awesome, especially when you really use them to that extent, and those kinds of languages usually provide you with the means to the special paltform specific properties such as End of Line characters and so on.

I would highly suggest you tap into those properties and use them instead of using your own handling logic for that matter as it'll make your life much easier and your system less error buggy when it comes to running on different platforms!


Hope you enjoyed this one, stick around for the upcoming one :)

Comments

Popular posts from this blog

Tech Tips Digest: 2