Common to Hardcode for Spring Boot Java Backend Coding?

huangapple go评论65阅读模式
英文:

Common to Hardcode for Spring Boot Java Backend Coding?

问题

这是我在后端工作的第一天。我的背景是前端Java开发人员。我一直在查看许多教程,我注意到在所有教程中,字符串文字都是硬编码的,没有类/方法/内联文档。这在后端编码中是否正常/可接受?

示例教程:

  1. https://www.jackrutorial.com/2018/04/spring-boot-user-registration-login.html

  2. https://medium.com/@kamer.dev/spring-boot-user-registration-and-login-43a33ea19745

在基于Java的前端项目中,我永远不会硬编码任何值。以此示例为例,

package com.jackrutorial.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.jackrutorial.model.User;
import com.jackrutorial.service.UserService;

@Controller
public class UserController {

 @Autowired
 private UserService userService;
 
 @RequestMapping(value= {"/", "/login"}, method=RequestMethod.GET)
 public ModelAndView login() {
  ModelAndView model = new ModelAndView();
  
  model.setViewName("user/login");
  return model;
 }
 
 @RequestMapping(value= {"/signup"}, method=RequestMethod.GET)
 public ModelAndView signup() {
  ModelAndView model = new ModelAndView();
  User user = new User();
  model.addObject("user", user);
  model.setViewName("user/signup");
  
  return model;
 }
 
 @RequestMapping(value= {"/signup"}, method=RequestMethod.POST)
 public ModelAndView createUser(@Valid User user, BindingResult bindingResult) {
  ModelAndView model = new ModelAndView();
  User userExists = userService.findUserByEmail(user.getEmail());
  
  if(userExists != null) {
   bindingResult.rejectValue("email", "error.user", "This email already exists!");
  }
  if(bindingResult.hasErrors()) {
   model.setViewName("user/signup");
  } else {
   userService.saveUser(user);
   model.addObject("msg", "User has been registered successfully!");
   model.addObject("user", new User());
   model.setViewName("user/signup");
  }
  
  return model;
 }
 
 @RequestMapping(value= {"/home/home"}, method=RequestMethod.GET)
 public ModelAndView home() {
  ModelAndView model = new ModelAndView();
  Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  User user = userService.findUserByEmail(auth.getName());
  
  model.addObject("userName", user.getFirstname() + " " + user.getLastname());
  model.setViewName("home/home");
  return model;
 }
 
 @RequestMapping(value= {"/access_denied"}, method=RequestMethod.GET)
 public ModelAndView accessDenied() {
  ModelAndView model = new ModelAndView();
  model.setViewName("errors/access_denied");
  return model;
 }
}

诸如"user/signup"或"errors/access_denied"等字符串值可以多次使用。最好创建常量以避免拼写错误并有一个参考点等。最终,我只是想了解后端开发的最佳实践是什么。如果这是前端代码,我的主管会严厉批评我。

英文:

This is my first day working on backend. My background is frontend Java developer. I have been looking at many tutorials and I have noticed that in all tutorials the String literals are hardcoded and there are no class/method/inline documentation. Is this normal/acceptable for backend coding?

Example tutorials

  1. https://www.jackrutorial.com/2018/04/spring-boot-user-registration-login.html

  2. https://medium.com/@kamer.dev/spring-boot-user-registration-and-login-43a33ea19745

In a frontend Java based project, I would never hardcode any values. Take this example,

package com.jackrutorial.controller;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.jackrutorial.model.User;
import com.jackrutorial.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value= {"/", "/login"}, method=RequestMethod.GET)
public ModelAndView login() {
ModelAndView model = new ModelAndView();
model.setViewName("user/login");
return model;
}
@RequestMapping(value= {"/signup"}, method=RequestMethod.GET)
public ModelAndView signup() {
ModelAndView model = new ModelAndView();
User user = new User();
model.addObject("user", user);
model.setViewName("user/signup");
return model;
}
@RequestMapping(value= {"/signup"}, method=RequestMethod.POST)
public ModelAndView createUser(@Valid User user, BindingResult bindingResult) {
ModelAndView model = new ModelAndView();
User userExists = userService.findUserByEmail(user.getEmail());
if(userExists != null) {
bindingResult.rejectValue("email", "error.user", "This email already exists!");
}
if(bindingResult.hasErrors()) {
model.setViewName("user/signup");
} else {
userService.saveUser(user);
model.addObject("msg", "User has been registered successfully!");
model.addObject("user", new User());
model.setViewName("user/signup");
}
return model;
}
@RequestMapping(value= {"/home/home"}, method=RequestMethod.GET)
public ModelAndView home() {
ModelAndView model = new ModelAndView();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User user = userService.findUserByEmail(auth.getName());
model.addObject("userName", user.getFirstname() + " " + user.getLastname());
model.setViewName("home/home");
return model;
}
@RequestMapping(value= {"/access_denied"}, method=RequestMethod.GET)
public ModelAndView accessDenied() {
ModelAndView model = new ModelAndView();
model.setViewName("errors/access_denied");
return model;
}
}

String values such as "user/signup" or "errors/access_denied", ect is (can be) used multiple times. It would be better to create constants to avoid any spelling mistakes and have one point of reference, ect. Ultimately, I am just trying to understand what is the best practice for backend development. If this was frontend code, my Lead would be tearing me a new one.

答案1

得分: 2

这基本上只是一个可维护性的问题。毕竟,在编译期间的优化很可能可以处理这些文字字面量,因此它们只会被创建一次,从而不会影响性能或内存使用。

对我来说,始终使用常量来表示不会更改的字符串是理所当然的,尤其是如果它们在代码中多次使用。

如果我是你的主管,看到这个情况,我也可能会批评你一番 Common to Hardcode for Spring Boot Java Backend Coding?

英文:

It's all basically just maintainability question. After all, the optimizations during compilation most probably can handle these literals so that they are only created once thus not affecting performance or memory usage.

For me it has always been no-brainer to use constants for strings that do not change and especially if they are used multiple times in the code.

And if I were your lead and saw this, I would probably tear you a new one too Common to Hardcode for Spring Boot Java Backend Coding?

答案2

得分: 1

正如@drodil所指出的,这涉及到可维护性问题,我个人不创建常量,因为这样可以更容易地在那里直接看到端点。您可以在控制器级别添加@RequestMapping,并将端点的公共部分移到那里。

除此之外,您还可以进行控制器测试,以避免拼写错误问题。

这两种方法都可以正常工作。

英文:

As @drodil pointed out its about maintainability, I personally do not make constants since it's easier to see the endpoint right then and there. You can add Controller level @RequestMapping and move the common part of the endpoint there.

Apart from that, you can have Controller tests so that you won't have misspelling issue.

Both of the approaches would work just fine.

huangapple
  • 本文由 发表于 2020年7月31日 18:28:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63190130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定