Yo whatup

  • 0 Posts
  • 19 Comments
Joined 9 months ago
cake
Cake day: September 28th, 2023

help-circle


  • Smart pointers model ownership. Instead of (maybe) a comment telling you if you have to free/delete a returned pointer this information is encoded into the type itself. But that’s not all, this special type even handles the whole deleting part once it goes away.

    Since they model ownership you should only use them when ownership should be expressed. Namely something that returns a pointer to a newly allocated thing should be a std::unique_ptr because the Callie has ownership of that memory. If they want to share it (multiple ownership of the object) there’s a cheap conversion to std::shared_ptr.

    How about a function that takes in an object cause it wants to look at it? Well that doesn’t have anything to do with ownership so make it a raw pointer, or better yet a reference to avoid nullability.

    What about when you’ve got a member function that wants to return a pointer to some memory the object owns? You guessed it baby raw pointer (or again reference if it’ll never be null).





  • Traister101@lemmy.todaytoProgrammer Humor@programming.devwait what
    link
    fedilink
    arrow-up
    1
    arrow-down
    1
    ·
    edit-2
    4 months ago

    Looking at code on somebody else’s screen is entirely missing the point of using tabs over spaces. The entire point is that mine looks like how I want and theirs looks like how they want even though the file is identical. We can each have wildly different tab width and it’ll look wildly different to each of us when we program. That’s again the point.

    Code formatters are great! I love them. Using tabs over spaces is objectively a better formatting option. One of my favorite features in code formatters is that they’ll swap out spaces to tabs for you insane people who insist on mashing the space bar to indent.


  • Traister101@lemmy.todaytoProgrammer Humor@programming.devwait what
    link
    fedilink
    arrow-up
    1
    arrow-down
    1
    ·
    edit-2
    4 months ago

    What’s yaml have to do with anything? It’s like python with syntactic whitespace which is unrelated to this discussion. The Tab vs Space debate is entirely around non syntactic whitespace which doesn’t effect how the code is parsed. And yes Python technically does both tabs and spaces but it’s all sorts of fucky.

    Terminal editors while still used a ton aren’t really what I was referring to. Newer terminal editors such as Helix have tab width configured per language most of which default to a width of 4 spaces but toml/yaml both default to 2 spaces. I was mainly referring to GUI editors as frankly that’s just what most people use nowadays. JetBrains IDEs, Visual Studio, Eclipse, VS Code, Notepad++ were primarily what I was thinking of as I’ve used all of them and they all default to a tab width of 4 hence why I said nearly universal. Also I said nearly terminal editors being the only editors I’ve used that don’t default to a width of 4 seems like a fair usage of the term.


  • Traister101@lemmy.todaytoProgrammer Humor@programming.devwait what
    link
    fedilink
    arrow-up
    6
    arrow-down
    2
    ·
    4 months ago

    Then don’t? The whole reason nearly all the spaces guys do 4 spaces is cause that’s the nearly universal tab width. You won’t like this but the same exact argument can be made for spaces yet I’d bet you haven’t even once configured the width of those.

    I don’t actually change tab width, it’s the default 4 spaces equivalent for me but just because I don’t take advantage of the ability doesn’t mean I should prevent others from doing so.


  • Traister101@lemmy.todaytoProgrammer Humor@programming.devwait what
    link
    fedilink
    arrow-up
    98
    arrow-down
    10
    ·
    4 months ago

    Tabs are objectively the better choice as it allows each dev individually to decide tab width in their editors. Spaces in contrast don’t allow this same flexibility as they are used for much more than simply indentation, for example you likely put a space after each argument or operator IE func(arg1, arg2) or 1 + 2.




  • It’s part of the type yet it’s also a unique identifier. That’s the whole thing with east or west const. const int * is a immutable mutable pointer that points to mutable immutable memory. int *const is a mutable immutable pointer that points to immutable memory. int const * is the same type as the first example, a immutable mutable pointer that points to mutable immutable memory.

    Same stuff applies to references which makes it easier to think of the variable owning the * or & as if you want that pointer or reference to be const it has to go after.

    Edit:I am a moron who managed to get it exactly backwards :|


  • Well like asembly has “int types” and “float types” as there’s specific instructions for those operations but those instructions don’t actually care if the bits are for a float or an int. Types in a language are used to restrict the valid operations. In a statically typed language you cannot call cat.bark() or dog.meow() because the property’s of the type, what things you can do with it are known before the program runs. In a dynamically typed language such as Python cat.bark() might or might not be valid so it has to check at runtime for a method throwing an error if it doesn’t exist.

    Static/Dynamic typing is a difference of when. Java has static typing but you can also just pass raw Objects around and cast when needed. It even throws a runtime exception similar to how Python or JavaScript would fail. However Java is of course ultimately statically typed everything just shares a common parent class and has types at runtime which allows for some some psudo dynamic behavior




  • Alternatively as both floats (32 bit) and doubles (64 bit) are represented in binary we can directly compare them to the possible values an int (32 bit) and a long (64 bit) has. That is to say a float has the same amount of possible values as an int does (and double has the same amount of values as a long) . That’s quite a lot of values but still ultimately limited.

    Since we generally use decimal numbers that look like this 1.5 or 3.14. It’s setup so the values are clustered around 0 and then every power of 2 you have half as many meaning you have high precision around zero (what you use and care about in practice) and less precision as you move towards negative infinity and positive infinity.

    In essence it’s a fancy fraction that is most precise when it’s representing a small value and less precise as the value gets farther from zero