Programmer Humor

This magazine is not receiving updates (last activity 54 day(s) ago).

Tolookah , in got him

I didn't know why, but *++p bugs me

captainjaneway ,
@captainjaneway@lemmy.world avatar

[Thread, post or comment was deleted by the author]

  • Loading...
  • allan , (edited )

    It's very likely plain old C

    captainjaneway ,
    @captainjaneway@lemmy.world avatar

    [Thread, post or comment was deleted by the author]

  • Loading...
  • SpaceNoodle ,

    Not if you've done a lot of pointer math

    eager_eagle ,
    @eager_eagle@lemmy.world avatar

    I thought it was just incrementing the address and dereferencing it, but I don't write C or C++. What is being overloaded there?

    Tyoda , (edited )

    Perhaps *(p += 1) will be to your liking?

    fluckx , (edited )
    p = 1
    
    x = ++p
    // x = 2
    // p = 2
    
    p = 1
    x  = p++
    // x = 1
    // p = 2
    

    ++p will increase the value and return the new value

    p++ will increase the value and return the old value

    I think p = p + 1 is the same as p++ and not as ++p.
    No?

    SpaceNoodle , (edited )

    (p += 1) resolves to the value of p after the incrementation, as does ( p = p + 1).

    fluckx ,

    Yes.

    p++ == p+= 1 == p = p + 1 are all the same if you use it in an assignment.

    ++p is different if you use it in an assignment.
    If it's in its own line it won't make much difference.

    That's the point I was trying to make.

    SpaceNoodle ,

    No.

    ++p returns incremented p.

    p += 1 returns incremented p.

    p = p + 1 returns incremented p.

    p++ returns p before it is incremented.

    fluckx ,

    Right. So i had them the other way around. :D

    Thanks for clarifying.

    Tyoda ,

    In C an assignment is an expression where the value is the new value of what was being assigned to.

    In a = b = 1, both a and b will be 1.

    a = *(p = p + 1)
    

    is the same as

    p += 1
    a = *p
    

    , so ++p.

    fluckx ,

    What I meant was:

    In the screenshot it said x = *(++p) and iirc that is not the same as saying x = *(p++) or x = *(p += 1)

    As in my example using ++p will return the new value after increment and p++ or p+=1 will return the value before the increment happens, and then increment the variable.

    Or at least that is how I remember it working based on other languages.

    I'm not sure what the * does, but I'm assuming it might be a pointer reference? I've never really learned how to code in c or c++ specifically. Though in other languages ( like PHP which is based on C ) there is a distinct difference between ++p and (p++ or p+= 1)

    The last two behave the same. Though it has been years since I did a lot of coding. Which is why I asked.

    I'll install the latest PHP runtime tonight and give it a try xD

    xmunk ,

    Much better... but can we make it *((void*)(p = p + 1))?

    shrugal ,
    @shrugal@lemm.ee avatar

    How about some JavaScript p+=[]**[]?

    Faresh ,

    Why are you casting to void*? How is the compiler supposed to know the size of the data you are dereferencing?

    xmunk ,

    This would probably cause a compiler error....

    But assuming it doesn't the context is p_ch = the bits above... the code declaring p_ch isn't shown but I'm guessing that the value here is actuality a pointer to a pointer so nothing illegal would be happening.

    Lastly... C++ is really lacking in guarantees so you can assign a char to the first byte of an integer - C++ doesn't generally care what you do unless you go out of bounds.

    The reason I'm casting to void* is just pure comedy.

    marcos , (edited )

    That *++ operator from C is indeed confusing.

    Reminds me of the goes-to operator: --> that you can use as:

    while(i --> 0) {
    
    letsgo ,

    That's not a real operator. You've put a space in "i--" and removed the space in "-- >". The statement is "while i-- is greater than zero". Inventing an unnecessary "goes to" operator just confuses beginners and adds something else to think about while debugging.

    And yes I have seen beginners try to use <-- and --<. Just stop it.

    marcos ,

    The sheer number of people that do not expect a joke on this community... (Really, if you are trying to learn how to program pay attention to the one without the Humor on the name, not here.)

    Well, I guess nobody expects.

    SpaceNoodle ,

    Where do you think we are?

    lseif ,

    welcome to C

    Damage , in There's no place like `[::1]`

    No place like localhost

    AndrasKrigare ,

    No place like loopback

    RedditWanderer , in got him

    Only the sith deal in absolutes

    Entropywins ,

    You must be a sith...

    69420 ,

    Absolutely.

    kureta ,

    They might be a sith.

    Entropywins ,

    Ah shit...

    diviledabit ,

    Ah bowel movement...

    It could be a fart brewing

    DoctorNope , in Best constant in all of iOS!

    What about the label for my father’s brother’s nephew’s cousin’s former roommate?

    DmMacniel ,

    Which would surmount to absolutely nothing?

    TORFdot0 ,

    Depends on if your Schwartz is as big as mine. And how you use it

    reverendsteveii , in got him

    rules aren't there to be enforced, they're there so that when you break them you take a second to think about why.

    ZILtoid1991 , in got him

    Why is multiple levels of indentation bad?

    IDK, but if the reason is "to break stuff into multiple functions", then I'm not necessarily writing yet another single-use function just to avoid writing a comment, especially in time critical applications. Did that with a text parser that could get text formatting from a specifically written XML file, but mainly due to it being way less time critical, and had a lot of reused code via templates.

    theherk ,

    Like with everything, context matters. Sometimes it can indicate poorly structured control flow, other times inefficient loop nesting. But many times it is just somebody’s preference for guard clauses. As long as the intent is clear, there are no efficiency problems, and it is possible to reach the fewest branches necessary, I see no issues.

    zea_64 ,

    Indentation implies there's some control structure causing it. Too many control structures nested gets hard to mentally keep track of. 3 is arbitrary, but in general more indentation => harder to understand, which is bad.

    frezik ,

    It's important to remember that Linus is primarily writing about C code formatting. C doesn't have things that tend to create more deeply nested structures, such as a formal class syntax, or nested functions.

    Going too deep is still bad--as zea notes, it's an indication of control structures run amok--but the exact number is dependent on the language and the context.

    diviledabit ,

    And you never include the switch-case-block indentation levels.

    dejected_warp_core ,

    Honestly I don't mind the indentation since C isn't going to give us many ways to address this with as little code.

    That said, with compilers that are good at inlining trivial functions, I really do appreciate the "it does what it says on the tin" approach to using functions on things like this. Even if they're only used once. Comments would help too.

    The logic in these if statements is inscrutable on a cold read like this. To me, that's a maintenance risk; imagine seeing a snippet this size on a PR. Having functions that name what the hell is going on could only help.

    Carighan , in Like getting 9 women pregnant and expecting a baby in 1 month
    @Carighan@lemmy.world avatar

    Also isn't Manor Lords plodding along fairly well? Who cares if they take a long time, better release done than Ubisoft.

    Merlin404 ,

    Bought it the first day, and I expect them to take a long time before its ready. The game have been in development for many years and it looks and feels amazing, so i want it to take time!

    flei , in Added Bugs to Keep my job

    "various changes"

    MsFlammkuchen ,

    Initial commit

    turbowafflz ,

    I have on multiple occasions done a much more verbose variant of that: "I genuinely cannot remember what I changed"

    Bougie_Birdie , in What's the difference between a light bulb and a programmer?
    @Bougie_Birdie@lemmy.blahaj.zone avatar
    Deebster , (edited ) in Best constant in all of iOS!

    CNLabelContactRelationYoungerCousinMothersSiblingsSonOrFathersSistersSon

    The label for the contact’s mother’s sibling’s younger son or father’s sister’s younger son.

    I thought it was just a male cousin, but it doesn't include a cousin who's your uncle's son. Which culture needs this?

    Kache ,

    It refers to a male cousin that is NOT in the same paternal line, so maybe not too uncommon?

    skullgiver ,
    @skullgiver@popplesburger.hilciferous.nl avatar

    This has come up in the past. I believe Mandarin has a short and quick word for this. English doesn't have the same cultural background so there's no quick name for it.

    Compare this to writing out "MothersOrFathersBrotherOrSistersDaughterOrSon" instead of "cousin". In fact, my own language doesn't even have a word for "sibling", all we have is "brother or sister", despite being surrounded by languages that do have such a word.

    Avalokitesha ,

    I think Chinese and Korean culture share this concept, and I wouldn't be surprised if there were more Asian languages who did. Since a daughter joins her husband's family upon marriage, their children are considered belonging to the other family. I recently learner that apparently there's a saying in Korean that daughters always leave things at their mother's house when they get married so they have a reason to come back despite having left the family.

    fibojoly ,

    China, at least.
    Lots of distinction between mother side and father side.
    Grandma can be 老老 laolao (mother's mother) or 奶奶 nainai (father's mother), for example.

    baseless_discourse ,

    *姥姥

    fibojoly ,

    Thanks for correcting. Pleco confirmed the one I wrote, but this is the one I learnt and actually wanted to write!

    abrahambelch , in The perfect way to round out your career
    @abrahambelch@programming.dev avatar

    Goose Farmer - Remote

    Aceticon , in Users

    I've actually worked with a genuine UX/UI designer (not a mere Graphics Designer but their version of a Senior Developer-Designer/Technical-Architect).

    Lets just say most developers aren't at all good at user interface design.

    I would even go as far as saying most Graphics Designers aren't all that good at user interface design.

    Certain that explains a lot the shit user interface design out there, same as the "quality" of most common Frameworks and Libraries out there (such as from the likes of Google) can be explained by them not actually having people with real world Technical Architect level or even Senior Designer-Developer experience overseeing the design of Frameworks and Libraries for 3rd party use.

    dependencyinjection ,

    Im a developer and I should not be allowed to wing it with UI/UX design.

    Lifter ,

    Yes you should. I think most comments here are about products that have millions of users where it's actually worthwhile spending all that extra time and money to perfect things.

    For most development, it isn't worthwhile and the best approach is to wing it, then return later to iterate, if need be.

    The same goes for most craftsmanship, carpentry in particular. A great carpenter knows that no-one will see the details inside the walls or what's up on the attic. Only spend the extra time where it actually matters.

    It triggers me immensely when people say "I could have made a better job than that" about construction work. Sure maybe with twice the budget and thrice the time.

    madcaesar ,

    Exactly. I'd also like to add, look at Google stuff their ui / ux is routinely horseshit. So don't tell me there are ui/ux gurus out there GIGAchading user interfaces.

    A lot of this shit is trial and error and even then they still fuck it up.

    Make it accessible, make it legible and then fine tune it after.

    Anamana ,

    Many Designers are not good at knowing what their users need, because they don't have the resources, background or education to understand user behaviour.

    Zagorath , in I can see the reference!
    @Zagorath@aussie.zone avatar

    I've used C but never C++. What does it mean for a variable's type to be int&? From when I've used it, & would be used to create a pointer to a variable, but the type of that pointer would be int*.

    edit:

    never mind, I looked it up. It's a "reference" instead of a pointer. Similar, but unlike a pointer it doesn't create a distinct variable in memory of its own.

    xmunk ,

    In my experience, it's rare to see int& in day to day as a regular old lvalue... it essentially just allows you to alias a variable to another name. It's much more common to see them used in function parameters to leverage pass by reference. In C++ pointers usually aren't particularly useful compared to just passing things by reference since stack variables get auto-gc'd it's the preferred style of frameworks like Qt and is extremely easy to use.

    Here's a breakdown if you want more information https://en.cppreference.com/w/cpp/language/reference

    TheEntity ,

    never mind, I looked it up. It’s a “reference” instead of a pointer. Similar, but unlike a pointer it doesn’t create a distinct variable in memory of its own.

    I'm almost sure it does create a distinct variable in memory. Internally it's still a pointer, specifically a const pointer (not to be confused with a pointer to a const value; it's the address that does not change). Think about it as a pointer that is only ever dereferenced and never used as a pointer. So yes, like the other commenter said, like an alias.

    Sonotsugipaa ,

    I don't think references are variables: you can't modify them, and AFAIR you can't have pointers to them, with the possible but unlikely exception of non-static member references.

    TheEntity ,

    An int& reference is just as much of a variable as int* const would be (a const pointer to a non-const int). "Variable" might be a misnomer here, but it takes just as much memory as any other pointer.

    firelizzard ,
    @firelizzard@programming.dev avatar

    For references within a scope, you’re probably right. For references that cross scope boundaries (i.e. function parameters), they necessarily must consume memory (or a register). Passing a parameter to a function call consumes memory or a register by definition. If a function call is inlined, that means its instructions are copy-pasted to the call location so there’s no actual call in the compiled code.

    RustyNova , (edited ) in got him

    While I totally agree with that philosophy, it heavily depends on the language.

    For Rust, my philosophy is more like this:

    • Impl + fn body don't count, as well as async blocks if they span the whole function
    • do not nest more than one if statement. You probably better using guard clauses or matches
    • do not put loops into an if statement.
    • do not nest loops unless clearly shown to be (X, Y) indexing
    • method chaining is free
    • do not nest closures, unless the nested closure doesn't have a {} block
    • do not use mod unless it's test for the current module. No I don't want to Star Wars scroll your 1000 line file. Split it.
    magikmw ,

    I'ma gonna steal this.

    calcopiritus ,

    Why have an async block spanning the whole function when you can mark the function as async? That's 1 less level of indentation. Also, this quite is unusable for rust. A single match statement inside a function inside an impl is already 4 levels of indentation.

    RustyNova ,

    Those async blocks exist when doing async in traits.

    And I never said I respected the 4 level of indentations. That's exactly the point of those rules. Keep it lowly indented but still readable

    Doods ,

    A single match statement inside a function inside an impl is already 4 levels of indentation.

    How about this?

    The preferred way to ease multiple indentation levels in a switch statement is to align the switch and its subordinate case labels in the same column instead of double-indenting the case labels. E.g.:

    switch (suffix) {
    case 'G':
    case 'g':
            mem <<= 30;
            break;
    case 'M':
    case 'm':
            mem <<= 20;
            break;
    case 'K':
    case 'k':
            mem <<= 10;
            /* fall through */
    default:
            break;
    }
    

    I had some luck applying this to match statements. My example:

    
    let x = 5;
    
    match x {
    5 => foo(),
    3 => bar(),
    1 => match baz(x) {
    	Ok(_) => foo2(),
    	Err(e) => match maybe(e) {
    		Ok(_) => bar2(),
    		_ => panic!(),
    		}
    	}
    _ => panic!(),
    }
    
    

    Is this acceptable, at least compared to the original switch statement idea?

    lseif ,

    i personally find this a lot less readable than the switch example. the case keywords at the start of the line quickly signify its meaning, unlike with => after the pattern. though i dont speak for everybody.

    Doods ,

    How about this one? it more closely mirrors the switch example:

    match suffix {
    'G' | 'g' => mem -= 30,
    'M' | 'm' => mem -= 20,
    'K' | 'k' => mem -= 10,
    _ => {},
    }
    

    How about this other one? it goes as far as cloning the switch example's indentation:

    match suffix {
    'G' | 'g' => {
    	mem -= 30;
            }
    'M' | 'm' => {
    	mem -= 20;
            }
    'K' | 'k' => {
    	mem -= 10;
            }
    _ => {},
    }
    
    lseif ,

    the problem is that, while skimming the start of each lines, nothing about 'G' | 'g' tells me that its a branch. i need to spend more time parsing it. mind you, this may simply be a problem with rust's syntax, not just ur formatting.

    RustyNova ,

    It's a lot less readable imo. As well than a cargo fmt later and it's gone (unless there's a nightly setting for it)

    Doods ,

    Formatters are off-topic for this, styles come first, formatters are developed later.

    My other reply:

    How about this one? it more closely mirrors the switch example:

    match suffix {
    'G' | 'g' => mem -= 30,
    'M' | 'm' => mem -= 20,
    'K' | 'k' => mem -= 10,
    _ => {},
    }
    

    How about this other one? it goes as far as cloning the switch example's indentation:

    match suffix {
    'G' | 'g' => {
      mem -= 30;
           }
    'M' | 'm' => {
      mem -= 20;
           }
    'K' | 'k' => {
      mem -= 10;
           }
    _ => {},
    }
    
    folkrav ,

    I mean, I use formatters everywhere I can exactly so I don’t have to think about code style. I’ll take a full code base that’s consistent in a style I dislike, over having another subjective debate about which style is prettier or easier to read, any day. So whatever cargo fmt spits out is exactly what I’ll prefer, regardless of what it looks like, if only for mere consistency.

    calcopiritus ,

    Well, of course you can have few indent levels by just not indenting, I don't think the readability loss is worth it though. If I had give up some indentation, I'd probably not indent the impl {} blocks.

    AVincentInSpace ,

    what if I need to nest if lets

    RustyNova ,

    Use a match? Unless it's for guard clauses, a match is fine enough

    AVincentInSpace ,

    what if i need to if let on the result of another if let

    RustyNova ,

    Oh, then you use and_then() or something similar.

    There's also the possibility to use the guard clauses patern and do let <...> = <...> else {}.

    And finally, you can always split into another function.

    It's not straight rules. It depends on what makes it more readable for your case.

    KillingTimeItself , in Users

    actually, i would like to counter this. Developers often times put together shitty UIs that are hard to navigate (mostly because UI design is bad and we've been living with floating WMs for the past 30 years so nobody knows any fucking better for some godforsaken reason)

    But it's no fault of the user for using a shitty interface if it was designed to be used in that manner, by the person who built it. This is why so many people like CLI, it's impossible to fuck up. You can use it wrong as a user, but that's because it has specific syntaxing. It's designed to only be used in that one manner, where as most graphical applications are designed to be "generally applicable" for some reason, and then when a user uses it in a "generally applicable" manner, somehow that's now the wrong way to use it?

    vox , (edited )
    @vox@sopuli.xyz avatar

    I'd argue floating wms are more intuitive and some can still tile pretty well if you want that

    KillingTimeItself ,

    floating WMs are intuitive, but the problem is that they're an incredibly mediocre solution, and the way that problems are often solved around one, is just entirely asinine. Let's build ten different ways to do the same thing, now we have 10x the code to build and maintain, and it's 10x more confusing to the end user who probably won't know about half of them, because 90% of our documentation is redundant!

    Tiling WMs have significantly less issues with this, because they often have a very strict set of management rules, and only those. Nothing more.

    Lifter ,

    People screw up CLI's all the time (looking at you Google Cloud). They (used to) insist on using my installed python which automatically upgrades and breaks the CLI. Good job python. Good job Gcloud.

    KillingTimeItself ,

    i'm not sure that's a CLI problem, sounds more like an application problem from what i'm hearing.

    Lifter ,

    Exactly! All applications can be shit, not just web sites.

    KillingTimeItself ,

    this is true, though websites are most often the culprit of this.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • [email protected]
  • kbinchat
  • All magazines