Why Bother Coding

Helping the utterly confused, one post at a time

Dynamic jQuery Control Manager

January 27
by Carleton DiLeo 27. January 2011 18:00

I have created a JavaScript module using jQuery that allows you to add, remove, update and get the current state of a series of controls. I will add full documentation and more examples as I get more time but I think this is enough to get started.

<div class="wrapper">
  <div id="Customer">
  </div>
  <input type="button" value="Add Customer" id="AddCustomer" />
  <br />
  <input type="button" value="Delete All" id="DeleteAll" /><br />
  <textarea id="CustomerXML" cols="40" rows="3"></textarea>
  <input type="button" value="Get All Customers In XML" 
    id="GetCustomerXML" /><br />
  <textarea id="CustomerJSON" cols="40" rows="3"></textarea>
  <input type="button" value="Get All Customers In JSON" 
    id="GetCustomerJSON" />
</div>
<script type="text/javascript">
    //Get the area to manage a control list and the element when clicked will add a new control
    var customer = $('#Customer');
    var addCustomer = $('#AddCustomer');
    WBC.ControlManager.add(customer, addCustomer);
    
    var customerMgr = WBC.ControlManager.get(customer);
    if (customerMgr) {
      //An important thing to note is that the name of the element maps to the name of the
      //xml or json element when getting state
      var customerControl = 
        $('<label>Name</label><input type"text" name="Name"/>');
      customerMgr.setControlToAdd(customerControl);
      $('#DeleteAll').click(customerMgr.deleteAll);
    }
    $('#GetCustomerXML').click(function (e) {
    $('#CustomerXML').val(customerMgr.getStateXml());
  });
  $('#GetCustomerJSON').click(function (e) {
  $('#CustomerJSON').val(customerMgr.getStateJson());
});
addCustomer.click();
</script>




wbc.controlManager.js (12.50 kb)

Tags:

WSIT Jar Location in Glassfish

October 11
by Carleton DiLeo 11. October 2010 20:16

While looking through a Java applcation I came across the following import.

import com.sun.xml.ws.mex.client.schema.GetMetadata;

The NetBeans project the application was developed with didn't have anything in the classpath that contained the class. I'm not sure why this class was being used so please avoid from flaming me about why not to use it. After some searching, I discovered the class was part of WSIT or Web Services Interoperability Technologies which is now bundled with GlassFish. I couldn't find anything on the internet pointing to the jar file I needed to add to my classpath so I dug inside the GlassFish install folder and found the class was in a jar file named webservices-osgi.jar in \Program Files\glassfish-3.0.1\glassfish\modules. After adding the jar to my class path, the application compiled.

Tags: , , ,

Java

Must Read Books for any Software Developers

September 25
by Carleton DiLeo 25. September 2010 22:37

Sometimes other developers ask me what books they should read. I'm sure the expected answer is to recommend something with the name of a technology and the latest version number associated with it. These types of reference books can be good to better understanding the platform you are working with but I find the best books to read are the ones that focus on understand the mind set around being a developer. Developing software is more than writing code. The following books provided me with insight into this mind set so hopefully it will help you as well. I will add more as they come to me. If you have any suggestions let me know and I will add them.

Code Complete

To be a developer is to always be on the receiving end of people's problems. It's our job to understand these problems and with a bit of magic, create a solution. After we finish we never have to touch that program again because the user is always happy...I'm sorry I can dream can't I? More often than not, the hardest part of developing something is maintaining it. We curse the existence of the "last developer" and their crappy code only to find out we created it. Code Complete doesn't focus on code maintenance specifically but all the topics it does cover ultimately help you write code that is much easier to work with. Read it now! Seriously, do it!

Buy Code Complete: A Practical Handbook of Software Construction

Head First into Design Patterns

One of the biggest mysteries to developers is the idea of Interfaces. Why bother right? You can just create a class, instantiate it and you're done. What we lose is the ability to create code that is loosely coupled, testable and easy to modify. Head First into Design Patterns will help you understand why interfaces are so important to comprehend. The book covers all of the Gang of Four design patterns in a way that anyone can grasp. It's not just interfaces you will come to know but also the importance of software design as whole. Be warned, this book uses Java for its code examples. As long as you understand other languages like jave (C#, C++ and VB.NET kind of) you should be able to understand the book.

Buy Head First Design Patterns

Software Estimation: Demystifying the Black Art

How long does it take you to do the tasks you're assigned? How often do you take longer than you first intended? The issue is that estimating is difficult. It's impossible to know exactly how long it will take to create a program every time (If you do know please email me and I will pay you to teach me). Steve McConnell's book teachs you to manage the uncertainties of estimation. It will help you get better but more important help you with your boss. I hope.

Buy Software Estimation: Demystifying the Black Art (Best Practices (Microsoft))

Rework

Rework

During my career as a developer I've notice many other developers making decisions based on reasons I don't tend to agree with. More often than not we use aspects like ego and coolness factor. It's important to remember that we ultimately need to finish the work we start and satisfy our user's needs. I'm not saying we should rush through our tasks flinging code and features as we go but it's important to analyze why we do the things we do. Rework is a short book that provides great insight into this process.

Buy Rework

Best Kept Secrets of Peer Code Review

Best Kept Secrets of Peer Code Review

I know this book is technically a 164 page advertisement for smartbearsoftware.com but i don't care. This free book provides a short, easy to read overview of an important quality control technique called code reviewing. If you are not doing code reviews now and want to get caught up, read this book.

Get Best Kept Secrets of Peer Code Review Free

Tags: , ,

Books

Debugging Javascript Tips

September 24
by Carleton DiLeo 24. September 2010 23:06

Common Mistakes

When working with JavaScript there a many things (and I mean many) that may cause your code to not work as you intended. I have found that there are a few mistakes that people, including myself, seem to make more often. When you encounter an issue with your code, try running through this list of possible mistakes before moving on to other troubleshooting techniques. These common mistakes can end up costing you a lot of time when they are really easy to correct.

Misspelled object/function calls

Providing invalid spelling of a function or variable you wish to access can be quite easy since writing Javascript code does not come with the frills of sophisticated intellisense. It's a dynamic language after all. Look carefully at your method calls and object properties when you call them. Classic examples of this type of error is using document.getElementByID() while trying to fetch an element on the page instead of the correct function, document.getElementById(). Common symptoms of this error is null or undefined error messages on methods you know were written and elements you know exist.

Unfortunately the only way of avoiding these types of issues is careful naming of the functions and variables you create. Make sure you avoid names that people misspell often. Functions like startMaintenance() are easy to misspell and screw up. Also, watch your use of letter casing. Functions like getIDBadge() can often be written as getIdBadge(). Sometimes these names are unavoidable. If you did not author the code unfortunately you have no say over what names were used. Just make sure that if you come across an error in your code where a function or variable is undefined, double check that your spelling is correct. If your lucky you can save yourself a lot of time and a headache.

Forgot external JavaScript dependencies

When moving your JavaScript code into external files and working with third party JavaScript libraries, its easy to forget to include a file that your code is dependent on. This is especially true when dealing with libraries that have interdependency between files. If you find that you're receiving strange errors about null or undefined objects and functions you know you didn't write, make sure you double check you have included everything you need to run the code causing the error. Look over the documentation the third party library provides. If you have external files that you wrote yourself, make sure that there are no syntax errors in the code that is preventing them from being included.

Invalid references to page elements

Accessing the DOM can be a task that can cause great grief if you are not careful. There are a few mistakes that can be made while performing this action. If you find that your code keeps complaining that an element you are trying to access on the page is null even though you swear its there, make sure the id of the element you are using is correct. Remember that element id's are case sensitive so "johnsBlog" is not the same as "JohnsBlog". If you find that events that you are attaching to an element are not firing, invalid element id's may also be the issue. Another possibility to this issue is you may have accidentally used the name specified in the name or class attribute of the element thinking you were accessing the id. If you have to, use copy and paste to make sure you're using the correct value.

Avoiding Mistakes

Trying to avoid mistakes is hard with JavaScript because you don't have the ability to compile your code before you deploy it. Instead you are locked into a cycle of write, deploy, test that may turn into a seemingly never ending loop just to get things right. This means you need to be more careful when writing code because no one is there to hold your hand. Luckily there are a few tools that will help make your life easier.

JSLint

JSLint is a tool written by Douglas Crockford that evaluates the JavaScript you wrote and informs you of any issues it finds. Some of these issues are trivial like adding ; to the end of every statement to issues that could cause frustrating problems in less forgiving browsers such as Internet Explorer. You would be wise to run all your JavaScript through this program before deploying it or even running it for the first time. You will find that over time this tool will also make you a better programmer by teaching you the common mistakes to look for when writing your code. Just be warned, if you have never used JSLint before, it may hurt your feelings the first time you run it.

Firebug

Firebug is a godsend to the web developing community. If your not using this tool do yourself a favor and go download and learn how to use it. It will save you time and frustration when trying to debug your Javascript code. Teaching you how to use this tool is out of the scope of this article. I will try to address some techniques of using this tool at a later time. You can always visit the firebug website to learn more.

Code Reviews

When you're developing, sometimes it's easy to get tunnel vision when it comes to your code. Code reviews are a good way to find mistakes early and keep your teams code base in-sync. Performing code reviews can be as easy as having your teammate glance at your code for obvious errors and formatting to being as detailed as having a standard checklist of common issues and team best practices that must be used for each review. There are some good resources out there for understanding the code review process. Steve McConnell's book Software Project Survival Guide has a good section on reviews. His site has a short summary of the code review process you can find here. You need to register to get the pdf but its worth it.

Summary

I hope some of these tips will help you through the tough times of writing JavaScript. The language can be really fun to write in if you don't cut corners and take your time. Over time as your code matures you will be amazed by the things that are possible. Until that time just keep trying.

Tags: , ,

JavaScript