• 0 Posts
  • 33 Comments
Joined 1 year ago
cake
Cake day: July 1st, 2023

help-circle








  • Easy access to small snippets of code you often need, but putting them in their own library would be crazy.

    • Opening a file / db connection
    • parsing xml/json/… ,
    • template for unit tests,
    • import and initialization of framework at work.

    Depending on the IDE snippets can also move parts of the code around: (intellij live templates)

    • variable.notnull -> if (variable != null) {… }
    • “text %s”.format -> String.format(“text %s”,…)



  • The variable is set once, but the if expression is still evaluated every time (unless the compiler can optimize it)

    (edit after skimming the article: yes,using the variable would solve the problem of the last example)

    So there would be the branching overhead in every iteration. But that’s something the cpu branch prediction should cover, especially since the taken branch will be identical in every loop.

    Same also applied to the implied condition to break the for loop (only the first few and last iteration should be wrong predictions)






  • Think of it more like bigger building blocks rather than single use functions. If there is an issue with the pizza arriving burnt black at the customer you don’t want to read through the logic for making the dough and adding toppings if the most likely cause is the oven.

    Sure, you could add comment blocks to mark the sections. But you can’t easily jump to that exact point. With function names you can easily skim over the unimportant calls, or go through a list of functions/methods in the file and jump there directly. With comments that’s not a standard feature in IDEs.

    Also that function does not scale well if you have more than 2 options of toppings. Maybe some are not compatible and logic needs to be added that you don’t use pineapple and banana on the same pizza, for example.

    But I understand your argument about following through multiple layers of abstractions. That’s something that irritates me as well a bit, if you follow a function, that does nothing, but pass the same parameters through another function and return the result.

    No guard clauses, or changes to the data, just 1:1 pass-through. And this them goes 2-3 levels deep before reaching real code again. Bonus of they all are in different files too.