Skip to content

Instantly share code, notes, and snippets.

@bennidi
Last active February 24, 2022 08:44
Show Gist options
  • Save bennidi/e4d60649a80aa81e796d97546acc52a8 to your computer and use it in GitHub Desktop.
Save bennidi/e4d60649a80aa81e796d97546acc52a8 to your computer and use it in GitHub Desktop.
Snippets with different design smells to learn from
// see https://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
private MyProfile myProfile;
public MyProfile getMyProfile(){
if(myProfile==null){
synchronized(this){
if(myProfile==null){
myProfile=getMyProfileProc.get(userId, databaseType);
}
}
}
return myProfile;
}

A collection of examples for typcial code smells taken from real world examples...

# An additional assignment is displayed if
# - the slot is selected and
# - the slot is not empty and
# - the slot is not already assigned to the selected course
showAdditionalAssignment: () ->
if [email protected] then return false
if @props.assignedCourseIds.length is 0 then return false
if @props.assignedCourseIds.indexOf(@props.selectedCourseId) isnt -1 then return false
true
# This is a very good example of a well intended but poor method comment
# It is a rewrite of the code into natural language!
# Beware: Developers CAN READ code! They DON'T NEED you to rewrite in english what is written in code.
# Comment on the intent instead of the how/what the code does.
# Give information about WHY this code is needed (context)
if (!hasReceivedSmileMessage) {
ConversationType convType;
Collection<MessageView> messages = loadMessages(recipientId, ctx, mCache);
for (MessageView message : messages) {
if (message.getMessageType() != null) {
convType = message.getMessageType().getConversationType();
if (!hasReceivedSmileMessage && convType == ConversationType.SMILE &&
message.getMessageDirection() == MessageDirection.INCOMING) {
hasReceivedSmileMessage = true;
} else if (convType == ConversationType.FREETEXT) {
if (message.getMessageDirection() == MessageDirection.INCOMING) {
latestMessageId = message.getMessageId();
hasReceivedSmileMessage = true;
break;
} else {
standardMessageSentCount++;
}
}
}
}
}
if (!hasReceivedSmileMessage) {
if (standardMessageSentCount > numFirstMessages) {
log.trace("First message(s) already sent to relation {}", recipientId);
throw new SecondMessageWithoutReplyException(
createSentMessage(0, recipientId, System.currentTimeMillis(), body, messageType),
"First message(s) already sent to relation");
} else if (standardMessageSentCount > 1) {
sendEmail = false;
}
}
public LikedTextsListResponse getLikedTexts( long relationId) {
log.info("Get liked texts by user [{}]", relationId);
UserContext userCtx = SessionFacade.getCurrent().getUserContext();
if(FakeUserService.useFake()){
ListRelationResponse relation = FakeUserService.getFakeListRelation(userCtx.getLocale());
if(relation!=null){
return new LikedTextsListResponse(relation);
}
}
Relation relation = relationService.getRelation(relationId, userCtx);
Profile relProfile = profileService.getProfile(relation.getUserId(),
relation.getDatabaseType());
SmallProfile relSmProfile = profileService.getSmallProfile(relation.getUserId(),
relation.getDatabaseType());
MyProfile profile = userDataService.getMyProfile(userCtx.getUserId(),
userCtx.getDatabaseType(), userCtx.getLocale());
messageService.markConversationAsRead(relationId, userCtx, ConversationType.FREETEXT);
return new LikedTextsListResponse(profile, relation, relProfile,
relSmProfile, profile.getFreetexts());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment