• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why does my struct, errr, class, suck according to PMD?  RSS feed

 
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
disclaimer: I've been doing Java on and off for maybe 2 months now, this is my third program and the first one in which I've actually tried to do anything.

I need a structure of data, which in C/C++ I would use a struct for. In java I made it a class. As in:



So the code is working and I decide Hey! Lets run -Wall and lint on it!". Checkstyle gives 112 warnings, most of which refer to my lack of comments (I know....), and where I put my braces (sod off...). Findbug finds..... nothing. Which to me seems pretty odd, then again I've been writing code for a living for 30 years. Then I run PMD. sigh.

PMD gives multiple errors for this. Things like "each class needs a constructor (not this one, trust me)", "Too many fields (I need em all, trust me)", "Found non-transient, non-static member or mark yadda yadda yadda (say what? where did I say I wanted to serialize this?)", etc etc etc.

I get 3 or 4 errors per element, depending on the element type (it really hates my ArrayList).

So, is there another way to declare a struct in Java, or should I ignore PMD?

/ hmm, I registered here in September
// ~/java/xmas was created September 2
/// prints days until xmas.
//// time flies when you're confused as hell
///// I run javac and java under windows with cygwin, fire up Eclipse when I have to


 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That class is severely error‑prone without a written constructor.
It breaches the conventions of encapsulation.

Who needs Findbugs?
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing the least bit object oriented about my program, it's basically a log file parser. There's no reason to have a constructor, each Node gets filled in when I parse it's line in the log file. If a node doesn't have a line in the log file then the object doesn't exist. If a node does exist in the log file then every field in the struct is filled in by my parseNode(String record) method.

Hmmm, I guess I could rename parseNode() so it's the constructor, and pass it the input record. Now that I think about it that would work.

Find bugs is also very unhappy with me not using setter/getter methods on my class.

Let's be honest. Node is not a class, it's a struct. A struct is the best data type for my application. Writing a constructor, along with getter/setter methods, is procedural programming with OO lipstick.
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serious questions: if you are not going to write OO code, why use Java at all? If you don't want to know how many ways your "struct" breaks OO conventions, why use PMD?

There is no "struct" in Java. You are using the language poorly, so it should be no surprise that tools that advise you to write the language well will complain.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because I'm trying to learn Java. Otherwise I'd have coded it up in Python.

I'm well aware my 'struct' isn't OO, but my problem domain isn't OO either. Parsing log files is inherently a procedural task.

I'm not trying to be snippy, I'm just trying to learn to write good Java code. I'm using checkstyle, PMD, and find bugs so I can nip bad habits in the bud, but I'm starting to think that's A Bad Idea (tm) for this problem.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:Because I'm trying to learn Java.

Java is OO. Learning Java is learning OO.

I'm well aware my 'struct' isn't OO, but my problem domain isn't OO either. Parsing log files is inherently a procedural task.

Then maybe Java is the wrong tool. If you don't want OO, you don't want Java. Same as not using a screwdriver to drive a nail.

Personally, I don't agree with "Parsing log files is inherently a procedural task". Procedural versus OO is just a choice. Either paradigm can be applied to the problem.

I'm not trying to be snippy, I'm just trying to learn to write good Java code.

A worthy goal. But I think you're off on the wrong foot. There's no such thing a "good but non-OO" Java.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Java is OO. Learning Java is learning OO..


I know OO. I've done OO in C++, perl, and Python. I'm not an expert in OO (I mostly write device drivers in C for a living), but I understand the concepts and have written a few pure OO programs.

Personally, I don't agree with "Parsing log files is inherently a procedural task". Procedural versus OO is just a choice. Either paradigm can be applied to the problem.


3 weeks ago, when I started this project, I wracked my brain trying to figure out how to do it in an OO way. I could think of 2 ways to do it. First, the core object is the node. The main data structure is an array of nodes. The node class reads a line and fills itself in. This seems like procedural programming with OO lipstick.

The other way I can think of is to have the parser itself be the core object, returning an ID:VALUE pair. Ex: ID:12, TYPE:printer, CONNECTION:30, etc. But again, this just seems like adding OO lipstick to a procedural pig.

In both cases OO adds complexity without bringing any real benefit.

So, I ask you this in all seriousness. How would you design an OO log file parser?
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:
So, I ask you this in all seriousness. How would you design an OO log file parser?


Instead of just thinking about OO, I recommend thinking of it as a library. Let's pretend that you are responsible for the parser, and only the parser. You provide the library, and your colleagues will be using it -- and consider them as your customers (who will complain if your library is crap).

To do this... possible setups could be a LogFileParser class, with constructors that your colleagues will need. Such as specifying the output file by name, or an output stream for cases where the file has already been open. As for methods, you will provide services of what your colleagues need. Everything else is implementation detail -- don't require data that your colleagues may not have, don't expect your colleagues to maintain state between calls, don't expect that the data classes returned won't be used in collections such as hashmaps and treemaps, don't expect that your colleagues won't be serializing that data, etc.

Ohh... and don't waste their time with learning your library. Make it intuitive -- the parser could be an object. The components that it returns could be objects. And these objects are laid out in regards to what they are, the stuff being logged (and not how they are logged).

And also, don't expect that your customers colleagues won't be requesting changes. Don't break legacy programs when you make changes. etc. You need to design something that can be changed as needed, while still remain backward compatible with older versions.


Think of object oriented programming more in terms of the issues that it is trying to solve (in satisfying your customers). That is the harder part. Do it right, and you won't have your colleagues complaining that you make crap.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!