Skip to main content
added 191 characters in body
Source Link
Flater
  • 59.6k
  • 8
  • 112
  • 171

In response to your comment

I am referring to programming competitions. Can you tell me why no one in there right mind don't want such code in industry

For the same reason no one drives a racecar to do grocery shopping. A racecar is built for competition between racers and car manufacturers, but a racecar is not a useful everyday car for the vast majority of car owners.

Similarly, industry code is not written as a challenge between developers, it is written to perform a task and to be maintainable so it can adapt when necessary.

The marginal speed gains (fractions of seconds) gained from pre-templated code is offset by the decreased readability of the code which can lead to hours of wasted effort.

Additionally, the templates might work in the scenario of one developer creating a one shot codebase; but it does not stand up to multiple developers having to revisit the same codebase.


In response to your comment

I am referring to programming competitions. Can you tell me why no one in there right mind don't want such code in industry

For the same reason no one drives a racecar to do grocery shopping. A racecar is built for competition between racers and car manufacturers, but a racecar is not a useful everyday car for the vast majority of car owners.

Similarly, industry code is not written as a challenge between developers, it is written to perform a task and to be maintainable so it can adapt when necessary.

The marginal speed gains (fractions of seconds) gained from pre-templated code is offset by the decreased readability of the code which can lead to hours of wasted effort.

Additionally, the templates might work in the scenario of one developer creating a one shot codebase; but it does not stand up to multiple developers having to revisit the same codebase.

added 191 characters in body
Source Link
Flater
  • 59.6k
  • 8
  • 112
  • 171

Trying to measure code quality by the amount of characters it takes to write your code is like measuring car performance by the amount of parts that went into the construction of the car.

I cannot stress this enough: readability trumps brevity every single time. KLOC (or character count) is not a valid measure of code quality. Compact code is generally bad because it makes zero difference to the compiler (no performance increase) while making things harder for the developer to understand.

I cannot stress this enough: readability trumps brevity every single time. KLOC (or character count) is not a valid measure of code quality. Compact code is generally bad because it makes zero difference to the compiler (no performance increase) while making things harder for the developer to understand.

Trying to measure code quality by the amount of characters it takes to write your code is like measuring car performance by the amount of parts that went into the construction of the car.

I cannot stress this enough: readability trumps brevity every single time. KLOC (or character count) is not a valid measure of code quality. Compact code is generally bad because it makes zero difference to the compiler (no performance increase) while making things harder for the developer to understand.

Source Link
Flater
  • 59.6k
  • 8
  • 112
  • 171

This is going to be a frame challenge, because I disagree with the underlying notion that the character count of your code is the most important metric for code quality, or that templates are the best way to write code faster.

Disclaimer: This is written from the perspective of a (predominantly) C# developer, but the argument I'm making is language agnostic.


I have some difficulty understanding those programs because they first create theyr own keywords using #define and typedef.

This is the exact reason why I don't like these templates. They massively detract from readability for any developer other than the dev who coined those templates. Rewriting basic language keywords to a name you like better is a subjective holy war that doesn't add any value.

On top of that, when you shorten it, you decrease configurability. E.g. the for loop is as verbose as it needs to be in order to provide the feature set that it aims to provide. If you make it less verbose, you omit part of its feature set.

The for loop is already a condensed version of a while loop that defines an external counter and uses that counter to check for its exit condition. It is as terse as it reasonably can be.


This is done to make the code more compact...

I cannot stress this enough: readability trumps brevity every single time. KLOC (or character count) is not a valid measure of code quality. Compact code is generally bad because it makes zero difference to the compiler (no performance increase) while making things harder for the developer to understand.

That doesn't mean there's no upper limit on how long code should be or course; but trying to condense language constructs is not reasonable.

There are fringe exceptions where less readable variants of code can be more performance than their more readable counterparts; but your use cases of basic language constructs is not one of these fringe cases.


...and faster to write.

There are other ways to handle this. An IDE can already facilitate quicker code writing without you needing to redefine the language's keywords.

As a Visual Studio example, if you type for and press Tab twice, it writes everything for you:

enter image description here

With further tab completion, it allows you to quickly write the three operations (initialization, exit condition, increment condition).

So is this better? Let's see:

  • Tab completion does take some time to master to make it become muscle memory, but using templates also needs time to master (because a new developer needs to learn the templates). It'll take about the same amount of time for you to get comfortable with it.
  • Different codebases may define different templates, but when working in the IDE you get the same features regardless of which codebase you're working in.
  • At the end of the day, when using the tab completion, the resulting code has better readability since it doesn't rely on arbitrarily created template names. Rather than needing you to know the arbitrary definitions, you can read the code itself.

I'm chalking that up as a big win in terms of using IDE tools such as tab completion as opposed to arbitrarily decided definitions.


Also, as a side note:

#define SC1(x)          scanf("%lld",&x)
#define PF3(x,y,z)      printf("%lld %lld %lld\n",x,y,z)
//...

Most of these can be written as normal methods, there is no reason to use templating here. It seems like you're drifting into the territory of misusing templating to not write a simple method.