r/AskProgramming • • Jul 16 '20

Other Has anyone else spent a long time implementing a solution to a problem only to discover there's a language feature that does exactly what you need?

I feel really dumb, I just spent almost 5 hours implementing a solution that is easily solved by a native feature of the language that I didn't know about.

103 Upvotes

55 comments sorted by

114

u/UkuCanuck Jul 16 '20

I’d call it five hours learning about the feature and how it might work underneath, and truly understanding what that feature’s benefits are

41

u/[deleted] Jul 16 '20

Wow, awesome advice thank you! Turned my perspective around on what I saw as a waste of time.

6

u/flagbearer223 Jul 16 '20

Yeah, I think as long as you're learning something new, you're not wasting your time! I spent 12 hours over the past 3 days working on adding OpenFaaS to our k8s development cluster, only to realize at the end of the 12 hours that it was gonna be more of a hassle to work with OpenFaaS compared to our current implementation. It could be considered wasted time, but now I understand a pretty widely used, very cool piece of technology!

4

u/bwz3r Jul 16 '20

If it's an open source project, maybe you can contribute something to it?

4

u/UkuCanuck Jul 16 '20

Or learn how someone who knows the subject really well implemented the same thing

2

u/MaestroLifts Jul 16 '20

Yeah this is not a waste of time at all. You’re also learning first hand the value in searching for the best solution to a problem.

8

u/Korzag Jul 16 '20

I did this once with C#'s FileSystemWatcher. Wrote a whole class that periodically scanned a targeted directory, checking when the files were last updated, new, or removed, then broadcast an event to inform listeners of changes.

Then by chance a few weeks later, I read something off topic to this where someone mentioned reliability issues with FSW and how it's not 100% reliable. Then I went back during a refactoring session and rewrote my class that did the same thing only using a timer instead of relying on system events like how FSW works

3

u/Mepperr Jul 17 '20

I had to do this exact same thing -- had to create my own FileSystemWatcher-like class using a timer, because the Windows API will skip publishing some File Watcher events when a lot of stuff is going on (I needed something that would detect ALL file changes).

I think I used the Stopwatch class for the timer.

3

u/Korzag Jul 17 '20

I used the actual System.Timers.Timer object I think. Pretty easy to configure it and all you gotta do is provide it an interval, a callback, tell it to autorestart and then start the thing.

1

u/variegatedvanilla Jul 17 '20

Yes! Think positive, cause you learned something!

11

u/TerminatedProccess Jul 16 '20

Sure.. it's happened to me.. it's a lesson to do you research before investing Dev time.. but sometimes you justc miss it.. it happens

3

u/[deleted] Jul 16 '20

hm, doing research before dev time is an interesting idea. i usually try to mentally map out the path i need to take (when working on an existing project, when buildign from scratch there's a whole design phase that would cover this).

what do you usually do in the research to make sure you're aware of everything?

the problem is i dont realize i need a particular implementation until i run up against some sort of error.

8

u/KernowRoger Jul 16 '20

Personally before I start to implement anything really complicated I Google "Do complicated thing in language" to see if I'm reinventing the wheel. But you can go years not knowing about some random thing that would make life easier until someone mentions it. It just how it goes haha

2

u/TerminatedProccess Jul 16 '20

I usually just Google.. it depends on the concept. You can ask the question of yourself. Has someone else already probably done this? You are just doing due diligence by looking around first. It's not a perfect science..

1

u/SapientMeat Jul 17 '20

Forget Google. Seriously. At least make it a last resort.

Googling "how to do this complicated thing" is the wrong way to do it. You may end up finding a solution that takes just a few steps. Easy enough, but each of those steps and all the functions involved are robust features themselves, not a means to one end. Sure, now you've got a working websocket, but you only know how it works in a very specific context.

If I can pass any one piece of information to the upcoming generation of programmers, it would be this: READ THE DOCUMENTATION. I'm baffled by the amount of questions I see on SO that scream "I didn't read the docs!" For any and every object you use, the documentation is your Bible. For every good language, countless hours were put into explaining to you how everything works in excruciating detail, and making it easily available.

19

u/kiwitims Jul 16 '20

It shows you had a good intuition on a clean way to solve the problem and just lacked the tool that would make it easier. It's much preferable to the inverse, where you throw language features at a problem while ignoring a simpler implementation.

For a mechanical analogy, it's like using a spanner to remove a wheel because you can't find the proper socket. You'll feel a bit dumb when someone shows you how to do it easier, but you're still better off than the guy who brought out an angle grinder instead.

4

u/theFoot58 Jul 16 '20

The ruby language , and RoR , got me once, real good, days!

1

u/snowe2010 Jul 17 '20

lol that was my first thought too. Anytime I do code golf I just assume I can get 95% of the way there with a single function call in ruby.

3

u/aelytra Jul 16 '20

One time I wrote a sorting algorithm for an array in C# as an intern cause I didn't know Array.Sort was a thing (I didn't know to look for static methods)...

3

u/[deleted] Jul 17 '20

Damn, this is really the type of problem that IDEs are supposed to solve ;-)

i.e. type your variable name and a . and see what the context thingy pops up. Nobody can remember all of the crazy stuff that's in the C#/Java/whatever libs these days.

Of course, it doesn't hurt to whack off stuff if you need it to do something specific.

3

u/aelytra Jul 17 '20

:) I was using Visual Studio at the time. I typed <varname> then a dot, saw there was no .Sort() and then spent the rest of the day implementing either merge sort or quick sort (I don't remember which - it's been over a decade..).

I think it got a chuckle or two.. Heh.

Felt pretty dumb after a day when Array.Sort came up in a search.

2

u/[deleted] Jul 17 '20

Ack! Ah well, it's all good experience :-)

1

u/OnlySeesLastSentence Jul 17 '20

What? Sorting is so easy.

sorted = false while sorted == false Array.shuffle for x=0, x<length-1, x++ Y=array[x] If Y

Actually, hold on, I'll finish the joke later. Gotta start work lol

I know I need to use that sorted flag and make it to where it goes to true if I manage to make to the end.

2

u/aelytra Jul 17 '20

..bogosort.. lol.

1

u/OnlySeesLastSentence Jul 17 '20

Ya.

I am too lazy to finish it at this point but my logic was essentially "sort it. And see if the next number is bigger. If it is, start over. But if you make it to the end of loop, and you do your comparison between the last value of x and the final element and it holds true, then congrats, set the flag to success. Otherwise leave the flag at false and keep trying.

3

u/[deleted] Jul 16 '20

The standard libraries on many languages are so large these days - you don't tend to know everything in each package/assembly/whatever.

This can happen alot.

One of the features of code reviews - is that you can find out when there's a better approach using standard techniques.

Instead of thinking I wasted time; look at how you've learn that feature.

3

u/WJMazepas Jul 16 '20

When I was learning programming on college i used to do this all the time because I didn't know how to look up on Google nor that it existed library's and functions for such things.

This only changed when I got a internship and had to started doing things that was above my programming level that i had to always look up how I could do such thing or what was that thing that I needed to do that I started to look for a built-in function or a library for it.

Then I started working with python and now I get real lazy if there are things that aren't built-in to the language

3

u/juiceboxbiotch Jul 16 '20

Well now you can say you pretty thoroughly understand that feature before implementing it!

0

u/SapientMeat Jul 17 '20

The one problem with Google is that most of the time, you have a pretty thorough understanding of that feature, but only in that specific context. That's the major pitfall of Googling: "How do I do this exact thing in this context using this language?"

You might have the problem solved, but if someone comes to you to replicate it, you'll probably say, "just download this package and add this class that inherits this class and copy this block of code."

It's like when Google came out, everyone forgot that languages comes with documentation, and now it's even organized so that you can see related classes that are in different areas of the library.

3

u/A_Philosophical_Cat Jul 17 '20

It takes a long time to build up the nose for "there should be a standard implementation of this". As a general rule for mature languages, if something could be useful in three wildly different applications, there's a good chance there's a standard implementation out there.

2

u/GobBeWithYou Jul 17 '20

Definitely. Spent a day or so trying to create proxy model in Qt to show an overview table of all shared columns in a bunch of other table models. Spent forever trying to hook the right signals and get it to update correctly. Then a typo in a Google search led my to this. Threw everything I did away and it took minutes to get working.

1

u/Blando-Cartesian Jul 16 '20

Long before StackOverflow existed, I spend some educational hours implementing a double parser in C++. Taught me to always look for a better way.

1

u/Got_no_chill Jul 17 '20

You sir, have discovered one of the many "face-palm till your face hurts" aspects of being a programer

1

u/paloumbo Jul 17 '20

Yes. Then you lurk API, and then the source code...

1

u/SapientMeat Jul 17 '20

This happens to me all the time.

I've been programming for going on 20 years now, though I am self-taught.

I started around the dot-com bubble, learning along with my dad who was had gotten laid off circa 1999 and started a web hosting/development company.

I wasn't trained by a software developer, it was just hours and hours of trying things and reading documentation. Lots and lots of documentation... Because of this, some elements of programming we just called whatever name sounded the most appropriate. Sometimes it was spot on (Inheritance), other times it was way off (turns out "Introspective Code" was called "Reflection" this whole time).

Regardless of whether or not you can do something; not having the vocabulary to summarize a complex topic makes it very difficult to get a straight answer. For me, that's the biggest culprit of these hour long search sessions only to arrive at an extremely straightforward solution - a lack of vocabulary/jargon.

It happens less now that I've had time over the years to take some courses and see how everyone else learned things, but it still happens occasionally.

If I'm approaching a project now with something I'm unfamiliar with - the first thing I do is browse the framework/language documentation. 9 times out of 10, you'll find a class that does what you need.

I brought up the naming discrepancies I've had earlier because more often than not a function/class/package is actually very intuitively named. If the documentation search doesn't yield any results, I search for repositories that contain function/class names I would use in my software. Again, if the name makes sense to you, someone else probably thought of it already. It's taken many frustrating hours of searching off my hands.

1

u/Skasch Jul 17 '20

That happened to me some time ago: I wrote a class in Python allowing me to create and clean a mock folder to run unit tests for some I/O functions, only to discover later that the standard library has a tempfile module.

1

u/OnlySeesLastSentence Jul 17 '20

Not hours, but I spent a lot of time starting out in programming making stuff for testing valid inputs when I could have just said try/except. Also in python I didn't know about

if "dead" in ("player1.status","player2.status", [and so on],"player10.status")

So I would stretch it out C style and say like "if player1=="dead" or player2=="dead"....

(Yes. I know C doesn't work right and doesn't let you compare strings with an == but play along.)

1

u/[deleted] Jul 17 '20

I implemented a streaming binary to text converter to link a Popen stdin it stdout to a text only stream reader or writer, without knowing that there's TextIOWrapper, in Python.

Wasn't a lot of work, but now I feel stupid that that code sits somewhere and is being used.

1

u/variegatedvanilla Jul 17 '20

All the time. Especially as a junior dev.

You don't learn everything in school. Things come with experience, and experience consists of a whole lot of times like this.

JavaScript has given me a lot of them recently, object destructuring and the ... operator especially.

List comprehensions in Python were the most amazing thing ever. Took me a while to figure out some code, but when I found them I started using them all the time. Now I have a lot of clean and readable one-liners for what was previously ten or more lines of code.

A lot of languages do have too many features. That said, learning more you do every so often find a gem that will improve your code and make you feel good too :)

1

u/vegetablestew Jul 16 '20

Watch out. You gonna get spoiled by programming language that will never get you a job.

4

u/Korzag Jul 16 '20

What do you mean? Why would a language that is feature rich never get you a job?

-2

u/vegetablestew Jul 16 '20 edited Jul 16 '20

Because a lot of times what is popular isn't necessarily good and vice versa.

How easy it is to hire plays a factor as well, a bigger one in fact.

2

u/SapientMeat Jul 17 '20

People are hating on the truth! If you can't Google the answer to it, "that language sucks". Wow, I'm upset this reply got down voted. It's true.

"Ugh, why would I ever need to know about data structures and unions when I can just write a JSON string and have one there."

Well, because eventually in your career you're going to have a deadline that includes a novel piece of code. And if you don't actually understood the fundamentals of how every detail that python script worked from the ground up, you're on your own.

1

u/zigs Jul 17 '20

I think the real problem is the delivery. It's a good point, but the way it's said is not very productive. Thus downvotes.

It's also something that irks me. If google doesn't have the answer, you gotta use your noggin'. It seems that a lot of programmers do not want to do this. Also, I google all the freaking time, so nobody even think of making that point. :p

1

u/vegetablestew Jul 17 '20 edited Jul 17 '20

It's common complaint. I'd love to work in Clojure, Elixir, Haskell, ReasonML, F# etc. where am at is the same boring stuff like JS PHP Ruby Java C# Python. What's popular keeps being popular by virtue of the amount of resources both googlable answer that exists and hire able people that can hit the ground running.

Unfortunate, but it is just the way things are. The best doesn't always win.

1

u/zigs Jul 17 '20

Functional languages are also harder to understand. We could argue forever (and many do) if it's worth understanding and converting everyone to, but like you say, the majority is not going to be convinced without a very large body behind it. A catch-22.

I have similar grievances with PHP and Python because there's a lot of bad advice out there. It's generally more difficult to find a good advice when the question is a matter of opinion and not fact, because there are so many novices and their pet blogs out there. StackOverflow somewhat mitigates this issue, but not completely.

So strange as it sounds, I'm actually happy that these languages are not as compelling. Go insularity!

1

u/SapientMeat Jul 20 '20

That's a very good point. Since the languages are so high level there are so many ways to perform the same action, and at least as many different opinions on what one is best.

One thing I rarely see though is performance tests, because in Python and PHP the difference in a single function is usually so minuscule. For high level languages you can go to StackOverflow and you'll see an answer that got accepted because it worked for OP's use-case, but it's actually a really hacky way to do it.

I've noticed that on questions about functional languages the accepted answer is generally the best answer. I think that has a lot to do with the rigidity of the language.

1

u/SapientMeat Jul 20 '20

You're also subjecting yourself to Google syndrome. Even if you find all the answers and you never have to use your noggin' you're simply not retaining the information in the same way as if you dug through the documentation to see how a function works.

1

u/zigs Jul 21 '20

I don't think that's 100% a bad thing. If you google you might find new info. of course, you might also find new *bad* info.

For me, most of the time I google to find the right page in the documentation.

1

u/SapientMeat Jul 22 '20

Google Syndrome is 100% a bad thing lol.

But if you're using it as a tool to get directly to the documentation/API that you need that's fine.

If you're going down a Google wormhole of something like: "How do I make Python code work inside my C++ application?" You're in for a very tedious road indeed.

0

u/SapientMeat Jul 22 '20

TangentialRant downvoteRant = new TangentialRant("First paragraph"); Answer answer = new Answer("It's at the end");

if (!_currentReader.PriorityIs(Read(answer)) downvoteRant = new TangentialRant("""Eh, delivery is always the scapegoat nowadays. Everyone needs to take a chill pill. If You read something written in a general context, and it happens to poke your ego wrong, the person who said it isn't at fault. Like Carlin said, change the ******* channel! Hide my post if you don't like part of it, but down voting is making it less likely for someone who may take something away from the rest of it to see it. In fact, if a score reaches 1, you shouldn't be able to down vote to zero. Either it gets removed for violating a rule or it stays at 1. It's little passive aggressive features like this, where a post with "bad delivery" but otherwise teachable information gets hidden and subtly propegates this insane cancel culture we live in!""'');

else answer= """I've had a new developer under me ask me the same question I posed in my reply, scoffing. We were working on optimizing finding and pairing very specific data in very large, complex data sets and it required everyone to think far outside the box.

This person was extremely efficient using JavaScript with Google on one screen, but as soon as we had to brainstorm about a novel way to use the fundamental algorithms, they brought nothing to the table."""

1

u/[deleted] Jul 17 '20

been using laravel forever. typically i'd do soemthing like

php artisan make:model Property

and then i'd do

php artisan make:migration CreateProperty

just to save me some time writing the model and the migration. just found out about

php artisan make:model Property -m

which creates the model and the migration with some basic columns already created. not exactly what you're talking about but after what feels like 10 years or something, just learned that... wow

1

u/SapientMeat Jul 17 '20

This is like when I found out that Visual Studio let's you type: "prop" or "propfull" + tab + tab, and get either a simple property with get/set or a full property with get/set methods and a private backing variable.

1

u/[deleted] Jul 17 '20

Nice, love when i uncover a new ide shortcut