Optimizing code: Local variables are your friend!
17 25 Jul 2015 03:35 by u/roznak
Looking through the Voat code I see this.
var commentsUrl = new Uri("http://" + System.Web.HttpContext.Current.Request.Url.Authority + "/v/" + submission.Subverse + "/comments/" + submission.Id);
var subverseUrl = new Uri("http://" + System.Web.HttpContext.Current.Request.Url.Authority + "/v/" + submission.Subverse);
New developers are always scared to use local variables and somehow assume that impressive huge one-liners are better but they are not.
var subVerse="http://" + System.Web.HttpContext.Current.Request.Url.Authority + "/v/" + submission.Subverse;
var subverseUrl = new Uri(subVerse);
var commentsUrl = new Uri(subVerse+ "/comments/" + submission.Id);
The second version is more performant since you aid the compiler in telling what it must do.
If you do not use that local variable, then your compiler will guess what to do and to play it safe will probably generate hidden temporary variables using more code than need to be.
The second example is also easier to debug. You can put a break point and actually introduce a faulty test variable.
var subVerse="http://" + System.Web.HttpContext.Current.Request.Url.Authority + "/v/" + submission.Subverse;
subVerse="AFuckedUpURL "
var subverseUrl = new Uri(subVerse);
var commentsUrl = new Uri(subVerse+ "/comments/" + submission.Id);
And now can be easily commented out without touching the code much.
var subVerse="http://" + System.Web.HttpContext.Current.Request.Url.Authority + "/v/" + submission.Subverse;
// subVerse="AFuckedUpURL "
var subverseUrl = new Uri(subVerse);
var commentsUrl = new Uri(subVerse+ "/comments/" + submission.Id);
And toy with it
var subVerse="http://" + System.Web.HttpContext.Current.Request.Url.Authority + "/v/" + submission.Subverse;
subVerse=subVerse+"DROP TABLE XXX";
var subverseUrl = new Uri(subVerse);
var commentsUrl = new Uri(subVerse+ "/comments/" + submission.Id);
I would even go this far
var subVerse="http://" + System.Web.HttpContext.Current.Request.Url.Authority + "/v/" + submission.Subverse;
var subVerseComments=subVerse+ "/comments/" + submission.Id;
var subverseUrl = new Uri(subVerse);
var commentsUrl = new Uri(subVerseComments);
Now you can go all the way
var subVerse=GetAndCheckSubVerse();;
var subVerseComments=GetAndCheckSubVerseComments(subVerse);
var subverseUrl = new Uri(subVerse);
var commentsUrl = new Uri(subVerseComments);
And keep toying with it
var subVerse=GetAndCheckSubVerse();;
var subVerseComments=GetAndCheckSubVerseComments(subVerse);
subVerseComments=subVerse
subVerse=subVerse+"bababa";
var subverseUrl = new Uri(subVerse);
var commentsUrl = new Uri(subVerseComments);
Edit:
- The first example has so much information on these 2 lines (aka noise= wall of text) that it is impossible to read.
- The last example has reduced noise and is actually very agile to modify the code in any direction you want.
4 comments
1 u/404_SLEEP_NOT_FOUND 25 Jul 2015 06:19
Exactly why, in Java, I force myself not to do many references on the same line. It's impossible, in Java, to know which reference (eg: obj1.getObj2().getgetObj3()) throw a null pointer exception. Having to deal with which is null in production is just a real time sink, and at a time where you need fast turn around.
1 u/awrk 25 Jul 2015 13:12
This would be my one piece of advice to aspiring developers: a line of code should do exactly one thing. I realize that terseness is attractive, and vertical screen space is precious, etc. but if you've written code for any time at all you realize that the time you spend looking at it through a debugger is more precious than any other time you spend writing code, so it should shape how you write code.