`
leonzhx
  • 浏览: 796826 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 50: Avoid strings where other types are more appropriate

阅读更多

1.  Strings are poor substitutes for other value types. If there’s an appropriate value type, whether primitive or object reference, you should use it; if there isn’t, you should write one.

 

2.  Strings are poor substitutes for enum types.

 

3.  Strings are poor substitutes for aggregate types. If an entity has multiple components, it is usually a bad idea to represent it as a single string. You can’t provide equals, toString, or compareTo methods but are forced to accept the behavior that String provides. A better approach is simply to write a class to represent the aggregate, often a private static member class.

 

4.  Strings are poor substitutes for capabilities. If the ThreadLocal API was designed as below :

// Broken - inappropriate use of string as capability!
public class ThreadLocal {
    private ThreadLocal() { } // Noninstantiable
    // Sets the current thread's value for the named variable.
    public static void set(String key, Object value);
    // Returns the current thread's value for the named variable.
    public static Object get(String key);
}

The problem with this approach is that the string keys represent a shared global namespace for thread-local variables. In order for the approach to work, the client-provided string keys have to be unique. Also, the security is poor. A malicious client could intentionally use the same string key as another client to gain illicit access to the other client’s data. This API can be fixed by replacing the string with an unforgeable key (sometimes called a capability), or even using ThreadLocal instance itself as a key with typesafe:

public final class ThreadLocal<T> {
    public ThreadLocal() { }
    public void set(T value);
    public T get();
}

 

分享到:
评论

相关推荐

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 62: Avoid strings where other types are more appropriate Item 63: Beware the performance of string concatenation Item 64: Refer to objects by their interfaces Item 65: Prefer interfaces to ...

    Wrox.Beginning Visual C++ 2008

    Chapter 4: Arrays, Strings, and Pointers Chapter 5: Introducing Structure into Your Programs Chapter 6: More about Program Structure Chapter 7: Defining Your Own Data Types Chapter 8: More on ...

    c++ Strings

    While this approach works, it lacks the convenience and functionality found in other high-level languages where strings are first-class citizens. The transition from C to C++ brought with it the ...

    CSharp 3.0 With the .NET Framework 3.5 Unleashed(english)

    - **Namespaces**: Namespaces are used to organize types and avoid naming conflicts. - **Namespace Directives**: Namespace directives are used to import namespaces into a file. - **Creating Namespaces*...

    两个Strings.xml的比较

    1、此程序用来比较2个Strings.xml中name的不同项,并打印出来; 2、使用前,请先将两个String.xml重命名为:strings_en.xml(英文或其他语言)和strings_zh.xml(中文); 3、此程序是通过strings_zh.xml去检索...

    Strings字符串分析工具

    《Strings字符串分析工具详解》 在计算机领域,字符串分析是一个重要的技术手段,尤其在病毒分析、逆向工程和软件调试中扮演着不可或缺的角色。Strings工具就是这样一个强大的实用程序,它能够从二进制文件中提取出...

    Python Programming - iCode Academy

    Here’s What You’ll Learn From This Python For Beginners Book: Introduction Chapter 1: Welcome to the World of the Python Chapter 2: Python Syntax Chapter 3: Important Strings and Console Output ...

    Exploring C++ 11

    Part 2: Custom Types – More About Member Functions Part 2: Custom Types – Access Levels Part 2: Custom Types – Introduction to Object-Oriented Programming Part 2: Custom Types – Inheritance Part 2...

    naturalsort:Go Strings的简单自然分类器

    自然排序Go的简单自然字符串排序器。 ## Usage实现sort.Interface 由sort.Sort(NaturalSort([]string))调用###示例 SampleStringArray := [] string { "z24" , "z2" , "z15" , "z1" , "z3" , "z20" , "z5" , "z11" ,...

    Google C++ Style Guide_英文版.pdf

    Classes:** Use structs for POD (Plain Old Data) types and classes for more complex types with behavior. - **Structs vs. Pairs and Tuples:** Use structs for named fields and pairs/tuples for small ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Other C++ Features Reference Arguments Function Overloading Default Arguments Variable-Length Arrays and alloca() Friends Exceptions Run-Time Type Information (RTTI) Casting Streams Preincrement and ...

    strings-truncation:用全角字符和ANSI代码截断字符串

    字符串::截断 使用全角字符和ANSI代码截断字符串。 特征 没有猴子修补String类 ... strings = Strings :: Truncation . new strings . truncate ( "I try all things, I achieve what I can." ) # =&gt; "I try all thing

    HappyStrings:Happy Strings是一家生活方式美容,健康和时尚产品的女性零售商。 该项目是使用Html,CSS,JS和SCSS的Happy Strings的网站

    【HappyStrings】项目是专为女性设计的生活方式美容、健康及时尚产品零售商——Happy Strings创建的一个网站。这个网站的开发采用了Web开发中的基础技术:HTML(超文本标记语言)、CSS(层叠样式表)、JavaScript...

    Algorithms in C++, Parts 1-4: Fundamentals, Data Structures, Sorting, Searching

    An example is quicksort, where the partitioning step is a constant-time operation, but the recursive calls are made on two smaller subarrays. 6. **Examples of Algorithm Analysis**: Detailed examples...

    Format Strings

    `Standard Date and Time Format Strings_files`、`Standard Numeric Format Strings_files`、`Custom Date and Time Format Strings_files`和`Custom Numeric Format Strings_files`这些文件夹可能包含支持上述网页...

    可设置文字颜色的listbox

    2)设置此控件属性:Has Strings : true owner draw : fix or variable selection : single 其他的属性随意。 3)为此控件添加变量,变量类型为类CColorListBox 4)接下来用CColorListBox重载的方法AddString等...

    android的strings.xml示例代码

    在Android开发中,`strings.xml`文件是管理应用中所有文本资源的核心文件。它使得开发者可以集中处理字符串,便于国际化和本地化,同时也方便代码维护。在这个`StringDemo`示例中,我们将深入探讨如何使用`strings....

Global site tag (gtag.js) - Google Analytics