﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>nick commented on Unprofessional code</title><description>@Ayende "depending on your context"</description><link>http://ayende.com/156450/unprofessional-code#comment41</link><guid>http://ayende.com/156450/unprofessional-code#comment41</guid><pubDate>Tue, 26 Jun 2012 08:50:08 GMT</pubDate></item><item><title>Kamran Ayub commented on Unprofessional code</title><description>I was thinking of guessing someone's timezone based on their IP by maybe looking it up via a service or whatever.

I ended up defaulting to the server timezone and letting the user pick it. It was much easier that way than trying to a) find a service to use, b) making a possibly totally wrong guess, and c) spend extra time.

If it requires more knowledge of what exactly is happening, I can't say what the issue is off-hand. I do have questions around the units of the radius (200km, 200m, 200in?) and why they are only taking 2 events.

Overall I would probably offer much more specific ways of looking up location (like Valeriob suggests) and fallback on this as a last resort, if I had to do it at all.</description><link>http://ayende.com/156450/unprofessional-code#comment40</link><guid>http://ayende.com/156450/unprofessional-code#comment40</guid><pubDate>Tue, 26 Jun 2012 02:06:10 GMT</pubDate></item><item><title>JV commented on Unprofessional code</title><description>http://pastebin.com/1Ta7hBDy</description><link>http://ayende.com/156450/unprofessional-code#comment39</link><guid>http://ayende.com/156450/unprofessional-code#comment39</guid><pubDate>Mon, 25 Jun 2012 23:06:32 GMT</pubDate></item><item><title>Valeriob commented on Unprofessional code</title><description>I would use this service on the client http://www.myipaddress.com/show-my-ip-address/ and pass the ip address as parameter.
And maybe implement an overload with lat/long as parameters for situations where i have user consent to acquire location (and mobile apps)</description><link>http://ayende.com/156450/unprofessional-code#comment38</link><guid>http://ayende.com/156450/unprofessional-code#comment38</guid><pubDate>Mon, 25 Jun 2012 22:13:04 GMT</pubDate></item><item><title>Robert Slaney commented on Unprofessional code</title><description>My immediate thought was about IPv4 and IPv6.
The IPAddress.TryParse is expecting an IPv4 format</description><link>http://ayende.com/156450/unprofessional-code#comment37</link><guid>http://ayende.com/156450/unprofessional-code#comment37</guid><pubDate>Mon, 25 Jun 2012 21:13:14 GMT</pubDate></item><item><title>Stan Kazakh commented on Unprofessional code</title><description>What about an overloaded version of GetLocationByIp() that takes a string?</description><link>http://ayende.com/156450/unprofessional-code#comment36</link><guid>http://ayende.com/156450/unprofessional-code#comment36</guid><pubDate>Mon, 25 Jun 2012 21:04:16 GMT</pubDate></item><item><title>Rafal commented on Unprofessional code</title><description>Can I have a try?
1. You do geolocation db search on each request which requires a GeoSession to be opened every time
2. Location information is not stored anywhere so you'll have to do it again in every page that needs the location
3. There's no way for the user to choose a different location if the IP-based location fails or gives incorrect result for some reason

</description><link>http://ayende.com/156450/unprofessional-code#comment35</link><guid>http://ayende.com/156450/unprofessional-code#comment35</guid><pubDate>Mon, 25 Jun 2012 20:58:56 GMT</pubDate></item><item><title>Alwin commented on Unprofessional code</title><description>The action is using the Request, but this is not clear from outside the function.</description><link>http://ayende.com/156450/unprofessional-code#comment34</link><guid>http://ayende.com/156450/unprofessional-code#comment34</guid><pubDate>Mon, 25 Jun 2012 20:53:33 GMT</pubDate></item><item><title>Thomas Krause commented on Unprofessional code</title><description>I would invert the if to have more linear-ITY in the function:

if(!TryParse(...)) return PartialView(...);

var location = ...

I like the normal function flow to be as linear as possible without any branches and use the if conditions only as early exit points/exceptions in my code. It's much easier to read.</description><link>http://ayende.com/156450/unprofessional-code#comment33</link><guid>http://ayende.com/156450/unprofessional-code#comment33</guid><pubDate>Mon, 25 Jun 2012 19:59:31 GMT</pubDate></item><item><title>Martin commented on Unprofessional code</title><description>There's absolutely NO reason to define location outside the scope. If it's not null, you return. you should have the definition of location inside the if statement of the try parse.

if (TryParse) {
    var location = blah...
    return partial view;
}

I hate when i see this kind of stuff in code too.</description><link>http://ayende.com/156450/unprofessional-code#comment32</link><guid>http://ayende.com/156450/unprofessional-code#comment32</guid><pubDate>Mon, 25 Jun 2012 19:43:32 GMT</pubDate></item><item><title>bblack commented on Unprofessional code</title><description>might be nice to know that your results were effected by a lack of or a malformed ip address.</description><link>http://ayende.com/156450/unprofessional-code#comment31</link><guid>http://ayende.com/156450/unprofessional-code#comment31</guid><pubDate>Mon, 25 Jun 2012 19:36:03 GMT</pubDate></item><item><title>bblack commented on Unprofessional code</title><description>sorry for re-post (edit to above)
overload method preferred?: GeoSpace.GetLocationByIP(string ipAddress) which returns null location if ipAddress is incorrect ?</description><link>http://ayende.com/156450/unprofessional-code#comment30</link><guid>http://ayende.com/156450/unprofessional-code#comment30</guid><pubDate>Mon, 25 Jun 2012 19:16:01 GMT</pubDate></item><item><title>bblack commented on Unprofessional code</title><description>add override = &gt; GeoSpace.GetLocationByIP(string ip) 
which returns null location if ip is incorrect ?  </description><link>http://ayende.com/156450/unprofessional-code#comment29</link><guid>http://ayende.com/156450/unprofessional-code#comment29</guid><pubDate>Mon, 25 Jun 2012 18:59:45 GMT</pubDate></item><item><title>Max Schmeling commented on Unprofessional code</title><description>Why have an extra if check when you could use the one you already have?</description><link>http://ayende.com/156450/unprofessional-code#comment28</link><guid>http://ayende.com/156450/unprofessional-code#comment28</guid><pubDate>Mon, 25 Jun 2012 18:57:02 GMT</pubDate></item><item><title>Ayende Rahien commented on Unprofessional code</title><description>Mike, and others.
I am quite amazed by how many people get all too hung up about minor code details. The actual issue that I had in mind was something else all together. And the micro seconds you'll spend on the emtpy string never crossed my mind.
</description><link>http://ayende.com/156450/unprofessional-code#comment27</link><guid>http://ayende.com/156450/unprofessional-code#comment27</guid><pubDate>Mon, 25 Jun 2012 17:57:27 GMT</pubDate></item><item><title>Ayende Rahien commented on Unprofessional code</title><description>Nick,
In the entire application, there is just a single location where this is used.
Why on earth would I want to encapsulate that before I have a need to do so?</description><link>http://ayende.com/156450/unprofessional-code#comment26</link><guid>http://ayende.com/156450/unprofessional-code#comment26</guid><pubDate>Mon, 25 Jun 2012 17:57:09 GMT</pubDate></item><item><title>Ayende Rahien commented on Unprofessional code</title><description>Mike, and others.
I am quite amazed by how many people get all too hung up about minor code details. The actual issue that I had in mind was something else all together. And the micro seconds you'll spend on the emtpy string never crossed my mind.</description><link>http://ayende.com/156450/unprofessional-code#comment25</link><guid>http://ayende.com/156450/unprofessional-code#comment25</guid><pubDate>Mon, 25 Jun 2012 17:54:32 GMT</pubDate></item><item><title>Dave Walker commented on Unprofessional code</title><description>Apart from that the method has about 1000 things/rules going on with only one descriptor (method name)?

Even the Events have so much logic going on - they are within 200 somethings of your lat/long. They are future events, ordered. And there are only two of them.

Method has far too much responsibility.</description><link>http://ayende.com/156450/unprofessional-code#comment24</link><guid>http://ayende.com/156450/unprofessional-code#comment24</guid><pubDate>Mon, 25 Jun 2012 16:40:26 GMT</pubDate></item><item><title>Matthew Douglass commented on Unprofessional code</title><description>To me the biggest annoyance with that block is that is is a function with one input (Request.UserHostAddress) and one output (Location) that failed to get abstracted out to its own function.</description><link>http://ayende.com/156450/unprofessional-code#comment23</link><guid>http://ayende.com/156450/unprofessional-code#comment23</guid><pubDate>Mon, 25 Jun 2012 15:45:24 GMT</pubDate></item><item><title>Miguel Lira commented on Unprofessional code</title><description>Ayende,

It should be noted that a proper X-Forwarded-For *can* be a collection of proxys along the request chain usually in the format of client, proxy1, proxy2, etc. so parse accordingly.

However, don't be quick to discard Keith's suggestion. Getting to an end user's IP address implicity will be a uphill climb unless you leverage some client side solution (javascript, flash, active-x) so why not explicitly ask the client for this information?

Personally, I like Paul's suggestion of gathering location via something other than IP and prompting the user accordingly.

</description><link>http://ayende.com/156450/unprofessional-code#comment22</link><guid>http://ayende.com/156450/unprofessional-code#comment22</guid><pubDate>Mon, 25 Jun 2012 15:05:48 GMT</pubDate></item><item><title>Nick Berardi commented on Unprofessional code</title><description>The location should be passed in or determined by an alternate method, through one of the many methods supported by browsers, by IP, Geolocation header, and even lat or long, or address through query string.  

But regardless of the method, the code should be extracted so that it can be used consistently through out the entire application.  Maybe something like var location = Helper.GetLocation(Request).</description><link>http://ayende.com/156450/unprofessional-code#comment21</link><guid>http://ayende.com/156450/unprofessional-code#comment21</guid><pubDate>Mon, 25 Jun 2012 14:47:42 GMT</pubDate></item><item><title>Ale Miralles commented on Unprofessional code</title><description>the first line should check hostadrress == "", if so, returns the corresponding view, if not the do the whole work.
</description><link>http://ayende.com/156450/unprofessional-code#comment20</link><guid>http://ayende.com/156450/unprofessional-code#comment20</guid><pubDate>Mon, 25 Jun 2012 14:03:36 GMT</pubDate></item><item><title>Jesse commented on Unprofessional code</title><description>The method does too much. Getting the location should be the job of some other service/object. It could be as simple as creating a view model for this action and then a custom model binder to wire it up.</description><link>http://ayende.com/156450/unprofessional-code#comment19</link><guid>http://ayende.com/156450/unprofessional-code#comment19</guid><pubDate>Mon, 25 Jun 2012 13:26:42 GMT</pubDate></item><item><title>Phil commented on Unprofessional code</title><description>I've never done it, but could you write the client ip out to a hidden field whilst rendering the page?</description><link>http://ayende.com/156450/unprofessional-code#comment18</link><guid>http://ayende.com/156450/unprofessional-code#comment18</guid><pubDate>Mon, 25 Jun 2012 12:39:50 GMT</pubDate></item><item><title>Wyatt Barnett commented on Unprofessional code</title><description>I wonder how slow the lookup for GetLocationByIp is -- if that is a remote call then it could really be ugly. Also should probably cache that once you've got it.</description><link>http://ayende.com/156450/unprofessional-code#comment17</link><guid>http://ayende.com/156450/unprofessional-code#comment17</guid><pubDate>Mon, 25 Jun 2012 12:38:10 GMT</pubDate></item><item><title>nick commented on Unprofessional code</title><description>Depending on your context that might be a bit much to lump all that in the controller. You can't test it easily because of the dependency on request.

Bet you're missing the catch all exceptions and log it to somewhere nobody ever checks. That's how the pros roll :)</description><link>http://ayende.com/156450/unprofessional-code#comment16</link><guid>http://ayende.com/156450/unprofessional-code#comment16</guid><pubDate>Mon, 25 Jun 2012 12:32:04 GMT</pubDate></item><item><title>Mike commented on Unprofessional code</title><description>Unprofessional? Really? This strikes me a much-a-do about nothing. I probably would not use a local for the ipString myself but it's a minor point. Running through Try.Parse on an empty string is maybe a a few cycles more but how often are you going to hit that case (maybe never depending on the calling code). It would be interesting to measure the actual performance checking for null/empty vs. the current method for the using the expected occurrence frequency for empty strings and publishing the results. Now that would be professional... </description><link>http://ayende.com/156450/unprofessional-code#comment15</link><guid>http://ayende.com/156450/unprofessional-code#comment15</guid><pubDate>Mon, 25 Jun 2012 12:05:46 GMT</pubDate></item><item><title>SanderD commented on Unprofessional code</title><description>@Ayende: in one of our clients' situation, we have two Amazon EC2 instances behind an Amazon load-balancer. 
The Request.UserHostAddress is the one of the loadbalancer, and the X-Forwarded-For-header is set to the original clients' ip by the loadbalancer.

We have installed an IIS module for that, called ARR Helper (http://blogs.iis.net/anilr/archive/2009/03/03/client-ip-not-logged-on-content-server-when-using-arr.aspx), which re-writes the X-Forwarded-For header into the UserHostAddress.

</description><link>http://ayende.com/156450/unprofessional-code#comment14</link><guid>http://ayende.com/156450/unprofessional-code#comment14</guid><pubDate>Mon, 25 Jun 2012 11:19:43 GMT</pubDate></item><item><title>Ayende Rahien commented on Unprofessional code</title><description>James,
If you are using a proxy, usually your internal IP is a local one, and thus not really relevant for GeoIP purposes</description><link>http://ayende.com/156450/unprofessional-code#comment13</link><guid>http://ayende.com/156450/unprofessional-code#comment13</guid><pubDate>Mon, 25 Jun 2012 10:56:14 GMT</pubDate></item><item><title>Ayende Rahien commented on Unprofessional code</title><description>Keith,
a) that is only available on the client side
b) that requires user permission
</description><link>http://ayende.com/156450/unprofessional-code#comment12</link><guid>http://ayende.com/156450/unprofessional-code#comment12</guid><pubDate>Mon, 25 Jun 2012 10:55:41 GMT</pubDate></item></channel></rss>