Skip to main content
added 520 characters in body
Source Link
haylem
  • 29k
  • 11
  • 106
  • 119

Articles in Variable Names

They seem to me to add very little valueThey add little to no value in most cases, but make the code longer to read.

Usually, I'd rarely have a the need for the distinction between a "a" or "the" in most casesthe flow of a function, but toso it doesn't make the code longersense to readuse articles.

It's a formal programming language and I am fine with it not reading exactly like a natural language. In fact, it's probably better this way, not everything's necessarily so great about natural languages.

There are a few cases where readability can benefit from using articles though, for instance if you want to implement a checker for a "is a" relationship with a method signature like boolean isA(Object o, Class<?> cl) (I'm not advocating this to be a good idea).

Hard-Coded Strings

It depends on whether you plan to reuse them or not, and whether that reuse is driven by semantics (and not just the fact that the String has the same content in different places) and spread across multiples classes or sub-systems.

To that extent, I usually use these rules:

  1. If you have the same string multiple times and it has the same meaning acros multiple classes, then extract to constants in an interface.

  2. If you have the same string multiple times within a single class and it has the same meaning within that class, then extract to constants within the class.

  3. If you have only a few number of repetitions of as tring, or they just happen to be identical but aren't guaranteed to always be in the future, then I'd leave them inlined.

  4. If any of the above would render maintenance difficult, screw rules 1 to 3 and do whatever works best for you.

Of course, make sure that the constant's name makes the code as semantically valid as the string it holds. That also applies for other things that aren't strings (like Predicates, for instance).

Articles in Variable Names

They seem to me to add very little value in most cases, but to make the code longer to read.

It's a formal programming language and I am fine with it not reading exactly like a natural language.

Hard-Coded Strings

It depends on whether you plan to reuse them or not, and whether that reuse is driven by semantics (and not just the fact that the String has the same content in different places) and spread across multiples classes or sub-systems.

To that extent, I usually use these rules:

  1. If you have the same string multiple times and it has the same meaning acros multiple classes, then extract to constants in an interface.

  2. If you have the same string multiple times within a single class and it has the same meaning within that class, then extract to constants within the class.

  3. If you have only a few number of repetitions of as tring, or they just happen to be identical but aren't guaranteed to always be in the future, then I'd leave them inlined.

  4. If any of the above would render maintenance difficult, screw rules 1 to 3 and do whatever works best for you.

Of course, make sure that the constant's name makes the code as semantically valid as the string it holds. That also applies for other things that aren't strings (like Predicates, for instance).

Articles in Variable Names

They add little to no value in most cases, but make the code longer to read.

Usually, I'd rarely have a the need for the distinction between a "a" or "the" in the flow of a function, so it doesn't make sense to use articles.

It's a formal programming language and I am fine with it not reading exactly like a natural language. In fact, it's probably better this way, not everything's necessarily so great about natural languages.

There are a few cases where readability can benefit from using articles though, for instance if you want to implement a checker for a "is a" relationship with a method signature like boolean isA(Object o, Class<?> cl) (I'm not advocating this to be a good idea).

Hard-Coded Strings

It depends on whether you plan to reuse them or not, and whether that reuse is driven by semantics (and not just the fact that the String has the same content in different places) and spread across multiples classes or sub-systems.

To that extent, I usually use these rules:

  1. If you have the same string multiple times and it has the same meaning acros multiple classes, then extract to constants in an interface.

  2. If you have the same string multiple times within a single class and it has the same meaning within that class, then extract to constants within the class.

  3. If you have only a few number of repetitions of as tring, or they just happen to be identical but aren't guaranteed to always be in the future, then I'd leave them inlined.

  4. If any of the above would render maintenance difficult, screw rules 1 to 3 and do whatever works best for you.

Of course, make sure that the constant's name makes the code as semantically valid as the string it holds. That also applies for other things that aren't strings (like Predicates, for instance).

Source Link
haylem
  • 29k
  • 11
  • 106
  • 119

Articles in Variable Names

They seem to me to add very little value in most cases, but to make the code longer to read.

It's a formal programming language and I am fine with it not reading exactly like a natural language.

Hard-Coded Strings

It depends on whether you plan to reuse them or not, and whether that reuse is driven by semantics (and not just the fact that the String has the same content in different places) and spread across multiples classes or sub-systems.

To that extent, I usually use these rules:

  1. If you have the same string multiple times and it has the same meaning acros multiple classes, then extract to constants in an interface.

  2. If you have the same string multiple times within a single class and it has the same meaning within that class, then extract to constants within the class.

  3. If you have only a few number of repetitions of as tring, or they just happen to be identical but aren't guaranteed to always be in the future, then I'd leave them inlined.

  4. If any of the above would render maintenance difficult, screw rules 1 to 3 and do whatever works best for you.

Of course, make sure that the constant's name makes the code as semantically valid as the string it holds. That also applies for other things that aren't strings (like Predicates, for instance).