You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-dev@xmlgraphics.apache.org by Glenn Adams <gl...@skynav.com> on 2013/10/29 15:01:56 UTC

Java Programming Basics

I've been noticing code recently that makes it clear the author was a C/C++
programmer unfamiliar with Java. Whenever I see these things, I am fixing
them on the spot:

(1) initializers are not required for class or instance members when their
initial values are null, false, 0, etc;

e.g.

public class Foo {
  private Bar bar = null;
  private boolean done = false;
  private int value = 0;
}

should be written as:

public class Foo {
  private Bar bar;
  private boolean done;
  private int value;
}

(2) it is never necessary to invoke super() in a constructor, e.g., super()
in the following is redundant:

public class Foo {
  public Foo() {
    super();
  }
}

(3) it is never necessary to define a default constructor if there is no
other defined constructor and it does nothing; e.g.,

public class Foo {
  public Foo() {
  }
}

should not define a default constructor; Java will always supply a default
constructor if no other constructor is defined;

(4) however, if you want to prevent the generation of a default, public
constructor, then you can define a private no-argument constructor:

public class CantInstantiate {
  private CantInstantiate() {
  }
}

Re: Java Programming Basics

Posted by Adrian Cumiskey <ad...@cumiskey.com>.
I know you could catch these easily with PMD.  Most projects that I have
been involved with perform these kind of syntactic checks as a commit/build
time hook.  FOP currently offers this just as a reporting facility.
On Oct 30, 2013 3:25 AM, "Glenn Adams" <gl...@skynav.com> wrote:

> Right now, we are only using Checkstyle and Findbugs. I'm not sure if
> there are Checkstyle rules that could catch these cases. If there are, they
> aren't enabled at present.
>
>
> On Tue, Oct 29, 2013 at 9:16 AM, Adrian Cumiskey <
> adrian.cumiskey@gmail.com> wrote:
>
>> My mistake, I spoke too soon before checking first.  PMD is available but
>> the pmd target is not run as part of the build.
>>
>
>

RE: Java Programming Basics

Posted by Simon Steiner <si...@gmail.com>.
Hi,

 

There is http://checkstyle.sourceforge.net/config_coding.html#ExplicitInitialization

 

Thanks

 

From: Glenn Adams [mailto:glenn@skynav.com] 
Sent: 29 October 2013 19:25
To: FOP Developers; Adrian Cumiskey
Subject: Re: Java Programming Basics

 

Right now, we are only using Checkstyle and Findbugs. I'm not sure if there are Checkstyle rules that could catch these cases. If there are, they aren't enabled at present.

 

On Tue, Oct 29, 2013 at 9:16 AM, Adrian Cumiskey <adrian.cumiskey@gmail.com <ma...@gmail.com> > wrote:

My mistake, I spoke too soon before checking first.  PMD is available but the pmd target is not run as part of the build.

 


Re: Java Programming Basics

Posted by Adrian Cumiskey <ad...@cumiskey.com>.
There is an ant "pmd" target which generates a report_pmd.html in the build
directory.  It is invoked from the "reports" target.
On Oct 30, 2013 3:25 AM, "Glenn Adams" <gl...@skynav.com> wrote:

> Right now, we are only using Checkstyle and Findbugs. I'm not sure if
> there are Checkstyle rules that could catch these cases. If there are, they
> aren't enabled at present.
>
>
> On Tue, Oct 29, 2013 at 9:16 AM, Adrian Cumiskey <
> adrian.cumiskey@gmail.com> wrote:
>
>> My mistake, I spoke too soon before checking first.  PMD is available but
>> the pmd target is not run as part of the build.
>>
>
>

Re: Java Programming Basics

Posted by Glenn Adams <gl...@skynav.com>.
Right now, we are only using Checkstyle and Findbugs. I'm not sure if there
are Checkstyle rules that could catch these cases. If there are, they
aren't enabled at present.


On Tue, Oct 29, 2013 at 9:16 AM, Adrian Cumiskey
<ad...@gmail.com>wrote:

> My mistake, I spoke too soon before checking first.  PMD is available but
> the pmd target is not run as part of the build.
>

Re: Java Programming Basics

Posted by Adrian Cumiskey <ad...@gmail.com>.
My mistake, I spoke too soon before checking first.  PMD is available but
the pmd target is not run as part of the build.

Re: Java Programming Basics

Posted by Adrian Cumiskey <ad...@gmail.com>.
Some PMD rules as part of the FOP build process would prevent these from
occuring.  Perhaps something to consider if everyone agrees with Glenn's
comments?
On Oct 29, 2013 10:02 PM, "Glenn Adams" <gl...@skynav.com> wrote:

> I've been noticing code recently that makes it clear the author was a
> C/C++ programmer unfamiliar with Java. Whenever I see these things, I am
> fixing them on the spot:
>
> (1) initializers are not required for class or instance members when their
> initial values are null, false, 0, etc;
>
> e.g.
>
> public class Foo {
>   private Bar bar = null;
>   private boolean done = false;
>   private int value = 0;
> }
>
> should be written as:
>
> public class Foo {
>   private Bar bar;
>   private boolean done;
>   private int value;
> }
>
> (2) it is never necessary to invoke super() in a constructor, e.g.,
> super() in the following is redundant:
>
> public class Foo {
>   public Foo() {
>     super();
>   }
> }
>
> (3) it is never necessary to define a default constructor if there is no
> other defined constructor and it does nothing; e.g.,
>
> public class Foo {
>   public Foo() {
>   }
> }
>
> should not define a default constructor; Java will always supply a default
> constructor if no other constructor is defined;
>
> (4) however, if you want to prevent the generation of a default, public
> constructor, then you can define a private no-argument constructor:
>
> public class CantInstantiate {
>   private CantInstantiate() {
>   }
> }
>

Re: Java Programming Basics

Posted by Glenn Adams <gl...@skynav.com>.
No, it wasn't you. It is 5 year old PDF related code.


On Tue, Oct 29, 2013 at 8:44 AM, Dridi Seifeddine
<sd...@iptech-group.com>wrote:

> Who is that author? I hope you’re not referring to me Glenn. Well I came
> from a C/C++ background, and I confess that my Java knowledge is a little
> bit rusty but I’m always learning, faster than you may expect :)****
>
> ** **
>
> If you look at my latest patch for whitespace management, you’ll see that
> I never do these mistakes.****
>
> ** **
>
> All the best.****
>
> ** **
>
> Seifeddine****
>
> ** **
>
> P.S. Ignore this email if it was destined to someone else.****
>
> ** **
>
> *De :* Glenn Adams [mailto:glenn@skynav.com]
> *Envoyé :* mardi 29 octobre 2013 15:02
> *À :* FOP Developers
> *Objet :* Java Programming Basics****
>
> ** **
>
> I've been noticing code recently that makes it clear the author was a
> C/C++ programmer unfamiliar with Java. Whenever I see these things, I am
> fixing them on the spot:****
>
> ** **
>
> (1) initializers are not required for class or instance members when their
> initial values are null, false, 0, etc;****
>
> ** **
>
> e.g.****
>
> ** **
>
> public class Foo {****
>
>   private Bar bar = null;****
>
>   private boolean done = false;****
>
>   private int value = 0;****
>
> }****
>
> ** **
>
> should be written as:****
>
> ** **
>
> public class Foo {****
>
>   private Bar bar;****
>
>   private boolean done;****
>
>   private int value;****
>
> }****
>
> ** **
>
> (2) it is never necessary to invoke super() in a constructor, e.g.,
> super() in the following is redundant:****
>
> ** **
>
> public class Foo {****
>
>   public Foo() {****
>
>     super();****
>
>   }****
>
> }****
>
> ** **
>
> (3) it is never necessary to define a default constructor if there is no
> other defined constructor and it does nothing; e.g.,****
>
> ** **
>
> public class Foo {****
>
>   public Foo() {****
>
>   }****
>
> }****
>
> ** **
>
> should not define a default constructor; Java will always supply a default
> constructor if no other constructor is defined;****
>
> ** **
>
> (4) however, if you want to prevent the generation of a default, public
> constructor, then you can define a private no-argument constructor:****
>
> ** **
>
> public class CantInstantiate {****
>
>   private CantInstantiate() {****
>
>   }****
>
> }****
>

RE: Java Programming Basics

Posted by Dridi Seifeddine <sd...@iptech-group.com>.
Who is that author? I hope you’re not referring to me Glenn. Well I came from a C/C++ background, and I confess that my Java knowledge is a little bit rusty but I’m always learning, faster than you may expect :)

 

If you look at my latest patch for whitespace management, you’ll see that I never do these mistakes.

 

All the best.

 

Seifeddine

 

P.S. Ignore this email if it was destined to someone else.

 

De : Glenn Adams [mailto:glenn@skynav.com] 
Envoyé : mardi 29 octobre 2013 15:02
À : FOP Developers
Objet : Java Programming Basics

 

I've been noticing code recently that makes it clear the author was a C/C++ programmer unfamiliar with Java. Whenever I see these things, I am fixing them on the spot:

 

(1) initializers are not required for class or instance members when their initial values are null, false, 0, etc;

 

e.g.

 

public class Foo {

  private Bar bar = null;

  private boolean done = false;

  private int value = 0;

}

 

should be written as:

 

public class Foo {

  private Bar bar;

  private boolean done;

  private int value;

}

 

(2) it is never necessary to invoke super() in a constructor, e.g., super() in the following is redundant:

 

public class Foo {

  public Foo() {

    super();

  }

}

 

(3) it is never necessary to define a default constructor if there is no other defined constructor and it does nothing; e.g.,

 

public class Foo {

  public Foo() {

  }

}

 

should not define a default constructor; Java will always supply a default constructor if no other constructor is defined;

 

(4) however, if you want to prevent the generation of a default, public constructor, then you can define a private no-argument constructor:

 

public class CantInstantiate {

  private CantInstantiate() {

  }

}