The Source for Java Technology Collaboration
User: Password:



Ahmed Hashim

Ahmed Hashim's Blog

Compare String to a literal.. coding style

Posted by ahashim on March 10, 2007 at 04:11 AM | Comments (6)

I saw many times code written by geeks, comparing String to literals in this style

public boolean compareUser(String name)
{
if("Ahmed Hashim".equals(name))
{
//do staff here
return true;
}
return false;
}
what is the difference between this and the normal code style?
public boolean compareUser(String name)
{
if(name.equals("Ahmed Hashim"))
{
//do staff here
return true;
}
return false;
}
You will miss the difference if you call both versions of the function like this


compareUser(null);

I think you got it, passing null will not need check in the first version because you are not accessing the “.equals” method inside the passed object, while the 2nd version will cause NullPointerException!

Mirror:
http://egjug.org/hashimblog/2007/03/08/compare-string-to-a-literal-coding-style/


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Surely the the first is the normal code style? The second is unsafe so the former is always going to better unless you actually want an NPE thrown.

    Writing safe code is not solely in the domain of geeks.

    Posted by: goron on March 10, 2007 at 05:14 AM

  • I agree. The first approach is the normal coding approach. Second is simply bad programming that can lead to errors.

    Posted by: hanuska on March 12, 2007 at 02:17 AM

  • Hi goron, yes, I think the first one is more safe than the second.

    sure hanuska.

    Posted by: ahashim on March 12, 2007 at 02:40 AM

  • I'd say the second one was "normal". The first one is certainly more "safe", however, it's disadvantage is (1) it doesn't read very well, especially with more complicated expressions, and (2) it may cause a subtle bug to go comepletely unnoticed.

    Ideally, you would want to do something like this:

    public boolean compareUser(String name) {
    if (name == null) throw new IllegalArgumentException("Name should be specified");
    return name.equals("Ahmed Hashim");
    }


    This way (1) it doesn't matter which way you do it, and (2) you are guarding against null values, which you should try not to use as much as possible, see NullObject pattern.

    Posted by: fiath on March 12, 2007 at 06:06 AM

  • The example is just good-style (or normal-style) Java

    Btw, in similar way you may find the following from people with serious C/C++ background:

    int x = ...;
    if (0 == x) {
    ...
    }
    or
    if ( someFunc() == x) {
    ...
    }


    Guess why variable is on right side of comparison ;)

    VS

    Posted by: vsilaev on March 12, 2007 at 09:30 AM

  • 门诊
    癌症
    肿瘤
    胃癌
    肺癌
    肝癌
    白血病
    鼻咽癌
    大肠癌
    宫颈癌
    膀胱肿瘤
    食管癌
    乳腺癌
    抗癌新药
    肾癌
    胰腺癌
    胆囊癌
    甲状腺癌
    脑瘤
    恶性黑色素瘤
    恶性淋巴瘤
    阴道恶性肿瘤
    卵巢恶性肿瘤
    绒毛膜癌
    子宫内膜癌
    阴茎癌
    睾丸肿瘤
    前列腺癌
    癌症
    肿瘤
    胃癌
    肺癌
    肝癌
    白血病
    鼻咽癌
    大肠癌
    宫颈癌
    膀胱癌
    食管癌
    食道癌
    乳腺癌
    抗癌新药
    肾癌
    胰腺癌
    胆囊癌
    直肠癌
    甲状腺癌
    脑膜瘤
    恶性黑色素瘤
    恶性淋巴瘤
    阴道恶性肿瘤
    卵巢癌
    绒毛膜癌
    子宫内膜癌
    阴茎癌
    睾丸肿瘤
    前列腺癌
    肝病
    肝病治疗
    乙肝
    乙型肝炎
    丙肝
    脂肪肝
    肝纤维化
    肝硬化
    肝性脑病
    紫杉醇注射液
    开普拓
    美罗华
    赫赛汀
    天地欣
    香菇多糖
    艾普升
    宁得朗
    康士得
    诺雷得
    瑞宁得
    芙瑞
    瑞婷
    法乐通
    易瑞沙
    多吉美
    特罗凯
    泰勒宁
    路盖克
    皮肤病
    银屑病|牛皮癣
    白癜风
    尖锐湿疣
    脱发
    痤疮|青春痘
    皮炎
    灰指甲
    疤痕|瘢痕
    疱疹
    系统性红斑狼疮
    色斑
    艾滋病
    丽科杰
    里亚美
    明欣利迪
    疣迪
    明竹欣
    无为白癜风胶囊
    维阿露
    白癜净
    达霏欣
    狼疮丸
    适今可
    克银丸
    云香十五味丸
    化瘀祛斑胶囊
    疤痕敌
    每宜
    新药网
    肺癌
    鼻咽癌
    食道癌
    胃癌
    肝癌
    大肠癌
    乳腺癌
    卵巢癌
    膀胱癌
    白血病
    胰腺癌
    宫颈癌
    神经内科
    癫痫
    帕金森病
    老年性痴呆
    脑血管病
    多发性硬化症
    偏头痛
    小儿脑瘫
    心血管
    高血压
    高血脂症
    心血管疾病
    精神心理
    精神分裂症
    抑郁症|焦虑症|躁抑症
    强迫症
    睡眠障碍
    多动症
    神经衰弱
    糖尿病
    类风湿
    瑞美隆
    肉蔻五味丸
    喜普妙
    怡诺思
    优克
    恩经复
    申捷
    重塑杰
    施捷因
    神经妥乐平
    七十味珍珠丸
    珊瑚七十味丸
    郝智片
    参蛇偏瘫胶囊
    扎冲十三味丸
    洛斯宝
    安理申
    金思平
    泰舒达
    珂丹
    利鲁唑
    力如太
    静灵口服液
    地牡宁神口服液
    健儿贴
    哈伯因
    肺癌
    小细胞肺癌
    非小细胞肺癌
    肺膦癌
    肺腺癌
    香菇多糖
    瑞宁得
    复方斑蝥胶囊
    金复康口服液

    癌症博客
    癌症博客
    癌症博客
    癌症博客
    癌症博客
    癌症博客
    中国癌症之家
    癌症博客
    癌症博客
    癌症博客
    癌症博客
    癌症博客
    癌症博客之家
    癌症博客
    癌症博客
    癌症博客
    癌症博客
    中国癌症博客
    癌症博客
    癌症博客
    癌症博客

    癌症博客
    癌症博客
    癌症博客
    癌症信息博客
    癌症博客
    癌症博客
    癌症博客
    癌症博客
    癌症博客
    癌症博客
    癌症博客
    癌症博客
    癌症博客
    癌症博客
    癌症博客网
    癌症博客
    癌症博客中国人
    癌症博客
    癌症博客
    癌症博客
    癌症博客
    中国癌症博客
    癌症博客
    癌症博客
    癌症博客
    癌症博客
    特罗凯博客
    特罗凯博客
    特罗凯博客
    特罗凯博客
    特罗凯博客
    特罗凯博客
    特罗凯博客
    特罗凯博客
    特罗凯博客
    抗癌博客
    抗癌博客
    抗癌博客
    抗癌博客
    抗癌博客
    抗癌博客
    抗癌博客
    抗癌博客
    抗癌博客
    抗癌博客
    抗癌博客
    特罗凯博客
    特罗凯博客
    特罗凯博客
    特罗凯博客
    特罗凯博客
    特罗凯博客
    特罗凯博客
    特罗凯博客
    特罗凯博客
    易瑞沙博客
    易瑞沙博客
    易瑞沙博客
    易瑞沙博客
    易瑞沙博客
    易瑞沙博客
    易瑞沙博客
    易瑞沙博客
    易瑞沙博客
    易瑞沙博客
    易瑞沙博客
    易瑞沙博客
    易瑞沙博客

    Posted by: bxz on August 17, 2007 at 01:24 AM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds