Click here to Skip to main content
15,879,326 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Is this a coding horror? Pin
patbob30-Aug-11 5:59
patbob30-Aug-11 5:59 
GeneralRe: Is this a coding horror? Pin
BloodyBaron29-Aug-11 22:38
BloodyBaron29-Aug-11 22:38 
GeneralRe: Is this a coding horror? Pin
GibbleCH30-Aug-11 11:44
GibbleCH30-Aug-11 11:44 
GeneralRe: Is this a coding horror? Pin
BloodyBaron30-Aug-11 22:21
BloodyBaron30-Aug-11 22:21 
GeneralRe: Is this a coding horror? Pin
GibbleCH31-Aug-11 3:56
GibbleCH31-Aug-11 3:56 
GeneralRe: Is this a coding horror? Pin
BobJanova31-Aug-11 0:03
BobJanova31-Aug-11 0:03 
GeneralRe: Is this a coding horror? Pin
Rob Grainger6-Sep-11 0:58
Rob Grainger6-Sep-11 0:58 
GeneralRe: Is this a coding horror? Pin
Eusebiu Marcu30-Aug-11 2:46
Eusebiu Marcu30-Aug-11 2:46 
What I don't like on this piece of code is that the verification part is missing.
If user is null -> NullReferenceException.
If inRoles is null && ((user.Roles & userRole) != 0) -> NullReferenceException.
If outRoles is null ((user.Roles & userRole) == 0) -> NullReferenceException.
Of course, one can use code contracts in .NET 4.0.

To overcome this, one will have to use an if-else block
C#
if (user == null)
    return false; // return should also say if the operation succeded or not => void->bool(or success/failure status).
if (string.IsNullOrEmpty(roleName)) // + other validation for roleName.
    return false;

if ((user.Roles & userRole) != 0) {
    if (inRoles == null)
        return false;
    inRoles.Add(roleName);
} else {
    if (outRoles == null)
        return false;
    outRoles.Add(roleName);
}

return true;

Eusebiu

GeneralRe: Is this a coding horror? Pin
BobJanova30-Aug-11 2:51
BobJanova30-Aug-11 2:51 
GeneralRe: Is this a coding horror? Pin
BobJanova30-Aug-11 2:47
BobJanova30-Aug-11 2:47 
GeneralRe: Is this a coding horror? Pin
BubingaMan30-Aug-11 3:03
BubingaMan30-Aug-11 3:03 
GeneralRe: Is this a coding horror? Pin
Vlad Krivoroutchko30-Aug-11 3:11
Vlad Krivoroutchko30-Aug-11 3:11 
GeneralRe: Is this a coding horror? Pin
BubingaMan30-Aug-11 3:17
BubingaMan30-Aug-11 3:17 
GeneralRe: Is this a coding horror? Pin
ii_noname_ii30-Aug-11 3:37
ii_noname_ii30-Aug-11 3:37 
GeneralRe: Is this a coding horror? Pin
Stefan_Lang30-Aug-11 3:49
Stefan_Lang30-Aug-11 3:49 
GeneralRe: Is this a coding horror? Pin
IAbstract30-Aug-11 4:13
IAbstract30-Aug-11 4:13 
GeneralRe: Is this a coding horror? Pin
Schmuli30-Aug-11 23:10
Schmuli30-Aug-11 23:10 
GeneralRe: Is this a coding horror? Pin
IAbstract31-Aug-11 1:27
IAbstract31-Aug-11 1:27 
GeneralRe: Is this a coding horror? Pin
Jim (SS)30-Aug-11 6:06
Jim (SS)30-Aug-11 6:06 
GeneralRe: Is this a coding horror? Pin
Kenneth Kasajian30-Aug-11 9:10
Kenneth Kasajian30-Aug-11 9:10 
GeneralRe: Is this a coding horror? Pin
OriginalGriff30-Aug-11 9:19
mveOriginalGriff30-Aug-11 9:19 
GeneralRe: Is this a coding horror? Pin
Kenneth Kasajian30-Aug-11 19:13
Kenneth Kasajian30-Aug-11 19:13 
GeneralRe: Is this a coding horror? Pin
OriginalGriff30-Aug-11 21:43
mveOriginalGriff30-Aug-11 21:43 
GeneralRe: Is this a coding horror? Pin
BobJanova30-Aug-11 23:07
BobJanova30-Aug-11 23:07 
GeneralRe: Is this a coding horror? Pin
Tech Code Freak30-Aug-11 20:30
Tech Code Freak30-Aug-11 20:30 

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.