Menu
CoddyTech

Pemeriksaan Validitas

Termasuk bagian Pemrograman Berorientasi Object dalam perjalanan R di Coddy. Pelajaran 24 dari 57.

Tipe diperiksa oleh R, aturan tentang nilai ditentukan olehmu. Argumen validity dari setClass() adalah fungsi dari object yang mengembalikan TRUE jika object sudah benar, atau string karakter yang menjelaskan masalahnya. new() menjalankannya untuk setiap object:

setClass("Account", slots = c(owner = "character", balance = "numeric"),
  validity = function(object) {
    if (object@balance < 0) return("balance must not be negative")
    TRUE
  })
ok <- new("Account", owner = "Ada", balance = 10)
msg <- tryCatch(new("Account", owner = "Bo", balance = -5), error = function(e) conditionMessage(e))
cat(msg)

Output:

invalid class “Account” object: balance must not be negative

Fungsi validasi dapat melaporkan beberapa masalah sekaligus dengan mengembalikan vektor character. R memberi nomor pada masalah tersebut dalam pesan error:

setClass("Range", slots = c(lo = "numeric", hi = "numeric"),
  validity = function(object) {
    problems <- character(0)
    if (length(object@lo) != 1) problems <- c(problems, "lo must be one number")
    if (length(object@hi) != 1) problems <- c(problems, "hi must be one number")
    if (length(problems) == 0) TRUE else problems
  })
msg <- tryCatch(new("Range", lo = c(1, 2), hi = numeric(0)), error = function(e) conditionMessage(e))
cat(msg)

Output:

invalid class “Range” object: 1: lo must be one number
invalid class “Range” object: 2: hi must be one number

@<- memeriksa tipe, tetapi tidak menjalankan fungsi validasi, sehingga penugasan dapat melanggar aturan. validObject() menjalankan pemeriksaan lagi. Panggil fungsi ini setelah mengubah slot:

setClass("Account", slots = c(balance = "numeric"),
  validity = function(object) if (object@balance < 0) "balance must not be negative" else TRUE)
a <- new("Account", balance = 10)
a@balance <- -99
cat("still created:", a@balance, "\n")
msg <- tryCatch(validObject(a), error = function(e) conditionMessage(e))
cat(msg, "\n")

Output:

still created: -99 
invalid class “Account” object: balance must not be negative 

R menempatkan invalid class “Account” object: di depan teksmu. Kode yang menampilkan pesan kepada pengguna dapat menyertakan hanya bagian setelah object: :

setClass("Account", slots = c(balance = "numeric"),
  validity = function(object) if (object@balance < 0) "balance must not be negative" else TRUE)
msg <- tryCatch(new("Account", balance = -1), error = function(e) conditionMessage(e))
cat(sub(".*object: ", "", msg))

Output:

balance must not be negative
challenge icon

Tantangan

Mudah

Deklarasikan class Rating di Rating.R dengan slot stars (numeric) dan comment (character), serta fungsi validasi yang mengembalikan aturan pertama yang dilanggar:

  • stars must be a whole number from 1 to 5 jika stars bukan salah satu dari 1, 2, 3, 4, 5;
  • comment is too long jika comment memiliki lebih dari 20 karakter.

Jika tidak, fungsi mengembalikan TRUE. Kode yang disediakan membaca baris 4;Great (comment boleh tidak ada), membuat setiap rating, lalu mencetak ok: 4/5 atau teks masalahnya.

Letakkan kodemu di Rating.R. main.R berisi kode input/output yang disediakan dan tidak boleh diedit.

Coba sendiri

source("Rating.R")

# Kode input/output yang disediakan: biarkan apa adanya
input <- suppressWarnings(readLines(file("stdin")))
for (line in input) {
  p <- strsplit(line, ";")[[1]]
  out <- tryCatch({
    r <- new("Rating", stars = as.numeric(p[1]), comment = if (length(p) > 1) p[2] else "")
    paste0("ok: ", r@stars, "/5")
  }, error = function(e) sub(".*object: ", "", conditionMessage(e)))
  cat(out, "\n", sep = "")
}
quiz iconUji dirimu

Pelajaran ini berisi kuis singkat. Mulai pelajarannya untuk menjawab kuis dan melacak progresmu.

Semua pelajaran di Pemrograman Berorientasi Object

Latihan sendiri: Compiler R online