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 McKibbin
Eric McKibbin
Treehouse Moderator 11,177 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.



Edit: fixed some typos

Cool! Thank you Mr.Mckibbin!