Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / Java
Tip/Trick

What If Names Would Contain White Spaces?

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
4 Jan 2016CPOL4 min read 20.9K   25   4   19
Is there a technical reason why white spaces aren't allowed in names of constants, variables, methods, classes and namespaces or is it in accordance with any convention?

Introduction

Is there a technical reason why white spaces aren't allowed in names of constants, variables, methods, classes and namespaces or is it in accordance with any convention? This question can also be asked in relation to the digits that can’t be placed at the beginning of the name.

The main reason to follow a naming convention is to reduce the effort needed to read and understand source code. The developers could focus on more important issues than arguing over syntax and naming standards. For this reason, there is a strict rule that names are usually case-sensitive. A name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter or the underscore character. White space in names is not permitted.

The last rule is used not only due to the difficulties with code understanding. The compiler needs to find out the meaning of words. It works on a “State Machine” method, and it needs to distinguish key words. However, there are languages like SQL or Maple where names can contain white spaces. They are enclosed on both sides of special quotation mark characters: “[” and “]” in SQL, “`” (U+0060, grave accent) in Maple. In C#, square brackets are used as indexers of collection items. But the grave accent symbol can be successfully used as enclosed symbol.

The Code Samples

Let's try to write a code in C# using quite long names containing white spaces and see what happens.

C#
[`Custom Serialization Attribute`]
public class `Config Data File Repository` : `Configuration Repository Interface` {
  public enum `Data Visualization Format` { `2D`, `3D` }
  public `Data Visualization Format` `Visualization Format` { get; set; } 
    = `Data Visualization Format`.`2D`;
  ... 
  private readonly string `repository file name`;
  public `Config Data File Repository`(string `repository file name`) {
    this.`repository file name` = `repository file name`;
  }
  public `Configuration Data` Load() {
    using(var fs = new `File Stream`(`repository file name`, `File Mode`.Open)) {
    return `Load From Stream`(fs);
    }
  }
  public void Save(`Configuration Data` `current configuration`) {
    using (var fs = new `File Stream`(
      this.`repository file name`, `File Mode`.`Create New`)) {
      `Save To Stream`(`current configuration`, fs);
    }
  }
  ...
  public const string `DEFAULT REPOSITORY FILE` =
    "Default Config Data File Repository.config";
  
  public static `Config Data File Repository` `Create Config Data File Repository`(
    string `file Name` = `Config Data File Repository`.`DEFAULT REPOSITORY FILE`) {
    return new `Config Data File Repository`(`file Name`);
  }
}

Also, let's write a code in Java close to real-world problems.

Java
@Entity
@Table(name = "[PERSONAL CONFIGURATION SECTIONS]")
@EntityListeners( { HierarchyListener.class } )
@NamedQueries({
  @NamedQuery(
    name = "`Personal Configuration Section`.`find All`",
    query = "SELECT cs FROM [PERSONAL CONFIGURATION SECTIONS] cs"),
  @NamedQuery(
    name = "`Personal Configuration Section`.`find By Configuration Section Id`",
    query = "SELECT cs FROM [CONFIGURATION SECTIONS] cs "
      + "WHERE cs.[configuration Section Id] = :`configuration Section Id`")})
public class `Personal Configuration Section`
  implements Serializable, `Hierarchy Element Interface` {
  private static final long `serial Version UID` = 1L;
  @Id @NotNull
  @Basic(optional = false)
  @`Generated Value`(
    strategy = `Generation Type`.SEQUENCE,
    generator = "global_id_gen")
  @`Sequence Generator`(name = "global_id_gen", `sequence Name` = "GLOBAL_ID_GEN")
  @Column(name = "[CONFIGURATION SECTION ID]")
  private Integer `configuration Section Id`;
  public Integer `get Configuration Section Id`()
    { return `configuration Section Id`;  }
  public void `set Configuration Section Id`(Integer `configuration Section Id`)
    { this.`configuration Section Id` = `configuration Section Id`; }
  
  @Column(name = "[FULL NAME]")
  private String `full Name`;
  public String `get Full Name`()
    { return `full Name`;  }
  public void `set Full Name`(String `full Name`)
    { this.`full Name` = `full Name`; }
  
  @Column(name = "[EXPERIENCE, SKILLS AND ARTIFACTS]")
  private `Collection Interface` `experience, skills and artifacts`;
  public `Collection Interface` `get Experience, skills and artifacts`()
    { return `experience, skills and artifacts`; }
  public void `set Experience, skills and artifacts`(
    `Collection Interface` `experience, skills and artifacts`)
    { this.`experience, skills and artifacts` = `experience, skills and artifacts`; }
  
  @`Many To One`(fetch = `Fetch Type`.LAZY)
  @`Join Columns`({
    @`Join Column`(
      name="[PARENT ID]",
      `referenced Column Name`="parent Configuration Section Id"),
    @`Join Column`(
      name="[PARENT VERSION]",
      `referenced Column Name`="parent Configuration Section Version")
    })
  private `Personal Configuration Section` parent;
  public `Personal Configuration Section` `get Parent`()
    { return parent; }
  public void `set Parent`(`Personal Configuration Section` parent)
    { this.parent = parent; }
  
  @`One To Many`(fetch=`Fetch Type`.LAZY, `mapped By`="parent")
  private Set<`Personal Configuration Section`> children;
  public Set<`Personal Configuration Section`> `get Children`() { return children; }
  
  public `Personal Configuration Section`() {}
  
  public `Personal Configuration Section`(
    Integer `personal Configuration Section Id`) {
    this.`personal Configuration Section Id` = `personal Configuration Section Id`;}
  
  ...
}

No doubt, it is possible to make the compiler parse this code. However, the code has become much harder to read. Surprising for me, the readability of the code decreased just slightly, especially for Java! The first place is taken by already existing issue of too long names. There are a large number of articles (“Very Long Descriptive Names That Programming Pairs Think Provide Good Descriptions” or “Best Practices for Variable and Method Naming”) to provide some useful solutions. This issue has existed since the origin of object-oriented languages. According to my point of view, while thinking about code, it is useful to look at it from the other developers side who may use it. This will help not only with picking names, but with keeping your code more readable without writing a documentation and unnecessary comments. If the name is too long, then maybe this part of the code has multiple responsibilities and it should be written separately. In other words, the code should be re-factored.

Also there is a question, how to improve the readability and where to use the feature. The main areas of application might be defined as unit testing classes and classes contained internationalization string properties implemented by string resource or configuration settings sections wrapper classes. The readability could be improved due to features of IDE. For example, the names with white spaces can be highlighted by thin dotted border. This approach will allow developers to identify such names visually and quickly, and will restrict the widespread thoughtless use of names with white spaces.

Let's try to write a simple unit test in C#.

Image 1

Also, the unit tests presented above re-written in Java.

Image 2

The unit test code in C# and Java has a very good readability. It doesn't need additional attributes to be displayed correctly in the test reporting window. However, it could be useful to write the attributes specified the test categories.

The often use of such long names in the code isn’t the best way. But if it is necessary to refer to them, it may be useful to apply the standard way to reduce such names by the method presented in natural languages. Some non-significant parts of the name can be replaced by ellipsis characters.

Image 3

Conclusion

I would greatly appreciate the opinion of readers who express their point of view and answer the following questions. Has the time already come to reconsider the position to the names having white spaces? And are there any areas where it's very important to write such names? Perhaps it's time to rethink the naming convention.

Points of Interest

It's wonderful to find and learn the good software engineering principles, nice approaches in source code and modern frameworks allow us to write really flexible code.

History

  • 04/01/2016 - First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor / Trainer Southern Federal University
Russian Federation Russian Federation
Senior lecturer of Nanotechnology department of Physics faculty of Southern Federal University (Russia).

My field of science is the numerical modeling of excitation, propagation and receiving of electromagnetic and acoustic waves in micro- and nanostructures; modeling microwave and optic devices based on ferromagnetic, ferroelectric and semiconducting films and whiskers; study of the periodic structures properties like magnonic and photonic crystals.

I also develop a software for theoretical modeling and experimental studies using modern methods of software engineering, programming languages, numerical methods and mathematical software packages, software platforms, parallel computation and asynchronous programming, database management systems, Web-programming and design, system programming (drivers , communication interfaces, microcontroller programming).

My hobbies are software architecture (UP, UML, TDD, BDD, DI, AOP), programming languages (C#, F#, C++, Java, LabVIEW, Delphi, Perl, PHP, ASP.NET, JavaScript, VBScript, ActionScript, Pascal, Basic, VBA for MS Office, Fortran, Assembler, PowerShell, SQL, VHDL), mathematical methods and mathematical packages (Maple, MatLab, Mathematica), programming for modern electronics (microcontrollers, PLIS, DSP, communication interfaces), Web-design (design, HTML, Flash, Silverlight, Moodle, Joomla, Drupal), technologies (WPF, WCF, WF, ADO.NET, COM, Services, WMI, MFC, jQuery, YUI), System Administration (Windows, Unix), office suites (Microsoft Office, Libre Office, using styles, LaTeX).

Comments and Discussions

 
QuestionGood old Fortran Pin
kalberts8-Jan-16 2:28
kalberts8-Jan-16 2:28 
AnswerRe: Good old Fortran, long life for Fortran :) Pin
Pavel Evgenjevich Timoshenko8-Jan-16 6:18
Pavel Evgenjevich Timoshenko8-Jan-16 6:18 
GeneralNot all languages are case-sensitive Pin
ozbear5-Jan-16 14:52
ozbear5-Jan-16 14:52 
AnswerRe: Not all languages are case-sensitive Pin
Pavel Evgenjevich Timoshenko5-Jan-16 23:40
Pavel Evgenjevich Timoshenko5-Jan-16 23:40 
Questionlower case and underscores ... Pin
Member 122460085-Jan-16 9:23
Member 122460085-Jan-16 9:23 
AnswerRe: lower case and underscores ... Pin
Pavel Evgenjevich Timoshenko5-Jan-16 9:35
Pavel Evgenjevich Timoshenko5-Jan-16 9:35 
GeneralRe: lower case and underscores ... Pin
Member 122460086-Jan-16 9:29
Member 122460086-Jan-16 9:29 
GeneralRe: lower case and underscores ... Pin
Pavel Evgenjevich Timoshenko8-Jan-16 6:35
Pavel Evgenjevich Timoshenko8-Jan-16 6:35 
AnswerRe: lower case and underscores ... Pin
Fernando A. Gomez F.6-Jan-16 12:20
Fernando A. Gomez F.6-Jan-16 12:20 
AnswerRe: lower case and underscores ... Pin
kalberts8-Jan-16 1:20
kalberts8-Jan-16 1:20 
GeneralRe: lower case and underscores ... Pin
Pavel Evgenjevich Timoshenko8-Jan-16 7:07
Pavel Evgenjevich Timoshenko8-Jan-16 7:07 
Generalcompiler only Pin
John Torjo4-Jan-16 23:15
professionalJohn Torjo4-Jan-16 23:15 
GeneralRe: compiler only Pin
Pavel Evgenjevich Timoshenko5-Jan-16 9:27
Pavel Evgenjevich Timoshenko5-Jan-16 9:27 
GeneralRe: compiler only Pin
John Torjo8-Jan-16 0:17
professionalJohn Torjo8-Jan-16 0:17 
GeneralRe: compiler only Pin
kalberts8-Jan-16 5:03
kalberts8-Jan-16 5:03 
GeneralRe: compiler only Pin
Pavel Evgenjevich Timoshenko8-Jan-16 7:03
Pavel Evgenjevich Timoshenko8-Jan-16 7:03 
GeneralRe: compiler only Pin
John Torjo8-Jan-16 20:16
professionalJohn Torjo8-Jan-16 20:16 
GeneralRe: compiler only Pin
Pavel Evgenjevich Timoshenko8-Jan-16 22:59
Pavel Evgenjevich Timoshenko8-Jan-16 22:59 
GeneralRe: compiler only Pin
John Torjo8-Jan-16 23:02
professionalJohn Torjo8-Jan-16 23:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.