What's wrong with this code?

Discussion in 'Java' started by Paul, Jun 28, 2004.

  1. Paul

    Paul Guest

    Considering all aspects like style, naming conventions, logic etc...
    Is this code OK? Please comment...

    public void ReadMyData(string myConnString)
    {
    string mySelectQuery = "SELECT OrderID, Customer FROM Orders";

    SqlConnection myConnection = new SqlConnection(myConnString);
    SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);

    myConnection.Open();

    SqlDataReader myReader = myCommand.ExecuteReader();

    while (myReader.Read())
    {
    Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
    }

    myReader.Close();
    myConnection.Close();
    }

    Thanks,
    Paul.
     
    Paul, Jun 28, 2004
    #1
    1. Advertising

  2. Paul

    Liz Guest

    "Paul" <> wrote in message
    news:...
    > Considering all aspects like style, naming conventions, logic etc...
    > Is this code OK? Please comment...
    >
    > public void ReadMyData(string myConnString)
    > {
    > string mySelectQuery = "SELECT OrderID, Customer FROM Orders";
    >
    > SqlConnection myConnection = new SqlConnection(myConnString);
    > SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
    >
    > myConnection.Open();
    >
    > SqlDataReader myReader = myCommand.ExecuteReader();
    >
    > while (myReader.Read())
    > {
    > Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
    > }
    >
    > myReader.Close();
    > myConnection.Close();
    > }
    >
    > Thanks,
    > Paul.


    I cut and pasted your offering and ran a tool "jcsc" on it. This tool
    is supposed to see if your code matches the Sun coding standards for java.
    Here is the output. Note that the NCSS = lines of code = 0, so it thinks
    what you have is not valid enough to count it.
    ---
    C:\tmp>jcsc test1.java
    Encountered "void" at line 1, column 8.
    Was expecting one of:
    "abstract" ...
    "interface" ...
    "public" ...
    "strictfp" ...
    "final" ...
    "class" ...

    File: test1.java

    Violations:

    test1.java:1:1:interface Declaration JavaDoc does not provide the required
    '@author' tag:TypeDeclarationAuthor:3
    test1.java:1:1:interface Declaration JavaDoc does not provide the required
    '@version' tag:TypeDeclarationVersion:3

    2 violation(s) found

    Metrics:


    Total NCSS count : 0
    Total Methods count : 0
     
    Liz, Jun 28, 2004
    #2
    1. Advertising

  3. Paul

    Sudsy Guest

    Paul wrote:
    > Considering all aspects like style, naming conventions, logic etc...
    > Is this code OK? Please comment...

    <snip>

    How are we to know? There are methods you haven't included so we
    don't know their signatures, specifically what exceptions can be
    thrown.
    Variable names are copacetic although the method names shouldn't
    have a leading capital letter. I prefer the K&R style of braces
    but this has always been a contentious issue.
    I'm not about to enter into a religious discussion on that topic...
     
    Sudsy, Jun 28, 2004
    #3
  4. Paul

    Roedy Green Guest

    On 27 Jun 2004 20:03:14 -0700, (Paul) wrote or
    quoted :

    >public void ReadMyData(string myConnString)
    >{
    > string mySelectQuery = "SELECT OrderID, Customer FROM Orders";
    >
    > SqlConnection myConnection = new SqlConnection(myConnString);
    > SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
    >
    > myConnection.Open();
    >
    > SqlDataReader myReader = myCommand.ExecuteReader();
    >
    > while (myReader.Read())
    > {
    > Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
    > }
    >
    > myReader.Close();
    >


    You could have discovered some of the errors with JavaC or better
    Jikes in pendant mode. See also http://mindprod.com/jgloss/lint.html

    The things that stand out for me is the violation of the caps
    conventions. See http://mindprod.com/jgloss/codingconventions.html

    You are writing "string" for "String" which is not only a sylistic
    error, but a syntax error as well.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jun 28, 2004
    #4
  5. Paul

    Roedy Green Guest

    On Mon, 28 Jun 2004 04:34:07 GMT, Roedy Green
    <> wrote or quoted :

    >You could have discovered some of the errors with JavaC or better
    >Jikes in pendant mode.


    rather "pedant" mode.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jun 28, 2004
    #5
  6. Paul wrote:

    > Considering all aspects like style, naming conventions, logic etc...
    > Is this code OK? Please comment...
    >
    > public void ReadMyData(string myConnString)
    > {
    > string mySelectQuery = "SELECT OrderID, Customer FROM Orders";
    >
    > SqlConnection myConnection = new SqlConnection(myConnString);
    > SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
    >
    > myConnection.Open();
    >
    > SqlDataReader myReader = myCommand.ExecuteReader();
    >
    > while (myReader.Read())
    > {
    > Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
    > }
    >
    > myReader.Close();
    > myConnection.Close();
    > }
    >
    > Thanks,
    > Paul.


    Why are you posting C# code on a Java newsgroup? They don't have the same
    naming conventions anyway.

    --
    Kind regards,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, Jun 28, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    3
    Views:
    2,254
    Ralf Hildebrandt
    Sep 10, 2003
  2. willem oosthuizen

    What is wrong with the following code?

    willem oosthuizen, Oct 10, 2003, in forum: VHDL
    Replies:
    9
    Views:
    1,340
  3. Matthew
    Replies:
    7
    Views:
    811
    Priscilla Walmsley
    Jan 7, 2005
  4. David. E. Goble
    Replies:
    9
    Views:
    499
    David. E. Goble
    Feb 2, 2005
  5. kiran
    Replies:
    12
    Views:
    1,201
    Scott Sauyet
    Dec 7, 2011
Loading...

Share This Page