Most new web developers find it challenging to go from following tutorials to building projects on their own. In this post, I will take you through the steps you would take to get you from project description to beginning to code out the project. By the time you write your first line of code for a project, you should already know exactly what you will be coding out, and it shouldn't feel like an overwhelming task.
There are three steps prior to writing code.
- Read and re-read the problem or project description to make sure you understand it well. If you can, ask any questions of your client. If it's an assignment or an algorithm with technical language, feed the description into an AI of your choice and ask it to rephrase it in simple language, without giving you any answers. I recommend turning the description into a list, as I exemplify below.
- Project decomposition: you need to break down the project into small parts and ordered tasks. I find the easiest way to do this is to first write out user stories and developer stories, so that is what I model down below.
- Finally, set yourself up for success by writing out the pseudo-code for the project in comment lines. Pseudo-code is using your natural language (i.e., English in my case) to describe what needs to be done, step by step. After this, you can get a good start by writing out your function stubs, as I show in the example below. This ensures that you have thought through the logic in advance.
Overall, the process of preparing to code is one of separating your thinking about what needs to be done, from how to do it (with code). If you think back to writing papers in school, this is like separating your writing a first draft from the editing process that makes that writing good. If you try to edit as you write, your writing will suffer. Same with code, the process of thinking through the problem in as much detail as you can, before actually writing the code, makes everything easier. Once you have it working, you can come back to your code with questions about how to improve your code, which involves another set of questions and skills.
I promise that, if you follow this process, writing code that is original and comes from you is 99.99% easier and more enjoyable.
The Project Description: Random Quote Generator¶
Create a web application that displays a random quote on the page. The application should have a button that, when clicked, generates a new random quote. The quotes can be stored in an array within the JavaScript file. Additionally, you can include an author name for each quote and display it along with the quote text.
Understand the Requirements¶
Start by turning the project description into a list¶
- The project requires creating a web application that displays a random quote on the page.
- There should be a button that, when clicked, generates a new random quote.
- The quotes should be stored in an array within the JavaScript file.
- The application should display the quote text and the author's name.
Now, think of the project from the perspective of the end user, and then from the perspective of the developer that needs to build it:
User Stories¶
User stories help to define the requirements and expectations from the perspective of the end-users, ensuring that the application meets their needs and provides a positive user experience.
- As a user, I want to see an initial quote and its author displayed on the page when I first load the application.
- As a user, I want to be able to click a button to generate a new random quote and author.
- As a user, I want to see a different quote and author each time I click the button to generate a new quote.
- As a user with disabilities, I want the application to be accessible and follow best practices for web accessibility. (Stretch goal)
- As a user, I want to be able to customize the styling of the quote display area and the button to match my preferences. (Stretch goal.)
- As a user, I want to be able to share the current quote and author on social media platforms or copy the text to the clipboard. (Stretch goal.)
Developer Stories¶
Developer stories are about capturing what are the concerns that the developer brings to the project:
- As a developer, I want to organize my code in a modular and maintainable way, separating concerns between HTML, CSS, and JavaScript files.
- As a developer, I want to store a collection of quotes and their authors in an array within the JavaScript file.
- As a developer, I want the quote and author to be displayed in a clear and readable format.
- As a developer, I want to include clear and concise comments in my code to explain the purpose and functionality of different sections, making it easier for other developers (or my future self) to understand and maintain the codebase.
- As a developer, I want the application to be responsive and display properly on different screen sizes and devices. (Stretch goal.)
- As a developer, I want to ensure that the application works consistently across different web browsers and versions. (Stretch goal.)
- As a developer, I want to optimize the application's performance by minimizing unnecessary computations, reducing file sizes, and implementing best practices for efficient rendering and execution. (Stretch goal.)
Write down everything you can think of, then go back through them and identify which goals are part of a minimum viable product (MVP), and which goals can be set aside as stretch goals - features that will make a working app even more awesome in future iterations.
Project Decomposition¶
Now you are ready to make an ordered list of all the tasks needed to fulfill these user and developer stories.
Project Set Up¶
- Create a git repository with a simple README for the project and clone it down to your machine.
- Add HTML, CSS, and JavaScript files to the project (e.g., index.html, styles.css, script.js).
- Create the boilerplate for the HTML file and link the CSS and JavaScript files in the HTML file.
HTML Set Up¶
- Create a basic HTML structure with a container for the quote and author, and a button.
- Create a container element (e.g., ) to hold the quote and author
- Add an element (e.g., ) to display the quote text
- Add an element (e.g., ) to display the author name
- Create a button element to trigger the quote generation
CSS Set Up¶
- Create a basic layout for your HTML elements
- Styles for the container element for the quote and author
- Style the quote text and author elements (e.g., font, size, color)
- Style the button element (e.g., background color, hover effect)
JavaScript Set Up¶
- Define an array of quote objects, where each object contains the quote text and author. Add three quotes to this structure to start with
- We will want to select a random quote from our array of quotes, so we will need to write a function to generate a random index number
- Write a function to select the quote and author based on the random index
- Write a function to display the quote and author to the DOM
- Write an event-handler for the button so a new quote is selected when it is clicked.
- Add an event listener to the button to call the quote generation function when clicked
Now, go back through your User and Developer Stories, and make sure that they are all accounted for. For example, as I was reading through the User stories, I noticed that this one was not yet accounted for:
- As a user, I want to see a different quote and author each time I click the button to generate a new quote.
How will we check to make sure the quote generated is new, and not the same as the last one? I will need to store the last used index number, and if the new index generated is the same, then the function that randomly selects a number needs to run again, until it generates a new index number.
Given this, I will need to add the following to step 14 of the JavaScript set up:
- We will want to select a random quote from our array of quotes, so we will need to write a function to generate a random index number.
- 14a. Create a global variable to store the selected index number (so that we can check against it the next time we want a new quote), and set it to undefined initially.
- 14b. write a function to generate a random index number.
- 14c. Write a function that checks the random number against the previous number. If it is repeated, it needs to call the number generation function again.
Okay, now we are ready for the last step,
Writing Pseudo-Code¶
Transpose your list above to the JavaScript file as comments
Now you are ready to put in some test data for your quotes and function stubs:
Narration of the Program Logic¶
You should now be able to narrate exactly what happens:
The user goes to the page and it loads. The HTML, then the CSS, then the JS loads. The JS loads and all the way at the bottom it gets to handleNewQuoteClick()
the program's entry point.
This handleNewQuoteClick()
function calls displayQuote()
, displayQuote()
calls selectQuote()
, selectQuote()
calls getUniqueRandomIndex()
, and getUniqueRandomIndex()
calls getRandomIndex()
.
With getRandomIndex()
at the end of the logic chain, we now move back through forwards: We get that random index number, we check to make sure it is not same as the previous one, it is used to select a quote from the array of quotes, the quote is passed to the function in charge of inserting the quote into the DOM, whereupon the quote is displayed for the user to see it.
This way of thinking is called "procedural thinking" or "algorithmic thinking," and if you haven’t done much of it before, it can make your head hurt a little bit when you are first learning to program. Don’t worry; it's a muscle you can strengthen through practice, and it gets easier!
From here, the logic of your program is clear, and you have function stubs written. Now is the time to write the code for each function, focusing on that one small task at a time. (Each function should have exactly and only one job to do!) You may still need to figure out some details from here -- this process need not be perfectly executed for it to be helpful. Hopefully, all the planning has paid off because you know exactly what you need your code to do. The logic solid, you can begin to write the code with confidence.
The more work you do upfront, the easier the coding will be. This is a long process, I know, but after you have taken the time to do it the long way a few times, you'll internalize it and be able to step through it more efficiently.
Think of making a sandwich - once you learned how it's done, you don't have to think about all the steps one by one, but can smoothly run through the steps to get the desired outcome.