Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re (tilly) 1: Trig in Perl

by tilly (Archbishop)
on Sep 04, 2001 at 16:27 UTC ( [id://110018]=note: print w/replies, xml ) Need Help??


in reply to Trig in Perl

I don't understand this attitude.

You know that we are going to advise you to use strict.pm. But you don't because you don't think you had time, and you don't think that it is really worth it with all of your code. Yet you think that we should take out the time to analyze and fix your code for you.

That is exactly backwards.

The times to not use strict.pm are when you are writing so little code that you don't need its assistance, or for short blocks where you don't want what it does for very specific reasons. Otherwise avoiding it leads to your taking more time. Furthermore the more code you write, the worse it is.

Beyond that my advice is standard. Indent properly. Think about your problem, try to find common logic so that you don't have to have large sections of similar code. (For instance don't have 3 sets of subroutines depending on which angle you are working on, instead relabel.) Avoid global variables. Etc.

Replies are listed 'Best First'.
Re: Re (tilly)1: Trig in Perl
by dr_lambado (Hermit) on Sep 04, 2001 at 21:01 UTC
    <html> <body>

    Just a few little points, this is not meant as flamebait, take it as you like, it's just some little things that I've noticed in the past 3 months of programming with perl....
    There seems to be something going on in the perl community (here, #perl, #perlhelp, and c.l.p) about the use strict; pragma. Fair enough if you want to do it if you want to distribute you code to be modified among others this is necessary, I see a use for it. And I take your point about if I want people to help with my code I should have used strict, fair enough. But isn't perl's slogan "There's more than one way to do it"? It seems silly to use it when I only had a half-dozen global variables.
    As for the indent thing, what do you mean? I have never read anything about this before. If you're not mad at me about the above a link would be nice so I can format the code :)
    One side note, I haven't actually read much about perl, I don't know that much, I learnt quite a lot just from reading the perldoc pages and working it out. Apart from that all I read is about a 6th of the Camel Book which I invested in a few weeks ago. The point I'm trying to make is I would apreciate you not going down my neck about not using a sepcific pragma :)
    Apart from that, thanks for the help :))

    Dr Lambado
    Btw, one day I will work out how to use strict; and then I will post code, but if I get this reception everytime I post I won't bother


    </body></html>
      About strict. What I was reacting to was your saying you knew about it but couldn't be bothered to use it. If you know about it, know that it is supposed to help, don't use it because you "can't be bothered", and then want help, that is irritating. If you don't know about it, different story. If you don't know how to use it, we should solve that.

      Had you said you hadn't figured out how to use it, a dozen people would have given you clear instructions. Had you not mentioned it, people would assume you didn't know about it and tell you about it.

      Let me tell you about strict.pm. All that you do is this. In the first 5 lines of the program put:

      use strict; use vars qw( );
      Then proceed to write your program, declaring variables with my as you did. The small number of global variables you have you put into the list you are feeding vars. Like this:
      use vars qw( $thing @list %lookup );
      Now as you develop Perl will be able to spot many typos for you.

      When should you do this? Well not based on the number of globals in your program (many of mine have no globals at all). Instead it depends on how many lines of code you might accidentally create a global with a typo. A decent rule of thumb is 20 lines. If your program goes over 20 lines, put strict in.

      Now why are people so quick to say this? Well TIMTOWTDI, but not all ways are created equal. Through long experience, most of us have learned that fact...

      Now about indents. The idea is simple. The indentation on each line should indicate scope. Every time you open a new function, if statement, etc, indent farther. That way just glancing at your code will show the intended logical structure. This makes code much easier to scan, analyze, and makes it possible to figure out many typos. To get an idea what it should look like you can download perltidy and run it on your code.

      Trust me. It makes a huge difference.

      The value of consistent formatting is not specific to Perl. It applies in all programming languages. An excellent place to learn about programming details like this is the often recommended Code Complete by Steve McConnell. Its examples tend to be in C and Pascal, but the advice is timeless.

      But isn't perl's slogan "There's more than one way to do it"?

      Indeed it is. You recognized that your way wasn't the best way, and you asked for advice. If you recognize that lots of people really know how to program Perl well, that those same people recommend strict, and if you ask those people for advice, why in the world wouldn't you take their advice?

      Suppose you misspelled just one of your variables in the middle of sub_b_sin(). How would you catch that? With strict, it's easy.

      You're getting free advice from people who had to learn Perl the hard way, often before there were such niceties as the Monastery and comp.lang.perl.misc. If you complain about it, expect the price to rise.

      I'll cut short this lecture and help you with the indenting issue. Have a read through perldoc perlstyle. Then take a look at perltidy. It's a wonderful program that nicely formats source code. With a little experience, you'll find that good indentation really contributes to the readability of a program.

      Good luck.

        <html><body>

        Thanks for that, I'll try those links out and perltidy seems to be an interesting program. Also, apologies if I sounded a little blunt or impolite with my last post. I appreciate that you dont have to help me and I am grateful of any help that you do give.
        Dr Lambado

        </body></html>
      There's nothing to "work out". It's very easy. Just add:
      use strict; use warnings;
      as the first lines in your program. That's it. Now sit back as it finds all your typo's for you, and warns about fishy things happening as it runs! Isn't that great? Isn't that what you were asking us for in the first place?

      I think you get the wrong idea of our "attitude" because you don't know what "strict" is for. Your initial comment about "not worth it because of all the subs" also makes me thing that.

      As for your formatting question, the concept is pretty basic: subordinate things are indented farther than its controlling logic or framing structures. Anything else is details and subject to personal taste. Just be consistant, and follow that guiding principle, and you'll be fine.

      Yes, you will get the reception "use strict, then ask again after fixing everything it finds for you" if you post code that doesn't. Every time.

      Good luck, and keep trying.

      —John

      Yes, TIMTOWDI, but do not forget: perl's concept of freedom is to give you enough rope to hang yourself.

      Look, many experienced members of our community can see that you are going to hang yourself, and they ask you friendly is this is your real intention, or just misunderstanding. And what are you doing? Your answer is: "No time to free my neck out of loop, too busy make loop fit and tightening it"...;-) ... Should we stop you? We are trying...;-)

      I am rather experienced programmer in other languages -- not in perl though... :( When I learned that perl by default will autodefine variables for me (before I learned about use strict I was scared, and I actively looked for -w and use strict from my first day of reading Camel book, because I know too well how easy is to misspell variable name. You are not afraid yet - because you did not spent hours staring at code looking for an error, and then another hour banging your head aginst wall how stupid you were to make this stupid mistake. No problem, they are coming your way...;-)

      pmas
      To make errors is human. But to make million errors per second, you need a computer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-03-28 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found