Archive for February 2008

Code Quality

One kind of cancer in software development is undoubtedly the lack of code quality. I’ve seen a lot of simple and complex applications suffering with bizarre algorithms, crazy logic flows, anti-patterns, ugly & unreadable code, and something else I cannot remember right now :)

I believe that code quality must be one of top things a developer must bear in mind. Quality means a lot of different things, such as :

  • your code works perfectly.
  • it does exactly what it is supposed to do
  • it’s testable
  • it’s readable
  • it’s maintainable
  • it’s extensible
  • it has a nice design
  • and more…

Every software project started on earth is to solve a customer problem, and it implies deadlines, pressure, new requirements, more pressure again and so on. But, we can’t let things like business complexity or pressure to affect our code quality, because every little hack left behind will become a dragon in the future, ready to eat us when we are out of the blue. :)

To achieve quality, a software must be simple, that means we should do things as simple as possible, for the sake of these things above. But, unfortunately simple does not mean “easy”. I can show an example of “simple” and “easy” :

I can state a simple development, as a software that is well designed, that has passed through some analysis, that has a good architecture, where the developers use unit tests and so on. It may forces you to spend a little more time to do these things, but the ROI will be that your life will be very simple whenever you try to extend or change your software.

On the other hand, an “easy” way would be to put every code inside the JSP file, or create a strong coupling among your classes and components, don’t respect any design pattern, and all of this for the sake of a “false agility”. But in this case this developer will have a very hard life whenever he or she try to change the software.

So, let’s have a very nice code with high quality !

Mentoring and coaching

Mentoring and Coaching are two indispensable duties that must be part of the regular life of any professional. It’s very nice when we achieve the next degree, when we get a promotion, a salary raise or any other kind of move towards our career. So, it’s nice as well to help people on that !

That’s why mentoring and coaching are so important, because doing so we can help people to achieve the next level in their profession, and in their lives. It’s very important that we share our experiences to the others, because it will give us the opportunity to grow up together.

I’m saying these things because I have this experience on my own life. I had some good mentors and coaches, and they made me grow a lot, and I remember them almost everyday. Maybe, if I hadn’t met them, I probably wouldn’t be able to achieve what I did.

So, the tip I wanna leave here today is that : DON’T retain your knowledge, SHARE IT !!! We should always bear in mind that we must be ready to coach and mentor people whenever is possible, and whenever they need, trying to push them to the next level, and of course be ready to be someone’s else padawan as well ;)

The truth is that, we receive on the same rate as we give. So, giving more knowledge we’ll receive more knowledge, pushing and helping people to their success we’ll be pushed to our success as well.

Will a persistence framework always be enough ?

Indeed, persistence is always a nice topic to discuss, and I’d like to bring here more one round about this so nice subject.

In the early Java days, we used to create our persistence layer programatically, we simply used an Active Record design pattern ( or something closer to it) to make our classes “persistable”. Some time later, we introduced the DAO design pattern inside our applications, creating an abstraction to separate our “dirty work” to access any data resource, in this case, most common a database connection, and CRUD operations.

Nowadays, we’ve seen the advent of Object-Relational mapping tools, such as Hibernate, TopLink, Kodo, among many others. Nobody can deny that these tools are becoming better and richier every day, adding new features, development plugins, database support and so on. The best advantage of using them is the bridge they make between our application to the database, solving or trying to solve the well known object-relational impedance mismatch problem. Besides that they have some mechanisms that facilitate our lives as developers. I can point out Hibernate features :

  • Transparent Persistence
  • Flexible Mapping
  • Query Facilities
  • Metadata Facilities, among others.

You can know more looking into Hibernate’s website.

In opposition what people tend to believe, an object-relational mapping tool will be enough when we won’t need some in depth database specific capability. But, sometimes we need more. Depending on what kind of system we are dealing with, we’ll need care more about performance, we may have some legacy database to deal with, there might have some company policy that forces us to access data through stored procedures, or issue some special SQL statement, a PL/SQL block, a Transact-SQL block, or something related.

In these cases, I’d like to stand against the myth about portability between databases !

As I told before, there are people who believe that a persistence framework will shield their applications about every single database system, or evey every single different problem found in an Enterprise Software. That’s indeed a really wrong ideia about enterprise software.

Instead, I do prefer to give an appropriate solution for each case. The the role of any software developer, is to choose the most suitable solution for a given problem. We should not limit everything to a single solution, doing so, we might be making things more difficult to solve than it should be.

It’s not a sin to use a separate DAO and make JDBC calls, and it WON’T prevent you to migrate your application to another database. Of course a dose of common sense, analysis and good design are always welcomed. There are design patterns and guidelines to help us in how to develop a better data access layer using both a persistence framework and plain sql/jdbc access, and the effort to migrate your application won’t be a big deal, as long as you didn’t build your entire software inside your database.

I consider two nice actions dealing with this situation :

  1. Keep any specific sql inside your database ( as a stored procedure ). Doing so,you’ll have a centralized repository, you can have your sql tested and validated for free, instead of keeping strings inside your application, that can change and break your functionality.
  2. Issue any batch operation to your database, because in most cases it’s is easier and cheaper to call a procedure/function to query and calculate something ( that is already there in the database), rather than try to load this data on a distributed environment, passing through a network, and summarize your information in memory.

So, my answer is NO, there are situations where a persistence framework won’t be enough to solve your problems. You should bear in mind that there is more than one way to solve a problem, and keep your view as pragmatic as possible, in order to don’t limit your solution to a single vendor / product / architecture / etc.

In this first post, I provided some ground about this discussion, there are a lot more to discuss about this subject, and I will be posting more about this sooner. ;)

Why Ruby and Rails are so delightful

Since the first time I typed the first lines of code, Ruby and Rails made me feel good and comfortable. I fell comfortable about the language itself, the syntax, and the amazing simplicity, that I want to show here ;)

Starting with Ruby, I can say that it is a very nice, clear and concise language. Of course I had some hard time, because it’s a dynamic language, that has a different philosophy rather than Java and C#, that by the way are strongly typed languages. They It made me remember my old friend clipper :)

I’d like to show below some nice thing I like in this language, such as a kind of “for”, or manipulating arrays :

10.times do print “Hello, world!” end

resultado : Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!
Hello, world!Hello, world!Hello, world!Hello, world!

x = []
x << “Word”
x << “Play”
x << “Fun”

As I told you before, Ruby is a dynamic language, so, there is no need to assign a type, but if we had to do the same thing in Java we would need it :

String[] x = new String[3];
x[0] = “Word”;
x[1] = “Play”;
x[2] = “Fun”;

Hmmm ok, somebody can ask me “but, what is the difference ?? they look similar !”. To answer this question I can say that they really seems to share the same idea, but it’s wrong. As I told before Ruby is a dynamic language, so, I can rest without having to worry about pre-defined types. Another good point about Ruby is that it provides me some “utility” methods that other languages don’t :

x = ["Word", "Play", "Fun"]
puts x.join(‘, ‘)
resultado : Word, Play, Fun

x = [1, 2, 3, 4, 5]
y = [1, 2, 3]
z = x – y
puts z.inspect
result : [4, 5]

The last thing I want to show today about Ruby is the Interpolation capability, as a piece of code can tell 100 times more than 200 pages, there we go :

x = 5
y = 7
puts “#{x} + #{y} = #{x + y}”
resultado : 5 + 7 = 12

This code blows up my mind ;)

Now, introducing Rails framework, it’s a web framework that was developed using Ruby language, and Rails implements the MVC pattern, and a persistence layer via Active Record. I can tell that SIMPLICITY was what made me love it. I was remembering what I need to develop a web application using Java : JSP, TagLibs, Struts ou JSF, Spring, Hibernate, configure a dozen of XML files and/or annotations, and a lot more, for sure, it’s too much !

It’s hard to deny that the below code is much more simple to write and mantain than all steps above :

class MyClass < ActiveRecord::Base

validate_presence_of :date, :value

validates_numericality_of :value

belongs_to :user

protected

def validate

errors.add(:value, “Should be positive”) if value.nil? || value <= 0

end

end

To summarize, the point here is neither try to prove that Ruby and Rails are the best things in the universe, nor, I’m saying that they gonna be the next plasma cannon to defeat any space invader :)

Instead, I’m trying to point out everything that makes this language and framework so great. Also, Ruby has limitations as any other language, and Rails doesn’t solve some problems, as pointed out by Martin Fowler on his Enterprise Rails post. Anyway, both Ruby and Rails deserve good attention, because they are easy, flexible and very comfortable. I strongly recommend you to try it if you haven’t so far !

Relearning to Learn

This first post won’t be about any “technical” stuff, instead, I will talk about something I’ve been hearing again, and again, and again these days.

Based on the statement above, I decided to share my experience with everybody, and sometimes learning is trick and hard. I can prove it remembering my childhood times, when I was learning how to hide a bike, indeed I can still remember that pain, every time I felt on the ground, all scratches alongside my legs and so on. :)

Bringing it to our professional world, the learning process is something that demand focus, time investment, money, dedication, effort and a lot of willingness. To show it I can point out the time I was beginning in Java, and I had no clue about where to start, because I was a Delphi programmer who had a lot of experience building client/server database centric applications, but how to program on a black screen ( then my old friend clipper came to my mind :) ). Facing this problem I decided to use the same technique I always used before that : READ BOOKS AND EXERCISE IT’S THEORY !

Books are great learning path, because they are products developed based on author’s experiences, and most part of times the person who wrote it applied some didactics to help the reader. A book has 99.9% of chance to be cheaper resource in comparison to any other learning source, and you can study it on your own pace.

The second part of learning process which I consider very important is experimentation, where we put in practice all theory we read on a book. We can run those examples, we can test it, change it, reconfigure, create our own examples, or we can even start to build that our preferred proof of concept application, such as a Point-of-sale application, CRM and so on. This part must not be skipped, because doing these exercises we’ll be able to “link” all theory ( proving that it works ) to the results.

I am confident that reading 10 pages and executing 10 lines of code won’t make you an expert in anything, but following the road map above will certainly make you progress smoothly.

Another learning characteristic is that it must be continuous, even after years working with Java I still learning, because new features are added, or I need to remember something old. To stress continuous learning process, I’d like to point out that even reading the same book 3 times in a row, it’s impossible to remember every single passage inside it, so, you must practice it, in order to fix this new knowledge.

The underlying point on this post is a push toward learning, because every day we can be a better professional than we were yesterday, we can learn something new today to be used tomorrow. This is my recipe : Read, Study, Experiment and Learn ( Continuous ).