Mananging static data in the code base.

23 views
Skip to first unread message

vivek poddar

unread,
Aug 19, 2015, 2:25:08 AM8/19/15
to Clean Code Discussion
Hi,

I am working on a java application where I have to maintain a whitelist of some kind. In the white list we can have either a simple email address or a domain name for more wider access.

So,

public class WhiteList {
   private static final Set<String> whiteList;
  
   whieList.add(new String("f...@example.com"));
   whieList.add(new String("b...@example.com"));

   public static boolean isAuthorized(String email) {
       if (email.contains('@bar.com') ||
                 email.contains('@foo.com')) {
         return true;
      } else {
         whiteList.contains(email);
      }
   }
}


The main problem is we need to consistently add either full email address or domain to the white list for which we need the modify code again and again (breaking open / closed principle). Is there a better way?

Thanks,

Sebastian Gozin

unread,
Aug 19, 2015, 8:54:53 AM8/19/15
to Clean Code Discussion
I think you'd need to use some kind of external storage. Like a database or a configuration file.

vivek poddar

unread,
Aug 19, 2015, 9:00:02 AM8/19/15
to clean-code...@googlegroups.com

Hmm, but why we are creating a new string object each time? Sry I am new to Java.

--
The only way to go fast is to go well.
---
You received this message because you are subscribed to the Google Groups "Clean Code Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clean-code-discu...@googlegroups.com.
To post to this group, send email to clean-code...@googlegroups.com.
Visit this group at http://groups.google.com/group/clean-code-discussion.

Sebastian Gozin

unread,
Aug 19, 2015, 9:38:59 AM8/19/15
to Clean Code Discussion
I don't know.
Why are you?

This is the same no?
public class WhiteList {
  private static final Set<String> whiteList;
 
  whiteList.add("f...@example.com");
  whiteList.add("b...@example.com");
To unsubscribe from this group and stop receiving emails from it, send an email to clean-code-discussion+unsub...@googlegroups.com.

Sebastian Gozin

unread,
Aug 19, 2015, 9:47:44 AM8/19/15
to Clean Code Discussion
Eh about string literals, deliberately instantiating string objects in Java.
I believe there are some rules with regards to interning strings which I'm not familiar with at all.

I'd not be surprised to hear writing new String("x") will be changed to simply "x" by the compiler.
I don't know though and would mostly just use string literals.
Reply all
Reply to author
Forward
0 new messages