Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript DOM Scripting By Example Improving the Application Code Refactor 1: Create List Items

Who else thinks that the code became less readable after this?

Maybe I'm just too much of a beginner but the code became impossible to read after this.

1 Answer

Eric M
Eric M
11,505 Points

Hi baderbouta,

I agree that it's sometimes easier to read code that's a simple line by line declaration of exactly what statements will be executed. Especially if you already know exactly what each of those lines does. It's often necessary to create functions for repeated tasks not for readability, but if you repeat a task 100 or 1000 times in a program and need to slightly tweak the way it happens it's a lot easier to edit it in one place than a thousand.

As you become more experienced, however, it will be easier to recognise the behavior of programs that have abstracted their statements into classes, functions, and methods. It's not done this way just to adhere to a convention, but because for more complex programs this logical grouping is necessary for maintenance and to allow interoperability (e.g. API) with other code while controlling what data and methods they have access to, and not requiring other programmers to copy and paste lines of your code.

When all of these abstractions are apt code does become a lot easier to read. It allows you to read a single name (e.g. storeInDatabase) and know what's going on rather than read hundreds of lines and try to figure out what they're trying to do (e.g. 5 lines to connect to the db, another 10 to create a db object representing the table to insert the data into, 15 to validate the data matches the table schema, 10 lines of error handling, etc. etc)

I will admit that when reading API documentation, if I've never seen a function before, I often have to check what each parameter of a function is supposed to represent and also see some examples. Overall though it makes your code easier to read and a lot easier to maintain and reuse.

Cheers,

Eric

Edit: fixed some typos

Cool! Thank you Mr.Mckibbin!

Gavin Eyquem
seal-mask
.a{fill-rule:evenodd;}techdegree
Gavin Eyquem
Front End Web Development Techdegree Student 17,382 Points

Yeah, I agree that with experience comes wisdom which allows more and more concision. It's getting the experience that can sometimes be the problem.