21 07, 2011

So Many Rights & So Many Wrongs

By | 2016-11-21T17:44:20+00:00 July 21st, 2011|Categories: Web Development|Tags: , , , |

Some people think there is no ‘right’ or ‘wrong’ way to code. I agree and disagree. I’m not saying there is only one right way and all developers should live by this method. I’m saying that there is a wrong way for anything. People can go about the same thing in many ways and it would be considered right. Each developer has their own style and comfort zone in how they do things and as long as it is efficient, doesn’t bear on page performance and doesn’t fall into the ‘wrong’ category then it’s still considered good code.

Right

  • Following the rules that the W3C has defined for creating websites
  • Following Web Standards, Best Practice and the KISS principle
  • All code validates and runs error free
  • Separate content from presentation
  • Semantically correct markup
  • Fully minimized and clean
  • Makes sense

 

Wrong

  • Using things in a manner they were NOT meant for (ex. tables for layout)
  • Hacks that are unneeded (hacks are normally only needed for bad code)
  • Overly complex (adding in some code because it was too simple)
  • Re-writing built in functions (re-inventing the wheel of php)
  • Doesn’t make sense

 

Right

To fully explain things there is the W3C which is an organization that standardizes Web technologies. They are the people who define how the web works in terms of HTML. Following these rules will not only keep the need for hacks at bay but it will also keep your code valid because that is what you are validating against when it comes to HTML and CSS. Yes… you are validating your code against the rules they have outlined for you. So tell me again why we wouldn’t follow the rules?

As for fully minimized and clean I mean can I look at it and point out anything not being used? Did the old stuff get cleaned out? Are there unnecessary things? Are there too many unneeded wrapping divs? Is the programming of the code logical? Can it be done in a simpler way? Basically take the code and remove everything you can from it. Make it as small and compact as you can without losing any functionality. Once you’re done it is minimized and clean.

Overall when I say there is a ‘right’ way I mean when any knowledgeable developer can look at the code and figure it out almost instantly because it is done the way it was intended and follows Web Standards and common sense then it is good code.

 

Wrong

Ahhhh the ‘wrong’ way. Man oh man is there a definite wrong way of doing anything in life. Let’s take going to a grocery store that is half a mile down the road. What is the right way to get there? There are a few ways that would work I mean you could walk, take a bus, ride your bike or even drive your car. All these work and are logical. Well what about walking 1 mile the opposite direction to get on a bus that takes you to the airport where you fly round trip to the furthest destination and on return get a rental car and drive straight to the grocery store. This in my eyes is wrong.

It may seem dramatic to some but you have to admit that’s not the most efficient way to get to the grocery store. You might catch yourself thinking ‘Why would anyone do that?’… And that is a good indicator question that determines bad code.

On a less obvious side there are other things that make it the ‘wrong’ way including invalid, erroneous, weighted, hacked and non-logical code. When people use tables for layout it is actually wrong because it is invalid. If errors pop up or it takes triple the time to load due to controllable factors it is wrong. If you use JavaScript to apply CSS classes or inline styles when there is no JavaScript functionality on the page then it is wrong. If you are re-writing built in functions for any scripting language then it is wrong. These are just a few examples that there are wrong ways of coding things.

 

Conclusion

In conclusion I understand that some developers use floats and some use positioning this is their style and both methods work. There is a difference between that style factor and the just plain wrong or outdated factor.

I know I can’t make everyone a believer overnight, some will just always believe if it runs then it must be good code. However I encourage you to Google the ‘benefits of web standards and best practices‘ and after a few good reads go ahead and try to search for the ‘downfalls of web standards and best practices‘. Since I don’t know of any downfalls please comment below if you do find any.

Also stay tuned for more blog posts about this topic.

14 03, 2011

Look or “View” Before You Leap

By | 2016-11-18T14:24:52+00:00 March 14th, 2011|Categories: Cascade CMS|Tags: , |

I think a good habit you can get into as a developer is to think about several ways to tackle a task instead of implementing the first solution that pops into your head.

For example, a client recently decided they wanted to duplicate one of their sites.  So we copied the code and the database. (The site’s data is stored in a Sql Server database.)  Part of the database contains data used for reporting purposes. The new users have access to this data but will not be updating the data. At first glance, if we used the copied database as is, we would need to duplicate the process that populates the reporting tables. However, we don’t want to go that route.  In addition to storing two copies of the same data, changes to the process would require coding the same updates in two different places.

The next idea was to change the all the database calls that reference the reporting tables to access the data from the original database.  This would work but would require changing a lot of calls in several stored procedures that are used to pull the reporting data from the database.

How do we access the data without changing any of the select statements wherever they may be? We then set up a view in the same name as each of the tables from which we wanted to access data from the original database.

Say we have a table named “products” in the original database.  We didn’t include that table in our new database. Within the code on the new site, the select statement might say something like SELECT name, color, weight FROM product.  If we changed the select to pull from the original database, the select statement would look like SELECT name, color, weight from originaldatabasename…product.  Instead of doing this, define a view named Product. This view contains the select from the original table. Now any Select statement that needs data from the original table has access to the data in the table via the view.

This saves time now and in the future in that we don’t have to change all the Select statements to access the Original database’s tables.