Trending

#TuesdayCodingTips

Latest posts tagged with #TuesdayCodingTips on Bluesky

Latest Top
Trending

Posts tagged #TuesdayCodingTips

C++ source code showing how constrained auto can simplify code.

C++ source code showing how constrained auto can simplify code.

Tip 93 of #TuesdayCodingTips - Constrained auto

Tired of convoluted templates in your C++? Want to make them more legible?

C++20 introduced concepts - a powerful feature that can restrict your templated APIs to only such arguments that fulfil a particular […]

[Original post on techhub.social]

1 0 0 0
C# snippet showing how to pin a byte array in c# before passing it to native code.

C# snippet showing how to pin a byte array in c# before passing it to native code.

Tip 92 of #TuesdayCodingTips - Pinning interop memory in C#

When interoping C# with, for example, C++, one can send a pointer to the managed memory directly to the native code and work with it without the need to copy it into the local address space. For as […]

[Original post on techhub.social]

0 1 1 0
C# code snippet that demonstrates immutability of strings and performance hits of using a string vs StringBuilder.

C# code snippet that demonstrates immutability of strings and performance hits of using a string vs StringBuilder.

Tip 91 of #TuesdayCodingTips - String or StringBuilder?

If you're a C++ programmer, you can easily troll yourself while working with C#'s `string` objects. In C++, they are optimized vectors of bytes and can be dynamically resized at will through […]

[Original post on techhub.social]

0 0 0 0
C# code snippet showcasing Enum.IsDefined usage.

C# code snippet showcasing Enum.IsDefined usage.

Tip 90 of #TuesdayCodingTips - Is enum value defined?

In both C++ and C#, enums are mere named numerical constants. Even if they appear to be distinct types, and switch statements can check whether you covered all names within an enum, that's simply not […]

[Original post on techhub.social]

1 1 0 0
Original post on techhub.social

Tip 89 of #TuesdayCodingTips - Semantic versioning

When creating a library, semantic versioning is a really useful versioning scheme to use.

It's a promise to your users that changes bumping the minor/patch version numbers are backward-compatible. As such, much of the software relies on these […]

0 1 1 0
A C++ code snippet which shows json (de)serialization of a vector of variants with overloaded adl_serializer and nlohmann::json.

A C++ code snippet which shows json (de)serialization of a vector of variants with overloaded adl_serializer and nlohmann::json.

Tip 88 of #TuesdayCodingTips - Enum-discriminated unions with nlohmann::json

JSON is a fairly trivial format and as such, its only way of supporting polymorphic data is an enum-discriminated approach.

In other words, you might have an array of objects where […]

[Original post on techhub.social]

0 1 0 0
Original post on techhub.social

They say learning by example is the best way to learn. I'd like to share one such example of API design process based on a recent discussion with one of my colleagues: medium.com/@nerudaj/a-case-study-on...

If you follow my #TuesdayCodingTips, this showcases a […]

0 1 0 0
C++ code snippet comparing the effectivity of pushing expensive type into vector of variants vs emplacing it vs pushing back cheaper type and emplacing over that memory.

C++ code snippet comparing the effectivity of pushing expensive type into vector of variants vs emplacing it vs pushing back cheaper type and emplacing over that memory.

Tip 87 of #TuesdayCodingTips - Emplace into a vector of variants

`emplace_*` is an alternative to methods like `insert` or `push_*` in standard C++ containers. It allows you to construct the inserted object directly inside the container, passing all required […]

[Original post on techhub.social]

0 0 0 0
Code snippet that shows an example of an interface that breaks with every release.

Code snippet that shows an example of an interface that breaks with every release.

Somewhat better interface since all properties are encapsulated in a context struct.

Somewhat better interface since all properties are encapsulated in a context struct.

Tip 86 of #TuesdayCodingTips - Extendable logger pattern

I like my logs structured. If nothing else, I can log into a CSV file, load that CSV into Excel, turn on filters, and boom, I have a quite nice log analyzer tool. To get the maximum out of it, I need to […]

[Original post on techhub.social]

0 1 0 0
C++ code snippet showing how to demangle C++ identifiers under GCC/Clang and how a "tag" type can happen.

C++ code snippet showing how to demangle C++ identifiers under GCC/Clang and how a "tag" type can happen.

Tip 85 of #TuesdayCodingTips - Incomplete types and name demangling

While writing type-safe APIs, a "tag" type is often useful. It is nothing more than a forward declaration of a type that will never be fully defined, just for the sake of creating a template […]

[Original post on techhub.social]

0 0 1 0
C++ code showing a type trait for reading callable type's parameters and return type.

C++ code showing a type trait for reading callable type's parameters and return type.

Tip 84 of #TuesdayCodingTips - C++ type trait for callable types

I recently asked myself: How do you make a reliable type trait for callable types so you can read their return values and call parameters?

The complexity lies in the fact that C++ has four […]

[Original post on techhub.social]

1 0 0 0
Expanded C++ implementation of NLOHMANN_JSON_SERIALIZE_ENUM macro.

Expanded C++ implementation of NLOHMANN_JSON_SERIALIZE_ENUM macro.

Tip XX of #TuesdayCodingTips - Curious case of NLOHMANN_JSON_SERIALIZE_ENUM

I recently debugged a case where code generated by NLOHMANN_JSON_SERIALIZE_ENUM failed at invariant assertions deep in the nlohmann_json library. When examined under a debugger, the […]

[Original post on techhub.social]

1 1 0 0
C# code snippet demonstrating exception filter.

C# code snippet demonstrating exception filter.

c# snippet showing that you can use references to objects in scope in exception filter.

c# snippet showing that you can use references to objects in scope in exception filter.

Tip 82 of #TuesdayCodingTips - C# exception filter

Ever found yourself in the situation where you have an exact same handling code for two (or more) selected exception types? C# has a nice way of deduplicating said code.

A `catch` block, processing a base […]

[Original post on techhub.social]

1 0 0 0
C++ code demonstrating serialization through typeid with a concept for type safety, deserialization through template magic and then mattern matching on the resulting type.

C++ code demonstrating serialization through typeid with a concept for type safety, deserialization through template magic and then mattern matching on the resulting type.

Tip 81 of #TuesdayCodingTips - Pattern matching a C++ enum

For a particular C++ application I wrote, I wanted to send an enum over a string-based protocol and pattern match on it on the receiving end to make sure that all possible values were handled.

You […]

[Original post on techhub.social]

1 0 0 0
C# code that prints a loop control variable in a delayed lambda.

C# code that prints a loop control variable in a delayed lambda.

C# code that copies the loop control variable into a local variable so it can correctly print it in a delayed lambda.

C# code that copies the loop control variable into a local variable so it can correctly print it in a delayed lambda.

Python code that demonstrates that you can capture a variable by copy in a lambda declaration.

Python code that demonstrates that you can capture a variable by copy in a lambda declaration.

Python code that demonstrates that a range-based loop creates an iteration-local variables anyway.

Python code that demonstrates that a range-based loop creates an iteration-local variables anyway.

Tip 80 of #TuesdayCodingTips - C# lambdas and mutability rules

Mutability rules in C# are funky. While trivial types are passed into functions by value, they are captured by reference for their usage in lambdas.

That can lead to stupid bugs - what if you […]

[Original post on techhub.social]

0 1 0 0
C++ code snippet that loads a file into a string through seeking to the end of the file, figuring its size and then reading into a buffer of that size.

C++ code snippet that loads a file into a string through seeking to the end of the file, figuring its size and then reading into a buffer of that size.

C++ code snippet that converts an input stream into an iterator that can be used to initialize a string.

C++ code snippet that converts an input stream into an iterator that can be used to initialize a string.

C++ code snippet that redirects internal buffer of std::ifstream into a std::stringstream and extracting a string out of it.

C++ code snippet that redirects internal buffer of std::ifstream into a std::stringstream and extracting a string out of it.

Tip 78 of #TuesdayCodingTips - Many ways to read a file in C++

In C#, you can easily read a whole file using `File.ReadAllText` method. Obviously, things can't be as easy in C++.

You can choose the 'old way' where you pre-allocate a buffer to which you can […]

[Original post on techhub.social]

1 1 1 0
C++ code implementing a custom class with multidimensional operator[].

C++ code implementing a custom class with multidimensional operator[].

A simple C++ sample using std::mdspan as a view over 1D array that behaves as a 3D array.

A simple C++ sample using std::mdspan as a view over 1D array that behaves as a 3D array.

Tip 77 of #TuesdayCodingTips - Multidimensional subscript operator

A quality-of-life C++23 feature has finally been integrated into Visual Studio (v17.12) after it was available in Clang (trunk) for quite some time. That feature is the support of the […]

[Original post on techhub.social]

0 0 0 0