|
|
||
Malcolm Davis's BlogProgramming ArchivesIt dependsPosted by malcolmdavis on September 26, 2004 at 10:31 PM | Permalink | Comments (3)When first starting Java, many developers are easily overwhelmed with the enormous options between development environments, technology, and implementations. Should a developer use the reference implementation (RI), open source, or commercial products? With new open source projects popping up all the time, the decisions become more difficult everyday. In Anti-Pattern - The Swiss Army Effect, John Reynolds expresses concern of the expansion and growth of Java technology, and the technology overlap that is occurring. For instances where does one go for database CRUD operations? JDBC, JDO, CMP? Web development can be just as frustrating. Web development products include JSF, Velocity, Struts, Tapestry, and Jetspeed for starters. The large selection provides grief to many. The hammer Microsoft's claim to fame is their one homogenous, well-integrated, do it all environment. Even though Microsoft VisualStudio falls way short of free tools like Eclipse, and the .NET architecture is lacking features like an Object/Relational Mapper (ORM), the developers don't have all those decisions to make, they just make what they have work. Is it choices that breed discontentment? The hammer mindset is understandable. Getting a solid grasp around just one of the tools is difficult. Both developers and customers try to learn one technology and apply it to all problems. Trying to look at the market place and second-guess the future is equally dreadful. It depends Knowing the basics of solid development far outweighs which package/technology/tool used. Tools & technology change, concepts and practices just adapt to the changes. Final thought: Regardless of the profession, doctor, lawyer, or engineer, the focus is on the 'principles' and not the 'tools'. Being a great doctor does not change based on new drugs or procedures. Great doctors work to understand the unique needs of the patient by doing such things as analyzing symptoms, understanding the patient history, and simply listening. Class variable namesPosted by malcolmdavis on August 26, 2004 at 09:14 AM | Permalink | Comments (20)In "Code Complete" section 9.4 informal naming conventions, Steve McConnell describes the use of module variable 'm_'. Since reading this in 1994, I have used the 'm_' convention in C programs, then C++. I've seen 'm_' used in MFC, and in coding standards for the commercial software companies. Even though Sun does not explicitly define class variable declaration in the coding standards, much of Sun's code aligns parameter names to an existing field with the same name. Much of Sun's code uses 'this.' to set member variables.
public void setName(String name) {
this.name = name;
}
I asked Steve McConnell his opinion about 'm_' versus 'this.'. The following is his response: Some people use the underscore '_' to determine a variable's scope, such as Docman at SourceForge. Hence, the above sample would be:
public void setName(String name) {
_name = name;
}
I've also know people to use 'p_' for differentiating parameters, as shown to the following sample:
public void setName(String p_name) {
m_name = p_name;
}
C# has a different twist by providing a 'property' feature: (note the implied 'value')
public string Name {
get { return name; }
set { name = value; }
}
Even though the C# 'property' feature looks good on paper, in practice the feature is widely misused. The software language scene will change a great deal over the next few years with the arrival of meta information, aspect oriented programming, code generation integration, and new programming features like C# properties. However, I think at this point, it is still important to differentiate the scope of variables. Even though it looks Hungarian in nature, the focus of Hungarian notation is to identify the type of a variable, not the scope. The issue with Hungarian is that the classical types did not fall into the Object-Oriented world, and types very often change. The Humble ProgrammerPosted by malcolmdavis on July 26, 2004 at 02:10 PM | Permalink | Comments (3)I've looked for the Humble Programmer sometime ago, and all I could find was a used copy on Amazon. You can now download a PDF copy from http://www.cs.utexas.edu/users/EWD/ewd03xx/EWD340.PDF Over the last 30 years, many of the themes Dikjkstra talked about have been re-communicated over and over again. For instance, "The competent programmer is fully aware of the strictly limited size of his own skull; therefore he approaches the programming task in full humility, and among other things he avoids clever tricks like the plague.." In Writing Solid Code, Steve Maguire says "Write code for the 'average' programmer". toString() - any format recommendation?Posted by malcolmdavis on June 10, 2004 at 05:30 PM | Permalink | Comments (10)Many times the toString() format is dependent on how the developer wants to see the state. I've seen formats to accommodate Swing, console, the debugger, and log files. To help format Value/Transfer objects consistently, I use a utility method that employs reflections.
Use:
public String toString() {
return ToString.format(this);
}
Sample output:
[com.company.business.Address: Key=0, State=AL, Country=USA, City=Helena, Street=1313 MockingBird Lane, Zip=35000]
To mitigate issues with logging, I avoid carriage return and line feeds. To help differentiate embedded objects, the objects are enclosed with '[' and ']'. [com....Customer: , Address=[com....Address: ], Phone=[com....Phone: ], , ] (where com... is short for the entire package path, com.companyname.business.etc..) Any thoughts? SimianPosted by malcolmdavis on June 06, 2004 at 09:36 PM | Permalink | Comments (6)A certain degree of copy and pasting occurs with every development project. The problem is many times overlooked, or missed during code inspection. Simian helps stop some of this monkey business by searching through text files (like Java and C#) and identifying duplications. To do some testing, I ran Simian against a recent project. After running through 250K+ lines of code (LOC) in 20 seconds, Simian had found 47129 duplicate lines. I guess you can expect some duplication with 250K+ LOC written by 10 developers in 10 months. Further investigation discovered many of the code duplications were in auto-generated sections. [Of course this makes you wonder about the code generation process]. Additionally, some of the dups found were over common coding variables usages in routines. Simian seems to look for exact copies and not similarities. A more meaningful tool would be something that found algorithm 'similarities' in code. A more common problem with copy and pasting is when a developer copies a section code, then makes a minor mod. In the following example, the developer intended to have a common display format of the form of identifier + description.
Class Member
public String displayText() {
return "[ " + memberId + " ] " + name;
}
Class Branch
public String displayText () {
return "[ " + branchId + " ] " + description;
}
The developer copied the code and made a small mod. [Yes, this is a real world example.] The concept is valid, and Simian does find many copy & paste problems. Simian helps automate the process by providing an Ant tag. Additionally there are plugins for Checkstyle and Eclipse. [I was able to get Simian to work in both 2.1 and 3.x of Eclipse.] Simian can be found at http://www.redhillconsulting.com.au/products/simian. Unfortunately, Simian is neither Open Source, nor free for commercial use. However, it is free for Open Source projects, and inexpensive to purchase for commercial. Clear the CLASSPATHPosted by malcolmdavis on May 03, 2004 at 10:26 AM | Permalink | Comments (4)One developer, 2 modifications The "NoSuchMethodError" problem is a common one seen by developers new to Java. The exception is difficult to track down. Everything compiles and works on the developers box, but the error occurs when the application is deployed. Generally, "NoSuchMethodError" is caused with when the method signature of a class has changed. So why is the problem only showing up when the application is deployed? The CLASSPATH variable is the first place I look. When the virtual machine starts up, the VM searches for and loads classes in the following order:
The preferred method
The nightmare begins NoSuchMethodError RI the applicationPosted by malcolmdavis on April 28, 2004 at 07:40 AM | Permalink | Comments (4)What is RI Tomcat demonstrates one of the core strengths of Suns work with Java, specifications and reference implementations. Why RI
Application portability Fusing with a single vendor is worse then selecting a single OS, IDE or anything for that matter, and saying we will never change. Change is inevitable due to business needs, technology, or vendor offerings. The best way to fight change is to stay application vendor neutral. The EJB container
Nevertheless, for several reasons developers have not used J2EE RI.
Included in the build Sun is working to solve the usage problem with J2EE RI by allowing it to be used in production, and working with tool vendors to have it included in product lines. Maybe someday I will see companies using the J2EE RI as the standard for development as many have done with Tomcat.
Specification insightPosted by malcolmdavis on March 09, 2004 at 09:03 PM | Permalink | Comments (0)There are many ways of getting started on a new piece of Java Technology. Developers do searches on Google, check their favorite online publication, go to Amazon for books, view tutorials on java.sun.com, etc… But, one source that is frequently overlooked is the specifications. When I started JSP and Servlet development about 4 years ago, I thought it was Sun’s version of Microsoft ASP technology. Then a friend of mine, Joe Wortmann, handed me a copy of the JSP and Servlet specs. Joe explained this importance of starting at the specs. I like to hack and experiment, I thought the specs were for wimps, but I gave it a try anyway. Reasons to start with specs
After spending some time with the docs, I found JSP & Servlet was not just another version of Microsoft technology. Additionally, the specs gave me insight into the workings of the technology. Specifications explain:
To the point Specs get to the point, and avoid the clutter (and slant) of vendor specific documentation and tools. Many times vendor documentation tends to be missing important parts, just cover installation and configuration, or just point out vendors specific extensions to the technology in question. Additionally, many times developers get so hung up on the vendor tools they don’t understand the underlying technology. Answering questions Having the specs around help answer questions, and help get projects done the correct way. For instance, a common web project question is: “Can I stick HTML and images into the WEB-INF folder?” To answer the question, I simply refer to the specs: “It is important to note that the WEB-INF node is not part of the public document tree of the application. No file contained in the WEB-INF directory may be served directly to a client.” Other resources Specifications are not a replacement for other resources such as books, articles, tutorials and vendor documentation. These other resources provide insight into usage, development, and implementation patterns that help smooth development, deployment and maintenance. Final note Some specifications have grown considerable over time. The Servlet specs have grown from 79 pages in version 2.2, to 257 pages in version 2.3, to a whopping 330 pages in 2.4. Don’t let the doc size scare you off. Generally the specifications are provided in multiple formats such as HTML, PDF and PS. I keep a bookmark to copies on my machine. Whenever I learn a new piece of Java technology, such as JMS, I download the specifications and start reading. The only problem with this approach is that you may get irritated at people that don’t read the specs, or the vendors that do NOT develop products to the specs. Thanks Joe. :-) Adapt the Java style for your ownPosted by malcolmdavis on February 29, 2004 at 09:21 PM | Permalink | Comments (21)Getting C/C++ developers to adapt new coding conventions can be frustrating to a team lead and other Java developers. Coming from a C/C++ and Microsoft background, my style was somewhat different from Sun's guidelines. I found that I was reformatting code all the time to my own convention. Reluctant to change my ways, and looking for every justification in the world not to, I asked experts for guidance. Then Bruce Eckel gave me some words of advice that are paraphrased as follows: "..not only has Sun worked it all out (and why re-invent a good wheel), but more importantly because everyone sees the Sun code and becomes used to it -- why add the extra mental cycles necessary to shift gears every time you jump to 'your format.'" After reading Bruce's comments, I got over my ego, and changed my coding habits. Within a few months, the Sun style had become my native convention and I had successfully re-tooled 10 years of C/C++. While the changes might seem minor and insignificant, I had a huge increase in productivity due to Java comprehension. [It's interesting how much an ego can get in the way of being a better programmer.] Sun's Code Conventions for the Java Programming Language can be found at http://java.sun.com/docs/codeconv/ Notes:
| ||
|
|