this post was submitted on 13 Jun 2024
2 points (100.0% liked)

Learn Programming

1560 readers
1 users here now

Posting Etiquette

  1. Ask the main part of your question in the title. This should be concise but informative.

  2. Provide everything up front. Don't make people fish for more details in the comments. Provide background information and examples.

  3. Be present for follow up questions. Don't ask for help and run away. Stick around to answer questions and provide more details.

  4. Ask about the problem you're trying to solve. Don't focus too much on debugging your exact solution, as you may be going down the wrong path. Include as much information as you can about what you ultimately are trying to achieve. See more on this here: https://xyproblem.info/

Icon base by Delapouite under CC BY 3.0 with modifications to add a gradient

founded 1 year ago
MODERATORS
 

I'm trying to make minesweeper using rust and bevy, but it feels that my code is bloated (a lot of for loops, segments that seem to be repeating themselves, etc.)

When I look at other people's code, they are using functions that I don't really understand (map, zip, etc.) that seem to make their code faster and cleaner.

I know that I should look up the functions that I don't understand, but I was wondering where you would learn stuff like that in the first place. I want to learn how to find functions that would be useful for optimizing my code.

top 9 comments
sorted by: hot top controversial new old
[–] CameronDev@programming.dev 2 points 1 month ago (1 children)

Experience is the best teacher. Keep writing code, revisit old code and rewrite it.

Also, is worth knowing when not to optimise. Code you can read is code you can maintain, and some optimisations are not as readable.

Learn how to use a profiler. Its a bit of an artform, but learning to interpret the results will help you find slow code sections. It'll also help you determine if your optimisations are actually worthwhile. Measure first, optimise second.

[–] hactar42@lemmy.world 1 points 1 month ago

Also, is worth knowing when not to optimise. Code you can read is code you can maintain, and some optimisations are not as readable.

This is one of my biggest pet peeves. So many people want to equate the number of lines as a sign of how well it's programmed. But all they really do is chain a bunch of stuff together that makes it harder to debug.

[–] fourwd@programming.dev 1 points 1 month ago* (last edited 1 month ago)

When I started learning programming, I was like "tf is a map function?" and I always forgot about it. Then I tried the functional programming language Erlang and understood all these functions very well. But there is a downside, now most for-loops in C++ look terrible to me :)

[–] brisk@aussie.zone 1 points 1 month ago* (last edited 1 month ago)

The functions you've called out are higher order functions regularly associated with the functional programming paradigm. "In the first place" for a lot of people would be a functional programming course at a university.

For your specific case, rust (like a few other languages) implements these through iterator programming. There's a section in the rust book that might help.

Apart from academia you learn from experience, including a healthy amount of reading other people's code, just like you did to find out about these functions in the first place!

[–] Vent@lemm.ee 1 points 1 month ago

I learned that type of stuff in college, so I can't personally recommend any online sources. However, I can tell you that what you're looking for falls under "Data Structures and Algorithms". IIRC my degree required 3 classes with that name. Lots of sorting algorithms in that field since they make great case studies.

You learn the various data structures and algorithms available, their strengths and weaknesses, how they work, when to use them, etc...

You also learn how to measure performance, like Big-O notation, the bane of many a CS student's existence.

[–] grue@lemmy.world 1 points 1 month ago* (last edited 1 month ago) (1 children)

What you're asking about (how to learn clever techniques to e.g. turn your naive O(n^2^) algorithm into an O(n log n) one) is the kind of stuff taught in an algorithms / theory course as part of a CS degree. Here are some of the first search results for open content courses I found that look like they cover the right topics:

https://ocw.mit.edu/courses/18-404j-theory-of-computation-fall-2020/pages/syllabus/

http://openclassroom.stanford.edu/MainFolder/CoursePage.php?course=IntroToAlgorithms

You also might want to consider a combinatorics / discrete math course.

[–] NostraDavid@programming.dev 2 points 4 weeks ago* (last edited 4 weeks ago)

In case others want to get into 18.404j, but don't have the prerequisite knowledge, I made a pretty complete graph of all available MIT courses (at least until 2023) and linked them as a left-to-right dependency graph:

https://thaumatorium.com/articles/mit-courses/mit.drawio.svg

Just search for 18.404j - it's an SVG, so search works.

[–] Akrenion@programming.dev 0 points 1 month ago (1 children)

Map, Filter, Reduce Those are the big three for data. More important than those however is mindset and patience with oneself. Writing code that works is the first and most impressive step. Optimizations are fun to think about but unless your computations are sluggish and repeat a lot of unnecessary steps they are rarely a priority.

Build something and shelf it. Trust me, in but a few months you will look back in bewilderment and realize how much you've grown.

[–] grue@lemmy.world 1 points 1 month ago

but unless your computations are sluggish and repeat a lot of unnecessary steps

In other words, the kind of optimizing that's worth it is choosing a better algorithm to reduce its big-O complexity class.