How many lines of code do you have?

Written on 2 December 2011, 05:41pm

Tagged with:

Do you want to know how many lines of code you have in your application? Challenge accepted πŸ™‚
Here’s a 10-minutes, 50-lines ColdFusion script that counts the number of lines in all the files from a given folder.

<cfsetting requesttimeout="300" showdebugoutput="no" />
<cfset initialDir = ExpandPath( '../' )>
<cfset files = 0> <!-- we will store the number of files that we traversed --->
<cfset lines = 0> <!--- this is what we need  --->
<cfset localRoot = "E:\your\input\folder" />
<cfset separator = '\'>
<cfset extensions = 'cfm,cfc,htm,html,css,js'> <!-- add here the list of extensions --->

<cfset tick = GetTickCount()> <!--- start the clock --->

<!--- get list of ALL files --->
<cfdirectory action="list" directory="#initialDir#" name="localQuery" recurse="true"/>

<cfoutput query="localQuery">
	<cfif type EQ 'File'>
		<cfset extension = LCase(listLast(name,".")) /> <!--- get the extension --->
		<cfset temp = ListFindNoCase(extensions, extension)> 
		<cfif temp IS NOT 0> <!--- if the file extension is one in our initial list --->
			<cfset path = directory & separator &  name>
			<cfset files = files + 1>
			<cfif FileExists(path)>
				<cftry>
					<cffile action="read" file="#path#" variable="content" charset="utf-8">
				<cfcatch type="all">Error reading file</cfcatch>
				</cftry>
				<cfset content = replace(content,Chr(10),Chr(13),"ALL")/> <!--- replace all LFs with CRs --->
				<cfset content = replace(content,Chr(13)&Chr(13),Chr(13),"ALL")/> <!--- replace all double CRs with single CRs --->
				
				<!--- 
				...and here is most important part: 
				we remove ALL the other characters that are NOT CR (\r)
				we end up with a string containing ONLY the CRs
				we simply count the length of this string and then add 1
				--->
				<cfset thisFileLines = Len( REReplace( content, "[^\r]+", "", "ALL" ) ) />
				<cfset thisFileLines = thisFileLines + 1> 

				<cfset lines = lines + thisFileLines>
				File #path# has <strong>#thisFileLines#</strong> lines<br /> 
			</cfif>
		</cfif> <!--- end if extension --->
	</cfif><!--- end if type file --->
</cfoutput>

<cfset tock = GetTickCount()> <!--- stop the clock --->
<cfset time = round((tock-tick)/1000)>

<cfoutput><hr />There are <strong>#lines#</strong> lines in #files# files. Search took #time# seconds.</cfoutput>

I got:

There are 101637 lines in 527 files. Search took 3 seconds.

See also

Amazing things you can do in HTML5

Written on 24 November 2011, 07:21pm

Tagged with: , , ,

  1. manipulate the browser history and build awesome asynchronous user interfaces like this one or highly responsive and intuitive interfaces like this one
  2. know the user location and serve him customized content (remember that ‘date single women near [your location]’ ads? :P)
  3. store data in the user browser: more than 4k of data, stored on the client, that persists beyond a page refresh and isn’t transmitted to the server. For example to store the number of the last page read in an online book or to save the settings of an online game between browser restarts
  4. use new input types (like the slider, color picker, date picker, etc) and unload the validation task on the browser
  5. allow the user to take your app offline, edit it, and synchronize it when back online. Example: GMail offline
  6. Play video for everybody
  7. Use the canvas element to draw amazing 3D text or sticky things (try this link on your mobile device, and shake it :))

Yes, you can use all of these features plus many, many more right now. Most of them will gracefully degrade if the browser is too old or simply does not support them.
Now read the list above once again thinking about the implications on the mobile browsing.

Here are some interesting things I found from Dive into the HTML5 ebook:
Note: a couple of months ago, when I started reading this ebook, it was available under diveintohtml5.org. However, the author – Mark Pilgrim – is 410 Gone from web. However, his ‘Dive into …’ book were mirrored and they are still available.

 
  1. XHTML vs HTML parser
  2. Even if you declare your DOCTYPE as being XHTML, if you serve the page as Content-Type: text/html, it will be parsed by the HTML engine, not by the ‘draconic’ XHTML one. #

  3. Double negative to convert to Boolean #
  4. /* You can use the double-negative trick 
    to force the result to a Boolean value (true or false).*/
    return !!document.createElement('canvas').getContext;
    
  5. Skip ‘skip to content’ πŸ™‚
  6. No need to add ‘skip to content’ links at the beginning of your page if you are using HTML5’s nav element. The screen readers will handle it correctly. [#]

  7. Character encoding
  8. Web Server’s HTTP Header:

    Content-Type: text/html; charset="utf-8"

    Briefly, this says that the web server thinks it’s sending you an HTML document, and that it thinks the document uses the UTF-8 character encoding.

    <meta charset="utf-8" />
    or
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    Briefly, this says that the web author thinks they have authored an HTML document using the UTF-8 character encoding.

    Both of these techniques still work in HTML5. The HTTP header is the preferred method, and it overrides the meta tag if present. But not everyone can set HTTP headers, so the meta tag is still around. [#]

  9. Document outline
  10. You can have multiple h1 tags in the same page. [#]
    You can check your HTML5 page outline using this outliner.

  11. Use Modernizr
  12. Modernizr is a small JavaScript library that detects the availability of native implementations for next-generation web technologies, i.e. features that stem from the HTML5 and CSS3 specifications. Many of these features are already implemented in at least one major browser (most of them in two or more), and what Modernizr does is, very simply, tell you whether the current browser has this feature natively implemented or not.

    if (Modernizr.canvas) 
    if (Modernizr.canvastext) 
    if (Modernizr.video) 
    if (Modernizr.localstorage) 
    if (Modernizr.webworkers) 
    if (Modernizr.applicationcache) 
    if (Modernizr.geolocation) 
    if (Modernizr.inputtypes.date)
    if (Modernizr.input.placeholder) 
    if (Modernizr.input.autofocus) 
    if (Modernizr.history) 
    
    //Example video:
    if (Modernizr.video) {
      // let's play some video! but what kind?
      if (Modernizr.video.webm) {
        // try WebM
      } else if (Modernizr.video.ogg) {
        // try Ogg Theora + Vorbis in an Ogg container
      } else if (Modernizr.video.h264){
        // try H.264 video + AAC audio in an MP4 container
      }
    }
    

    More on the video element

  13. HTML5 canvas element
  14. It kind of looks like Flex without the Flash Builder πŸ™‚ To use it in older IE browsers as well, use ExploreCanvas [#]
    Here’s a nice application which lets you draw 3D sketches with animating strokes on HTML5 canvas.