Five Tips to ‘Hot-Rod’ Your Web Code – Moz
Skip to content
Moz logo
Menu open
Menu close
Search
Products
Moz Pro
Moz Pro Home
Moz Local
Moz Local Home
STAT
Mozscape API
Free SEO Tools
Competitive Research
Link Explorer
Keyword Explorer
Domain Analysis
MozBar
More Free SEO Tools
Learn SEO
Beginner’s Guide to SEO
SEO Learning Center
Moz Academy
SEO Q&A
Webinars, Whitepapers, & Guides
Blog
Why Moz
Agency Solutions
Enterprise Solutions
Small Business Solutions
Case Studies
The Moz Story
New Releases
Log in
Log out
Products
Moz Pro
Your All-In-One Suite of SEO Tools
The essential SEO toolset: keyword research, link building, site audits, page optimization, rank tracking, reporting, and more.
Learn more
Try Moz Pro free
Moz Local
Complete Local SEO Management
Raise your local SEO visibility with easy directory distribution, review management, listing updates, and more.
Learn more
Check my presence
STAT
Enterprise Rank Tracking
SERP tracking and analytics for SEO experts, STAT helps you stay competitive and agile with fresh insights.
Learn more
Book a demo
Mozscape API
The Power of Moz Data via API
Power your SEO with the proven, most accurate link metrics in the industry, powered by our index of trillions of links.
Learn more
Get connected
Compare SEO Products
Free SEO Tools
Competitive Research
Competitive Intelligence to Fuel Your SEO Strategy
Gain intel on your top SERP competitors, keyword gaps, and content opportunities.
Find competitors
Link Explorer
Powerful Backlink Data for SEO
Explore our index of over 40 trillion links to find backlinks, anchor text, Domain Authority, spam score, and more.
Get link data
Keyword Explorer
The One Keyword Research Tool for SEO Success
Discover the best traffic-driving keywords for your site from our index of over 500 million real keywords.
Search keywords
Domain Analysis
Free Domain SEO Analysis Tool
Get top competitive SEO metrics like Domain Authority, top pages, ranking keywords, and more.
Analyze domain
MozBar
Free, Instant SEO Metrics As You Surf
Using Google Chrome, see top SEO metrics instantly for any website or search result as you browse the web.
Try MozBar
More Free SEO Tools
Learn SEO
Beginner’s Guide to SEO
The #1 most popular introduction to SEO, trusted by millions.
Read the Beginner’s Guide
How-To Guides
Step-by-step guides to search success from the authority on SEO.
See All SEO Guides
SEO Learning Center
Broaden your knowledge with SEO resources for all skill levels.
Visit the Learning Center
Moz Academy
Upskill and get certified with on-demand courses & certifications.
Explore the Catalog
On-Demand Webinars
Learn modern SEO best practices from industry experts.
View All Webinars
SEO Q&A
Insights & discussions from an SEO community of 500,000+.
Find SEO Answers
August 7-9, 2023
Lock in Super Early Bird savings for MozCon
Snag tickets
Blog
Why Moz
Small Business Solutions
Uncover insights to make smarter marketing decisions in less time.
Grow Your Business
The Moz Story
Moz was the first & remains the most trusted SEO company.
Read Our Story
Agency Solutions
Earn & keep valuable clients with unparalleled data & insights.
Drive Client Success
Case Studies
Explore how Moz drives ROI with a proven track record of success.
See What’s Possible
Enterprise Solutions
Gain a competitive edge in the ever-changing world of search.
Scale Your SEO
New Releases
Get the scoop on the latest and greatest from Moz.
See What’s New
New Feature: Moz Pro
Surface actionable competitive intel
Learn More
Log in
Moz Pro
Moz Local
Moz Local Dashboard
Mozscape API
Mozscape API Dashboard
Moz Academy
Avatar
Moz Home
Notifications
Account & Billing
Manage Users
Community Profile
My Q&A
My Videos
Log Out
S
By: ShyGuy48
March 26, 2007
Five Tips to ‘Hot-Rod’ Your Web Code
Design
This YouMoz entry was submitted by one of our community members. The author’s views are entirely his or her own (excluding an unlikely case of hypnosis) and may not reflect the views of Moz.
1. Separate code from html
I mix. I do. When the project or web page is minimal, it makes a lot of sense to have a small amount of code in the same file as the .html. But when the project starts to grow or requires lots of code, it is time to separate my code from the .html. I feel strongly about this, probably due to my type ‘A’ personality that has a distaste for messiness. But I also have two ‘reasonable’ reasons for giving this advice:
Maintenance is simpler. I am usually returning to a project, trying to remember how it all works, searching for the target of my change, and attempting to ignore the .html code interspersed with the code. If the code and .html are separate, I can knock one of those challenges off my list.
Web Designers. All the ones I have worked with are nice guys. I don’t have any grudges against them…until they modify a page of mixed code and .html carelessly and, in the process, “discover” a new error in the code. Splitting the .html from my code usually keeps their fingers out of my code cookie jar.
BTW, I’m still on speaking terms with every Web Designer I’ve ever worked with. I do believe, however, this is due my insistence on code and .html separation.
2. What is templatification?
This is my personal favorite. Many sites I have worked are prime for the use of templates. I generally use my own templating system, which is based on the functionality found in Smarty. Because my templates contain only html code and a few special characters to denote variables, I am automatically following the advice given in tip number one. The best reason for templatifaction, however, is that it allows one template to be used for many pages easing the workload at maintenance time.
3. Javascript is good for form validation (and not much else)
Harsh words, indeed. I don’t dislike Javascript. It was originally designed to assist in front end form validation, and it does that job well. But its uses have expanded exponentially since its inception, and some of those uses are troublesome. I won’t waste a bunch of time justifying my choices in the following lists, as I’ve probably already alienated half of my readers with this tip’s title.
Good: Front end form validation. Choosing which .css file to include based on browser version.
Bad: Making anything move Javascript code in an tag’s href value.
To sum it up: My pages don’t rely on Javascript. If Javascript is available, I may use it to enhance the user’s experience.
4. Define what is good instead of what is bad. How many times have I relearned this one? Plenty. Two data validation examples follow:
$a=preg_replace(”/[\r\n;]/”,””,$a); //strip out certain special characters I believe are ‘bad’
In the regular expression above, I think I know what type of special characters might be in a malicious submission and strip them out.
$b=preg_replace(”/[^a-zA-Z0-9\.,\”‘;]/”,””,$b); //strip out anything not in my list of accepted characters
This regular expression reveals that I have accepted the fact I don’t know what might be contained in a malicious submission. Instead, I assume almost every character is “bad” and strip out anything that isn’t in my list of known “good” characters.
My reasoning is twofold. First, I don’t know as much as I think I know. The test in the first example will fail because I can’t reliably predict what type of maliciousness will be submitted. Those malicious types keep getting better and I can’t keep up with their sneakiness. The second is maintainability. In the first example, missing a restricted character may result in the submission of disallowed characters into my data. Now I need to clean my data and modify my regular expression. In the second example, if my list of allowable characters is too strict, I can simply add the good character(s) I missed.
5. Do more with SQL, less with code. (And keep the boss happy!)
Hypothetically, I will become wealthy and own my own company. To keep the boss happy, I will throw a party every Friday and use the next employee’s birthday as the excuse. In this scenario I became rich by selling my computer skills, so I might as well put them to use to find the next birthday. I can attack this in one of two ways. Every Friday morning I could:
a) Ask my database for all employees and their birth dates.
SQL: SELECT fname,lname,bday FROM employeelist;
Then I would use code that would cycle through the list. At each employee the number of days till the employee’s next birthday would be calculated. The employee with the lowest number would be on the bubble until my code had reached the end of the list. Now the name on the bubble will be the guest of honor on that Friday’s party.
OR
b) Ask my database a more complex question that will instruct it to calculate the number of days, sort them in the server’s memory, then return only the record with the shortest number of days.
SQL: SELECT fname,lname,( (TO_DAYS(CURDATE())-TO_DAYS(employeelist.bday)) % 365.25) AS numberofdays FROM employeelist ORDER BY numberofdays LIMIT 1;
Then my code would simply read a single record that contained that special employee.
OK, this scenario and my example code are ridiculous (except for the part about my wealth), but I hope it helps to illustrate my point. All too often I have encountered existing projects that contain versions of example A. It usually works but isn’t optimal. Here’s why:
The code asks the database for much more data than it needs. Why ask for every record when the code, ultimately, is only looking for one? When my scenario goes from a five to a 5,000 employee company, I had better get to work early on Friday to start the query. That is a bunch of extra data moving across the network.
At some point I’ll retire and my replacement will still want Friday parties, but will want a different selection method for the guest of honor. In the code-based example, many lines of code will need to change. In the SQL-based example, only a single query will need to be modified. And less maintenance expense pleases every boss!
With Moz Pro, you have the tools you need to get SEO right — all in one place.
Start your free trial!
Read Next
One Secret to Improve SEO in 2021: Guestographics
Read this post
Pop-Ups, Overlays, Modals, Interstitials, and How They Interact with SEO
Read this post
Strategic SEO Decisions to Make Before Website Design and Build
Read this post
Comments
Please keep your comments TAGFEE by following the community etiquette
Comments are closed. Got a burning question? Head to our Q&A section to start a new conversation.
Moz logo
Contact
Community
Free Trial
Terms & Privacy
Jobs
Help
News & Press
Copyright 2022 © Moz, Inc. All rights reserved.