Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

A Quesiton of Numbers

by FoncÚ (Scribe)
on Jul 10, 2002 at 12:55 UTC ( #180736=perlmeditation: print w/replies, xml ) Need Help??

This is something I've struggled with for a while now...the question of line numbers. Having gotten my foundations in BASIC programming using BASICA and GWBASIC, I'm quite accustomed to using line numbers out of necessity. Then, in later schooling, we were granted the miracle that is QBASIC, and were forced by the teachers to not use line numbers at all (and eliminate all GOTOs, more spaghetti code for us). Now that I'm learning Perl (and absolutely loving it), I am in a quandry over line numbers.

Many server log error messages will give a line number and nothing else to work with. Also, line numbers keep things organized. However, most people don't use that, at least, that I've seen (I saw a couple that did, hence why I ask), so I wonder...are they passÚ now? What are any benefits from them, and what are the disadvantages?

Just a theoretical question for you.

Oh...and thanks for all your help thusfar. I appreciate it!

Edited: ~Wed Jul 10 14:18:58 2002 (GMT), by footpad:
Added <P> tags to indicate author's original formatting.

Replies are listed 'Best First'.
Re: A Quesiton of Numbers
by Abigail-II (Bishop) on Jul 10, 2002 at 13:15 UTC
    Line numbers make your code far less flexible.

    If your want to insert a piece of code, you will have to renumber all your lines. Sure, you might number with increments of 10, but then, what if you want to insert 11 lines of code? Or first you insert 10 lines, then later on, between that, another 10?

    Also, what are you going to do with statements that are longer than a line? Can you just arbitrary put numbers in those statements? It gets worse with strings that span multiple lines. Do they need to be numbered?

    The bottom line is, there is absolutely no reason to number the lines of your code. If you find it handy to have line numbers while coding, use power editor like vi that will allow you to view line numbers.

    Note that you can use line numbers in Perl. Or at least, sort of:

    L0: print "Begin of program.\n"; L1: print "Hello, world.\n"; L2: ;# Some comment. L3: print "End of program.\n";
    Begin of program.
    Hello, world.
    End of program.


      Well, that settles that. I wasn't necessarily planning on programming by numbers, but I wanted to gather thoughts on doing so. Fortunately, I've been working for a long time without line numbers, so I'll be just fine there.

      As for a language that should be used for beginning said above, BASIC isn't bad, but it isn't good, either. Perhaps Java would be a good way to start out. The school I attended has since dropped BASIC and gone to C, which I think is a mistake (due to complexity).

      You never know what you'll learn from asking a question like this :)
Re: A Quesiton of Numbers
by RMGir (Prior) on Jul 10, 2002 at 13:19 UTC
    You're right, line numbers have a limited utility.

    In fact, they're even more limited than you'd think in perl, since in some cases, the error message points to the wrong line.

    But they're still a very good way to know where the bug was, as long as you still have the exact version of the script that was running by the time you see the error in your logs. (That's one of the reasons version control is so important...)

    For more useful, but more verbose error messages, you might want to use the cluck and confess calls from the Carp module. They give you a full backtrace for where you warned/died, which might be more robust than just the line number...

Re: A Quesiton of Numbers
by ignatz (Vicar) on Jul 10, 2002 at 14:29 UTC
    I think that teaching beginners how to program with BASIC is not such a good thing. I remember how totally confused I was back in high school when I first made the transition from using line numbers to programming in Pascal. It was a nice epiphany once I finally figured that they just got in the way. Why not learn good programming techniques right off the bat? Of course, that was a long time ago. Maybe schools don't teach that way any more.
      Should we start them on C++ or Java instead? Don't laugh, I've seen it done! As wrong as BASIC is I think it makes a pretty good starter language. Can you suggest any reasonable alternatives?


        A reasonable alternative for BASIC? No, I don't think there is. Nor do I think BASIC is reasonable. But I do think there are good candidates for good starter languages: Python, Modula, Haskell, Java, and probably Eifel as well. Not BASIC, not C, not C++ and certainly not Perl.


Re: A Quesiton of Numbers
by ichimunki (Priest) on Jul 10, 2002 at 15:12 UTC
    Line numbers serve some purposes, both as labels and as reference points for errors. Fortunately, we don't need to hardcode the numbers in anymore to get either of these benefits. Perl has labels to put on blocks, should you want one for a goto or whatever. Any decent text editor (well, since I only use emacs, this is my yardstick for decent) should be able to tell you what line the cursor is on-- allowing you to find the point at which the error occurred.

    As others have said, line numbering is quite annoying if you need to reorder or refactor your code, since changing them (and only them) is probably necessary. In fact, I'm now having flashbacks to programming my C-64. Thanks a lot! ;)

Re: A Quesiton of Numbers
by FoxtrotUniform (Prior) on Jul 10, 2002 at 15:31 UTC

    I would hope that explicit (label) line numbers are passé. That said, I generally edit with line numbers displayed (set nu in vi and friends); it makes moving quickly around the screen very easy, and gives you another, very visible indication of how long your (block|function|code) is getting.

    The hell with paco, vote for Erudil!

Re: A Quesiton of Numbers
by jryan (Vicar) on Jul 11, 2002 at 02:14 UTC

    Of course, you can always reindex and (label) your line numbers using the line pragma. For instance:

    #!perl -w use strict; # after the next line, error messages will be indexed # starting at line 300 and contain the phrase "marked_point" #line 300 marked_point my $some_var; $some_var = "some_value"; blah;

    Which yields:

    Bareword "blah" not allowed while "strict subs" in use at marked_point + line 302.

    This is pretty handy from a debugging standpoint during development. For instance, you could give each major block of code its own series; such as, main code gets lines 0-999, and each subroutine gets a 1000 line block and a label. So if you got an error message like:

    Missing right curly or square bracket at subroutine_save_report line 3 +010, at end of line

    You'd know exactly where to look. Best to take them out line labeling when code goes production, however...

Re: A Quesiton of Numbers
by Aristotle (Chancellor) on Jul 11, 2002 at 21:32 UTC

    While you do make valid arguments in favour of line numbers, none of them mandate that the line numbers be in the file. A good editor alone will address your needs.

    Take vi: :set number shows line numbers, and either :10 or 10G will take you to the 10th line of the file. Any other decent editor will offer equivalents.

    There is no reason to have line numbers in the source.

    Makeshifts last the longest.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://180736]
Approved by footpad
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2023-03-27 10:34 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (65 votes). Check out past polls.