Subscribe to immeraufdemhund's Blog        RSS Feed
-----

Clean Code fundamental in good programming

Icon Leave Comment
Well this is my first blog ever. Never have had interest in doing this, but I have had so many thoughts lately about many things, that I thought that I would share just one of the things on my mind.

I saw a ad a few weeks back about a book called "Clean Code" by Robert C. Martin. I must say that his book is very educational and if you have a interest in cleaning up your code, or want a good read than this book is well worth it. I am on chapter 3 right now about functions, and am blown away about how easy it makes programming look. Lately too I have been finding my self looking at other code that I've done and been changing to better meet this "clean code" standard.

My goal is to not reiterate what he has said, but rather show what steps I have done to not only make my code easier to read, but also easier for others to read as well.

I believe I am going to start in on something that I am/was very guilty of when I first joined here. In that book there is a picture in the introduction that describes the only valid measurement of code quality: WTF/minute. I fell on the floor laughing. It had 2 rooms side by side that are for code review. To the left is like one or 2 WTF's with good code written under the door, and to the right is about a billion WTF's and bad code wrote under that door. What a funny picture. Which reminds me that I used to often post code on here and about 5 minutes later I would get someone who said "your code is horrible".

Now at first that got under my skin because it wasn't followed up with a why. I came to a realization though that this isn't a place to learn how to do much other than fix your errors and bounce a few ideas off of people. Which means that the person responsible to learn how to code well falls upon you. Well hopefully I can help you out a little bit. I am still learning a bunch too, so I'm sure my current opinion will change as I learn more. Just keep that in mind though. This is just "my opinion".

Now thankfully he goes to describe how you can go from writing bad code to good code. His first chapter was on why you should have clean code. To sum it up it basically said that you will spend longer on the code to write it, but reading it will save you time especially when it comes to fixing errors and adding on.

So I started looking through all my old code and seeing if I could read any of it, and I had a resounding no. So let me show you some of my old code, and what I did to fix it.

  JTable calTable;
  int month,today,year,end,start,week;
  Calendar hi = Calendar.getInstance();
  public CalendarMath(JTable jTable1)
  {
    month = hi.get(Calendar.MONTH); //0 = january; 11 = December
    today = hi.get(Calendar.DAY_OF_MONTH);
    year = hi.get(Calendar.YEAR);
    this.calTable = jTable1;

  }


the first that that pops out to me is that I have stupid names for some of my variables...such as hi..and my constructor's name isn't very well understood as to what it's purpose is. One thing the author stated is that you shouldn't have to have too many comments in your code if you code it right. I'm not saying comments are bad, but they can be cut down with good naming and common sense. So how can I help out this little bit of code?

  public JTableCalendar()
  {
    month = currentDate.get(Calendar.MONTH);
    dayOfMonth = currentDate.get(Calendar.DAY_OF_MONTH);
    year = currentDate.get(Calendar.YEAR);
  }



Now I know it's not much of a change. But a few things that stick out are a name that makes sense for my constructor. CalendarMath didn't actually do any math what so ever!!! hi is a greeting so why was it a name for a variable? I changed it to better reflect what my data really is. Also I changed my JTable calTable (which would also get a different name) from public to private and made a get/set method. Cleans up the code very much, and most anyone could read this and understand what I am trying to do without too much effort. So that wraps up what I am thinking for tonight. Till next time.

ERT

0 Comments On This Entry

 

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

August 2014

S M T W T F S
     12
3456789
10111213141516
17181920212223
242526 27 282930
31      

Recent Entries

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)

Categories