- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
It might be enterprisey, but:
Admin
isLatitude true or false?
No no no, what you got to do is write an Enum whose values are CoordinateType.Latitude and CoordinateType.Longude, and load it from an XML file (in case you want to expand the technique to use e.g. generalised Cartesian coordinates, or perhaps include time as a further coordinate so as to measure times of travel, etc. etc.
Admin
Obviously solutinos get absorbed by dense brain matter, emitting negative enterprisons and gluons W, T and F.
Admin
If that is the only use-case then, yes WTF.... but there are plenty of use-cases where they type of method can be appropriate. Much more of the codebase would be needed to declare this a WTF.
[However the use of a Boolean rather than an enum IS a WTF, at a minimum for readability and also for extensibility]
Admin
public enum IsLatitude { true, false, locationNotFound }
Admin
Yeah I was gonna say TRWTF was not running a spell check on the article text.
Admin
if we have solutinos its too late for spell check, we need spill check
Admin
Spill check or spell chick?
Admin
An additional parameter to select? How wasteful.
Make the method stateful, and return latitude or longitude on alternate calls. Eliminates a wasted parameter, simplifies the code, and only requires a little discipline to always call the method twice in the same order. Bob's your well-ordered uncle!
Admin
Why not just return both the lat and lon with one call? 99% of the time you probably want both.
Admin
Your solution is alright I think, but I like Code Golfer's solution better.
Admin
Am I missing something? The first time this is run it will return the first point that matches the required latitude; the second time it will return the first point with the required longitude.
Those points are unlikely to belong to the same location unless the dataset is very small. These functions are useless. If you need X and Y then both X and Y have to be part of the key.
Admin
As boring as I am, I thought of altitude first.
But now it's too late - the code is already written, and rewriting a function is un-enterprisey. Unlike expanding a function.
public static decimal? GetPosition(string endpointId, ICollection<UserGeoPositionModel> userGeoPositions, bool isLatitude, bool isLongitude, bool isAltitude, bool isTime, bool isMultiverseIndex)
Or we'd do it right and expand the definition of bool:
enum Bool { True, False, FileNotFound , IsLongitude , IsLatitude , Maybe , IsAltitude , IsAltitudeOrDepth, FristPost };
Addendum 2016-08-17 10:41: I miss the preview and easy formatting ...
Addendum 2016-08-17 10:43: And now that the comment design is toatlly fucked up:
My Italian is a bit rusty - what is a solutino?
Admin
"CPU time is cheap. Programmer time is expensive. "
Software is like a car: it has to be cheap when you buy it. Who would ever be so paranoid to have a look at maintenance cost and TCO before it's broken.
Admin
Did anyone else notice that the endPointId.Contains(x.UserID) is actually broken?
If you pass in endPointID = 1000 then userID = 1, 10, 100, 1000 would all be true...
Admin
Yep mee too. Much more potential foe evilness by burying a call somewhere in a (seldom) used function and wait for the fireworks. :smiling_imp:
Admin
Iff
endPointID instanceof string && x.UserID instanceof string
, then true. IffendPointID instanceof IEnumerable<typeof(x.UserID)>, than it is checking if the value of
x.UserID` is a member of that enumeration.So, at the end of the day, you have to ask yourself -- Do you want to partial match your
endPointID
, or do you want multipleendPointID
s for yourPoint
?Admin
Solutinos are Italian solutions.
Admin
I just threw up in my mouth a little bit.
Admin
var point = userGeoPositions.Where(x => x.userId = userId);
um, no, you couldn't write like that, it wouldn't even compile.........
Admin
This isn't so much a WTF as it is a minor headscratcher. Yes, there's a better way, but this way is pretty OK. It's not like we're looking at an ERE or a hardcoded mess of spaghetti code here.
Admin
The original example is simpler, but it two does two passes over the collection for the userId. Why? Because LINQ expressions are not fully evaluated until after they are actually need. Actually, the original example would not actually work for multiple reasons, especially since lines 2 and 3 are trying to read a property and at this point the value point would be an IQueryable(Of T), or similar depending on the LINQ provider that userGeoPositions actually is.
Yes, there are OTHER ways to write that code, but none that I can think of that are any cleaner.
Admin
What language is this with => and ? and decimal?
Admin
Swift#?
Admin
C#
Admin
C#. If you (or anyone else reading this) were wondering => is a lambda expression, ? is a ternary operator, and decimal? indicates a nullable decimal. In C# adding ? to the end of any value-typed object (basically primitives) makes a nullable version of that type.
Admin
You know what else works to reduce the LOC? Not using a property where a public member will do.
Admin
Well, obviously the two calls to get the latitude and longitude will be right after one another, so this function should cache the object in a static variable and check there first. If there are N threads typically running, it might make sense to have the cache contain N items.
Genius!
Admin
solutino n. a computer programming implementation solution that saves at least one line of source code per use; especially when the solutino is insaneo or idiotico
Admin
Yeah, first he should compare instead of assign and second, he really should use First or FirstOrDefault instead of Where if he wants to use properties (or has he really defined the properties as extension for his enumerable...?)
Admin
IMO the WTF starts with having two separate variables for longitude and latitude.
Admin
Indeed. It should be:
var point = userGeoPositions.First(x => x.userId == userId);
Also, the example of calling the GetPosition method wouldn't work - you can't assign a Nullable<decimal> to a decimal.
Admin
Unless you want public access to read the value, but only private access to write it. Or you can make it read-only so that it can only be written by the constructor if it is never meant to change once initialised.
Admin
Defining Latitude and Longitude extension properties of an enumerable... wow! That would a WTF!
Admin
"You know what else works to reduce the LOC? Not using a property where a public member will do."
First, there are several very good reasons for generally using properties and not public members. You may also be forced to do so by your toolset (data binding in XAML GUIs, for example).
Secondly, it doesn't save any LOCs in C# because that language has auto properties that create the code and handle the backing variable in one line (C# 3.0 and up), so declaring a property is a single LOC (by my count anyway):
public decimal longitude { get; set; }
Admin
Why not improve the solution? I think this is far better.
public static void GetPosition(string endpointId, ICollection<UserGeoPositionModel> userGeoPositions, out decimal? latitude, out decimal? longitude) { var position = userGeoPositions.FirstOrDefault(x => endpointId == x.UserID); if (position == null) { latitude = null; longitude = null; return; } latitude = Convert.ToDecimal(position.latitude); longitude = Convert.ToDecimal(position.longitude); }
Usage:
decimal? lat; decimal? lon;
GetPosition(endpointId, geoPositions, out lat, out lon);
// At this point we have both values in just one search...
Admin
Or return a tuple or an object with the lat and the lon.