Thursday, August 29, 2013

Jeremy's Programming Blog - Common Code Smells

JeremyMorgan.com Newsletter

 
Sent to Insider Member blogs.sherrylinx.8442@blogger.com



Jeremy's Programming Blog - Common Code Smells

You may have heard the term “code smells” lately, it seems its being talked about frequently again. In this short post I’ll explain what they are, and a few of them you may run across.

What is a Code Smell?

A Code Smell is just a fancy word for an indicator of a bigger problem with your code. It’s language agnostic because you can have code smells in any application. It’s just a sign of bad construction that you can spot fairly quickly.

The biggest problem with code smells is not that programmers are ignorant about them, it’s that they choose to ignore them. They’ll jump into someone’s code, or their own code and see the problems and make the application work, with the intention of fixing it later. This rarely happens.

Checking for code smells

Generally you find code smells when examining code, or doing refactoring. Small cycle refactoring is something you should be doing quite frequently. Often you can take a small method and make it a little better, build some better tests and make it more solid. Eventually you’ll get through enough of the application to make it solid, or decide on a full rewrite.

"Code Smells"The best ways to find code smells are:

  • Casual code inspection
  • Refactoring
  • Heuristics analysis
  • Tools such as PMD, CheckStyle or ReSharper

Common code smells

So what can you expect to find that might indicate a bigger problem? The list is very long and depends on how deep you choose to inspect your software. But here are some very common ones:


Repetition - Easily one of the most common ones. Do you have sections of code repeated all over the place? This is a sure sign of amateur work, and deserves a deeper look.

When refactoring repeated code, you have to effectively search for all instances of that code to get the results you want. While abstracting it into a method is a good fix, it doesn’t mean you’re in the clear.


Needless Complexity - This is somewhat subjective but most of the time you know it when you see it. Its more complex than it needs to be to solve a given problem and is difficult to comprehend. Needless complexity stems from only a few things:

  • A beginning programmer who is making copy and pasted code work to solve a solution.

  • A show off who enjoys creating complex solutions to simple problems in order to appear smarter.

  • A programmer seeking job security - if they are the only ones who understand it, you can’t get rid of them (or so they think).

  • A programmer who has inherited some nasty code and was forced to build in place. (Arguably the worst case scenario).


Rigidity - While usually considered a good thing, the wrong kind of rigidity can turn your software into a house of cards. When you have methods with too many dependencies that are strictly enforced by breakage, you have a system that can’t be touched or modified. Two solutions to this are loose coupling and high cohesion.

Make the methods as independent as they can be, yet closely tied to the objects that objects that use them. Break up your objects if they get too big, or have too many dependencies on other unrelated objects.


Configuration scattered within the code - This is a basic design flaw that is shown frequently in the wild. The most common one is keeping the database credentials in a database class. This can be a disaster when it gets out of control.

Keep all configurable data (anything anyone can change) at a very high level in a centralized location. This is one of the reasons you see huge config.php or similar files. By having them all in one place you make changes simple and easy. By scattering your data through multiple files and locations you end up creating more time (and technical debt) for the next programmer.


Immobile code - Everyone has seen this one. Classes or methods that are marked “don’t touch” are bad and a sign of a much larger problem. If you have something that cannot be modified without taking down the whole system, you’ve found your weakest link. Fix it immediately.

These are just a few of the basic code smells you’ll see. There are far more out there but these are pretty high level and common.

"Code Smells"

Where do code smells come from?

As I stated earlier, it’s not always programmer ignorance that causes code smells. It usually comes from a rushed design and a disregard for technical debt. They’re created by a programmer who knows better but wants to just “get the job done”. This is a false approach for many reasons.

Technical debt is simply the term for the amount of future work you create when you try to save time up front. You have two choices:


Do it the right way - Use best practices and develop a design that is the best of your ability and something you know can scale and grow appropriately. This is your boss’ least favorite method because it takes longer.


Do it the fast way - Ignore crucial design decisions that will take too much time to implement. Create a “hacked together” design that you keep as clean as you can, but you know it’s not right. You convince yourself you can always come back later and fix it. This is your boss’ favorite way because it takes much less time.

The problem with the second approach is very simple:

The time you save up front making a bad design will come back to haunt you exponentially during maintenance.

No matter how good you are or how much you think you can break this rule, it will always prove itself true. You need to educate your boss on the studies done on this, because he/she will not be very happy when maintenance takes considerably more time for “simple fixes”.

Conclusion

I hope I’ve outlined what code smells are and some of the common ones you’ll see. The message I really want to get across here is do it right the first time. If you don’t know how to do it right, stop “winging it” and start learning more about software design.

Software development is heavily dependent on time. Given enough time developers can make solid gold perfect software, but by the time it’s finished it’s no longer needed or the company is questioning where all their money is going to. It’s a constant battle.

Take a little more time up front to save loads of time in the future, and you’ll see many of these code smells start to go away.


Do you like articles like this?

I’m constantly hacking on stuff and writing about happenings in the programmer world. You can subscribe to my feed here, or you can get the programmer newsletter spam free!
     


More Recent Articles

 

 

Thank you for subscribing to my updates! 

Follow Jeremy on Facebook | Twitter | Linkedin

 




Your requested content delivery powered by FeedBlitz, LLC, 9 Thoreau Way, Sudbury, MA 01776, USA. +1.978.776.9498

 

Thursday, August 1, 2013

Jeremy's Programming Blog - 2 new articles

JeremyMorgan.com Newsletter

 
Sent to Insider Member blogs.sherrylinx.8442@blogger.com



Jeremy's Programming Blog - 2 new articles

How to Install A Wireless USB Network Card on Raspberry Pi

Today I’m going to show you how to install a wireless networking card on your Raspberry Pi. I’m using the ultra cheap Ralink wireless card, but the instructions are similar for any Wireless Device.

Update Your System

For this tutorial I’m using Raspian. This is a great beginner OS for the Pi and quite easy to use.

You’ll want to make sure you’re at least wired in so you can do an update:

  sudo apt-get update  sudo apt-get upgrade  sudo apt-get autoremove  

Get Started

We’re going to do this from SSH, though it’s easier to do from the desktop. But you got a Raspberry Pi to learn didn’t you? Many people use their pi strictly over SSH and don’t run a desktop at all. I have decided to include those kinds of instructions rather than do it the graphical way.

1. Plug in your USB device and find it

Once plugged in, again we’ll look at our USB devices:

  lsusb  

It looks like in my case it’s installed, so now we’ll make a copy of the WPA supplicant file:

  sudo cp /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf.bak  

Now open up the file:

  sudo nano /etc/wpa_supplicant/wpa_supplicant.conf  

And add the following:

  network={      ssid="[YOUR NETWORK SSID]"      psk="[YOUR NETWORK PASSWORD]"  }  

It should look like this:

Now, save the file. CTRL + X - Save “Y”

Start up Your Adapter

Now we need to stop the wlan service:

  sudo wpa_action wlan0 stop  

Then bring it back up:

  sudo ifup wlan0  

Test Connectivity

  sudo wpa_cli status  

You should see something like this:

If not, double check your network SSID and Password, that’s the most common problem. You may have a card that isn’t supported by the Raspberry Pi, if not get a new one! They’re super cheap and well worth it.

(Optional) - If You End up Needing a Driver

Sometimes there are issues that prevent you from loading the drivers for your card. Much of the firmware comes with raspian, but if not you’ll have to find the drivers for it. You’ll need a Windows machine to get the drivers you’ll need (at least for Ralink).

Insert the device and run the driver disk provided. Then after successfully installing it, open up your control panel, and select the properties of the device:

Click on “driver details”

This will show you the location of the driver files to copy off the machine, you can put them on a USB stick and put them on your Pi from there.

Get The Drivers off the USB Stick The Hard Way

I put the driver files on a USB stick. Getting them from the desktop is really easy, but what if you only have SSH access? Don’t fret. With Raspian it should automount just fine, you only need to find it.

Run the following command to list your USB devices:

  lsusb  

Now, you want to see all the disks that are attached to your system:

  sudo fdisk -l  

As you can see in my case, my USB stick is mounted at /dev/sda1 so the device is connected to my pi. But where is it?

  mount  

There it is! Under /media/MYLINUXLIVE but that probably won’t be your folder, it will show the drive label you gave it when you formatted it. (I happen to use and love the LinuxLive program whenever I can).

Now I can just look for the drivers on the disk and copy them to a folder in my home directory:

  cd ~  mkdir usbdriver  cp -r /media/MYLINUXLIVE/driver/* usbdriver/  

Now the files are copied, safely unmount the drive:

  sudo udisks --unmount /dev/sda1  sudo udisks --detach /dev/sda  

Now you’ll have the drivers you need, but keep in mind the device may not be supported at all.

Conclusion

I hope this has helped to show you how to install a wireless USB card on the Raspberry Pi. Raspian has a lot of firmware already built in and a lot of USB Wifi adapters are supported right out of the box. Wpa_supplicant makes things really easy. I decided to show how to do it from the prompt for those who want to learn more about the prompt, or install from SSH.

The Raspberry Pi is all about learning so I encourage you to experiment with this stuff as much as possible. The worst that will happen is you’ll need to reinstall the OS. Remember, you’re learning Linux at the same time which opens a lot of doors for your projects. Enjoy!


Do you like my Rasbperry Pi Tutorials?

I’m constantly hacking on stuff with the Raspberry Pi. If you want to get notified every time I post a new article subscribe to my Raspberry Pi feed here, or you can Sign up for the Raspberry Pi Newsletter spam free!
     


Cheap, Fast or Good. Pick any Two

This old saying in Programming holds true, and I’m still amazed at the amount of folks out there who still don’t get it. The matrix is quite simple yet so many folks want it all. Programmers want to build beautiful awesome software built at their own pace, and get lots of money for it. Managers want beautiful awesome software immediately at the lowest price possible. I think there is a way both parties can be satisfied.

Constructing Software is Craftsmanship

The biggest mistake managers often make is thinking that all software is created equal, it’s a commodity and [ set task ] should be completed in [ set time ]. This is a common yet expensive mistake. Software building is craftsmanship just like building a house, furniture or many other products out there. By drawing this parallel you can better understand how to get what you want.

Let’s think about furniture for a minute. What’s the difference between a handcrafted Italian Leather sofa and an Ikea piece together couch? Thousands of dollars for one thing, but why? Because of the process it takes to make it.

Italian Leather Sofa

image

The Italian Leather Sofa is assembled by hand out of the finest materials. The wood frame is made of high quality wood that is expensive and lasts a long time. It’s cut with detail and precision, and carefully assembled so it will not come apart easily. Then each piece of leather is cut and inspected, sewn together and put over the most comfortable springs and stuffing that they can find. It is put under a microscope to ensure its comfortable, looks good and can last 100 years or more.

Ikea Self Assembled Sofa

image

The Ikea model is much different. There are machines that mass produce the sections of cheap, readily available wood. If it isn’t broken and completely decayed it passes. They are slapped together and cheap readily available stuffing, the cloth is stapled over and it flies out the door. They don’t even assemble it for, but that’s your job.

Two very different models

You have two very different products made in entirely different ways. One sofa is made from the finest materials by skilled craftsmen (or women) and the other is made of commodity items and people pushing buttons on machines. So which one is better?

The answer is neither one. The sofa that’s better is the one that is better for your particular application and ability to afford it. If you don’t have $10,000 for a sofa you can at least plant your butt in something that won’t break the bank. Or maybe you don’t care enough about sofas to spend that kind of money. Maybe sofas aren’t really that important to you and you’d rather spend it on a boat, or you have kids!

Those who have the means and desire for a certain level of quality will purchase the handcrafted sofa and gladly fork over $10,000 for it. They’ll wait for it to be delivered and pay the premium rather than expecting to take it home for $200 bucks in 15 minutes. Why isn’t software treated the same way?

How to Build Your Sofa

If you’re in charge of a software project, you have to ask yourself some pretty simple questions, and they’re the same questions you’d ask when purchasing a sofa.

  • How much can I afford? (Talent and Tools)
  • How much time do I have to do it?
  • How important is each feature?
  • Is it worth the investment?
  • Is “good enough” a better choice than “the best”?

With software you have a lot more options and things to think about but the idea is very similar.

Things that are hard to control

  • How many developers you have
  • The quality of those developers
  • How well they work together
  • The amount of time your bosses give you
  • The amount of other projects on the table
  • The resources you’ll need (hardware, etc.)

These are difficult to control in most companies. Unless you have the luxury of a giant budget, tons of high quality programmers and “whenever” deadlines, these are challenges you’ll have to work with. Welcome to the real world.

Things that are easier to control

  • Talent of your developers - You can work hard to find the right people, push them to keep improving and shed the ones who hold you back. It takes time, but it can be done.

  • The environment - You can give the programmers a quiet environment free of distractions and give them the tools they need. By holding these things back you’re also punishing yourself.

  • Project Priorities - By careful prioritization of your needs you can have them focus on the important parts of the project. Giving them 200 small things to do at once will only leave with you with low quality unfinished work.

  • Time given for testing - In all the places I’ve worked I have found the managers that give ample time for testing rarely get embarrassed by shipping a product full of bugs that will embarrass them. Sell your boss on the importance of this.

  • Keep them focused - I don’t mean blocking Reddit (though that may help) but keep them focused on a small set of tasks. If programmers have more than 3 concurrent projects going their productivity drops off exponentially. Narrow their work down to a few important things and follow them to they are done.

You need to focus on the things that are easy to control and make an effort to work on it.

Focus is the key

I consider this step the most important part of the things a manager can control. Focus is more important than the talent of the programmers in my opinion. You can take the best programmers in the world and overload them with one-offs and get junk.

You don’t see doctors performing heart surgery while talking to another patient about tonsillitis while answering the phone at the front desk. Focus is important. Overloading a developer isn’t “pushing them to their peak” it’s shooting yourself in the foot.

The formula for your sofa

  • Determine what resources you have.
  • Nail down the specifications and be realistic.
  • Decide what’s the most important.
  • Be realistic about the quality. The difference between good enough and fine art is vast and expensive.
  • Divide the tasks so the important things are finished first.
  • Make a solid plan. Plan for the inevitable roadblocks.
  • Set expectations and hold each person to them.
  • Give them everything you can to help them complete it.
  • Show your bosses some awesome stuff.

Summary

Instead of relying on wishful thinking, a proper analysis of what you have and what you can do will go a long way. You don’t have to be in the software industry for long before you’ll see a team of a few people who crank out awesome software way ahead of a big group of programmers that are mismanaged.

Just use common sense and determine where you should put your focus. If you want it fast and cheap, you’ll have to expect low quality and maybe that’s ok. If you want fine art, get ready to spend some money and wait a few months. The best place to be is somewhere in the middle.

If you’re building a small widget to show the local weather on your website fast and cheap is fine. If you’re building a shopping cart and sales portal with customer data you may want to give it some more time and let the programmers build it right. It’s worth it when it works.

     


More Recent Articles

 

 

Thank you for subscribing to my updates! 

Follow Jeremy on Facebook | Twitter | Linkedin

 




Your requested content delivery powered by FeedBlitz, LLC, 9 Thoreau Way, Sudbury, MA 01776, USA. +1.978.776.9498