Jump to content

How To Know Which Code To Use?

mr.rob

 

Hey guys, 

A beginner programming related question that I'm having trouble wrapping my head around regarding the many various codes one can use to accomplish the same thing.

 

I'm currently fixing the error on my Wordpress site to Remove Query Strings From Static Resources to help speed my site up a little by adding some code to the .PHP file.

 

I've been researching which code to add and it seems every new place I look recommends a different code to use, and I'm lost as to how to know which code I should use... Does it even matter and each of the codes will accomplish the same thing? Is there a secret to determine which one is of higher quality?

 

Example: Here are two codes I've been recommended to Remove Query Strings From Static Resources on my site... Can anyone shed some light as to why I should use one over the other?

#1
 

Quote

function _remove_script_version( $src ){ 
$parts = explode( '?', $src ); 
return $parts[0]; 

add_filter( 'script_loader_src', '_remove_script_version', 15, 1 ); 
add_filter( 'style_loader_src', '_remove_script_version', 15, 1 );
 

VS.
#2
 

Quote

//* TN - Remove Query String from Static Resources
function remove_css_js_ver( $src ) {
if( strpos( $src, '?ver=' ) )
$src = remove_query_arg( 'ver', $src );
return $src;
}
add_filter( 'style_loader_src', 'remove_css_js_ver', 10, 2 );
add_filter( 'script_loader_src', 'remove_css_js_ver', 10, 2 );


I would be ultra excited to have some of you experts wipe the dirt off my glasses and let me see the light here! Also any programming/coding 101 articles/resources you have I'd love to take a look at to better understand all this.

 

Thanks for your help!

-Rob

Link to comment
Share on other sites

Link to post
Share on other sites

If it accomplishes the same thing, assuming no other side effects, it doesn't matter what code you use. It only matters if you care about performance.

 

However, it would help to understand what each code solution you're looking at is really doing. Copying and pasting code blackbox style isn't really useful.

Link to comment
Share on other sites

Link to post
Share on other sites

On 12/22/2018 at 5:59 PM, M.Yurizaki said:

If it accomplishes the same thing, assuming no other side effects, it doesn't matter what code you use. It only matters if you care about performance.

 

However, it would help to understand what each code solution you're looking at is really doing. Copying and pasting code blackbox style isn't really useful.

Also matter if you want your code to be maintainable and understood by others. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

Hey I appreciate the replies here peeps,

 

Sorry for my late response, been xmas traveling.

 

On 12/22/2018 at 4:59 PM, M.Yurizaki said:

If it accomplishes the same thing, assuming no other side effects, it doesn't matter what code you use. It only matters if you care about performance.

 

Well I definitely care about performance, the only reason I'm looking into this in the first place to speed my site up. But I suppose the gist is that there is more than one way to skin a cat here....

 

What would you all suggest to someone who owns a wordpress site that is spending the bulk of their time on the business/marketing/content sides of things (I don't have time to dedicate to learning/mastering coding or programming) but would just like to have a base level competence to understand the backend of my site and how it relates to what I'm trying to accomplish (namely a site that is fast and secure for my customers)? Resources or starting places for someone like me?

 

What would it take to go from essentially clueless to being able to understand the difference between 2 code snippets to use such as in my example?

 

I'd love to get at least an understanding of why when I do X in the backend of my site Y happens and how to diagnose issues.

 

Thinking about hiring someone on Upwork to answer my questions and walk me through some stuff, or maybe take an entry level udemy course or something.

 

Thanks guys,

-Rob

Link to comment
Share on other sites

Link to post
Share on other sites

26 minutes ago, mr.rob said:

-Snip-

Unless there are severe performance issues already or there's a well defined requirement, I don't recommend looking into optimizing the performance of your application just to optimize it. Static analysis may also only go so far, you need a way to profile your application to see what's taking the longest and work on those parts.

 

Aside from static analysis and looking at functions that you used from libraries or whatnot, the only other way to really get a handle on what your application does or how it performs is to run it through a debugger and profiler. I don't know what you're using and my knowledge of tool sets don't include what you're doing, but if you can find something that lets you inspect the application while its running, then you can start to better understand what your application does.

 

But otherwise when optimizing, you need to find where the bottlenecks are and fix them or work around them. Like for example, if your application is loading a resource from the internet, you don't make a blocking call to grab it. You make it an asynchronous call so that the app can go do other things even though resource retrieval operation will still take relatively forever.

Link to comment
Share on other sites

Link to post
Share on other sites

On 12/25/2018 at 2:12 PM, mr.rob said:

(namely a site that is fast and secure for my customers)?

If you have customers (people paying you money) and expecting a secure service from you and you are a total beginner when it comes to programming the proper thing to do is hire an experienced professional to do it for you. 

Link to comment
Share on other sites

Link to post
Share on other sites

On 12/25/2018 at 4:12 PM, mr.rob said:

 

 

Well I definitely care about performance, the only reason I'm looking into this in the first place to speed my site up. But I suppose the gist is that there is more than one way to skin a cat here....

 

 

Only care about performance if your application needs it. Optimizing something that does not need to optimize can result in ugly code, unreadable/maintainable code, and bunch of bugs. 

 

Premature optimization is the root of all evil 

http://wiki.c2.com/?PrematureOptimization

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

The first chunk of code searches for the "?" character in url and splits the url in multiple chunks of text  treating ? as separator

 

So the text  site.com/file.css?ver=300   is split into to  :  site.com/file.css   and  ver=300

Then it returns the first string it found.

 

The second chunk of code searches exactly for the ?ver  text inside the url. Once it found the sequence, it stops and returns the text from start until the occurence of the ? character.

 

The second chunk of code is in theory better because the code doesn't create an additional variable, the array which temporarily stores the parts of the url, and the code doesn't search the whole length of the url for occurences of "?"character, as soon as "?ver" is found it stops. 

The explode function has to search all the length of the string for occurrences of the "?" character when normally you only care about the first one.

 

However, there's a tiny flaw in the second snippet of code, it looks for precisely "?ver" in the url string - if there's a "?VER" in url, then the function won't detect that.  The fix would be to use stripos instead of strpos , where i means case insensitive, and then the function will stop looking regardless how ver is written 

 

In real world however, we're talking about performance differences so extremely small it's not worth it ... searching 120 characters versus 100 characters is basically pico seconds, a few Hz ... your cpu cores have billions of Hz.  The additional creation of that array variable would also add just a few bytes of memory usage and a few cpu cycles, it's practically not worth stressing about.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×