The “scope” of knowledge about what the valid parameters are

I have a question which I’m not sure how to generalize and ask properly, so I’ll try with an example.

Suppose that I design a parking lot management system. In my system, I have the objects Car and ParkingLot, where ParkingLot holds a map from car registration number to Car objects. One of the methods of ParkingLot is getCar(n), which can fail if n is not a valid registration number, or if a car with such registration number doesn’t exist in the parking lot.

I have two options:

  1. Have getCar validate the registration number. It can then return a not_valid error if the number is invalid. The downside as I see it is that the ParkingLot object does validation of parameters which logically belong to Car.

  2. Have getCar just check whether the registration number is in the map. The downside is that the only possible error would be not_found, so the caller wouldn’t be able to know whether the function failed because the registration number was invalid, or the car just isn’t there.

Is there any preferred way to go with this? Perhaps there’s a superior solution which will allow to save the extra validation, which increases coupling, and still provide full information for the caller?

0

To me, it seems the validity check of a car registration number is nothing which belongs “logically to a Car“, as you wrote, because when you only have this number, you don’t have inherently a related car object in scope, and you probably won’t need one for doing the validation. (Of course, it also does not belong logically to a ParkingLot). The registration number is something with a life time which could be independent from other objects.

The validation might verify some national rules about the order of digits and letters. Or, it could ask at the national car registration board which numbers are valid. So it could either be function in a RegistrationNumber object, or one of a RegistrationBoard class. This makes the validation available in any method of the ParkingLot class, as well as in any method of the Car class, or other places of the code, whereever it is required.

Note further that the design of the getCar method, and what it returns, should not depend on where the validation check can be implemented best. The design of the getCar method should depend on its intended usage, on nothing else. I think it is a little bit strange to think of a ParkingLot validating the correctness of a cars registration number by asking at the national registration board, but if in your system such a validation makes sense when that method is called, then do it, utilizing for example the mentioned RegistrationBoard object.

I think there’s some confusion here over whether you’re trying to model a real-world domain, or implement an actual software program.

In reality, most parking lots don’t “know” which car is in what spot, or even if any given car is in the lot at all. (Some places have automated image recognition of license plates, but that’s a separate issue).

In software, depending on what the ParkingLot class is actually supposed to do, the simplest interface is likely addCar(Car c) and removeCar(Car c), this way ParkingLot is free to record a hash map of each Car object and when it entered the lot so that total charges can be computed. In this way, there doesn’t need to be requirement leakage into the Car class that it have a VIN or license plate # field of a given data type.

(A small optimization on this would be to generate a Ticket class whenever addCar is called, that has a unique transaction identifier. Since the ParkingLot is the one that cares about the unique field, it’s appropriate that it provide an interface for generating one, rather than demanding it from a caller).

Its a bad example, why check for valid car registrations at all.

Lets say it credit cards and you want to do a quick lun 10 check before you make a slow api call to the bank.

I would have a separate object for validation of the number. After all you might want several different validation schemes based on the type of card. Or you might expect entry errors on the UI and want to present/log them differently from api failures.

The flexibility to assemble the raw api client and the validation class in various ways is usually better than having both bits of logic encapsulated in the same class.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật