Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: OO method wont parse

by talexb (Chancellor)
on Aug 28, 2017 at 20:52 UTC ( [id://1198208]=note: print w/replies, xml ) Need Help??


in reply to OO method wont parse

    my $class; my $sql; my @bindparms; my $Rowcount; my $Status; my $cntl; my $db; my $start; my $end; my $posn; my $SQLStr; my $host; my $Properties; @bindparms = $_; $class = shift @bindparms; $cntl = shift @bindparms; $sql = shift @bindparms;

A couple of stylistic points, from the above sub-set of your code.

  • It's neater to declare variables in a list, so as to make better use of space. So, my ( $class, $sql, .. ); is much cleaner than what you have, which is one variable declaration per line. I would probably group related variables together -- they don't all have to be on a single line.
  • You don't have to group variable declarations all at the beginning of a block, as we used to do in C. (I believe that was because they needed to be allocated on the stack by the compiler.) Declare variables when you're about to use them.
  • I won't comment on the choice of camelCase versus snake_case, but it is a good idea to be consistent. You've got some variable names that are lower case, and some that are Capital case (initial capital letter, followed by lower case).
  • You set @bindparams to @_, then use shift to initialize the three variables $class, $cntl and $sql. It's probably easier and cleaner just to say my ( $class, $cntl, $sql ) = @_;
  • Whenever you're passing arguments into a method, you should do validation on the arguments. At the very least, check each input to find out a) Is it defined? and b) Does it have a reasonable value? Only move forward if the input arguments are absolutely fine; otherwise, output a useful error message and return (or die/croak, whichever is appropriate).

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Replies are listed 'Best First'.
Re^2: OO method wont parse
by afoken (Chancellor) on Aug 29, 2017 at 05:53 UTC
    You don't have to group variable declarations all at the beginning of a block, as we used to do in C. (I believe that was because they needed to be allocated on the stack by the compiler.) Declare variables when you're about to use them.

    Just a little bit of bean counting: C99 allows to declare variables where you need them. Today's C compilers have become smart enough. Here's a silly example:

    /tmp>cat c99.c #include <stdio.h> #include <string.h> int main(int argc, char ** argv) { char buf1[10]; snprintf(buf1,sizeof(buf1),"%d",argc); int n=0; for (int i=0; i<argc; i++) { int len=strlen(argv[i]); len++; n+=len; } char buf2[10]; snprintf(buf2,sizeof(buf2),"%d",n); char buf3[80]; snprintf(buf3,sizeof(buf3),"%s args using %s bytes",buf1,buf2) +; puts(buf3); return 0; } /tmp>CFLAGS="-std=c99 -pedantic -Wall" make c99 cc -std=c99 -pedantic -Wall c99.c -o c99 /tmp>./c99 foo bar baz 4 args using 18 bytes /tmp>

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Thanks -- my experience in this matter comes from looking at the output from a VMS cross-compiler that generated 68000 assembler in the mid-80's. I imagined that this shortcoming would have been solved since then. :)

      Alex / talexb / Toronto

      Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

        my experience in this matter comes from looking at the output from a VMS cross-compiler that generated 68000 assembler in the mid-80's. I imagined that this shortcoming would have been solved since then

        I use C99 in my daily job, since about two years. C99 is an improvement over C89 (ANSI C), yes. And it looks like C11 improves even more. (C11 is not supported by our legacy build environment.) But my wish list for a better C standard becomes longer every day. It's hard to use C after using Perl for years. You get used to have a lot of stuff done automatically for you, and being forced to do that manually again is annoying. Especially when you know that the computer could do that for you, if only the compiler and the runtime environment were a little bit smarter.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1198208]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-25 10:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found