https://youtu.be/0aKIeVr0n_E?t=867
swarm intellegence is so beautiful!
Heikki Artiainen
@Heikki Artiainen
Best posts made by Heikki Artiainen
-
RE: Random but Not Dumb Videos Thread
-
I think i might be producing WTF code
I think my preferred style of coding might be an abomination. I tend to make solutions with lots of public static variables and public static methods that implement the "api" for that class.
I cleaned up the code shown a bit to make it more like what i would pass on(translated comments and re-read them and added a few, but not many). Deleted contents of most functions (they all hold the same style so including everything felt reduntant)This is for a project that only i will work on and maintain. Would still like to hear your opinnions on just how bad this is, since one the goals of the project is to just get better in readability.I kind of fear the maintenance guys i have passed code onto have been afraid to call me out, since i did not hear complaints.
public class Post : IComparable<Post> { public static Point center = new Point(0, 0); //Post.point.toString() -> post public static Dictionary<string, Post> posts = new Dictionary<string, Post>(); //when to delete -> post. Used to find disliked post quickly public static UtilityClasses.OverflowDictionary<long, Post> dyingThreads = new UtilityClasses.OverflowDictionary<long, Post>(); //ordered by inevitable deletion moment. Used to delete naturally dying threads public static UtilityClasses.IndexedQueue<Post> deleteQueue = new UtilityClasses.IndexedQueue<Post>(1000); //posts sorted by axises, used to pull closest posts to user public static Loyc.Collections.BDictionary<float, Point> AxisXpoints = new Loyc.Collections.BDictionary<float, Point>(); public static Loyc.Collections.BDictionary<float, Point> AxisYpoints = new Loyc.Collections.BDictionary<float, Point>(); string storageLocation, type; long timePosted, deleteAt; double angle, distance; int likes = 0; bool uploadComplete = false; JObject serializedForm; CloudBlockBlob blob; Point point; static string createPost(string json) { JObject o = JObject.Parse(json); Point pos = new Point(JsonConvert.DeserializeObject<float>(o["x"].ToString()), JsonConvert.DeserializeObject<float>(o["x"].ToString())); string storageLocationName = pos.ToString(); CloudBlockBlob blob = AzureStuff.cloudBlobContainer.GetBlockBlobReference(storageLocationName); string readLink = blob.Uri + blob.GetSharedAccessSignature(AzureStuff.readOnlyPolicy); string uploadLink = blob.Uri + blob.GetSharedAccessSignature(AzureStuff.uploadPolicy); Post p = new Post(pos, o["type"].ToString(), readLink); posts[storageLocationName] = p; deleteQueue.push(p); return uploadLink; } //called often, the rest of the solution is built around this static string getPostsAroundPoint(Point p) { JArray arr = new JArray(); //lots of code return arr.ToString(); } static void markPostAsUploaded(string loc); static void updateBackup(); //TODO synchronizes likes across clients void updateLikeCounts(); string serialize(); string toString(); static Post deserialize(string json); void calculateDeleteMoment() { deleteAt = timePosted + effectOfLikes(); if (deleteAt < DateTimeOffset.Now.ToUnixTimeMilliseconds()) { delete(); } } //used to update time till deletion int effectOfLikes() { switch (likes) { case -1: return (int)TimeSpan.FromMinutes(60 * 12).TotalMilliseconds; case -2: return (int)TimeSpan.FromMinutes(60 * 8).TotalMilliseconds; case -3: return (int)TimeSpan.FromMinutes(60 * 4).TotalMilliseconds; case -4: return (int)TimeSpan.FromMinutes(60).TotalMilliseconds; case -5: return (int)TimeSpan.FromMinutes(10).TotalMilliseconds; case -6: return 0; default: return (int)TimeSpan.FromDays(7).TotalMilliseconds; } } void delete(long timerEstimate = 0) { string key = point.ToString(); dyingThreads.remove(this.deleteAt, this); posts.Remove(key); deleteQueue.remove(this); AxisXpoints.Remove(point.X); AxisYpoints.Remove(point.Y); long currentTime = DateTimeOffset.Now.ToUnixTimeMilliseconds(); Post p = deleteQueue.peek(); while (p != null && p.deleteAt < currentTime) { p.delete(); } } public void like() { likes++; updateLikeCounts(); if (likes > 0) return; //over 0 like's means there is no need to change timers string key = point.ToString(); if (likes == 0) { dyingThreads.remove(deleteAt, this); return; } deleteAt = DateTimeOffset.Now.ToUnixTimeMilliseconds() + effectOfLikes(); if (deleteAt < DateTimeOffset.Now.ToUnixTimeMilliseconds()) { delete(); return; } } public void dislike(); public int CompareTo([AllowNull] Post other); private Post(Point p, string type, string storage) { if (type == "text") { storageLocation = type.Substring(10, type.Length - 10); } else { storageLocation = storage; } distance = GetDistance(p, center); angle = angleOf(p, center); timePosted = DateTimeOffset.Now.ToUnixTimeMilliseconds(); calculateDeleteMoment(); JObject o = new JObject(); o["sas"] = storageLocation; o["type"] = type; o["posted"] = timePosted; o["likes"] = likes; o["x"] = point.X; o["y"] = point.X; serializedForm = o; } }
What do you think?
-
RE: I think i might be producing WTF code
@Gribnit well that would be the dream, but i doubt it will happen. Im just trying to limit the amount of traffic waste that happens i serve each client 1000 or 10000 points when 10 would have been enough
-
RE: I think i might be producing WTF code
@Gribnit And also i just really enjoy this kind of coding where i get to actually ponder about a CS problem and create an algorith. So much more rewarding than the drudgework that is 90% of a software project.
-
RE: I think i might be producing WTF code
@Gribnit
Its unbelieveable that hard to parse for humans things like HTML ever became the standard when things like Markdown show how much better things could be done. Shouldnt the first solution tried be the one that is easy to adopt? >:(
The user applications for this will be made in unity so throwing in HTML compatability would be over engineering at this point. That is also the reason why my coordinates are floats (im sure theres plenty of great engineering reasons their coordinates are floats.. but fuck me is that not the worst possible data type for coordinates ?)I might move to 3D points, but the scale of the Z axis would so tiny by comparison, that i would just ignore it in the calculations .. were talking range of -1 to 1 vs -1 000,000 to 1 000 000. where increments 0.01 below are imperceptible (just ignored) A point does not need to change once it is created, but as i understand final and const qualifiers are just for readability effectively since any compiler of even relative competense should optimize everything to those.
I have been writing this code with the goal being to maintain 100,000 posts up at the same time at least running on a potato (in the client application i dont care about performance. It wont be limited by mem or processing speed.
The biggest reason i made this thread was i realised, that i may become blind to how weird my coding style is, because i was in the position of being the one who's coding style was spreading at work for a few years. The code on display is not fully representative, as to the debth of qualifiers i would use in a work environment
-
RE: I think i might be producing WTF code
@Gribnit
can you go into further detail ? Sounds interesting
Ah i just didnt know the correct term for this. I was actually thinking of doing "bucketing" for the coordinates to intervals where it should be impossible for a client to notice that they are only served a smaller set instead of the whole. The intervals for the histogram i will have to test and find in real time tho.
I'll add that to the list of good ideas on how to improve the performance if it becomes a problem. Thank you
Latest posts made by Heikki Artiainen
-
RE: Random but Not Dumb Videos Thread
https://youtu.be/0aKIeVr0n_E?t=867
swarm intellegence is so beautiful! -
RE: I think i might be producing WTF code
@Bulb
Thank you for the suggestion. I will deffinetly make that a priority as for scaling up it would be critical. Currently for the proof-of-concept phase i wont waste board-gaming time to make it happen, but hopefully in the future ^^@Gribnit
Wish my dad was as cool as yours@Bulb said in I think i might be producing WTF code:
@Zecc Also, this is one of the cases where prototyping without a database and later refactoring to use it might be a waste of effort. If you need some kind of spatial index, at least PostgreSQL (PostGIS extension) and SQLite (SpatiaLite extension) support them (other databases have similar extensions implementing this Simple Features standard), so it would be easier to just use them from the start instead of writing a prototype in C# just to replace it with the SQL version later anyway.
did not know about postGIS. THANK YOU! still had great fun coming up with my own solution.
-
RE: I think i might be producing WTF code
To everyone sorry for the long absence. I am surprised by the amount of responses this thread is getting
@Gąska
Great point!
In this particular case you have to know how the JSON library being used performs. The toString method is going to fiddle down to just giving a ref char array where the expression is being held in this case. The lib pays the price in memory usage. There are only minimal transformations going on
-
RE: I think i might be producing WTF code
@Gąska
Mostly superstition.
if i were to map by point i believe the hashing algorithm, used(by default) would be something done over the complete memory representation of the object.
I assume that my points are going cluster heavily, and so would pref the extra bits of input in helping produce less uniform hashes
I also think that would make it way harder to keep backups- since my DB would be machine dependent? -
RE: I think i might be producing WTF code
@Gribnit
can you go into further detail ? Sounds interesting
Ah i just didnt know the correct term for this. I was actually thinking of doing "bucketing" for the coordinates to intervals where it should be impossible for a client to notice that they are only served a smaller set instead of the whole. The intervals for the histogram i will have to test and find in real time tho.
I'll add that to the list of good ideas on how to improve the performance if it becomes a problem. Thank you -
RE: I think i might be producing WTF code
@Gribnit
Its unbelieveable that hard to parse for humans things like HTML ever became the standard when things like Markdown show how much better things could be done. Shouldnt the first solution tried be the one that is easy to adopt? >:(
The user applications for this will be made in unity so throwing in HTML compatability would be over engineering at this point. That is also the reason why my coordinates are floats (im sure theres plenty of great engineering reasons their coordinates are floats.. but fuck me is that not the worst possible data type for coordinates ?)I might move to 3D points, but the scale of the Z axis would so tiny by comparison, that i would just ignore it in the calculations .. were talking range of -1 to 1 vs -1 000,000 to 1 000 000. where increments 0.01 below are imperceptible (just ignored) A point does not need to change once it is created, but as i understand final and const qualifiers are just for readability effectively since any compiler of even relative competense should optimize everything to those.
I have been writing this code with the goal being to maintain 100,000 posts up at the same time at least running on a potato (in the client application i dont care about performance. It wont be limited by mem or processing speed.
The biggest reason i made this thread was i realised, that i may become blind to how weird my coding style is, because i was in the position of being the one who's coding style was spreading at work for a few years. The code on display is not fully representative, as to the debth of qualifiers i would use in a work environment
-
RE: I think i might be producing WTF code
@Gribnit And also i just really enjoy this kind of coding where i get to actually ponder about a CS problem and create an algorith. So much more rewarding than the drudgework that is 90% of a software project.
-
RE: I think i might be producing WTF code
@Gribnit well that would be the dream, but i doubt it will happen. Im just trying to limit the amount of traffic waste that happens i serve each client 1000 or 10000 points when 10 would have been enough
-
RE: I think i might be producing WTF code
@Gribnit The center point meant to be the origin point. I calculate and store the distance and angle between the origin all the other points. I should propable put that inside the point class definition instead, and rename it to origin.
Im thinking of keeping a list of largest clusters of points in which direction they are in relation to that origin and keep a list of very dense areas.
Thought i would try to make an algorith for that tries to reason about those by using just the angle and distance and lets me assign some "local density metric" value to each pointHow can i get similiar highlights as you have in your posts btw?