SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Mar 2009
    Location
    new jersey
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cut + paste vs. from scratch...

    Hi, just a newbie with a couple of simple questions.

    I'm teaching myself Javascript (years and years of HTML/CSS) and I'm struggling with learning to write the code. I can read and interpret okay, in other words if you hand me a script I can follow along and tell you what it's doing and why, but actually sitting down and writing fresh code is another story.

    So, I'm just wondering how much in your coding practice do you cut and paste from old scripts and how much is written from scratch? Does it depend on the project? Are there some things (like rollovers) that it would be stupid to sit and recode every time you want to use them?

    I know with HTML, folk very rarely code the main structure (html,head, body) from scratch. Is Javascript similar? I'm not talking about being dependent on a library here, but just writing individual scripts.

    And for clarification, I'm not looking for the lazy way out, just trying to find a way to get myself jumpstarted and moving forward.

    Thanks!

  2. #2
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would start by deciding where your knowledge is lacking, if anywhere... Is it the JavaScript language or the DOM API which is preventing the step forward?

    In either case, it may be beneficial to start by setting yourself small challenges.

    In answer to your first question, yes, most people just cut and paste and use helper libraries or frameworks to get the work done but doing that is never a good idea when starting out. The first step is understanding the code, the next step is reproducing the functionality with your own flavour and then improving upon it.

    An example challenge:
    ---
    Go to your most visited website, try and think of ways to enhance it, if your were the web developer behind it...

    Next, create that enhancement using JavaScript and place it in a GreaseMonkey script so you can share it and benefit from it yourself.

    For example, you might not like the fact that Sitepoint shows advertisements on all pages - you could write some JavaScript to get rid of them... Just an idea...
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  3. #3
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    Hi littleoracle, welcome to the forums.

    When I first started learning javascript years ago, I had a list of element properties and a few "beginners" books on javascript. My first experiments were copy/paste of various "stupid javascript tricks" just to get an idea of what was going on and how different things worked. One drawback to doing this was that I also "learned" some bad practices that were in the code.

    I then began trying to change various scripts and add code to them in a kind of
    test - modify - test - fix - test - modify - test - fix - test - modify - test ......
    cycle until my understanding improved. With time some things became "natural" and I didn't need to consult the references for them.

    After a while I had quite a collection of working code examples (and a larger one of buggy code) and if I needed to write something new that was similar to one of them or incorporated one of them in it, I would copy/paste the working code rather than write it again from scratch.

    But this was before there were javascript libraries. Now there a so many good libraries available it makes sense to learn how to work with a library and use it rather than write your own functions. In any case, as JimmyP mentioned, it is important that you have a working knowledge of javascript and understand what the code is doing before you use them.
    Last edited by Mittineague; Apr 19, 2009 at 11:16. Reason: typo

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    This reminds me, I have a backlog of code saved away from when solving problems on these forums, that may well benefit from being blogged about.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    About the only times I cut and paste is when I have some code that I have already written that I can reuse in the new script.

    Anthing where you can create the entire script just using copy and paste the script will either be performing a trivial task or will many times longer than it really needs to be and much harder to understand.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    This I suppose is where principles of object oriented programming can be useful, in allowing greater reuse of code.

    Some good examples of reusable code are:
    css classes: http://snipplr.com/view/3561/addclas...lass-hasclass/
    cookies: http://www.quirksmode.org/js/cookies.html
    Array.forEach: https://developer.mozilla.org/en/Cor.../Array/forEach
    bulletproof ajax: http://bulletproofajax.com/code/
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Member
    Join Date
    Mar 2009
    Location
    new jersey
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you all for your responses! this has been very helpful and super informative.

    thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •