Jump to content


Photo

Counting Lines of code


  • Please log in to reply
16 replies to this topic

#1 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 10 March 2006 - 08:27 PM

Does anyone know of a tool that could count the number of lines of code in a project or just in a bunch of files on a server?

I've been working on a number of applications for work that are all web-based and I'm just curious. For example, one of my backend ajax files is almost 900 lines of code by itself. If I had to guess, I'd say I have close to 20,000 - 30,000 lines of code in all the various applications. And I've written 99% of it myself (I'd say .2% is javascript I've found elsewhere and another .8% is PHP that I picked up on here or other places.

Info: PHP Manual


#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 10 March 2006 - 08:33 PM

Not exactly a tool, but with a short script you could read all .php files in a folder, read each file into an array, count array size, add to total, do next file. A few lines of code should be able to handle that.
Legend has it that reading the manual never killed anyone.
My site

#3 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 10 March 2006 - 09:04 PM

That's kinda what I thought about doing... I'll have to play around with it.

Info: PHP Manual


#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 10 March 2006 - 09:49 PM

Quick and dirty in the folder where you want to count:
<?php
$lines = 0;
foreach (glob("*.php") as $filename) {
    $thefile = file($filename);
    $lines+= count($thefile);
}
echo "Lines of code: ". $lines;
?>

Legend has it that reading the manual never killed anyone.
My site

#5 Guest_askjames01_*

Guest_askjames01_*
  • Guests

Posted 12 March 2006 - 12:36 PM

How about TextPad ober?
It will make your life easier i think? :)

[a href=\"http://www.textpad.com/\" target=\"_blank\"]http://www.textpad.com/[/a]

it has Document Select for multiple files... and it can count lines of codes... and it's FREE.


try it!
and GOOD LUCK!


-/james

#6 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 13 March 2006 - 02:37 PM

Apparently I underestimated myself. Using Andy's code, I'm showing 44,167 lines of code. And that's only the top level of each application, but it also includes a little over 2000 lines that I didn't write. So if it all balances out, I'd say I've written at least 43000 lines of PHP/JS/HTML in the past year and a half.

Crazy.

Info: PHP Manual


#7 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 13 March 2006 - 07:41 PM

[!--quoteo(post=354505:date=Mar 13 2006, 02:37 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Mar 13 2006, 02:37 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Apparently I underestimated myself. Using Andy's code, I'm showing 44,167 lines of code. And that's only the top level of each application, but it also includes a little over 2000 lines that I didn't write. So if it all balances out, I'd say I've written at least 43000 lines of PHP/JS/HTML in the past year and a half.

Crazy.
[/quote]

who at the zend offices decides to make a function called 'glob'? lol lovely, but andy - thank you, i've been looking for something like that for another little project i'm testing out.

ober, my guess is that if you want a more accurate guess, then unless you neve have blank lines between your lines of code, it maybe a good idea to do a check for lines that are literally just empty.
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#8 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 13 March 2006 - 08:20 PM

I don't have many blank lines, and I think it'd be hard to differentiate between blank lines and comment lines.

And I'd guess that if you took both of those out, I'd still be above 40,000.

Alright... I had to know:

$lines = 0;
foreach (glob("*.php") as $filename) {
    $thefile = file($filename);
    foreach($thefile as $key => $val){
        if(trim($val) != '')
            $lines++;
    }
}
echo "Lines of code: ". $lines;

That takes one of my biggest applications from 9474 lines of code to 8630, about an 8.5% drop, which as I guessed, still leaves me about 40000 lines if the ratio is similar in the other apps. Keep in mind that this doesn't remove comment lines.

Info: PHP Manual


#9 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 14 March 2006 - 09:03 AM

[!--quoteo(post=354639:date=Mar 13 2006, 08:20 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Mar 13 2006, 08:20 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Alright... I had to know:
[/quote]
lol curiosity always wins in the end...
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#10 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 16 March 2006 - 08:42 PM

Aahhh, why not check for commented lines too while you're at it...
$lines = 0;
foreach (glob("*.php") as $filename) {
    $thefile = file($filename);
    foreach($thefile as $key => $val){
        if(trim($val) != '' || substr(trim($val),0,2) != "//")
            $lines++;
    }
}
echo "Pretty accurate estimate lines of code: ". $lines;


#11 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 16 March 2006 - 08:50 PM

Your || should be an &&.

Well let's just go full out and list both ;-)

$comment_lines = 0;
$code_lines = 0;
foreach (glob("*.php") as $filename) {
    $thefile = file($filename);
    foreach($thefile as $key => $val){
        if(trim($val) != '' && substr(trim($val),0,2) != "//" && substr(trim($val),0,2) != "/*")
            $code_lines++;
        if(trim($val) != '' && (substr(trim($val),0,2) == "//" || substr(trim($val),0,2) == "/*"))
            $comment_lines++;
    }
}
echo "Lines of code: $code_lines<br/>";
echo "Lines of comments: $comment_lines<br/>";

And we could get more tricky with it... look for lines that have series of "========", "---------", etc... and you could single out full comment blocks as well, looking for the beginning and ending /* */

Crazy.

Info: PHP Manual


#12 TuxToaster

TuxToaster
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 11 April 2006 - 09:30 PM

Here's my updated version, that checks for comment blocks... I'm sure there's probably a better way to do this, but none that I could think of at the moment. It's a quick hack anyways ;-)

<?php
$comment_lines = 0;
$code_lines = 0;
$in_comment = false;
foreach (glob("*.php") as $filename) {
    $thefile = file($filename);
    foreach($thefile as $key => $val){
        if (trim($val) != '') {
            if ($in_comment) {
                if (trim($val) != "*/") {
                    $comment_lines++;
                }else{
                    $comment_lines++;
                    $in_comment = false;
                }
            }else{
                if (substr(trim($val),0,2) == "//") {
                    $comment_lines++;
                }else if (substr(trim($val),0,2) == "/*") {
                    $comment_lines++;
                    if (substr(trim($val), -2) != "*/") {
                        $in_comment = true;
                    }
                }else{
                    $code_lines++;
                }
            }
        }
    }
}
echo "Lines of code: $code_lines\n";
echo "Lines of comments: $comment_lines\n";
?>


#13 TuxToaster

TuxToaster
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 11 April 2006 - 09:55 PM

Ok, one more fix, this one includes checks to see how much code is between <?php and ?> tags... again there's probably a better or more accurate way, but this at least gives a rough idea. Used it because the project I'm doing includes alot of HTML outside the tags and I don't have time yet to get it separated like I'd like to. It also totals everything up at the end.

Here it is:

<?php
// Code Counter
$comment_lines = 0;
$code_lines = 0;
$other_lines = 0;
$total_lines = 0;
$in_comment = false;
foreach (glob("*.php") as $filename) {
    $thefile = file($filename);
    foreach($thefile as $key => $val){
        if (trim($val) != '') {
            if (preg_match('/\<\?php/', trim($val))) {
                $in_php = true;
            }
            if ($in_php) {
                if ($in_comment) {
                    if (trim($val) != "*/") {
                        $comment_lines++;
                    }else{
                        $comment_lines++;
                        $in_comment = false;
                    }
                }else{
                    if (substr(trim($val),0,2) == "//") {
                        $comment_lines++;
                    }else if (substr(trim($val),0,2) == "/*") {
                        $comment_lines++;
                        if (substr(trim($val), -2) != "*/") {
                            $in_comment = true;
                        }
                    }else{
                        $code_lines++;
                    }
                }
            }else{
                $other_lines++;
            }
            if (preg_match('/\?\>/', trim($val))) {
                $in_php = false;
            }
        }
    }
}
echo "Lines of PHP code: $code_lines\n";
echo "Lines of other code: $other_lines\n";
echo "Lines of comments: $comment_lines\n";
$total_lines = $code_lines + $other_lines;
echo "---------------------------------------------\n";
echo "Total Lines of code: $total_lines\n";
$total_lines += $comment_lines;
echo "Total Lines(including comments): $total_lines\n";
?>


#14 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 12 April 2006 - 05:35 PM

Awesome work!

Info: PHP Manual


#15 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 12 April 2006 - 05:48 PM

[!--quoteo(post=364124:date=Apr 12 2006, 01:35 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Apr 12 2006, 01:35 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Awesome work!
[/quote]


i just realized that with NONE of the above are you checking for comments starting with "#"... just figured i'd throw that into the mix ;-)

sorry, couldn't resist

now, for the trick i'm working on... make this recursive to read all the sub-folders and keep a log of each folder as well as a running total... that would be awesome

one other problem i just realized... i use quick echoes quite often ("<?= ?>"), and those as well as straight "<?" tags won't be recognized by the above code... something else to keep in mind.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#16 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 12 April 2006 - 05:56 PM

I happy to see everyone working at turning one little glob into the All_Powerful_Code_Counting_Tool :)
Legend has it that reading the manual never killed anyone.
My site

#17 ypirc

ypirc
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 16 April 2006 - 10:02 PM

Also, to just count lines of numerous files of any type in a directory, you might also want to check out the *nix command "wc".

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
%wc -l /etc/passwd /etc/services
37 /etc/passwd
2114 /etc/services
2151 total
[/quote]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users