Video: Splitting code into different files

We can end up with hundreds or thousands of lines of code very quickly. And although we will be splitting them up into functions, they can still get difficult to read and navigate. Another way to make your code more manageable-- and this happens in every programming language-- is we stop trying to keep it all in one file and we just split it up into several files. Now you wouldn't do this randomly. You wouldn't have half a function in one file and half in the other. But you might group several functions together in one file and several more in another. This also makes it easier for multiple people to work on the same project by working on separate files.

This course provides the core knowledge to begin programming in any language. Simon Allardice uses JavaScript to explore the core syntax of a programming language, and shows how to write and execute your first application and understand what's going on under the hood. The course covers creating small programs to explore conditions, loops, variables, and expressions; working with different kinds of data and seeing how they affect memory; writing modular code; and how to debug, all using different approaches to constructing software applications.

Finally, the course compares how code is written in several different languages, the libraries and frameworks that have grown around them, and the reasons to choose each one.

Splitting code into different files

We can end up with hundreds or thousands of lines of code very quickly.And although we will be splitting them up into functions, they can still getdifficult to read and navigate.Another way to make your code more manageable-- and this happens in everyprogramming language--is we stop trying to keep it all in one file and we just split it upinto several files.Now you wouldn't do this randomly.You wouldn't have half a function in one file and half in the other.But you might group several functions together in one file and several more in another.This also makes it easier for multiple people to work on the same project byworking on separate files.

And in complex applications built by teams of software developers, it's notunusual to have hundreds or thousands of separate code files for one project.Now JavaScript isn't a language that you would want to split up into hundredsor thousands of files.It's unusual to have more than a handful.The question is if you do split it up, how does the web page know where to find everything?But we have seen that the way that we tell our HTML page where our JavaScript is, is we use a script tag.This is what we've been using all along and if we split our JavaScript intomultiple files we simply add more script tags. That's it.

The JavaScript engine, the interpreter that's built into the browser, will loadall of these and run the code inside all of these files.Now because the JavaScript engine will try and run your code as soon as it getsits hands on it, order can be important here.So let's say we are linking to three separate JavaScript files, script.js,morefunctions.js, and functions.js.Let's say we have a bunch of really important functions that are going to becalled by some code in our script.js file.

Well, by default what's going to happen is the JavaScript engine is going to tryand run the code in script.js as soon as it loads it.If it's looking for functions that it hasn't gone to yet we are going to have a problem.So order is important.Just be aware of any dependencies that you might be creating and whether you'veloaded all the code you need to load.To make sure those important functions are loaded into the interpreter first.Now having said that for us, this won't be an issue.In this course, it's perfectly acceptable to keep all the things in one script file.

But for future reference, and particularly with other languages, you will seethe code that you work with typically split up into different files to makeit easier to work on.

Find answers to the most frequently asked questions about Foundations of Programming: Fundamentals .

Here are the FAQs that matched your search "" :

Expand all | Collapse all

please wait ...

Q: Using TextEdit with Mac OS 10.9 Mavericks?

A: If you're using the built-in TextEdit program in Mavericks to write your first examples and your code doesn't seem to be working, here's one reason why: by default, "smart quotes" are now turned on in TextEdit Preferences.

This is where TextEdit will automatically change pairs of double quotes to "smart quotes" - where the opening and closing quote are different, like a 66 and 99.

While this is fine for human eyes, programming languages don't want this - when writing code, they need to be the plain, generic straight-up-and-down quotes.

So make sure that in TextEdit > Preferences, that "Smart quotes" are unchecked.

Important! Whenever you make a change to TextEdit preferences, make sure to then completely quit out of the program (Command-Q or using TextEdit > Quit TextEdit) and then re-open it, as changes won't take effect on documents you already have open.

However, we're not finished - just because you've changed the preferences, it does **not** change any *existing* smart quotes back to "regular" quotes - it just doesn't add new ones - so make sure to go through your files for any time you wrote quotes and TextEdit may have changed them to smart quotes - look in both the JavaScript, and your HTML too, and compare to the downloadable exercise files if necessary.

If that sounds like a bit of a chore, I recommend just downloading a code editor like Sublime Text (www.sublimetext.com) or TextMate (www.macromates.com) and using that instead of TextEdit - it's only a matter of time before you'd move away from TextEdit anyway - we only used it in the course because it was built-in and a quick way to get started, but it's now become more of a inconvenience than it was before.

Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Already a member ?

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships.
Learn more

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.