Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 18, 2025

This PR refactors if/else statements to use guard clauses where efficient and clear, reducing code nesting and improving readability.

Changes Made

Route Files - Converted the pattern if (condition) { action } else { error } to if (!condition) { error; return } action:

// Before
.post(auth.checkJwt, (req, res, next) => {
    if (rest.checkPatchOverrideSupport(req, res)) {
        controller.patchSet(req, res, next)
    }
    else {
        res.statusMessage = 'Improper request method for updating, please use PATCH to add new keys to this object.'
        res.status(405)
        next(res)
    }
})

// After  
.post(auth.checkJwt, (req, res, next) => {
    if (!rest.checkPatchOverrideSupport(req, res)) {
        res.statusMessage = 'Improper request method for updating, please use PATCH to add new keys to this object.'
        res.status(405)
        next(res)
        return
    }
    controller.patchSet(req, res, next)
})

App Configuration - Simplified maintenance mode check:

// Before
app.all('*', (req, res, next) => {
  if(process.env.DOWN === "true"){
      res.status(503).json({"message":"RERUM v1 is down for updates..."})
  }
  else{
      next() //pass on to the next app.use
  }
})

// After
app.all('*', (req, res, next) => {
  if(process.env.DOWN === "true"){
      res.status(503).json({"message":"RERUM v1 is down for updates..."})
      return
  }
  next() //pass on to the next app.use
})

Files Modified

  • routes/patchSet.js
  • routes/patchUnset.js
  • routes/patchUpdate.js
  • app.js

The changes follow the principle of using early returns for error conditions, reducing nesting levels and making the happy path more prominent. All other if/else patterns in the codebase were analyzed and determined to be appropriate for their specific use cases (business logic with multiple valid paths, configuration assignments, etc.).

Fixes #173.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copy link
Contributor Author

Copilot AI commented Aug 18, 2025

@cubap 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Copilot AI and others added 2 commits August 18, 2025 20:17
Co-authored-by: cubap <1119165+cubap@users.noreply.github.com>
Co-authored-by: cubap <1119165+cubap@users.noreply.github.com>
Copilot AI changed the title [WIP] reduce if/else Refactor if/else statements to use guard clauses for improved readability Aug 18, 2025
Copilot AI requested a review from cubap August 18, 2025 20:24
Copy link
Member

@cubap cubap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect. I would love to see the same treatment across the board for all our routes in another issue/PR.

@cubap cubap marked this pull request as ready for review August 18, 2025 20:52
@cubap cubap requested a review from thehabes as a code owner August 18, 2025 20:52
Copy link
Member

@cubap cubap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect. I would love to see the same treatment across the board for all our routes in another issue/PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

reduce if/else

3 participants